From a076083dbf49fd0f48feba6f8966ff4fc6407cf2 Mon Sep 17 00:00:00 2001 From: Lxxyx Date: Wed, 20 Apr 2022 16:58:40 +0800 Subject: [PATCH 1/2] fix: report an error whe running site:create nameflag is invalid --- src/commands/sites/sites-create.js | 13 ++++++++++ tests/integration/140.command.sites.test.js | 27 +++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/commands/sites/sites-create.js b/src/commands/sites/sites-create.js index f2e0eec7b0e..c1eff7bda5c 100644 --- a/src/commands/sites/sites-create.js +++ b/src/commands/sites/sites-create.js @@ -85,6 +85,19 @@ const sitesCreate = async (options, command) => { } const { name: nameFlag } = options + + // netlify sites:create --name + if (nameFlag === true) { + error('Please specify site name, example: --name ') + return + } + // netlify sites:create --name + const MAX_SITE_NAME_LENGTH = 63 + if (typeof nameFlag === 'string' && nameFlag.length > MAX_SITE_NAME_LENGTH) { + error(`--name should be less than 64 characters, input length: ${nameFlag.length}`) + return + } + let user let site diff --git a/tests/integration/140.command.sites.test.js b/tests/integration/140.command.sites.test.js index da481c1e31d..d07b3762580 100644 --- a/tests/integration/140.command.sites.test.js +++ b/tests/integration/140.command.sites.test.js @@ -38,6 +38,7 @@ const validateTemplateStub = sinon.stub(templatesUtils, 'validateTemplate').call const jsonRenderSpy = sinon.spy(prettyjson, 'render') const { createSitesFromTemplateCommand, fetchTemplates } = require('../../src/commands/sites/sites-create-template') +const { createSitesCreateCommand } = require('../../src/commands/sites/sites-create') /* eslint-enable import/order */ const { withMockApi } = require('./utils/mock-api') @@ -179,3 +180,29 @@ test.serial('should return an array of templates with name, source code url and ]) }) }) + +test.serial('should throw error when name flag is incorrect', async (t) => { + await withMockApi(routes, async ({ apiUrl }) => { + Object.defineProperty(process, 'env', { + value: { + NETLIFY_API_URL: apiUrl, + NETLIFY_AUTH_TOKEN: 'fake-token', + }, + }) + + const program = new BaseCommand('netlify') + + createSitesCreateCommand(program) + + const missingNameError = await t.throwsAsync(async () => { + await program.parseAsync(['', '', 'sites:create', '--name']) + }) + t.truthy(missingNameError.message.includes('Please specify site name')) + + const lengthError = await t.throwsAsync(async () => { + const LENGTH = 64 + await program.parseAsync(['', '', 'sites:create', '--name', Array.from({ length: LENGTH }).fill('a').join('')]) + }) + t.truthy(lengthError.message.includes('--name should be less than 64 characters')) + }) +}) From edfba81034c7098c7fccff33892f9729e43657dc Mon Sep 17 00:00:00 2001 From: Daniel Tschinder <231804+danez@users.noreply.github.com> Date: Mon, 15 Aug 2022 13:00:29 +0200 Subject: [PATCH 2/2] chore: use command option validator --- src/commands/sites/sites-create.js | 31 ++++++++++----------- tests/integration/140.command.sites.test.js | 8 ++---- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/commands/sites/sites-create.js b/src/commands/sites/sites-create.js index c1eff7bda5c..e2f7689e34a 100644 --- a/src/commands/sites/sites-create.js +++ b/src/commands/sites/sites-create.js @@ -1,6 +1,7 @@ // @ts-check const slugify = require('@sindresorhus/slugify') +const { InvalidArgumentError } = require('commander') const inquirer = require('inquirer') const pick = require('lodash/pick') const sample = require('lodash/sample') @@ -84,20 +85,6 @@ const sitesCreate = async (options, command) => { accountSlug = accountSlugInput } - const { name: nameFlag } = options - - // netlify sites:create --name - if (nameFlag === true) { - error('Please specify site name, example: --name ') - return - } - // netlify sites:create --name - const MAX_SITE_NAME_LENGTH = 63 - if (typeof nameFlag === 'string' && nameFlag.length > MAX_SITE_NAME_LENGTH) { - error(`--name should be less than 64 characters, input length: ${nameFlag.length}`) - return - } - let user let site @@ -123,7 +110,7 @@ const sitesCreate = async (options, command) => { } } } - await inputSiteName(nameFlag) + await inputSiteName(options.name) log() log(chalk.greenBright.bold.underline(`Site Created`)) @@ -188,6 +175,16 @@ const sitesCreate = async (options, command) => { return site } +const MAX_SITE_NAME_LENGTH = 63 +const validateName = function (value) { + // netlify sites:create --name + if (typeof value === 'string' && value.length > MAX_SITE_NAME_LENGTH) { + throw new InvalidArgumentError(`--name should be less than 64 characters, input length: ${value.length}`) + } + + return value +} + /** * Creates the `netlify sites:create` command * @param {import('../base-command').BaseCommand} program @@ -200,8 +197,8 @@ const createSitesCreateCommand = (program) => `Create an empty site (advanced) Create a blank site that isn't associated with any git remote. Will link the site to the current working directory.`, ) - .option('-n, --name [name]', 'name of site') - .option('-a, --account-slug [slug]', 'account slug to create the site under') + .option('-n, --name ', 'name of site', validateName) + .option('-a, --account-slug ', 'account slug to create the site under') .option('-c, --with-ci', 'initialize CI hooks during site creation') .option('-m, --manual', 'force manual CI setup. Used --with-ci flag') .option('--disable-linking', 'create the site without linking it to current directory') diff --git a/tests/integration/140.command.sites.test.js b/tests/integration/140.command.sites.test.js index d07b3762580..39dac2c8c71 100644 --- a/tests/integration/140.command.sites.test.js +++ b/tests/integration/140.command.sites.test.js @@ -189,20 +189,18 @@ test.serial('should throw error when name flag is incorrect', async (t) => { NETLIFY_AUTH_TOKEN: 'fake-token', }, }) + const exitSpy = sinon.stub(process, 'exit') const program = new BaseCommand('netlify') createSitesCreateCommand(program) - const missingNameError = await t.throwsAsync(async () => { - await program.parseAsync(['', '', 'sites:create', '--name']) - }) - t.truthy(missingNameError.message.includes('Please specify site name')) - const lengthError = await t.throwsAsync(async () => { const LENGTH = 64 await program.parseAsync(['', '', 'sites:create', '--name', Array.from({ length: LENGTH }).fill('a').join('')]) }) t.truthy(lengthError.message.includes('--name should be less than 64 characters')) + + exitSpy.restore() }) })