forked from lyft/react-javascript-to-typescript-transform
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.ts
34 lines (29 loc) · 980 Bytes
/
cli.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
#!/usr/bin/env node
import * as program from 'commander';
import * as glob from 'glob';
import * as fs from 'fs';
import * as path from 'path';
import { run } from '.';
program
.version('1.0.0')
.usage('[options] <filename or glob>')
.command('* <glob>')
.action(globPattern => {
if (!globPattern) {
throw new Error('You must provide a file name or glob pattern to transform');
}
const files = glob.sync(globPattern, {});
for (const file of files) {
const filePath = path.resolve(file);
const newPath = filePath.replace(/\.jsx?$/, '.tsx');
try {
fs.renameSync(filePath, newPath);
const result = run(newPath);
fs.writeFileSync(newPath, result);
} catch (error) {
console.warn(`Failed to convert ${file}`);
console.warn(error);
}
}
});
program.parse(process.argv);