-
Notifications
You must be signed in to change notification settings - Fork 0
/
almin-migration-tools.js
executable file
·112 lines (109 loc) · 3.17 KB
/
almin-migration-tools.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
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 CodeMigrator = require("code-migrator").CodeMigrator;
const meow = require("meow");
const updateNotifier = require("update-notifier");
const migrationVersions = require("./migrations.js");
const cli = meow(`
Usage
$ almin-migration-tools [<file|glob>]
Options:
--script Run with specified migration script
--dry-run Enable dry run mode
--force, -f Bypass safety checks and forcibly run codemods
Examples
# Interactive mode
$ almin-migration-tools
# Interactive mode, but it has default targets
$ almin-migration-tools "src/**/*.js"
# Non interactive mode, specified script name
$ almin-migration-tools --script "store-group-arguments" "src/**/store/**/*.js"
`,
{
flags: {
script: {
type: "string"
},
dryRun: {
type: "boolean"
},
force: {
type: "boolean"
},
help: {
type: "boolean",
alias: "h"
},
version: {
type: "boolean",
alias: "v"
}
}
}
);
updateNotifier({ pkg: cli.pkg }).notify();
// Initialize code migrator
const migrator = new CodeMigrator({
moduleName: "almin",
migrationList: migrationVersions,
binCreator: ({ script, filePathList }) => {
if (script.type === "babel-codemod") {
const binArgs = cli.flags.dryRun ? ["--dry"] : [];
return {
binPath: require.resolve(".bin/codemod"),
binArgs: binArgs.concat(["-p", script.filePath]).concat(filePathList)
};
}
const binArgs = cli.flags.dryRun ? ["--dry"] : [];
return {
binPath: require.resolve(".bin/jscodeshift"),
binArgs: binArgs.concat(["-t", script.filePath]).concat(filePathList)
};
}
});
// --help
if (cli.flags.help) {
cli.showHelp();
}
// --version
if (cli.flags.version) {
cli.showVersion();
}
// main
if (cli.flags.script && cli.input) {
console.log(`Run migration: ${cli.flags.script}`);
migrator
.runScripts({
force: cli.flags.force,
scripts: [cli.flags.script],
files: cli.input
})
.then((result) => {
console.log("Migration results:");
console.log("- scripts:", result.scripts.map(script => script.name));
console.log("- files count:", result.filePathList.length);
process.exit(0);
})
.catch(error => {
console.error(error);
process.exit(1);
});
} else {
migrator
.run({
force: cli.flags.force,
defaultValue: {
files: cli.input
}
})
.then((result) => {
console.log("Migration results:");
console.log("- scripts:", result.scripts.map(script => script.name));
console.log("- files count:", result.filePathList.length);
process.exit(0);
})
.catch(error => {
console.error(error);
process.exit(1);
});
}