-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathrun.js
More file actions
executable file
·131 lines (106 loc) · 3.53 KB
/
Copy pathrun.js
File metadata and controls
executable file
·131 lines (106 loc) · 3.53 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env node
/**
* a temporary utility to run local codemods quickly
* before we improve stuff upstream.
*/
const helpMsg = `
run.js parser=flow codemodsToRun fileOrDirectoryToModify extensions="js,jsx,ts,tsx"
codemodsToRun - paths to codemods which have a codeshift.config.js file.
separated by spaces.
can also just provide keys from the "shorthands.json" file
examples:
./run.js flow ./community/@pipedrive__convention-ui-react/src/5.0.0/transform.ts ~/projects/some-project-which-uses-cui4/src/
./run.js flow cui5 ~/projects/some-project-which-uses-cui4/src/ # cui5 is from shorthands.json
`;
const path = require('path');
const cp = require('child_process');
const peekNextArg = () => process.argv[0];
const eatNextArg = () => process.argv.shift();
const shouldPrintHelp = () => (
(should =
!process.argv.length ||
['-h', '--help', '-help', 'help'].includes(peekNextArg())),
should && console.log(helpMsg),
should
);
const parseArgv = () => (
process.argv.splice(0, 2),
shouldPrintHelp() && process.exit(1),
{
parser: eatNextArg() || 'flow',
transformsToRun: parseArrayFromCsv(eatNextArg() || ''),
fileOrDirectoryToModify: eatNextArg() || '',
extensions: eatNextArg() || 'js,jsx,ts,tsx',
}
);
/**
* will pipe stdio as if we were running the command ourselves!
*
* see https://stackoverflow.com/a/47338488/9285308
*
* usage:
*
* ```js
* const command = "ls -la";
* require("child_process").execSync(command, { ...pipeStdioOpts() });
* ```
*
*/
const pipeStdioOpts = (cwd = process.cwd()) => ({ cwd, stdio: 'inherit' });
run();
function run() {
let {
parser, //
transformsToRun,
fileOrDirectoryToModify,
extensions,
} = parseArgv();
const shorthands = require(path.join(__dirname, './shorthands.json'));
console.log({ shorthands });
transformsToRun = transformsToRun
.map(t => {
if (t in shorthands) {
return resolveTransformsFromShorthand(shorthands[t]);
} else {
const dir = path.dirname(t);
const cmd = `yarn --cwd ${dir} build`;
console.log('transform to run, build cmd', { cmd });
cp.execSync(cmd, { ...pipeStdioOpts() });
return t;
}
})
.flat()
.join(',');
const cliPath = path.join(__dirname, './packages/cli/bin/codeshift-cli.js');
const cmdToExec = `${cliPath} --parser ${parser} -e ${extensions} -t ${transformsToRun} ${fileOrDirectoryToModify}`;
console.log({ cmdToExec });
cp.execSync(cmdToExec, { ...pipeStdioOpts() });
}
function parseArrayFromCsv(csv = '') {
return csv
.split(',')
.filter(c => !!c)
.map(c => c.trim())
.filter(c => !!c);
}
function resolveTransformsFromShorthand([pathToCodemodPkg, transformVersion]) {
cp.execSync(`yarn --cwd ${pathToCodemodPkg} build`, { ...pipeStdioOpts() });
console.log('built');
const pathToCodemodConfig = path.join(
pathToCodemodPkg,
'dist',
'codeshift.config.js',
);
console.log({ pathToCodemodConfig });
const codemodCfg = require(path.join(__dirname, pathToCodemodConfig));
const { transforms } = codemodCfg;
const transformsApplicable = Object.entries(transforms)
.map(([version, relPathToTransform]) => {
if (version === transformVersion) {
return relPathToTransform;
// return path.join(pathToCodemodPkg, 'dist', relPathToTransform); // TODO must ensure it's compiled / run with ts-node / require from 'dist'
}
})
.filter(x => !!x);
return transformsApplicable;
}