forked from hypermod-io/hypermod-community
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
executable file
·191 lines (154 loc) · 5.03 KB
/
run.js
File metadata and controls
executable file
·191 lines (154 loc) · 5.03 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env node
/* eslint-disable @typescript-eslint/no-var-requires */
/**
* 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' });
const execSyncP = (cmd, opts) =>
cp.execSync(cmd, { ...opts, ...pipeStdioOpts() });
const shorthands = require(path.join(__dirname, './shorthands.json'));
console.log({ shorthands });
run();
function run() {
process.on('SIGTERM', () => process.exit(1));
if (!!process.env.OLD_FULL_REBUILD) {
execSyncP('yarn clean');
execSyncP('yarn install');
execSyncP('yarn types:check');
}
let {
parser, //
transformsToRun,
fileOrDirectoryToModify,
extensions,
} = parseArgv();
transformsToRun = transformsToRun
.map(t => {
if (t in shorthands) {
return resolveTransformsFromShorthand(shorthands, t);
} else {
if (!!process.env.OLD_FULL_REBUILD) {
const dir = path.dirname(t);
const cmd = `yarn --cwd ${dir} build`;
console.log('transform to run, build cmd', { cmd });
execSyncP(cmd);
}
return t;
}
})
.flat();
const transformsToRunStr = transformsToRun.join(',');
console.log({ transformsToRun });
const cliPath = path.join(__dirname, './packages/cli/bin/codeshift-cli.js');
const cmdToExec = `${cliPath} --parser ${parser} -e ${extensions} -t ${transformsToRunStr} ${fileOrDirectoryToModify}`;
console.log({ cmdToExec });
execSyncP(cmdToExec);
}
function parseArrayFromCsv(csv = '') {
return csv
.split(',')
.filter(c => !!c)
.map(c => c.trim())
.filter(c => !!c);
}
function resolveTransformsFromShorthand(
shorthands,
currentShorthandKey,
alreadyVisitedShorthands = [currentShorthandKey],
) {
if (!(currentShorthandKey in shorthands)) {
throw new Error('non-existing shorthand: ' + currentShorthandKey + '\n');
}
const [relPathToCodemodPkg, transformVersion] = shorthands[
currentShorthandKey
];
if (relPathToCodemodPkg === 'alias') {
const unaliasedShorthands = transformVersion;
return unaliasedShorthands
.map(shorthand => {
if (alreadyVisitedShorthands.includes(shorthand)) {
console.error('\ncycle in shorthands.json file:', {
shorthand,
alreadyVisitedShorthands,
});
throw new Error(
'cycle in shorthands.json file. see above error output.\n',
);
}
return resolveTransformsFromShorthand(
shorthands,
shorthand,
alreadyVisitedShorthands.concat(shorthand),
);
})
.flat();
}
const pathToCodemodPkg = path.join(__dirname, relPathToCodemodPkg);
if (!!process.env.OLD_FULL_REBUILD) {
execSyncP(`yarn --cwd ${pathToCodemodPkg} build`);
console.log('built');
}
const pathToCodemodConfig = path.join(
pathToCodemodPkg,
// 'dist',
'src',
'codeshift.config.js',
);
const codemodCfg = require(pathToCodemodConfig);
console.log({ pathToCodemodConfig, codemodCfg, __dirname });
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'
return path.join(pathToCodemodPkg, 'src', relPathToTransform);
}
})
.filter(x => !!x);
return transformsApplicable;
}