Skip to content

Commit 1902c28

Browse files
committed
Refactor transform type detection
A generated transform now exports its own type. This allows us to * remove the `type` arguments from `runTransform()`, which was problematic anyway as that type would have to passed in the project's `cli.js`, so would not have allowed to have different transform types per project. Instead the type is taken from the transform's exported type itself. * same for `runTransformTest()`: `options.type` is not needed anymore, as the type is also taken from the transform itself * instead of failing when a user tries to generate a fixture for a transform with non-matching types, the generate fixture command now implicitly determines the fixture type from its transform type
1 parent 6bf475a commit 1902c28

8 files changed

Lines changed: 308 additions & 42 deletions

File tree

commands/local/generate/codemod.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ function jsHandler(options) {
4343
.join('');
4444
})
4545
.toSource();
46-
}
46+
};
47+
48+
module.exports.type = 'js';
4749
`,
4850
'utf8'
4951
);
@@ -54,8 +56,7 @@ function jsHandler(options) {
5456
5557
const { runTransformTest } = require('codemod-cli');
5658
57-
runTransformTest({
58-
type: 'jscodeshift',
59+
runTransformTest({
5960
name: '${codemodName}',
6061
});
6162
`,
@@ -122,6 +123,8 @@ function hbsHandler(options) {
122123
};
123124
});
124125
};
126+
127+
module.exports.type = 'hbs';
125128
`,
126129
'utf8'
127130
);
@@ -133,7 +136,6 @@ function hbsHandler(options) {
133136
const { runTransformTest } = require('codemod-cli');
134137
135138
runTransformTest({
136-
type: 'template',
137139
name: '${codemodName}',
138140
});
139141
`,

commands/local/generate/fixture.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,20 @@ module.exports.builder = function builder(yargs) {
88
})
99
.positional('fixture-name', {
1010
describe: 'the name of the fixture to generate',
11-
})
12-
.option('type', {
13-
alias: 't',
14-
describe: 'choose the fixture type',
15-
choices: ['js', 'hbs'],
16-
default: 'js',
1711
});
1812
};
1913

2014
module.exports.handler = function handler(options) {
2115
const fs = require('fs-extra');
16+
const { getTransformType } = require('../../../src/transform-support');
2217

2318
let { codemodName, fixtureName } = options;
2419
let codemodDir = `${process.cwd()}/transforms/${codemodName}`;
20+
let codemodTransform = `${codemodDir}/index.js`;
2521
let fixturePath = `${codemodDir}/__testfixtures__/${fixtureName}`;
2622

27-
fs.outputFileSync(`${fixturePath}.input.${options.type}`, '');
28-
fs.outputFileSync(`${fixturePath}.output.${options.type}`, '');
23+
let transformType = getTransformType(codemodTransform);
24+
25+
fs.outputFileSync(`${fixturePath}.input.${transformType}`, '');
26+
fs.outputFileSync(`${fixturePath}.output.${transformType}`, '');
2927
};

src/bin-support.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
const DEFAULT_JS_EXTENSIONS = 'js,ts';
44

