Skip to content

Commit a21d5ca

Browse files
committed
Cleanup after PR feedback
1 parent 1902c28 commit a21d5ca

7 files changed

Lines changed: 85 additions & 131 deletions

File tree

commands/local/generate/fixture.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ module.exports.handler = function handler(options) {
1717

1818
let { codemodName, fixtureName } = options;
1919
let codemodDir = `${process.cwd()}/transforms/${codemodName}`;
20-
let codemodTransform = `${codemodDir}/index.js`;
2120
let fixturePath = `${codemodDir}/__testfixtures__/${fixtureName}`;
2221

23-
let transformType = getTransformType(codemodTransform);
22+
let transformType = getTransformType(codemodDir);
2423

2524
fs.outputFileSync(`${fixturePath}.input.${transformType}`, '');
2625
fs.outputFileSync(`${fixturePath}.output.${transformType}`, '');

src/bin-support.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,21 @@
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-
11-
async function runJsTransform(binRoot, transformName, args, extensions = DEFAULT_JS_EXTENSIONS) {
5+
async function runJsTransform(root, transformName, args, extensions = DEFAULT_JS_EXTENSIONS) {
126
const globby = require('globby');
137
const execa = require('execa');
148
const chalk = require('chalk');
159
const path = require('path');
1610
const { parseTransformArgs } = require('./options-support');
11+
const { getTransformPath } = require('./transform-support');
1712

1813
let { paths, options } = parseTransformArgs(args);
1914

2015
try {
2116
let foundPaths = await globby(paths, {
2217
expandDirectories: { extensions: extensions.split(',') },
2318
});
24-
let transformPath = getTransformPath(binRoot, transformName);
19+
let transformPath = getTransformPath(root, transformName);
2520

2621
let jscodeshiftPkg = require('jscodeshift/package');
2722
let jscodeshiftPath = path.dirname(require.resolve('jscodeshift/package'));
@@ -43,15 +38,16 @@ async function runJsTransform(binRoot, transformName, args, extensions = DEFAULT
4338
}
4439
}
4540

46-
async function runTemplateTransform(binRoot, transformName, args) {
41+
async function runTemplateTransform(root, transformName, args) {
4742
const execa = require('execa');
4843
const chalk = require('chalk');
4944
const { parseTransformArgs } = require('./options-support');
45+
const { getTransformPath } = require('./transform-support');
5046

5147
let { paths, options } = parseTransformArgs(args);
5248

5349
try {
54-
let transformPath = getTransformPath(binRoot, transformName);
50+
let transformPath = getTransformPath(root, transformName);
5551
let binOptions = ['-t', transformPath, ...paths];
5652

5753
return execa('ember-template-recast', binOptions, {
@@ -70,16 +66,18 @@ async function runTemplateTransform(binRoot, transformName, args) {
7066
}
7167

7268
async function runTransform(binRoot, transformName, args, extensions) {
73-
const { getTransformType } = require('./transform-support');
69+
const { getTransformType, getTransformPath } = require('./transform-support');
70+
const path = require('path');
7471

75-
let transformPath = getTransformPath(binRoot, transformName);
72+
let root = path.join(binRoot, '..');
73+
let transformPath = getTransformPath(root, transformName);
7674
let type = getTransformType(transformPath);
7775

7876
switch (type) {
7977
case 'js':
80-
return runJsTransform(binRoot, transformName, args, extensions);
78+
return runJsTransform(root, transformName, args, extensions);
8179
case 'hbs':
82-
return runTemplateTransform(binRoot, transformName, args);
80+
return runTemplateTransform(root, transformName, args);
8381
default:
8482
throw new Error(`Unknown type passed to runTransform: "${type}"`);
8583
}

src/test-support.js

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,70 @@
11
'use strict';
22

3-
const jscodeshiftTest = require('./test-support/jscodeshift');
4-
const templateTest = require('./test-support/template');
3+
/* global it, describe, beforeEach, afterEach */
4+
5+
const fs = require('fs-extra');
6+
const path = require('path');
7+
const globby = require('globby');
58
const { transformDetails } = require('./test-support/utils');
69

10+
function testRunner(options, runTest) {
11+
let details = transformDetails(options);
12+
13+
let transform = require(details.transformPath);
14+
15+
describe(details.name, function() {
16+
globby
17+
.sync('**/*.input.*', {
18+
cwd: details.fixtureDir,
19+
absolute: true,
20+
})
21+
.map(entry => entry.slice(entry.indexOf('__testfixtures__') + '__testfixtures__'.length + 1))
22+
.forEach(filename => {
23+
let extension = path.extname(filename);
24+
let testName = filename.replace(`.input${extension}`, '');
25+
let testInputPath = path.join(details.fixtureDir, `${testName}${extension}`);
26+
let inputPath = path.join(details.fixtureDir, `${testName}.input${extension}`);
27+
let outputPath = path.join(details.fixtureDir, `${testName}.output${extension}`);
28+
let optionsPath = path.join(details.fixtureDir, `${testName}.options.json`);
29+
let options = fs.pathExistsSync(optionsPath) ? fs.readFileSync(optionsPath) : '{}';
30+
31+
describe(testName, function() {
32+
beforeEach(function() {
33+
process.env.CODEMOD_CLI_ARGS = options;
34+
});
35+
36+
afterEach(function() {
37+
process.env.CODEMOD_CLI_ARGS = '';
38+
});
39+
40+
it('transforms correctly', function() {
41+
runTest(
42+
transform,
43+
{ path: testInputPath, source: fs.readFileSync(inputPath, 'utf8') },
44+
fs.readFileSync(outputPath, 'utf8')
45+
);
46+
});
47+
48+
it('is idempotent', function() {
49+
runTest(
50+
transform,
51+
{ path: testInputPath, source: fs.readFileSync(outputPath, 'utf8') },
52+
fs.readFileSync(outputPath, 'utf8')
53+
);
54+
});
55+
});
56+
});
57+
});
58+
}
59+
760
function runTransformTest(options) {
861
let details = transformDetails(options);
962

1063
switch (details.transformType) {
1164
case 'js':
12-
return jscodeshiftTest(options);
65+
return testRunner(options, require('./test-support/jscodeshift'));
1366
case 'hbs':
14-
return templateTest(options);
67+
return testRunner(options, require('./test-support/template'));
1568
default:
1669
throw new Error(`Unknown type of transform: "${details.transformType}"`);
1770
}

src/test-support/jscodeshift.js

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,7 @@
11
'use strict';
22

3-
/* global it, describe, beforeEach, afterEach */
4-
53
const { runInlineTest } = require('jscodeshift/dist/testUtils');
6-
const fs = require('fs-extra');
7-
const path = require('path');
8-
const globby = require('globby');
9-
const { transformDetails } = require('./utils');
10-
11-
module.exports = function jscodeshiftTest(options) {
12-
let details = transformDetails(options);
13-
14-
let transform = require(details.transformPath);
15-
16-
describe(details.name, function() {
17-
globby
18-
.sync('**/*.input.*', {
19-
cwd: details.fixtureDir,
20-
absolute: true,
21-
})
22-
.map(entry => entry.slice(entry.indexOf('__testfixtures__') + '__testfixtures__'.length + 1))
23-
.forEach(filename => {
24-
let extension = path.extname(filename);
25-
let testName = filename.replace(`.input${extension}`, '');
26-
let testInputPath = path.join(details.fixtureDir, `${testName}${extension}`);
27-
let inputPath = path.join(details.fixtureDir, `${testName}.input${extension}`);
28-
let outputPath = path.join(details.fixtureDir, `${testName}.output${extension}`);
29-
let optionsPath = path.join(details.fixtureDir, `${testName}.options.json`);
30-
let options = fs.pathExistsSync(optionsPath) ? fs.readFileSync(optionsPath) : '{}';
31-
32-
describe(testName, function() {
33-
beforeEach(function() {
34-
process.env.CODEMOD_CLI_ARGS = options;
35-
});
36-
37-
afterEach(function() {
38-
process.env.CODEMOD_CLI_ARGS = '';
39-
});
40-
41-
it('transforms correctly', function() {
42-
runInlineTest(
43-
transform,
44-
{},
45-
{ path: testInputPath, source: fs.readFileSync(inputPath, 'utf8') },
46-
fs.readFileSync(outputPath, 'utf8')
47-
);
48-
});
494

50-
it('is idempotent', function() {
51-
runInlineTest(
52-
transform,
53-
{},
54-
{ path: testInputPath, source: fs.readFileSync(outputPath, 'utf8') },
55-
fs.readFileSync(outputPath, 'utf8')
56-
);
57-
});
58-
});
59-
});
60-
});
5+
module.exports = function runJsTest(transform, input, expectedOutput) {
6+
return runInlineTest(transform, {}, input, expectedOutput);
617
};

src/test-support/template.js

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
'use strict';
22

3-
/* global expect, it, describe */
3+
/* global expect */
44

5-
const fs = require('fs-extra');
6-
const path = require('path');
7-
const globby = require('globby');
8-
const { transformDetails } = require('./utils');
95
const { transform, parse } = require('ember-template-recast');
106

11-
function runTemplateTest(plugin, { path: pluginPath, source }, expectedOutput) {
7+
module.exports = function runTemplateTest(plugin, { path: pluginPath, source }, expectedOutput) {
128
const code = plugin(
139
{
1410
path: pluginPath,
@@ -24,44 +20,4 @@ function runTemplateTest(plugin, { path: pluginPath, source }, expectedOutput) {
2420
);
2521

2622
expect(code || '').toEqual(expectedOutput);
27-
}
28-
29-
module.exports = function templateTest(options) {
30-
let details = transformDetails(options);
31-
32-
let plugin = require(details.transformPath);
33-
34-
describe(details.name, function() {
35-
globby
36-
.sync('**/*.input.*', {
37-
cwd: details.fixtureDir,
38-
absolute: true,
39-
})
40-
.map(entry => entry.slice(entry.indexOf('__testfixtures__') + '__testfixtures__'.length + 1))
41-
.forEach(filename => {
42-
let extension = path.extname(filename);
43-
let testName = filename.replace(`.input${extension}`, '');
44-
let testInputPath = path.join(details.fixtureDir, `${testName}${extension}`);
45-
let inputPath = path.join(details.fixtureDir, `${testName}.input${extension}`);
46-
let outputPath = path.join(details.fixtureDir, `${testName}.output${extension}`);
47-
48-
describe(testName, function() {
49-
it('transforms correctly', function() {
50-
runTemplateTest(
51-
plugin,
52-
{ path: testInputPath, source: fs.readFileSync(inputPath, 'utf8') },
53-
fs.readFileSync(outputPath, 'utf8')
54-
);
55-
});
56-
57-
it('is idempotent', function() {
58-
runTemplateTest(
59-
plugin,
60-
{ path: testInputPath, source: fs.readFileSync(outputPath, 'utf8') },
61-
fs.readFileSync(outputPath, 'utf8')
62-
);
63-
});
64-
});
65-
});
66-
});
6723
};

src/test-support/utils.js

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

33
function transformDetails(options) {
4-
const { getTransformType } = require('../transform-support');
4+
const { getTransformType, getTransformPath } = require('../transform-support');
5+
const path = require('path');
56

6-
let root = process.cwd() + `/transforms/${options.name}/`;
7-
let transformPath = root + 'index.js';
7+
let transformPath = getTransformPath(process.cwd(), options.name);
8+
let root = path.dirname(transformPath);
89
let transformType = getTransformType(transformPath);
910

1011
return {
1112
name: options.name,
1213
root,
1314
transformPath,
1415
transformType,
15-
fixtureDir: root + '__testfixtures__/',
16+
fixtureDir: path.join(root, '__testfixtures__/'),
1617
};
1718
}
1819

src/transform-support.js

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

3-
function getTransformType(transformPath) {
4-
const fs = require('fs-extra');
3+
function getTransformPath(root, transformName) {
4+
const path = require('path');
55

6-
if (!fs.existsSync(transformPath)) {
7-
throw new Error(`Transform ${transformPath} not found.`);
8-
}
6+
return require.resolve(path.join(root, 'transforms', transformName));
7+
}
98

9+
function getTransformType(transformPath) {
1010
return require(transformPath).type || 'js'; // fallback to 'js' if `type` export does not exist
1111
}
1212

@@ -30,4 +30,5 @@ module.exports = {
3030
getParser: getJSCodeshiftParser,
3131
},
3232
getTransformType,
33+
getTransformPath,
3334
};

0 commit comments

Comments
 (0)