diff --git a/bin/migi-create.ts b/bin/migi-create.ts index b5b5b1a..90d5f0b 100644 --- a/bin/migi-create.ts +++ b/bin/migi-create.ts @@ -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')) @@ -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) }) } diff --git a/lib/ask.ts b/lib/ask.ts index 39db2c0..d932a1b 100644 --- a/lib/ask.ts +++ b/lib/ask.ts @@ -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 = { 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: { @@ -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 } diff --git a/package.json b/package.json index 24ae8c9..4fa9fdc 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b0ffe4d..2b4a83b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -35,6 +35,12 @@ dependencies: ora: specifier: 5.0.0 version: 5.0.0 + semver: + specifier: ^7.6.0 + version: 7.6.0 + validate-npm-package-name: + specifier: ^5.0.0 + version: 5.0.0 devDependencies: '@types/ejs': @@ -49,6 +55,9 @@ devDependencies: '@types/node': specifier: ^20.11.28 version: 20.11.28 + '@types/validate-npm-package-name': + specifier: ^4.0.2 + version: 4.0.2 bumpp: specifier: ^9.4.0 version: 9.4.0 @@ -510,6 +519,10 @@ packages: '@types/node': 20.11.28 dev: true + /@types/validate-npm-package-name@4.0.2: + resolution: {integrity: sha512-lrpDziQipxCEeK5kWxvljWYhUvOiB2A9izZd9B2AFarYAkqZshb4lPbRs7zKEic6eGtH8V/2qJW+dPp9OtF6bw==} + dev: true + /acorn@8.11.3: resolution: {integrity: sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==} engines: {node: '>=0.4.0'} @@ -646,6 +659,12 @@ packages: ieee754: 1.2.1 dev: false + /builtins@5.0.1: + resolution: {integrity: sha512-qwVpFEHNfhYJIzNRBvd2C1kyo6jz3ZSMPyyuR47OPdiKWlbYnZNyDWuyR175qDnAJLiCo5fBBqPb3RiXgWlkOQ==} + dependencies: + semver: 7.6.0 + dev: false + /bumpp@9.4.0: resolution: {integrity: sha512-T+N+B5mSSvsTt7kgxGXCKQd957IxmEZwMn2nWirspGUY08u/cBNrpQDMXk43fzLegZtKtXS1v9S06vq2zf6lXQ==} engines: {node: '>=10'} @@ -1658,7 +1677,6 @@ packages: engines: {node: '>=10'} dependencies: yallist: 4.0.0 - dev: true /make-dir@1.3.0: resolution: {integrity: sha512-2w31R7SJtieJJnQtGc7RVL2StM2vGYVfqUOvUDxH6bC6aJTxPxTF0GnIgCyu7tjockiUWAYQRbxa7vKn34s5sQ==} @@ -2200,7 +2218,6 @@ packages: hasBin: true dependencies: lru-cache: 6.0.0 - dev: true /shebang-command@2.0.0: resolution: {integrity: sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==} @@ -2534,6 +2551,13 @@ packages: resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==} dev: false + /validate-npm-package-name@5.0.0: + resolution: {integrity: sha512-YuKoXDAhBYxY7SfOKxHBDoSyENFeW5VvIIQp2TGQuit8gpK6MnWaQelBKxso72DoxTZfZdcP3W90LqpSkgPzLQ==} + engines: {node: ^14.17.0 || ^16.13.0 || >=18.0.0} + dependencies: + builtins: 5.0.1 + dev: false + /wcwidth@1.0.1: resolution: {integrity: sha512-XHPEwS0q6TaxcvG85+8EYkbiCux2XtWG2mkc47Ng2A77BQu9+DqIOJldST4HgPkuea7dvKSj5VgX3P1d4rW8Tg==} dependencies: @@ -2598,7 +2622,6 @@ packages: /yallist@4.0.0: resolution: {integrity: sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==} - dev: true /yaml@2.4.1: resolution: {integrity: sha512-pIXzoImaqmfOrL7teGUBt/T7ZDnyeGBWyXQBvOVhLkWLN37GXv8NMLK406UY6dS51JfcQHsmcW5cJ441bHg6Lg==}