From 9bbd1a4d959bafeca0ba2534c03dea114fc26183 Mon Sep 17 00:00:00 2001 From: Mark Tang Date: Sun, 8 May 2022 23:37:44 +0800 Subject: [PATCH] notice --- commands/init/lib/index.js | 48 ++++++++++++++++++++++++++------- commands/init/lib/templates.js | 16 +++++++++++ commands/init/package-lock.json | 21 +++++++++++++++ commands/init/package.json | 3 ++- core/cli/package-lock.json | 24 ++++++++++++++++- 5 files changed, 101 insertions(+), 11 deletions(-) create mode 100644 commands/init/lib/templates.js diff --git a/commands/init/lib/index.js b/commands/init/lib/index.js index a6c7051..b67bf0c 100644 --- a/commands/init/lib/index.js +++ b/commands/init/lib/index.js @@ -3,8 +3,10 @@ const fs = require('fs'); const inquirer = require('inquirer'); const fse = require('fs-extra'); +const semver = require('semver'); const Command = require('@dapp-cli/command'); const log = require('@dapp-cli/log'); +const templates = require('./templates'); const TYPE_PROJECT = 'project'; const TYPE_COMPONENT = 'component'; @@ -19,9 +21,12 @@ class InitCommand extends Command { async exec() { try { - const ret = await this.prepare(); - if (ret) { + const projectInfo = await this.prepare(); + if (projectInfo) { // 2. Download template + log.verbose('projectInfo', projectInfo); + this.projectInfo = projectInfo; + this.downloadTemplate(); // 3. Install template } } catch(e) { @@ -29,6 +34,10 @@ class InitCommand extends Command { } } + downloadTemplate() { + console.log(this.projectInfo, templates); + } + async prepare() { const localPath = process.cwd(); if(!this.isDirEmpty(localPath)) { @@ -62,7 +71,7 @@ class InitCommand extends Command { } async getProjectInfo() { - const projectInfo = {}; + let projectInfo = {}; const { type } = await inquirer.prompt({ type: 'list', name: 'type', @@ -79,13 +88,20 @@ class InitCommand extends Command { log.verbose('type', type); if (type === TYPE_PROJECT) { - const o = await inquirer.prompt([{ + const project = await inquirer.prompt([{ type: 'input', name: 'projectName', message: 'Please enter the project name', default: '', validate: function(v) { - return typeof v === 'string'; + const done = this.async(); + setTimeout(function () { + if (!/^[a-zA-Z]+([-][a-zA-Z][a-zA-Z0-9]*|[_][a-zA-Z][a-zA-Z0-9]*|[a-zA-Z0-9])*$/.test(v)) { + done('Please enter the project name in the correct format'); + return; + } + done(null, true); + }, 0); }, filter: function(v) { return v; @@ -94,15 +110,29 @@ class InitCommand extends Command { type: 'input', name: 'projectVersion', message: 'Please enter the project version number', - default: '', + default: '1.0.0', validate: function(v) { - return typeof v === 'string'; + const done = this.async(); + setTimeout(function () { + if (!(!!semver.valid(v))) { + done('Please enter the project version number in the correct format'); + return; + } + done(null, true); + }, 0); }, filter: function(v) { - return v; + if (!!semver.valid(v)) { + return semver.valid(v); + } else { + return v; + } } }]); - console.log(o); + projectInfo = { + type, + ...project + } } else if (type === TYPE_COMPONENT) { } diff --git a/commands/init/lib/templates.js b/commands/init/lib/templates.js new file mode 100644 index 0000000..93fa224 --- /dev/null +++ b/commands/init/lib/templates.js @@ -0,0 +1,16 @@ +module.exports = [ + { + id: 1, + name: 'normal', + npmName: 'dapp-cli-template-normal', + version: '1.0.0', + disabled: false + }, + { + id: 2, + name: 'NFT-Market', + npmName: 'dapp-cli-template-nft-market', + version: '0.0.0', + disabled: true + } +] \ No newline at end of file diff --git a/commands/init/package-lock.json b/commands/init/package-lock.json index a55d9c1..185d330 100644 --- a/commands/init/package-lock.json +++ b/commands/init/package-lock.json @@ -233,6 +233,14 @@ "is-unicode-supported": "^0.1.0" } }, + "lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "requires": { + "yallist": "^4.0.0" + } + }, "mimic-fn": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", @@ -314,6 +322,14 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "semver": { + "version": "7.3.7", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.7.tgz", + "integrity": "sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==", + "requires": { + "lru-cache": "^6.0.0" + } + }, "signal-exit": { "version": "3.0.7", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz", @@ -403,6 +419,11 @@ "string-width": "^4.1.0", "strip-ansi": "^6.0.0" } + }, + "yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" } } } diff --git a/commands/init/package.json b/commands/init/package.json index 383b78f..73653d5 100644 --- a/commands/init/package.json +++ b/commands/init/package.json @@ -27,7 +27,8 @@ "@dapp-cli/command": "file:../../models/command", "@dapp-cli/log": "file:../../utils/log", "fs-extra": "^10.1.0", - "inquirer": "^8.2.4" + "inquirer": "^8.2.4", + "semver": "^7.3.7" }, "bugs": { "url": "https://github.com/MarkTangCd/dapp-cli/issues" diff --git a/core/cli/package-lock.json b/core/cli/package-lock.json index 89323e5..f82b384 100644 --- a/core/cli/package-lock.json +++ b/core/cli/package-lock.json @@ -3366,7 +3366,8 @@ "@dapp-cli/command": "file:../../models/command", "@dapp-cli/log": "file:../../utils/log", "fs-extra": "^10.1.0", - "inquirer": "^8.2.4" + "inquirer": "^8.2.4", + "semver": "^7.3.7" }, "dependencies": { "@dapp-cli/command": { @@ -3941,6 +3942,14 @@ "is-unicode-supported": "^0.1.0" } }, + "lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "requires": { + "yallist": "^4.0.0" + } + }, "mimic-fn": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", @@ -4022,6 +4031,14 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "semver": { + "version": "7.3.7", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.7.tgz", + "integrity": "sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==", + "requires": { + "lru-cache": "^6.0.0" + } + }, "signal-exit": { "version": "3.0.7", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz", @@ -4111,6 +4128,11 @@ "string-width": "^4.1.0", "strip-ansi": "^6.0.0" } + }, + "yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" } } },