-
Notifications
You must be signed in to change notification settings - Fork 4
/
codemod-imports-sort
executable file
·112 lines (106 loc) · 2.88 KB
/
codemod-imports-sort
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
#!/usr/bin/env node
'use strict';
const appRoot = require('app-root-path');
const Runner = require('jscodeshift/dist/Runner.js');
const fs = require('fs');
const path = require('path');
const pkg = require('../package.json');
const opts = require('nomnom')
.script('codemod-imports-sort')
.options({
path: {
position: 0,
help: 'Files or directory to transform',
list: true,
metavar: 'FILE',
required: true
},
sortConfig: {
help: 'JSON File with imports sort config',
},
cpus: {
abbr: 'c',
help: '(all by default) Determines the number of processes started.'
},
verbose: {
abbr: 'v',
choices: [0, 1, 2],
default: 0,
help: 'Show more information about the transform process'
},
dry: {
abbr: 'd',
flag: true,
help: 'Dry run (no changes are made to files)'
},
print: {
abbr: 'p',
flag: true,
help: 'Print output, useful for development'
},
extensions: {
default: 'js',
help: 'File extensions the transform file should be applied to'
},
ignorePattern: {
full: 'ignore-pattern',
list: true,
help: 'Ignore files that match a provided glob expression'
},
ignoreConfig: {
full: 'ignore-config',
list: true,
help: 'Ignore files if they match patterns sourced from a configuration file (e.g., a .gitignore)',
metavar: 'FILE'
},
runInBand: {
flag: true,
default: false,
full: 'run-in-band',
help: 'Run serially in the current process'
},
silent: {
abbr: 's',
flag: true,
default: false,
full: 'silent',
help: 'No output'
},
parser: {
choices: ['babel', 'babylon', 'flow'],
default: 'babel',
full: 'parser',
help: 'The parser to use for parsing your source files (babel | babylon | flow)'
},
version: {
flag: true,
help: 'print version and exit',
callback: function() {
const requirePackage = require('jscodeshift/utils/requirePackage');
return [
`jscodeshift: ${pkg.version}`,
` - babel: ${require('babel-core').version}`,
` - babylon: ${requirePackage('babylon').version}`,
` - flow: ${requirePackage('flow-parser').version}`,
` - recast: ${requirePackage('recast').version}`,
].join('\n');
},
},
})
.parse();
const transform = appRoot + '/dist/index.js';
let sortConfig;
const sortConfigPath = opts.sortConfig;
if (sortConfigPath) {
try {
sortConfig = JSON.parse(fs.readFileSync(path.resolve(sortConfigPath)));
} catch (err) {
console.error(`Unable to read sort config file ${sortConfigPath}.`, err);
process.exit(1);
}
}
Runner.run(
path.resolve(transform),
opts.path,
Object.assign({}, Object.assign(opts, { sortConfig: sortConfig }), { transform: transform, babel: true })
);