diff --git a/src/commands/sites/sites-create.js b/src/commands/sites/sites-create.js index f2e0eec7b0e..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,7 +85,6 @@ const sitesCreate = async (options, command) => { accountSlug = accountSlugInput } - const { name: nameFlag } = options let user let site @@ -110,7 +110,7 @@ const sitesCreate = async (options, command) => { } } } - await inputSiteName(nameFlag) + await inputSiteName(options.name) log() log(chalk.greenBright.bold.underline(`Site Created`)) @@ -175,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 @@ -187,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 c10907fa037..fc49110fbde 100644 --- a/tests/integration/140.command.sites.test.js +++ b/tests/integration/140.command.sites.test.js @@ -44,6 +44,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') @@ -185,3 +186,27 @@ 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 exitSpy = sinon.stub(process, 'exit') + + const program = new BaseCommand('netlify') + + createSitesCreateCommand(program) + + 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() + }) +})