-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.ts
101 lines (91 loc) · 2.42 KB
/
index.ts
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
#!/usr/bin/env node
const {parseArgs} = require('node:util');
import {s1_to_s2} from './s1-to-s2/src';
import {use_monopackages} from './use-monopackages/src';
interface JSCodeshiftOptions {
/**
* The parser for jscodeshift to use for parsing the source files: https://github.com/facebook/jscodeshift?tab=readme-ov-file#parser.
*
* @default 'tsx'
*/
parser?: 'babel' | 'babylon' | 'flow' | 'ts' |' tsx',
/**
* A glob pattern of files to ignore: https://github.com/facebook/jscodeshift?tab=readme-ov-file#ignoring-files-and-directories.
*
* @default '*\*\/node_modules/*\*\'
*/
ignorePattern?: string,
/**
* Whether to run the codemod in dry mode, which will not write any changes to disk.
*
* @default false
*/
dry?: boolean,
/**
* The path to the directory to run the codemod in.
*
* @default '.'
*/
path?: string
}
export interface S1ToS2CodemodOptions extends JSCodeshiftOptions {
/**
* An optional subset of components to have the s1-to-s2 codemod apply to.
* Provide a comma-separated list of component names.
*/
components?: string
}
export interface UseMonopackagesCodemodOptions extends JSCodeshiftOptions {
/**
* The packages to apply the use-monopackages codemod to.
*/
packages?: string
}
const codemods: Record<string, (options: S1ToS2CodemodOptions | UseMonopackagesCodemodOptions) => void> = {
's1-to-s2': s1_to_s2,
'use-monopackages': use_monopackages
};
// https://github.com/facebook/jscodeshift?tab=readme-ov-file#usage-cli
const options = {
'parser': {
type: 'string'
},
'ignore-pattern': {
type: 'string'
},
'dry': {
type: 'boolean',
short: 'd'
},
'path': {
type: 'string'
},
'components': {
type: 'string'
}
};
const {values, positionals} = parseArgs({
options,
allowPositionals: true
});
if (positionals.length < 1) {
console.error('Please specify a codemod to run. Available codemods: ', Object.keys(codemods).join(', '));
process.exit(1);
}
const codemodName = positionals[0];
const codemodFunction = codemods[codemodName];
if (!codemodFunction) {
console.error(`Unknown codemod: ${codemodName}, available codemods: ${Object.keys(codemods).join(', ')}`);
process.exit(1);
}
try {
codemodFunction({
parser: 'tsx',
ignorePattern: '**/node_modules/**',
path: '.',
...values
});
} catch (error) {
console.error(`Error running codemod: ${error}`);
process.exit(1);
}