Skip to content

Commit

Permalink
feat(CLI Onboarding): Support template-path param
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed May 14, 2021
1 parent 03011ba commit 98c9700
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
3 changes: 3 additions & 0 deletions lib/cli/commands-schema/no-service.js
Expand Up @@ -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'],
});
Expand Down
36 changes: 26 additions & 10 deletions lib/cli/interactive-setup/service.js
Expand Up @@ -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}$/);

Expand Down Expand Up @@ -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`
);
Expand Down
13 changes: 13 additions & 0 deletions test/unit/lib/cli/interactive-setup/service.test.js
@@ -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'));
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 98c9700

Please sign in to comment.