Skip to content

Commit

Permalink
fix: fix instructions transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed May 4, 2020
1 parent f928daf commit 6303871
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 20 deletions.
153 changes: 147 additions & 6 deletions __test__/transforms.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const test = require('ava');
const fs = require('fs');
const Vinyl = require('vinyl');
const {Writable} = require('stream');
const mockfs = require('mock-fs');
Expand All @@ -9,13 +10,13 @@ const [
instructionsForSkippedFiles
] = append;

test('Append transforms', t => {
test.serial('Append transforms', t => {
t.is(typeof extTransform, 'function');
t.is(typeof skipDotnetCsprojIfExists, 'function');
t.is(typeof instructionsForSkippedFiles, 'function');
});

test.cb('ext-transform translates .ext file to .js file when typescript is not selected', t => {
test.serial.cb('ext-transform translates .ext file to .js file when typescript is not selected', t => {
const jsExt = extTransform({}, []);
const files = [];

Expand Down Expand Up @@ -47,7 +48,7 @@ test.cb('ext-transform translates .ext file to .js file when typescript is not s
}));
});

test.cb('ext-transform translates .ext file to .ts file when typescript is selected', t => {
test.serial.cb('ext-transform translates .ext file to .ts file when typescript is selected', t => {
const jsExt = extTransform({}, ['typescript']);
const files = [];

Expand Down Expand Up @@ -79,7 +80,7 @@ test.cb('ext-transform translates .ext file to .ts file when typescript is selec
}));
});

test.cb('skips project.csproj if project.csproj file exists', t => {
test.serial.cb('skips project.csproj if project.csproj file exists', t => {
mockfs({
'project.csproj': 'csproj'
});
Expand Down Expand Up @@ -108,7 +109,7 @@ test.cb('skips project.csproj if project.csproj file exists', t => {
}));
});

test.cb('skips project.csproj if any .csproj file exists', t => {
test.serial.cb('skips project.csproj if any .csproj file exists', t => {
mockfs({
'some.csproj': 'csproj'
});
Expand Down Expand Up @@ -137,7 +138,7 @@ test.cb('skips project.csproj if any .csproj file exists', t => {
}));
});

test.cb('writes project.csproj if no .csproj file exists', t => {
test.serial.cb('writes project.csproj if no .csproj file exists', t => {
mockfs({});

const skipCsproj = skipDotnetCsprojIfExists({}, [], '.');
Expand Down Expand Up @@ -165,3 +166,143 @@ test.cb('writes project.csproj if no .csproj file exists', t => {
contents: Buffer.from('new-csproj')
}));
});

test.serial.cb('does not write instructions if file is not skipped', t => {
mockfs({});

const instructions = instructionsForSkippedFiles({}, [], '.');
const files = [];

instructions.pipe(new Writable({
objectMode: true,
write(file, enc, cb) {
files.push(file);
cb();
}
}));

instructions.on('end', () => {
t.is(files.length, 1);
t.is(files[0].path.replace(/\\/g, '/'), 'test/file.js');
t.is(files[0].writePolicy, 'skip');
t.is(files[0].contents.toString(), 'new file');
mockfs.restore();
t.end();
})

instructions.write(new Vinyl({
path: 'test/file.js__instructions',
base: 'test/',
contents: Buffer.from('do something')
}));

instructions.end(new Vinyl({
path: 'test/file.js',
base: 'test/',
writePolicy: 'skip',
contents: Buffer.from('new file')
}));
});

test.serial.cb('writes instructions if file is skipped', t => {
mockfs({
'folder': {
'file.js': 'old file'
}
});

const instructions = instructionsForSkippedFiles({}, [], '.');
const files = [];

instructions.pipe(new Writable({
objectMode: true,
write(file, enc, cb) {
files.push(file);
cb();
}
}));

instructions.on('end', () => {
t.is(files.length, 1);
t.is(files[0].path.replace(/\\/g, '/'), 'test/folder/file.js');
t.is(files[0].writePolicy, 'skip');
t.is(fs.readFileSync('instructions.txt', 'utf8'), 'do something\n');
mockfs.restore();
t.end();
})

instructions.write(new Vinyl({
path: 'test/folder/file.js__instructions',
base: 'test/',
contents: Buffer.from('do something')
}));

instructions.end(new Vinyl({
path: 'test/folder/file.js',
base: 'test/',
writePolicy: 'skip',
contents: Buffer.from('new file')
}));
});

test.serial.cb('merges instructions if multiple files is skipped', t => {
mockfs({
'folder': {
'file.js': 'old file'
},
'file.c': 'old c file'
});

const instructions = instructionsForSkippedFiles({}, [], '.');
const files = [];

instructions.pipe(new Writable({
objectMode: true,
write(file, enc, cb) {
files.push(file);
cb();
}
}));

instructions.on('end', () => {
t.is(files.length, 2);
t.is(files[0].path.replace(/\\/g, '/'), 'test/folder/file.js');
t.is(files[0].writePolicy, 'skip');
t.is(files[0].contents.toString(), 'new file');

t.is(files[1].path.replace(/\\/g, '/'), 'test/file.c');
t.is(files[1].writePolicy, 'skip');
t.is(files[1].contents.toString(), 'new c file');

t.is(fs.readFileSync('instructions.txt', 'utf8'), 'do something\ndo something on c\n');
mockfs.restore();
t.end();
})

instructions.write(new Vinyl({
path: 'test/folder/file.js__instructions',
base: 'test/',
contents: Buffer.from('do something')
}));

instructions.write(new Vinyl({
path: 'test/folder/file.js',
base: 'test/',
writePolicy: 'skip',
contents: Buffer.from('new file')
}));

instructions.write(new Vinyl({
path: 'test/file.c__instructions',
base: 'test/',
contents: Buffer.from('do something on c')
}));

instructions.end(new Vinyl({
path: 'test/file.c',
base: 'test/',
writePolicy: 'skip',
contents: Buffer.from('new c file')
}));
});

21 changes: 7 additions & 14 deletions transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,18 @@ function instructionsForSkippedFiles(properties, features, targetDir) {
}
});

if (!text) {
if (!all) {
cb();
return;
}

const instFileName = 'instructions.txt';
logger.warn('Manual changes are necessary:\n');
console.log(text + '\n');
logger.info(`If you would like to do this at a later time, we've written these instructions to a file called '${instFileName}' in the project directory for you.\n`);

const cwd = process.cwd();
const base = path.join(cwd, targetDir);
const instFile = new Vinyl({
cwd,
base,
path: path.join(base, instFileName),
contents: Buffer.from(text)
});
cb(null, instFile);
console.warn('Manual changes are necessary:\n');
console.log(all + '\n');
console.info(`If you would like to do this at a later time, we've written these instructions to a file called '${instFileName}' in the project directory for you.\n`);
// Avoid using Vinyl file, in order to have zero dependencies.
fs.writeFileSync(path.join(process.cwd(), targetDir, instFileName), all);
cb();
}
});
}
Expand Down

0 comments on commit 6303871

Please sign in to comment.