Skip to content

Commit

Permalink
feat(devtool): support feflow-plugin dev
Browse files Browse the repository at this point in the history
  • Loading branch information
cpselvis committed Jan 16, 2020
1 parent 41afeeb commit 2f2323e
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 2 deletions.
25 changes: 23 additions & 2 deletions packages/feflow-plugin-devtool/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ enum DEVTOOL_TYPE {
}

module.exports = (ctx: any) => {
const { args, commander, logger } = ctx;
const {
args,
commander,
logger,
root,
rootPkg
} = ctx;
const [ action ] = args['_'];

let templatePath: string;
Expand Down Expand Up @@ -80,7 +86,22 @@ module.exports = (ctx: any) => {
console.log('Happy coding!');
break;
case 'dev':
console.log('dev');
logger.info('Start dev');
const pkgJson = require(path.join(process.cwd(), 'package.json'));
const rootPkgJson = require(rootPkg);
const rootDependenciesPath = path.join(root, 'node_modules');
const pkgName = pkgJson.name;
const pkgVersion = pkgJson.version;

logger.info('Start register %s to feflow home', pkgName);
if (!rootPkgJson.dependencies[pkgName]) {
rootPkgJson.dependencies[pkgName] = pkgVersion;
}
logger.info('Syncing %s to feflow client system', pkgName)
fs.writeFileSync(rootPkg, JSON.stringify(rootPkgJson, null, 2));
fs.copySync(process.cwd(), path.join(rootDependenciesPath, pkgName));

logger.info('End dev, run feflow commands now!');
break;
default:
return null;
Expand Down
21 changes: 21 additions & 0 deletions packages/feflow-plugin-devtool/templates/plugin-template/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20 changes: 20 additions & 0 deletions packages/feflow-plugin-devtool/templates/plugin-template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# feflow-plugin-example

This is an official demo for feflow plugin. It's a simple calculator.

For example, if you install the plugin, you can input command in terminal like this:

```sh
feflow add 1 2 3
# output: 6
feflow minus 1 2 3
# output: -4
feflow multiply 2 2 4
# output: 16
feflow divide 2 2 4
# output: 0.25
```

## License

[MIT](https://tldrlegal.com/license/mit-license)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module.exports = function add(feflow) {
// 原函数逻辑
return {
add(args) {
const sum = args.reduce((sum, item) => {
return sum + item;
}, 0);

// console.log(sum)
feflow.logger.info(sum);
return sum;
},
multiply(args) {
const product = args.reduce((product, item) => {
return product * item;
}, 1);

// console.log(sum)
feflow.logger.info(product);
return product;
},
// 减法看成加法计算
minus(args) {
const _args = args.map((arg, index) => index ? -arg : arg);
return this.add(_args);
},
// 除法看成乘法来算
divide(args) {
// 除数不为零
if (args.slice(1).indexOf(0) > -1) {
return feflow.logger.error('除数不能为零');
}
const _args = args.map((arg, index) => index ? 1 / arg : arg);
return this.multiply(_args);
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const calculate = require('./calculate');

module.exports = (context) => {
const calculator = calculate(context);
const { args } = context;

// 注册一个 add 命令
context.commander.register('add', '加法运算器', () => {
// args 是 add 后面的参数,已被 minimist 库解析
// 例如 `feflow add 1 2 3`,args 就是 { _: [1, 2, 3] },
// 再比如 `feflow add -x 1 -y 2 --z-value 3 4 5 6`,args 就是 { _: [ 4, 5, 6 ], x: 1, y: 2, 'z-value': 3 }
// 调用主要的逻辑
return calculator.add(args._);
});

// 注册乘、除、减三个命令
context.commander.register('multiply', '乘法运算器', () => calculator.multiply(args._));
context.commander.register('minus', '减法运算器', () => calculator.minus(args._));
context.commander.register('divide', '除法运算器', () => calculator.divide(args._));
};


Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@feflow/feflow-plugin-example",
"version": "1.0.0",
"description": "a simple calculator",
"main": "lib/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"feflow-plugin"
],
"author": "",
"license": "ISC"
}

0 comments on commit 2f2323e

Please sign in to comment.