Skip to content

Commit c809c5d

Browse files
committed
Add bin-support for templates, updated test-support for full plugin rather than visitor fn
1 parent b293927 commit c809c5d

2 files changed

Lines changed: 74 additions & 22 deletions

File tree

src/bin-support.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

3-
const DEFAULT_EXTENSIONS = 'js,ts';
3+
const DEFAULT_JS_EXTENSIONS = 'js,ts';
4+
const DEFAULT_TEMPLATE_EXTENSIONS = 'hbs';
45

5-
async function runTransform(binRoot, transformName, args, extensions = DEFAULT_EXTENSIONS) {
6+
async function runJsTransform(binRoot, transformName, args, extensions = DEFAULT_JS_EXTENSIONS) {
67
const globby = require('globby');
78
const execa = require('execa');
89
const chalk = require('chalk');
@@ -37,6 +38,55 @@ async function runTransform(binRoot, transformName, args, extensions = DEFAULT_E
3738
}
3839
}
3940

41+
async function runTemplateTransform(
42+
binRoot,
43+
transformName,
44+
args,
45+
extensions = DEFAULT_TEMPLATE_EXTENSIONS
46+
) {
47+
const globby = require('globby');
48+
const execa = require('execa');
49+
const chalk = require('chalk');
50+
const path = require('path');
51+
const { parseTransformArgs } = require('./options-support');
52+
53+
let { paths, options } = parseTransformArgs(args);
54+
55+
try {
56+
let foundPaths = await globby(paths, {
57+
expandDirectories: { extensions: extensions.split(',') },
58+
});
59+
let transformPath = path.join(binRoot, '..', 'transforms', transformName, 'index.js');
60+
61+
let templateRecastPkg = require('ember-template-recast/package');
62+
let templateRecastPath = path.dirname(require.resolve('ember-template-recast/package'));
63+
let binPath = path.join(templateRecastPath, templateRecastPkg.bin);
64+
65+
let binOptions = ['-t', transformPath, ...foundPaths];
66+
67+
return execa(binPath, binOptions, {
68+
stdio: 'inherit',
69+
env: {
70+
CODEMOD_CLI_ARGS: JSON.stringify(options),
71+
},
72+
});
73+
} catch (error) {
74+
console.error(chalk.red(error.stack)); // eslint-disable-line no-console
75+
process.exitCode = 1;
76+
77+
throw error;
78+
}
79+
}
80+
81+
async function runTransform(binRoot, transformName, args, extensions, type = 'jscodeshift') {
82+
switch (type) {
83+
case 'jscodeshift':
84+
return runJsTransform(binRoot, transformName, args, extensions);
85+
case 'template':
86+
return runTemplateTransform(binRoot, transformName, args, extensions);
87+
}
88+
}
89+
4090
module.exports = {
4191
runTransform,
4292
};

src/test-support/template.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
'use strict';
22

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

55
const fs = require('fs-extra');
66
const path = require('path');
77
const globby = require('globby');
88
const { transformDetails } = require('./utils');
9+
const { transform, parse } = require('ember-template-recast');
910

10-
function runTemplateTest(transformFn, input, expectedOutput) {
11-
const { transform } = require('ember-template-recast');
12-
13-
let { code } = transform(input, transformFn);
11+
function runTemplateTest(plugin, { path, source }, expectedOutput) {
12+
const code = plugin(
13+
{
14+
path,
15+
source,
16+
},
17+
{
18+
parse,
19+
visit(ast, callback) {
20+
const results = transform(ast, callback);
21+
return results && results.code;
22+
},
23+
}
24+
);
1425

1526
expect((code || '').trim()).toEqual(expectedOutput.trim());
1627
}
1728

1829
module.exports = function templateTest(options) {
1930
let details = transformDetails(options);
2031

21-
let transform = require(details.transformPath);
32+
let plugin = require(details.transformPath);
2233

2334
describe(details.name, function() {
2435
globby
@@ -30,32 +41,23 @@ module.exports = function templateTest(options) {
3041
.forEach(filename => {
3142
let extension = path.extname(filename);
3243
let testName = filename.replace(`.input${extension}`, '');
44+
let testInputPath = path.join(details.fixtureDir, `${testName}${extension}`);
3345
let inputPath = path.join(details.fixtureDir, `${testName}.input${extension}`);
3446
let outputPath = path.join(details.fixtureDir, `${testName}.output${extension}`);
35-
let optionsPath = path.join(details.fixtureDir, `${testName}.options.json`);
36-
let options = fs.pathExistsSync(optionsPath) ? fs.readFileSync(optionsPath) : '{}';
3747

3848
describe(testName, function() {
39-
beforeEach(function() {
40-
process.env.CODEMOD_CLI_ARGS = options;
41-
});
42-
43-
afterEach(function() {
44-
process.env.CODEMOD_CLI_ARGS = '';
45-
});
46-
4749
it('transforms correctly', function() {
4850
runTemplateTest(
49-
transform,
50-
fs.readFileSync(inputPath, 'utf8'),
51+
plugin,
52+
{ path: testInputPath, source: fs.readFileSync(inputPath, 'utf8') },
5153
fs.readFileSync(outputPath, 'utf8')
5254
);
5355
});
5456

5557
it('is idempotent', function() {
5658
runTemplateTest(
57-
transform,
58-
fs.readFileSync(outputPath, 'utf8'),
59+
plugin,
60+
{ path: testInputPath, source: fs.readFileSync(inputPath, 'utf8') },
5961
fs.readFileSync(outputPath, 'utf8')
6062
);
6163
});

0 commit comments

Comments
 (0)