Skip to content

Commit 9c991ac

Browse files
authored
feat(cli): support --script option (#4)
* feat(cli): support --script option * docs(README): add old migration scripts * docs(cli): Update usage * docs(readme): add gif
1 parent b533fb3 commit 9c991ac

File tree

6 files changed

+718
-361
lines changed

6 files changed

+718
-361
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Migration scripts for [Almin](https://github.com/almin/almin "Almin").
44

5+
![Screen Shot GIF](./docs/screen-shot.gif)
6+
57
## Install
68

79
Install with [npm](https://www.npmjs.com/):
@@ -16,14 +18,20 @@ You can pass a filename directly to the CLI. If you do not, you will be prompted
1618
Ensure you have a backup of your source code or commit the latest changes before running this.
1719

1820
Usage
19-
$ almin-migration-tools <path|glob>
21+
$ almin-migration-tools [<file|glob>]
2022

2123
Options:
24+
--script Run with specified migration script
2225
--dry-run Enable dry run mode
2326
--force, -f Bypass safety checks and forcibly run codemods
2427

2528
Examples
26-
$ almin-migration-tools "src/**/*.js"
29+
# Interactive mode
30+
$ almin-migration-tools
31+
# Interactive mode, but it has default targets
32+
$ almin-migration-tools "src/**/*.js"
33+
# Non interactive mode, specified script name
34+
$ almin-migration-tools --script "store-group-arguments" "src/**/store/**/*.js"
2735

2836
## Migrations
2937

@@ -113,11 +121,10 @@ Please do following steps.
113121

114122
```bash
115123
# Install to global
116-
npm install -g jscodeshift @almin/migration-tools
124+
npm install -g @almin/migration-tools
117125
# Run migration scripts
118126
## Target: your almin store files
119-
### Notice: Use this script with `--run-in-band` arguments(serial running)
120-
jscodeshift --run-in-band -t `npm root -g`/@almin/migration-tools/scripts/store-get-state-return-object-to-flat.js <path>
127+
almin-migration-tools --script "store-get-state-return-object-to-flat" "src/**/store/**/*.js"
121128
```
122129

123130
Store#getState return value migration.
@@ -153,10 +160,10 @@ The `almin-store-state-mapping.json` is used with next script(Convert StoreGroup
153160

154161
```bash
155162
# Install to global
156-
npm install -g jscodeshift @almin/migration-tools
163+
npm install -g @almin/migration-tools
157164
# Run migration scripts
158165
## Target: your almin StoreGroup file
159-
jscodeshift -t `npm root -g`/@almin/migration-tools/scripts/store-group-arguments.js <path>
166+
almin-migration-tools --script "store-group-arguments" "src/**/store/**/*.js"
160167
```
161168

162169
Migrate StoreGroup constructor arguments.

almin-migration-tools.js

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,48 @@ const CodeMigrator = require("code-migrator").CodeMigrator;
44
const meow = require("meow");
55
const updateNotifier = require("update-notifier");
66
const migrationVersions = require("./migrations.js");
7-
const cli = meow(
8-
`
7+
const cli = meow(`
98
Usage
109
$ almin-migration-tools [<file|glob>]
1110
1211
Options:
12+
--script Run with specified migration script
1313
--dry-run Enable dry run mode
1414
--force, -f Bypass safety checks and forcibly run codemods
1515
1616
Examples
17-
$ almin-migration-tools "src/**/*.js"
17+
# Interactive mode
18+
$ almin-migration-tools
19+
# Interactive mode, but it has default targets
20+
$ almin-migration-tools "src/**/*.js"
21+
# Non interactive mode, specified script name
22+
$ almin-migration-tools --script "store-group-arguments" "src/**/store/**/*.js"
1823
`,
1924
{
2025
flags: {
26+
script: {
27+
type: "string"
28+
},
2129
dryRun: {
2230
type: "boolean"
2331
},
2432
force: {
2533
type: "boolean"
34+
},
35+
help: {
36+
type: "boolean",
37+
alias: "h"
38+
},
39+
version: {
40+
type: "boolean",
41+
alias: "v"
2642
}
2743
}
2844
}
2945
);
3046

3147
updateNotifier({ pkg: cli.pkg }).notify();
32-
48+
// Initialize code migrator
3349
const migrator = new CodeMigrator({
3450
moduleName: "almin",
3551
migrationList: migrationVersions,
@@ -41,15 +57,49 @@ const migrator = new CodeMigrator({
4157
};
4258
}
4359
});
44-
migrator
45-
.run({
46-
filePatterns: cli.input
47-
})
48-
.then(() => {
49-
console.log("Done");
50-
process.exit(0);
51-
})
52-
.catch(error => {
53-
console.error(error);
54-
process.exit(1);
55-
});
60+
// --help
61+
if (cli.flags.help) {
62+
cli.showHelp();
63+
}
64+
// --version
65+
if (cli.flags.version) {
66+
cli.showVersion();
67+
}
68+
// main
69+
if (cli.flags.script && cli.input) {
70+
console.log(`Run migration: ${cli.flags.script}`);
71+
migrator
72+
.runScripts({
73+
force: cli.flags.force,
74+
scripts: [cli.flags.script],
75+
files: cli.input
76+
})
77+
.then((result) => {
78+
console.log("Migration results:");
79+
console.log("- scripts:", result.scripts.map(script => script.name));
80+
console.log("- files count:", result.filePathList.length);
81+
process.exit(0);
82+
})
83+
.catch(error => {
84+
console.error(error);
85+
process.exit(1);
86+
});
87+
} else {
88+
migrator
89+
.run({
90+
force: cli.flags.force,
91+
defaultValue: {
92+
files: cli.input
93+
}
94+
})
95+
.then((result) => {
96+
console.log("Migration results:");
97+
console.log("- scripts:", result.scripts.map(script => script.name));
98+
console.log("- files count:", result.filePathList.length);
99+
process.exit(0);
100+
})
101+
.catch(error => {
102+
console.error(error);
103+
process.exit(1);
104+
});
105+
}

docs/screen-shot.gif

2.76 MB
Loading

migrations.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ module.exports = {
1010
{
1111
name: "remove-ChangedPayload",
1212
filePath: require.resolve("./scripts/remove-ChangedPayload.js")
13+
},
14+
{
15+
name: "store-get-state-return-object-to-flat",
16+
filePath: require.resolve("./scripts/store-get-state-return-object-to-flat"),
17+
}, {
18+
name: "store-group-arguments",
19+
filePath: require.resolve("./scripts/store-group-arguments"),
1320
}
1421
],
1522
"versions": [

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@
4545
},
4646
"homepage": "https://github.com/almin/migration-tools",
4747
"dependencies": {
48-
"code-migrator": "^1.0.2",
48+
"code-migrator": "^1.3.2",
4949
"jscodeshift": "^0.4.0",
5050
"meow": "^4.0.0",
5151
"npm-run-path": "^2.0.2",
5252
"update-notifier": "^2.3.0"
5353
},
5454
"devDependencies": {
5555
"husky": "^0.14.3",
56-
"jest": "^21.1.0",
56+
"jest": "^22.1.4",
5757
"lint-staged": "^6.0.1",
5858
"prettier": "^1.10.2"
5959
}

0 commit comments

Comments
 (0)