Skip to content

Commit

Permalink
feat(almin): Almin 0.15.x → 0.18.x (#5)
Browse files Browse the repository at this point in the history
Add migration scripts for `executor` to `execute`

## Blocker

Wait to release almin 0.18
- almin/almin#358
  • Loading branch information
azu committed Aug 27, 2018
1 parent dbd1895 commit 81c934c
Show file tree
Hide file tree
Showing 14 changed files with 2,871 additions and 1,354 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,54 @@ Ensure you have a backup of your source code or commit the latest changes before

## Migrations

### 0.17.x → 0.18.x

#### How to migrate?

Run following command and select `0.17.x → 0.18.x`

```
$ almin-migration-tools
```


#### What is changed?

`executor()` is deprecated.
You should use `UseCaseExecutor#execute` instead of `UseCaseExecutor#executor`.

- [Deprecate `executor()` · Issue #356 · almin/almin](https://github.com/almin/almin/issues/356)

Before: `executor()`

```ts
import { UseCase, Context } from "almin";
class MyUseCaseA extends UseCase {
execute(_a: string) {}
}
const context = new Context({
store: createStore({ name: "test" })
});

// executor
context.useCase(new MyUseCaseA()).executor(useCase => useCase.execute("A"));
```

After: `execute()`

```ts
import { UseCase, Context } from "almin";
class MyUseCaseA extends UseCase {
execute(_a: string) {}
}
const context = new Context({
store: createStore({ name: "test" })
});

//execute
context.useCase(new MyUseCaseA()).execute("A");
```

### 0.13.x → 0.15.x

#### How to migrate?
Expand Down
11 changes: 9 additions & 2 deletions almin-migration-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,18 @@ updateNotifier({ pkg: cli.pkg }).notify();
const migrator = new CodeMigrator({
moduleName: "almin",
migrationList: migrationVersions,
binCreator: () => {
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: binArgs.concat(["-t", script.filePath]).concat(filePathList)
};
}
});
Expand Down
22 changes: 21 additions & 1 deletion migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ module.exports = {
{
name: "store-get-state-return-object-to-flat",
filePath: require.resolve("./scripts/store-get-state-return-object-to-flat"),
}, {
},
{
name: "store-group-arguments",
filePath: require.resolve("./scripts/store-group-arguments"),
},
{
name: "executor-to-execute",
filePath: require.resolve("./scripts/babel-codemod/executor-to-execute.js"),
type: "babel-codemod"
}
],
"versions": [
Expand All @@ -35,6 +41,20 @@ module.exports = {
"scripts": [
"remove-ChangedPayload"
]
},
{
"version": "0.16.0",
"scripts": []
},
{
"version": "0.17.0",
"scripts": []
},
{
"version": "0.18.0",
"scripts": [
"executor-to-execute"
]
}
]
};
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@
},
"homepage": "https://github.com/almin/migration-tools",
"dependencies": {
"code-migrator": "^1.3.2",
"jscodeshift": "^0.4.0",
"meow": "^4.0.0",
"babel-codemod": "^2.0.6",
"code-migrator": "^2.0.0",
"jscodeshift": "^0.5.1",
"meow": "^5.0.0",
"npm-run-path": "^2.0.2",
"update-notifier": "^2.3.0"
"update-notifier": "^2.5.0"
},
"devDependencies": {
"husky": "^0.14.3",
"jest": "^22.1.4",
"lint-staged": "^6.0.1",
"prettier": "^1.10.2"
"jest": "^23.5.0",
"lint-staged": "^7.2.2",
"prettier": "^1.14.2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const expression = useCase => useCase.execute("A");
context.useCase(new MyUseCaseA()).executor(expression);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const value: string = "asasea";
context.useCase(createMyUseCaseA()).executor(useCase => {
return useCase.execute(value)
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
context.useCase(new MyUseCaseA()).executor(useCase => useCase.execute());
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
context.useCase(createMyUseCaseA()).executor(function (useCase) {
return useCase.execute(1, 2, { key: "3" })
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { UseCase, Context } from "almin";

class MyUseCaseA extends UseCase {
execute(_a) {
console.log(_a)
}
}

const context = new Context({
store: createStore({ name: "test" })
});

context.useCase(new MyUseCaseA()).executor(useCase => useCase.execute("A"));
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { UseCase, Context } from "almin";

class MyUseCaseA extends UseCase {
execute(_a: string) {
}
}

const context = new Context({
store: createStore({ name: "test" })
});

context.useCase(new MyUseCaseA()).executor(useCase => useCase.execute("A"));
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`executor-to-execute does-not-change-executor.ts 1`] = `
"const expression = useCase => useCase.execute(\\"A\\");
context.useCase(new MyUseCaseA()).executor(expression);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const expression = useCase => useCase.execute(\\"A\\");
context.useCase(new MyUseCaseA()).executor(expression);
"
`;

exports[`executor-to-execute evincive-return-execute.ts 1`] = `
"const value: string = \\"asasea\\";
context.useCase(createMyUseCaseA()).executor(useCase => {
return useCase.execute(value)
});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const value: string = \\"asasea\\";
context.useCase(createMyUseCaseA()).execute(value);
"
`;

exports[`executor-to-execute no-arguments.ts 1`] = `
"context.useCase(new MyUseCaseA()).executor(useCase => useCase.execute());
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
context.useCase(new MyUseCaseA()).execute();
"
`;

exports[`executor-to-execute return-function-expression.ts 1`] = `
"context.useCase(createMyUseCaseA()).executor(function (useCase) {
return useCase.execute(1, 2, { key: \\"3\\" })
});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
context.useCase(createMyUseCaseA()).execute(1, 2, { key: \\"3\\" });
"
`;

exports[`executor-to-execute simple.js 1`] = `
"import { UseCase, Context } from \\"almin\\";
class MyUseCaseA extends UseCase {
execute(_a) {
console.log(_a)
}
}
const context = new Context({
store: createStore({ name: \\"test\\" })
});
context.useCase(new MyUseCaseA()).executor(useCase => useCase.execute(\\"A\\"));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import { UseCase, Context } from \\"almin\\";
class MyUseCaseA extends UseCase {
execute(_a) {
console.log(_a)
}
}
const context = new Context({
store: createStore({ name: \\"test\\" })
});
context.useCase(new MyUseCaseA()).execute(\\"A\\");
"
`;

exports[`executor-to-execute simple.ts 1`] = `
"import { UseCase, Context } from \\"almin\\";
class MyUseCaseA extends UseCase {
execute(_a: string) {
}
}
const context = new Context({
store: createStore({ name: \\"test\\" })
});
context.useCase(new MyUseCaseA()).executor(useCase => useCase.execute(\\"A\\"));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import { UseCase, Context } from \\"almin\\";
class MyUseCaseA extends UseCase {
execute(_a: string) {
}
}
const context = new Context({
store: createStore({ name: \\"test\\" })
});
context.useCase(new MyUseCaseA()).execute(\\"A\\");
"
`;
47 changes: 47 additions & 0 deletions scripts/babel-codemod/__tests__/executor-to-execute.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use strict";
const childProcess = require('child_process');
const { promisify } = require('util');
const path = require('path');
const fs = require('fs');
const exec = promisify(childProcess.exec)
const codemod = require.resolve(".bin/codemod");

const run = (pluginFilePath, filePath) => {
if (!fs.existsSync(pluginFilePath)) {
return Promise.reject(new Error(`Not exist ${pluginFilePath}`));
}
if (!fs.existsSync(filePath)) {
return Promise.reject(new Error(`Not exist ${filePath}`));
}
// TODO: https://github.com/square/babel-codemod/issues/13
return exec(`cat "${filePath}" | ${codemod} --plugin "${pluginFilePath}" --stdio`).then(result => {
// TODO: IT IS DEBUG HACK
if (result.stderr) {
console.log(result.stderr);
}
return result.stdout;
})
};

const testFile = (testName) => {
it(testName, async () => {
const filePath = path.join(__dirname, `../__testfixtures__/executor-to-execute/${testName}`);
const inputContent = fs.readFileSync(filePath, "utf-8");
const stdout = await run(
path.join(__dirname, "../executor-to-execute.js"),
filePath,
);
expect(`${inputContent}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
${stdout}`).toMatchSnapshot();
});
};
jest.setTimeout(20000);
describe("executor-to-execute", () => {
testFile("simple.js");
testFile("simple.ts");
testFile("evincive-return-execute.ts");
testFile("return-function-expression.ts");
testFile("no-arguments.ts");
testFile("does-not-change-executor.ts");
});
39 changes: 39 additions & 0 deletions scripts/babel-codemod/executor-to-execute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";
module.exports = function(babel) {
const { types: t } = babel;
return {
visitor: {
CallExpression(path) {
const isExecuteCallExpression = (path) => {
return path.node.callee &&
path.node.callee.property &&
path.node.callee.property.name === "execute" &&
path.node["arguments"] !== undefined
};
const findParentExecutorCalleePath = (path) => {
return path.findParent((path) => {
return path.node.type === "CallExpression" &&
path.node.callee &&
path.node.callee.type === "MemberExpression" &&
path.node.callee.property &&
path.node.callee.property.type === "Identifier" &&
path.node.callee.property.name === "executor"
});
};
if (!isExecuteCallExpression(path)) {
return;
}
const executorPath = findParentExecutorCalleePath(path);
if (!executorPath) {
return;
}
// [object].executor(useCase => useCase.execute(args));
// =>
// [object].execute(args)
const executeArgs = path.node["arguments"];
executorPath.node.callee.property.name = "execute";
executorPath.node["arguments"] = executeArgs
}
}
};
};

0 comments on commit 81c934c

Please sign in to comment.