Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
feat: 支持自定义模板 (#28)
Browse files Browse the repository at this point in the history
* fix: change json to ts

* chore: fix lint

* feat: support remote package
  • Loading branch information
czy88840616 committed Jan 21, 2020
1 parent 6734339 commit 7aaba06
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 23 deletions.
3 changes: 3 additions & 0 deletions packages/faas-cli-plugin-create/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# faas-cli create

`f create` command
57 changes: 45 additions & 12 deletions packages/faas-cli-plugin-create/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BasePlugin } from '@midwayjs/fcli-command-core';
import { join } from 'path';
const { LightGenerator } = require('light-generator');
import { join, isAbsolute } from 'path';
import { templateList } from './list';
import { LightGenerator } from 'light-generator';
const { Select, Input, Form } = require('enquirer');

async function sleep(timeout) {
Expand All @@ -19,6 +20,8 @@ export class CreatePlugin extends BasePlugin {
npmClient = 'npm';
_innerPrompt;
templateList;
npmPackageName;
localBoilerplatePath;

constructor(core, options) {
super(core, options);
Expand All @@ -28,15 +31,23 @@ export class CreatePlugin extends BasePlugin {
usage: 'Create new Ali FaaS service',
lifecycleEvents: ['create'],
options: {
template: {
'template': {
usage: `Template for the service. Available templates: ${this.humanReadableTemplateList()}`,
shortcut: 't',
},
path: {
'path': {
usage:
'The path where the service should be created (e.g. --path my-service)',
'The path where the service should be created (e.g. --path my-service).',
shortcut: 'p',
},
'template-path': {
usage:
'Your code will be created from this local template.',
},
'template-package': {
usage:
'Your code will be created from this npm package.',
}
},
},
};
Expand Down Expand Up @@ -64,6 +75,17 @@ export class CreatePlugin extends BasePlugin {
async create() {
this.core.cli.log('Generating boilerplate...');
if (this.options['template']) {
this.npmPackageName = this.templateList[this.options.template].package;
await this.createFromTemplate();
} else if (this.options['template-package']) {
this.npmPackageName = this.options['template-package'];
await this.createFromTemplate();
} else if (this.options['template-path']) {
if (!isAbsolute(this.options['template-path'])) {
this.localBoilerplatePath = join(this.servicePath, this.options['template-path']);
} else {
this.localBoilerplatePath = this.options['template-path'];
}
await this.createFromTemplate();
} else {
this.prompt = new Select({
Expand All @@ -79,6 +101,7 @@ export class CreatePlugin extends BasePlugin {
});

this.options.template = await this.prompt.run();
this.npmPackageName = this.templateList[this.options.template].package;
await this.createFromTemplate();
}
// done
Expand All @@ -99,11 +122,21 @@ export class CreatePlugin extends BasePlugin {
const boilerplatePath = this.options.path || '';
const newPath = join(this.servicePath, boilerplatePath);
const lightGenerator = new LightGenerator();
const generator = lightGenerator.defineNpmPackage({
npmClient: this.npmClient,
npmPackage: this.templateList[this.options.template].package,
targetPath: newPath,
});
let generator;
if (this.npmPackageName) {
// 利用 npm 包
generator = lightGenerator.defineNpmPackage({
npmClient: this.npmClient,
npmPackage: this.npmPackageName,
targetPath: newPath,
});
} else {
// 利用本地路径
generator = lightGenerator.defineLocalPath({
templatePath: this.localBoilerplatePath,
targetPath: newPath,
});
}

const args = await generator.getParameterList();
const argsKeys = Object.keys(args);
Expand All @@ -128,7 +161,7 @@ export class CreatePlugin extends BasePlugin {
await generator.run();
}
this.core.cli.log(
`Successfully generated boilerplate for template: "${this.options.template}"`
`Successfully generated boilerplate for template: "${this.options.template || this.npmPackageName || this.localBoilerplatePath}"`
);
this.core.cli.log();
}
Expand Down Expand Up @@ -157,7 +190,7 @@ export class CreatePlugin extends BasePlugin {
}

loadTemplateList() {
this.templateList = require('./list');
this.templateList = templateList;
}

humanReadableTemplateList() {
Expand Down
10 changes: 0 additions & 10 deletions packages/faas-cli-plugin-create/src/list.json

This file was deleted.

10 changes: 10 additions & 0 deletions packages/faas-cli-plugin-create/src/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const templateList = {
'faas-standard': {
desc: 'A serverless boilerplate for aliyun fc, tecent scf and so on',
package: '@midwayjs/faas-boilerplate-standard'
},
'faas-layer': {
desc: 'A serverless runtime layer boilerplate',
package: '@midwayjs/faas-boilerplate-layer'
}
};
30 changes: 30 additions & 0 deletions packages/faas-cli-plugin-create/test/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,34 @@ describe('/test/create.test.ts', () => {
assert(/serverless-hello-world/.test(contents));
await remove(baseDir);
});

it('base create from remote npm name', async () => {
const core = new CommandHookCore({
config: {
servicePath: baseDir,
},
commands: ['create'],
service: loadSpec(baseDir),
provider: 'aliyun',
options: {
'template-package': '@midwayjs/faas-boilerplate-standard',
'path': 'my_serverless',
},
log: console,
});
core.addPlugin(TestCreatePlugin);
await core.ready();
await core.invoke(['create']);
assert(existsSync(join(baseDir, 'my_serverless/f.yml')));
assert(existsSync(join(baseDir, 'my_serverless/src')));
assert(existsSync(join(baseDir, 'my_serverless/test')));
assert(existsSync(join(baseDir, 'my_serverless/tsconfig.json')));
assert(existsSync(join(baseDir, 'my_serverless/package.json')));
const contents = readFileSync(
join(baseDir, 'my_serverless/f.yml'),
'utf-8'
);
assert(/serverless-hello-world/.test(contents));
await remove(baseDir);
});
});
2 changes: 1 addition & 1 deletion packages/serverless-invoke/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('/test/index.test.ts', () => {
const result: any = await invoke({
functionDir: join(__dirname, 'fixtures/baseApp'),
functionName: 'http',
data: [{ name: 'params' }]
data: [{ name: 'params' }],
});
assert(result && result.body === 'hello http world');
});
Expand Down

0 comments on commit 7aaba06

Please sign in to comment.