5+
function getTransformPath(binRoot, transformName) {
6+
const path = require('path');
7+
8+
return path.join(binRoot, '..', 'transforms', transformName, 'index.js');
9+
}
10+
511
async function runJsTransform(binRoot, transformName, args, extensions = DEFAULT_JS_EXTENSIONS) {
612
const globby = require('globby');
713
const execa = require('execa');
@@ -15,7 +21,7 @@ async function runJsTransform(binRoot, transformName, args, extensions = DEFAULT
1521
let foundPaths = await globby(paths, {
1622
expandDirectories: { extensions: extensions.split(',') },
1723
});
18-
let transformPath = path.join(binRoot, '..', 'transforms', transformName, 'index.js');
24+
let transformPath = getTransformPath(binRoot, transformName);
1925

2026
let jscodeshiftPkg = require('jscodeshift/package');
2127
let jscodeshiftPath = path.dirname(require.resolve('jscodeshift/package'));
@@ -40,13 +46,12 @@ async function runJsTransform(binRoot, transformName, args, extensions = DEFAULT
4046
async function runTemplateTransform(binRoot, transformName, args) {
4147
const execa = require('execa');
4248
const chalk = require('chalk');
43-
const path = require('path');
4449
const { parseTransformArgs } = require('./options-support');
4550

4651
let { paths, options } = parseTransformArgs(args);
4752

4853
try {
49-
let transformPath = path.join(binRoot, '..', 'transforms', transformName, 'index.js');
54+
let transformPath = getTransformPath(binRoot, transformName);
5055
let binOptions = ['-t', transformPath, ...paths];
5156

5257
return execa('ember-template-recast', binOptions, {
@@ -64,11 +69,16 @@ async function runTemplateTransform(binRoot, transformName, args) {
6469
}
6570
}
6671

67-
async function runTransform(binRoot, transformName, args, extensions, type = 'jscodeshift') {
72+
async function runTransform(binRoot, transformName, args, extensions) {
73+
const { getTransformType } = require('./transform-support');
74+
75+
let transformPath = getTransformPath(binRoot, transformName);
76+
let type = getTransformType(transformPath);
77+
6878
switch (type) {
69-
case 'jscodeshift':
79+
case 'js':
7080
return runJsTransform(binRoot, transformName, args, extensions);
71-
case 'template':
81+
case 'hbs':
7282
return runTemplateTransform(binRoot, transformName, args);
7383
default:
7484
throw new Error(`Unknown type passed to runTransform: "${type}"`);

src/test-support.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
const jscodeshiftTest = require('./test-support/jscodeshift');
44
const templateTest = require('./test-support/template');
5+
const { transformDetails } = require('./test-support/utils');
56

67
function runTransformTest(options) {
7-
switch (options.type) {
8-
case 'jscodeshift':
8+
let details = transformDetails(options);
9+
10+
switch (details.transformType) {
11+
case 'js':
912
return jscodeshiftTest(options);
10-
case 'template':
13+
case 'hbs':
1114
return templateTest(options);
1215
default:
13-
throw new Error(`Unknown type passed to runTransformTest: "${options.type}"`);
16+
throw new Error(`Unknown type of transform: "${details.transformType}"`);
1417
}
1518
}
1619

src/test-support/utils.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
'use strict';
22

33
function transformDetails(options) {
4+
const { getTransformType } = require('../transform-support');
5+
46
let root = process.cwd() + `/transforms/${options.name}/`;
7+
let transformPath = root + 'index.js';
8+
let transformType = getTransformType(transformPath);
59

610
return {
711
name: options.name,
812
root,
9-
transformPath: root + 'index',
13+
transformPath,
14+
transformType,
1015
fixtureDir: root + '__testfixtures__/',
1116
};
1217
}

src/transform-support.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
'use strict';
22

3+
function getTransformType(transformPath) {
4+
const fs = require('fs-extra');
5+
6+
if (!fs.existsSync(transformPath)) {
7+
throw new Error(`Transform ${transformPath} not found.`);
8+
}
9+
10+
return require(transformPath).type || 'js'; // fallback to 'js' if `type` export does not exist
11+
}
12+
313
function getJSCodeshiftParser(api) {
414
try {
515
let parser = require('recast/parsers/typescript');
@@ -19,4 +29,5 @@ module.exports = {
1929
jscodeshift: {
2030
getParser: getJSCodeshiftParser,
2131
},
32+
getTransformType,
2233
};

tests/cli-test.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,6 @@ QUnit.module('codemod-cli', function(hooks) {
177177
'fixture',
178178
'main',
179179
'this-dot-owner',
180-
'--type',
181-
'hbs',
182180
]);
183181

184182
assert.equal(result.exitCode, 0, 'exited with zero');
@@ -582,7 +580,7 @@ QUnit.module('codemod-cli', function(hooks) {
582580
});
583581
});
584582

585-
QUnit.module('runTransform type=template', function(hooks) {
583+
QUnit.module('runTransform type=hbs', function(hooks) {
586584
let userProject;
587585

588586
hooks.beforeEach(async function() {
@@ -607,13 +605,7 @@ QUnit.module('codemod-cli', function(hooks) {
607605
},
608606
});
609607

610-
await CodemodCLI.runTransform(
611-
codemodProject.path('bin'),
612-
'main',
613-
'foo/*thing.hbs',
614-
undefined,
615-
'template'
616-
);
608+
await CodemodCLI.runTransform(codemodProject.path('bin'), 'main', 'foo/*thing.hbs');
617609

618610
assert.deepEqual(userProject.read(), {
619611
foo: {
@@ -643,6 +635,8 @@ QUnit.module('codemod-cli', function(hooks) {
643635
};
644636
});
645637
};
638+
639+
module.exports.type = 'hbs';
646640
`,
647641
},
648642
},
@@ -652,13 +646,13 @@ QUnit.module('codemod-cli', function(hooks) {
652646
foo: { 'something.hbs': `{{foo}}` },
653647
});
654648

655-
await CodemodCLI.runTransform(
656-
codemodProject.path('bin'),
657-
'main',
658-
['--biz', 'A', '--baz', 'B', 'foo/*ing.hbs'],
659-
undefined,
660-
'template'
661-
);
649+
await CodemodCLI.runTransform(codemodProject.path('bin'), 'main', [
650+
'--biz',
651+
'A',
652+
'--baz',
653+
'B',
654+
'foo/*ing.hbs',
655+
]);
662656

663657
assert.deepEqual(userProject.read(), {
664658
foo: { 'something.hbs': `{{AB}}` },

0 commit comments

Comments
 (0)