-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathbin-support.js
113 lines (92 loc) · 3.38 KB
/
bin-support.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict';
const DEFAULT_JS_EXTENSIONS = 'js,ts';
async function runJsTransform(transformPath, args, extensions = DEFAULT_JS_EXTENSIONS) {
const globby = require('globby');
const execa = require('execa');
const chalk = require('chalk');
const path = require('path');
const { Readable } = require('stream');
const { parseTransformArgs } = require('./options-support');
let { paths, options, transformerOptions } = parseTransformArgs(args);
try {
let foundPaths = await globby(paths, {
expandDirectories: { extensions: extensions.split(',') },
gitignore: true,
});
let jscodeshiftPkg = require('jscodeshift/package');
let jscodeshiftPath = path.dirname(require.resolve('jscodeshift/package'));
let binPath = path.join(jscodeshiftPath, jscodeshiftPkg.bin.jscodeshift);
let binOptions = [
'-t',
transformPath,
'--extensions',
extensions,
...transformerOptions,
'--stdin', // tell jscodeshift to read the list of files from stdin
];
let handle = execa(binPath, binOptions, {
stdout: 'inherit',
stderr: 'inherit',
stdin: 'pipe', // must be pipe for the below
env: {
CODEMOD_CLI_ARGS: JSON.stringify(options),
},
});
// https://github.com/ember-codemods/es5-getter-ember-codemod/issues/34
let pathsStream = new Readable();
pathsStream.push(foundPaths.join('\n'));
pathsStream.push(null);
pathsStream.pipe(handle.stdin);
return await handle;
} catch (error) {
console.error(chalk.red(error.stack)); // eslint-disable-line no-console
process.exitCode = 1;
throw error;
}
}
async function runTemplateTransform(transformPath, args) {
const execa = require('execa');
const chalk = require('chalk');
const path = require('path');
const { parseTransformArgs } = require('./options-support');
let { paths, options } = parseTransformArgs(args);
try {
let binOptions = ['-t', transformPath, ...paths];
let templateRecastDir = path.dirname(require.resolve('ember-template-recast/package.json'));
let templateRecastPkg = require('ember-template-recast/package');
// npm@6 changes `bin: 'lib/bin.js'` into `bin: { 'ember-template-recast':
// 'lib/bin.js' }` automatically this ensures that we read either format
let templateRecastBinPath =
typeof templateRecastPkg.bin === 'string'
? templateRecastPkg.bin
: templateRecastPkg.bin['ember-template-recast'];
return execa(path.join(templateRecastDir, templateRecastBinPath), binOptions, {
stdio: 'inherit',
env: {
CODEMOD_CLI_ARGS: JSON.stringify(options),
},
});
} catch (error) {
console.error(chalk.red(error.stack)); // eslint-disable-line no-console
process.exitCode = 1;
throw error;
}
}
async function runTransform(binRoot, transformName, args, extensions) {
const { getTransformType, getTransformPath } = require('./transform-support');
const path = require('path');
let root = path.join(binRoot, '..');
let transformPath = getTransformPath(root, transformName);
let type = getTransformType(transformPath);
switch (type) {
case 'js':
return runJsTransform(transformPath, args, extensions);
case 'hbs':
return runTemplateTransform(transformPath, args);
default:
throw new Error(`Unknown type passed to runTransform: "${type}"`);
}
}
module.exports = {
runTransform,
};