Skip to content

Commit e748b6e

Browse files
committed
feat(scripts): add context-onhandler-to-events
1 parent f6d9612 commit e748b6e

File tree

11 files changed

+138
-22
lines changed

11 files changed

+138
-22
lines changed

LICENSE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1717
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1818
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
SOFTWARE.
20+
21+
and
22+
23+
MIT ? James Talmage
24+
https://github.com/avajs/ava-codemods
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const pkgConf = require("pkg-conf");
99
const inquirer = require("inquirer");
1010
const npmRunPath = require("npm-run-path");
1111
const utils = require("./cli-utils");
12-
const codemods = require("./codemods");
12+
const migrationVerions = require("./migrations");
1313

1414
function runScripts(scripts, files) {
1515
const spawnOptions = {
@@ -54,9 +54,9 @@ if (!utils.checkGitStatus(cli.flags.force)) {
5454
process.exit(1);
5555
}
5656

57-
codemods.sort(utils.sortByVersion);
57+
migrationVerions.sort(utils.sortByVersion);
5858

59-
const versions = utils.getVersions(codemods);
59+
const versions = utils.getVersions(migrationVerions);
6060

6161
const avaConf = pkgConf.sync("ava");
6262
const defaultFiles = "test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js";
@@ -65,13 +65,13 @@ const questions = [
6565
{
6666
type: "list",
6767
name: "currentVersion",
68-
message: "What version of AVA are you currently using?",
68+
message: "What version of Almin are you currently using?",
6969
choices: versions.slice(0, -1)
7070
},
7171
{
7272
type: "list",
7373
name: "nextVersion",
74-
message: "What version of AVA are you moving to?",
74+
message: "What version of Almin are you moving to?",
7575
choices: versions.slice(1)
7676
},
7777
{
@@ -91,7 +91,7 @@ inquirer.prompt(questions, answers => {
9191
return;
9292
}
9393

94-
const scripts = utils.selectScripts(codemods, answers.currentVersion, answers.nextVersion);
94+
const scripts = utils.selectScripts(migrationVerions, answers.currentVersion, answers.nextVersion);
9595

9696
runScripts(scripts, globby.sync(files));
9797
});

codemods.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

migrations.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"version": "0.13.0",
4+
"scripts": [
5+
"scripts/context-onhandler-to-events.js",
6+
]
7+
}
8+
]

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"name": "@almin/migration-tools",
1212
"version": "1.0.3",
1313
"main": "index.js",
14+
"bin":{
15+
"almin-migration-tools": "./almin-migration-tools.js"
16+
},
1417
"scripts": {
1518
"precommit": "lint-staged",
1619
"postcommit": "git reset",
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {UseCase} from "almin";
2+
export default class AsyncUseCase extends UseCase {
3+
// 1. call onWillExecuteEachUseCase
4+
execute() {
5+
// 2. call onDispatch
6+
this.dispatch({ type : "start" });
7+
return Promise.resolve().then(() => {
8+
// does async function
9+
}).then(() => {
10+
// 4. call onCompleteEachUseCase
11+
});
12+
}
13+
// 3. call onDidExecuteEachUseCase
14+
}
15+
// listen on*
16+
context.onWillExecuteEachUseCase((payload, meta) => {});
17+
context.onDispatch((payload, meta) => {});
18+
context.onDidExecuteEachUseCase((payload, meta) => {});
19+
context.onCompleteEachUseCase((payload, meta) => {});
20+
context.onErrorDispatch((payload, meta) => {});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {UseCase} from "almin";
2+
export default class AsyncUseCase extends UseCase {
3+
// 1. call onWillExecuteEachUseCase
4+
execute() {
5+
// 2. call onDispatch
6+
this.dispatch({ type : "start" });
7+
return Promise.resolve().then(() => {
8+
// does async function
9+
}).then(() => {
10+
// 4. call onCompleteEachUseCase
11+
});
12+
}
13+
// 3. call onDidExecuteEachUseCase
14+
}
15+
// listen on*
16+
context.events.onWillExecuteEachUseCase((payload, meta) => {});
17+
context.events.onDispatch((payload, meta) => {});
18+
context.events.onDidExecuteEachUseCase((payload, meta) => {});
19+
context.events.onCompleteEachUseCase((payload, meta) => {});
20+
context.events.onErrorDispatch((payload, meta) => {});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// MIT © 2017 azu
2+
"use strict";
3+
const defineTest = require('jscodeshift/dist/testUtils').defineTest;
4+
const tests = [
5+
'context'
6+
];
7+
describe('context-onhandler-to-events', () => {
8+
tests.forEach(test =>
9+
defineTest(
10+
__dirname,
11+
'context-onhandler-to-events',
12+
{
13+
dry: false
14+
},
15+
`context-onhandler-to-events/${ test }`
16+
)
17+
);
18+
});

scripts/__tests__/store-get-state-return-object-to-flat.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('store-getState', () => {
1111
__dirname,
1212
'store-get-state-return-object-to-flat',
1313
{
14-
dry: false
14+
dry: true
1515
},
1616
`store-get-state-return-object-to-flat/${ test }`
1717
)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// MIT © 2017 azu
2+
"use strict";
3+
module.exports = function transformer(file, api) {
4+
const j = api.jscodeshift;
5+
const isContext = path => {
6+
const parent = path.parent;
7+
if (!parent) {
8+
return false;
9+
}
10+
const parentNode = parent.value;
11+
if (!parentNode) {
12+
return false;
13+
}
14+
if (parentNode.type !== "MemberExpression") {
15+
return false;
16+
}
17+
const callee = parentNode;
18+
if (callee.object.type !== "Identifier") {
19+
return false;
20+
}
21+
return callee.object.name === "context";
22+
};
23+
24+
return j(file.source)
25+
.find(j.Identifier)
26+
.filter(path => {
27+
return isContext(path);
28+
})
29+
.replaceWith(p => {
30+
switch (p.value.name) {
31+
case "onWillExecuteEachUseCase":
32+
return "events.onWillExecuteEachUseCase";
33+
case "onDispatch":
34+
return "events.onDispatch";
35+
case "onDidExecuteEachUseCase":
36+
return "events.onDidExecuteEachUseCase";
37+
case "onCompleteEachUseCase":
38+
return "events.onCompleteEachUseCase";
39+
case "onErrorDispatch":
40+
return "events.onErrorDispatch";
41+
default:
42+
return p.value;
43+
}
44+
})
45+
.toSource();
46+
};

0 commit comments

Comments
 (0)