feat(CLI Onboarding): Support template-path param

This commit is contained in:
Piotr Grzesik 2021-05-13 10:23:01 +02:00
parent 03011baf07
commit 98c9700bcd
3 changed files with 42 additions and 10 deletions

View File

@ -12,6 +12,9 @@ commands.set('', {
hasAwsExtension: true,
options: {
'help-interactive': { usage: 'Show this message', type: 'boolean' },
'template-path': {
usage: 'Template local path for the service.',
},
},
lifecycleEvents: ['initializeService', 'setupAws', 'autoUpdate', 'tabCompletion', 'end'],
});

View File

@ -9,6 +9,7 @@ const readConfiguration = require('../../configuration/read');
const createFromTemplate = require('../../utils/createFromTemplate');
const resolveVariables = require('../../configuration/variables');
const { confirm } = require('./utils');
const createFromLocalTemplate = require('../../utils/create-from-local-template');
const isValidServiceName = RegExp.prototype.test.bind(/^[a-zA-Z][a-zA-Z0-9-]{0,100}$/);
@ -65,17 +66,32 @@ module.exports = {
name: 'shouldCreateNewProject',
});
if (!isConfirmed) return;
const projectType = await projectTypeChoice();
if (projectType === 'other') {
process.stdout.write(
'\nRun “serverless create --help” to view available templates and create a new project ' +
'from one of those templates.\n'
);
return;
let projectDir;
// TOOD: CLEANUP
let projectName;
if (context.options && context.options['template-path']) {
projectName = await projectNameInput(workingDir);
projectDir = join(workingDir, projectName);
createFromLocalTemplate({
templatePath: context.options['template-path'],
projectDir,
projectName,
});
} else {
const projectType = await projectTypeChoice();
if (projectType === 'other') {
process.stdout.write(
'\nRun “serverless create --help” to view available templates and create a new project ' +
'from one of those templates.\n'
);
return;
}
projectName = await projectNameInput(workingDir);
projectDir = join(workingDir, projectName);
await createFromTemplate(projectType, projectDir);
}
const projectName = await projectNameInput(workingDir);
const projectDir = join(workingDir, projectName);
await createFromTemplate(projectType, projectDir);
process.stdout.write(
`\n${chalk.green(`Project successfully created in '${projectName}' folder.`)}\n`
);

View File

@ -1,10 +1,13 @@
'use strict';
const chai = require('chai');
const path = require('path');
const sinon = require('sinon');
const configureInquirerStub = require('@serverless/test/configure-inquirer-stub');
const step = require('../../../../../lib/cli/interactive-setup/service');
const templatesPath = path.resolve(__dirname, '../../../../../lib/plugins/create/templates');
const { expect } = chai;
chai.use(require('chai-as-promised'));
@ -51,6 +54,16 @@ describe('test/unit/lib/cli/interactive-setup/service.test.js', () => {
const stats = await fsp.lstat('test-project/serverless.yml');
expect(stats.isFile()).to.be.true;
});
it('Should create project at not existing directory from a provided `template-path`', async () => {
configureInquirerStub(inquirer, {
confirm: { shouldCreateNewProject: true },
input: { projectName: 'test-project-from-local-template' },
});
await step.run({ options: { 'template-path': path.join(templatesPath, 'aws-nodejs') } });
const stats = await fsp.lstat('test-project-from-local-template/serverless.yml');
expect(stats.isFile()).to.be.true;
});
});
it('Should not allow project creation in a directory in which already service is configured', async () => {