Skip to content

Commit

Permalink
chore: validate project name
Browse files Browse the repository at this point in the history
  • Loading branch information
w2xi committed Mar 30, 2024
1 parent 7bfdbad commit d0d2392
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
6 changes: 2 additions & 4 deletions bin/migi-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ask from '../lib/ask'
import generate from '../lib/generate'
import getOptions from '../lib/options'
import fetchTemplates from '../lib/fetch-templates'
import { setPromptDefault } from '../lib/ask'
import { setPromptDefault, setValidateName } from '../lib/ask'
import type { AnswerOptions, GitHubRepo, Options } from '../lib/types'

const download = promisify(require('download-git-repo'))
Expand Down Expand Up @@ -91,13 +91,11 @@ async function run() {
}

setPromptDefault('name', projectName)
setValidateName()

ask().then(result => {
Object.assign(answers, result)
const opts = getOptions(answers, { projectName })

console.log(opts)

downloadAndGenerate(opts)
})
}
Expand Down
28 changes: 23 additions & 5 deletions lib/ask.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import inquirer from "inquirer";
import inquirer from "inquirer"
import validatePkgName from "validate-npm-package-name"
import type { Question } from "inquirer"

export const prompts = {
export const prompts: Record<string, Question> = {
name: {
name: "name",
type: "string",
required: true,
message: "Project name",
},
version: {
name: 'version',
type: 'string',
required: false,
message: 'Project version',
default: '0.0.1'
},
description: {
name: "description",
type: "string",
required: false,
message: "Project description",
},
author: {
Expand All @@ -34,6 +33,25 @@ export function setPromptDefault(name: string, value: string) {
}
}

export function setValidateName() {
const name = prompts.name
const customValidate = name.validate
name.validate = (name: string) => {
if (!name) {
return 'Project name is required'
}
const its = validatePkgName(name)
if (!its.validForNewPackages) {
const errors = (its.errors || []).concat(its.warnings || [])
return 'Sorry, ' + errors.join(' and ') + '.'
}
if (typeof customValidate === 'function') {
return customValidate(name)
}
return true
}
}

export default function ask() {
return inquirer.prompt(Object.values(prompts)).then((answers) => {
return { ...answers }
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@
"find-up": "5.0.0",
"fs-extra": "^11.2.0",
"inquirer": "8.2.6",
"ora": "5.0.0"
"ora": "5.0.0",
"semver": "^7.6.0",
"validate-npm-package-name": "^5.0.0"
},
"devDependencies": {
"@types/ejs": "^3.1.5",
"@types/fs-extra": "^11.0.4",
"@types/inquirer": "^9.0.7",
"@types/node": "^20.11.28",
"@types/validate-npm-package-name": "^4.0.2",
"bumpp": "^9.4.0",
"tsup": "^8.0.2",
"typescript": "^5.4.2"
Expand Down
29 changes: 26 additions & 3 deletions pnpm-lock.yaml

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

0 comments on commit d0d2392

Please sign in to comment.