Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: report an error whe running site:create nameflag is invalid #4553

Merged
merged 3 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/commands/sites/sites-create.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -84,7 +85,6 @@ const sitesCreate = async (options, command) => {
accountSlug = accountSlugInput
}

const { name: nameFlag } = options
let user
let site

Expand All @@ -110,7 +110,7 @@ const sitesCreate = async (options, command) => {
}
}
}
await inputSiteName(nameFlag)
await inputSiteName(options.name)

log()
log(chalk.greenBright.bold.underline(`Site Created`))
Expand Down Expand Up @@ -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 <A string of more than 63 words>
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
Expand All @@ -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>', 'name of site', validateName)
.option('-a, --account-slug <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')
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/140.command.sites.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()
})
})