This repository has been archived by the owner on Jan 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ava-codemods.js
executable file
·90 lines (74 loc) · 2.13 KB
/
ava-codemods.js
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
#!/usr/bin/env node
'use strict';
const execa = require('execa');
const meow = require('meow');
const updateNotifier = require('update-notifier');
const arrify = require('arrify');
const globby = require('globby');
const pkgConf = require('pkg-conf');
const inquirer = require('inquirer');
const npmRunPath = require('npm-run-path');
const utils = require('./cli-utils');
const codemods = require('./codemods');
function runScripts(scripts, files) {
const spawnOptions = {
env: Object.assign({}, process.env, {PATH: npmRunPath({cwd: __dirname})}),
stdio: 'inherit'
};
scripts.forEach(script => {
const result = execa.sync(require.resolve('.bin/jscodeshift'), ['-t', script].concat(files), spawnOptions);
if (result.error) {
throw result.error;
}
});
}
const cli = meow(`
Usage
$ ava-codemods [<file|glob> ...]
Options
--force, -f Bypass safety checks and forcibly run codemods
Available upgrades
- 0.16.x → 0.17.x
- 0.13.x → 0.14.x
`, {
boolean: ['force'],
string: ['_'],
alias: {
f: 'force',
h: 'help'
}
});
updateNotifier({pkg: cli.pkg}).notify();
if (!utils.checkGitStatus(cli.flags.force)) {
process.exit(1);
}
codemods.sort(utils.sortByVersion);
const versions = utils.getVersions(codemods);
const avaConf = pkgConf.sync('ava');
const defaultFiles = 'test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js';
const questions = [{
type: 'list',
name: 'currentVersion',
message: 'What version of AVA are you currently using?',
choices: versions.slice(0, -1)
}, {
type: 'list',
name: 'nextVersion',
message: 'What version of AVA are you moving to?',
choices: versions.slice(1)
}, {
type: 'input',
name: 'files',
message: 'On which files should the codemods be applied?',
default: (avaConf.files && arrify(avaConf.files).join(' ')) || defaultFiles,
when: !cli.input.length,
filter: files => files.trim().split(/\s+/).filter(v => v)
}];
inquirer.prompt(questions, answers => {
const files = answers.files || cli.input;
if (!files.length) {
return;
}
const scripts = utils.selectScripts(codemods, answers.currentVersion, answers.nextVersion);
runScripts(scripts, globby.sync(files));
});