From 98c9700bcda328552f04116a51549f66e3d7b026 Mon Sep 17 00:00:00 2001 From: Piotr Grzesik Date: Thu, 13 May 2021 10:23:01 +0200 Subject: [PATCH] feat(CLI Onboarding): Support `template-path` param --- lib/cli/commands-schema/no-service.js | 3 ++ lib/cli/interactive-setup/service.js | 36 +++++++++++++------ .../lib/cli/interactive-setup/service.test.js | 13 +++++++ 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/cli/commands-schema/no-service.js b/lib/cli/commands-schema/no-service.js index e4908975e67..81d54ad52c1 100644 --- a/lib/cli/commands-schema/no-service.js +++ b/lib/cli/commands-schema/no-service.js @@ -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'], }); diff --git a/lib/cli/interactive-setup/service.js b/lib/cli/interactive-setup/service.js index 7e78475c7d1..34f2a4403dc 100644 --- a/lib/cli/interactive-setup/service.js +++ b/lib/cli/interactive-setup/service.js @@ -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` ); diff --git a/test/unit/lib/cli/interactive-setup/service.test.js b/test/unit/lib/cli/interactive-setup/service.test.js index 68a470c92e8..c4f73617b7c 100644 --- a/test/unit/lib/cli/interactive-setup/service.test.js +++ b/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')); @@ -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 () => {