Skip to content

Commit

Permalink
fix: report an error whe running site:create nameflag is invalid (#4553)
Browse files Browse the repository at this point in the history
* fix: report an error whe running site:create nameflag is invalid

* chore: use command option validator

Co-authored-by: Daniel Tschinder <231804+danez@users.noreply.github.com>
  • Loading branch information
Lxxyx and danez authored Aug 15, 2022
1 parent 9e1e55b commit 0c90be7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
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()
})
})

1 comment on commit 0c90be7

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 222 MB

Please sign in to comment.