Skip to content

Commit

Permalink
fix: follow standard pattern for auto-generated site names (#5007)
Browse files Browse the repository at this point in the history
* fix: follow standard pattern for auto-generated site names

* chore(deps): remove @sindresorhus/slugify as it is no longer needed

* docs: add comment to prevent people from adding invalid suggestion names

* refactor: remove server dependent site name generation util

* fix: pass empty string if no name is provided

* fix: use template name if site name is not available

* fix: avoid using default value as display text

* chore: fix tests

Co-authored-by: Daniel Tschinder <231804+danez@users.noreply.github.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 8, 2022
1 parent 85f961d commit 7c5e675
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 113 deletions.
68 changes: 3 additions & 65 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@
"@netlify/local-functions-proxy": "^1.1.1",
"@netlify/zip-it-and-ship-it": "^7.1.2",
"@octokit/rest": "^19.0.0",
"@sindresorhus/slugify": "^1.1.0",
"ansi-escapes": "^5.0.0",
"ansi-styles": "^5.0.0",
"ansi-to-html": "^0.7.2",
Expand Down
7 changes: 3 additions & 4 deletions src/commands/sites/sites-create-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,18 @@ const sitesCreateTemplate = async (repository, options, command) => {
}

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

// Allow the user to reenter site name if selected one isn't available
const inputSiteName = async (name) => {
const { name: inputName, siteSuggestion } = await getSiteNameInput(name, user, api)
const { name: inputName } = await getSiteNameInput(name)

try {
const siteName = inputName ? inputName.trim() : siteSuggestion
const siteName = inputName.trim()

// Create new repo from template
repoResp = await createRepo(templateName, ghToken, siteName)
repoResp = await createRepo(templateName, ghToken, siteName || templateName)

if (repoResp.errors) {
if (repoResp.errors[0].includes('Name already exists on this account')) {
Expand Down
41 changes: 5 additions & 36 deletions src/commands/sites/sites-create.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,28 @@
// @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')
const prettyjson = require('prettyjson')
const { v4: uuidv4 } = require('uuid')

const { chalk, error, getRepoData, log, logJson, track, warn } = require('../../utils')
const { configureRepo } = require('../../utils/init/config')
const { link } = require('../link')

const SITE_NAME_SUGGESTION_SUFFIX_LENGTH = 5

const getSiteNameInput = async (name, user, api) => {
let siteSuggestion
if (!user) user = await api.getCurrentUser()

const getSiteNameInput = async (name) => {
if (!name) {
let { slug } = user
let suffix = ''

// If the user doesn't have a slug, we'll compute one. Because `full_name` is not guaranteed to be unique, we
// append a short randomly-generated ID to reduce the likelihood of a conflict.
if (!slug) {
slug = slugify(user.full_name || user.email)
suffix = `-${uuidv4().slice(0, SITE_NAME_SUGGESTION_SUFFIX_LENGTH)}`
}

const suggestions = [
`super-cool-site-by-${slug}${suffix}`,
`the-awesome-${slug}-site${suffix}`,
`${slug}-makes-great-sites${suffix}`,
`netlify-thinks-${slug}-is-great${suffix}`,
`the-great-${slug}-site${suffix}`,
`isnt-${slug}-awesome${suffix}`,
]
siteSuggestion = sample(suggestions)

const { name: nameInput } = await inquirer.prompt([
{
type: 'input',
name: 'name',
message: 'Site name (you can change it later):',
default: siteSuggestion,
message: 'Site name (leave blank for a random name; you can change it later):',
validate: (input) =>
/^[a-zA-Z\d-]+$/.test(input || undefined) || 'Only alphanumeric characters and hyphens are allowed',
},
])
name = nameInput || siteSuggestion
name = nameInput || ''
}

return { name, siteSuggestion }
return { name }
}

/**
Expand Down Expand Up @@ -83,12 +53,11 @@ const sitesCreate = async (options, command) => {
accountSlug = accountSlugInput
}

let user
let site

// Allow the user to reenter site name if selected one isn't available
const inputSiteName = async (name) => {
const { name: siteName } = await getSiteNameInput(name, user, api)
const { name: siteName } = await getSiteNameInput(name)

const body = {}
if (typeof siteName === 'string') {
Expand Down
35 changes: 28 additions & 7 deletions tests/integration/420.command.init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ test('netlify init new site', async (t) => {
answer: answerWithValue(DOWN),
},
{ question: 'Team: (Use arrow keys)', answer: CONFIRM },
{ question: 'Site name (you can change it later)', answer: answerWithValue('test-site-name') },
{
question: 'Site name (leave blank for a random name; you can change it later)',
answer: answerWithValue('test-site-name'),
},
{
question: 'Your build command (hugo build/yarn run build/etc)',
answer: answerWithValue(command),
Expand Down Expand Up @@ -220,7 +223,10 @@ test('netlify init new Next.js site', async (t) => {
answer: answerWithValue(DOWN),
},
{ question: 'Team: (Use arrow keys)', answer: CONFIRM },
{ question: 'Site name (you can change it later)', answer: answerWithValue('test-site-name') },
{
question: 'Site name (leave blank for a random name; you can change it later)',
answer: answerWithValue('test-site-name'),
},
{
question: 'Your build command (hugo build/yarn run build/etc)',
answer: answerWithValue(command),
Expand Down Expand Up @@ -320,7 +326,10 @@ test('netlify init new Next.js site with correct default build directory and bui
answer: answerWithValue(DOWN),
},
{ question: 'Team: (Use arrow keys)', answer: CONFIRM },
{ question: 'Site name (you can change it later)', answer: answerWithValue('test-site-name') },
{
question: 'Site name (leave blank for a random name; you can change it later)',
answer: answerWithValue('test-site-name'),
},
{
question: 'Your build command (hugo build/yarn run build/etc)',
answer: CONFIRM,
Expand Down Expand Up @@ -509,7 +518,10 @@ test('netlify init new Gatsby site with correct default build directory and buil
answer: answerWithValue(DOWN),
},
{ question: 'Team: (Use arrow keys)', answer: CONFIRM },
{ question: 'Site name (you can change it later)', answer: answerWithValue('test-site-name') },
{
question: 'Site name (leave blank for a random name; you can change it later)',
answer: answerWithValue('test-site-name'),
},
{
question: 'Your build command (hugo build/yarn run build/etc)',
answer: CONFIRM,
Expand Down Expand Up @@ -612,7 +624,10 @@ windowsSkip('netlify init monorepo root and sub directory without netlify.toml',
answer: answerWithValue(DOWN),
},
{ question: 'Team: (Use arrow keys)', answer: CONFIRM },
{ question: 'Site name (you can change it later)', answer: answerWithValue('test-site-name') },
{
question: 'Site name (leave blank for a random name; you can change it later)',
answer: answerWithValue('test-site-name'),
},
{
question: 'Base directory (e.g. projects/frontend):',
answer: CONFIRM,
Expand Down Expand Up @@ -732,7 +747,10 @@ test('netlify init monorepo root with netlify.toml, subdirectory without netlify
answer: answerWithValue(DOWN),
},
{ question: 'Team: (Use arrow keys)', answer: CONFIRM },
{ question: 'Site name (you can change it later)', answer: answerWithValue('test-site-name') },
{
question: 'Site name (leave blank for a random name; you can change it later)',
answer: answerWithValue('test-site-name'),
},
{
question: 'Base directory (e.g. projects/frontend):',
answer: CONFIRM,
Expand Down Expand Up @@ -854,7 +872,10 @@ windowsSkip('netlify init monorepo root and sub directory with netlify.toml', as
answer: answerWithValue(DOWN),
},
{ question: 'Team: (Use arrow keys)', answer: CONFIRM },
{ question: 'Site name (you can change it later)', answer: answerWithValue('test-site-name') },
{
question: 'Site name (leave blank for a random name; you can change it later)',
answer: answerWithValue('test-site-name'),
},
{
question: 'Base directory (e.g. projects/frontend):',
answer: CONFIRM,
Expand Down

1 comment on commit 7c5e675

@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: 227 MB

Please sign in to comment.