From 5c7e255b613ac4f64d2eaddd613c6f0611caceff Mon Sep 17 00:00:00 2001 From: Mark Tang Date: Fri, 13 May 2022 23:58:36 +0800 Subject: [PATCH] template install done --- README-CN.md | 0 README.md | 10 +++++++++- commands/init/lib/index.js | 28 ++++++++++++++++++++++++++-- commands/init/lib/templates.js | 8 ++++++-- core/exec/lib/index.js | 11 +---------- core/exec/package-lock.json | 13 +++++++++++++ core/exec/package.json | 3 ++- utils/utils/lib/index.js | 14 ++++++++++++-- 8 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 README-CN.md diff --git a/README-CN.md b/README-CN.md new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md index eebc87d..bb9c724 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -## dapp-cli +[中文](https://github.com/MarkTangCd/dapp-cli/blob/main/README-CN.md) / English +
+ +

Dapp Cli

+

+ Definitive Guide for Decentralized-app(Dapp) Development on Blockchain +

+

Step-by-step Dapp practice through actual projects.

+
\ No newline at end of file diff --git a/commands/init/lib/index.js b/commands/init/lib/index.js index caad6ae..5ebc556 100644 --- a/commands/init/lib/index.js +++ b/commands/init/lib/index.js @@ -9,7 +9,7 @@ const userHome = require('user-home'); const Command = require('@dapp-cli/command'); const Package = require('@dapp-cli/package'); const log = require('@dapp-cli/log'); -const { spinnerStart, sleep } = require('@dapp-cli/utils'); +const { spinnerStart, sleep, exec: spwan } = require('@dapp-cli/utils'); const templates = require('./templates'); const TYPE_PROJECT = 'project'; @@ -48,7 +48,29 @@ class InitCommand extends Command { } async installNormalTemplate() { - console.log('normal template') + log.verbose('templateNpm', this.templateNpm); + let spinner = spinnerStart('Template being installed'); + await sleep(); + try { + const templatePath = path.resolve(this.templateNpm.cacheFilePath, 'template'); + const targetPath = process.cwd(); + fse.ensureDirSync(templatePath); + fse.ensureDirSync(targetPath); + fse.copySync(templatePath, targetPath); + } catch(e) { + throw e; + } finally { + spinner.stop(true); + log.success('Template installed successfully.'); + } + // 依赖安装 + const { installCommand, startCommand } = this.templateInfo; + if (installCommand && installCommand.length > 0) { + const cmd = installCommand[0]; + const args = installCommand.slice(1); + console.log(cmd, args); + } + // 启动命令执行 } async downloadTemplate() { @@ -75,6 +97,7 @@ class InitCommand extends Command { spinner.stop(true); if (templateNpm.exists()) { log.success('Download template successfully'); + this.templateNpm = templateNpm; } } } else { @@ -88,6 +111,7 @@ class InitCommand extends Command { spinner.stop(true); if (templateNpm.exists()) { log.success('Update template successfully'); + this.templateNpm = templateNpm; } } } diff --git a/commands/init/lib/templates.js b/commands/init/lib/templates.js index 33d9e42..1824dd9 100644 --- a/commands/init/lib/templates.js +++ b/commands/init/lib/templates.js @@ -4,14 +4,18 @@ module.exports = [ name: 'Normal', npmName: 'dapp-cli-template-normal', version: '1.0.0', - disabled: false + disabled: false, + installCommand: ["yarn"], + startCommand: [] }, { id: 2, name: 'NFT-Market', npmName: 'dapp-cli-template-nft-market', version: '0.0.0', - disabled: true + disabled: true, + installCommand: ["npm", "install"], + startCommand: [] }, ] \ No newline at end of file diff --git a/core/exec/lib/index.js b/core/exec/lib/index.js index 6c34cb4..ff382de 100644 --- a/core/exec/lib/index.js +++ b/core/exec/lib/index.js @@ -1,9 +1,9 @@ 'use strict'; -const cp = require('child_process'); const path = require('path'); const Package = require('@dapp-cli/package'); const log = require('@dapp-cli/log'); +const { exec: spawn } = require('@dapp-cli/utils'); const SETTINGS = { init: '@dapp-cli/init' @@ -88,13 +88,4 @@ async function exec() { } -function spawn(command, args, options) { - const win32 = process.platform === 'win32'; - - const cmd = win32 ? 'cmd' : command; - const cmdArgs = win32 ? ['/c'].concat(command, args) : args; - - return cp.spawn(cmd, cmdArgs, options || {}); -} - module.exports = exec; diff --git a/core/exec/package-lock.json b/core/exec/package-lock.json index 417076e..e544a6d 100644 --- a/core/exec/package-lock.json +++ b/core/exec/package-lock.json @@ -158,6 +158,19 @@ }, "@dapp-cli/package": { "version": "file:../../models/package" + }, + "@dapp-cli/utils": { + "version": "file:../../utils/utils", + "requires": { + "cli-spinner": "^0.2.10" + }, + "dependencies": { + "cli-spinner": { + "version": "0.2.10", + "resolved": "https://registry.npmjs.org/cli-spinner/-/cli-spinner-0.2.10.tgz", + "integrity": "sha512-U0sSQ+JJvSLi1pAYuJykwiA8Dsr15uHEy85iCJ6A+0DjVxivr3d+N2Wjvodeg89uP5K6TswFkKBfAD7B3YSn/Q==" + } + } } } } diff --git a/core/exec/package.json b/core/exec/package.json index de0903e..1428e84 100644 --- a/core/exec/package.json +++ b/core/exec/package.json @@ -25,7 +25,8 @@ }, "dependencies": { "@dapp-cli/package": "file:../../models/package", - "@dapp-cli/log": "file:../../utils/log" + "@dapp-cli/log": "file:../../utils/log", + "@dapp-cli/utils": "file:../../utils/utils" }, "bugs": { "url": "https://github.com/MarkTangCd/dapp-cli/issues" diff --git a/utils/utils/lib/index.js b/utils/utils/lib/index.js index 2b7ad3f..c375824 100644 --- a/utils/utils/lib/index.js +++ b/utils/utils/lib/index.js @@ -1,5 +1,5 @@ 'use strict'; - +const cp = require('child_process'); function isObject(o) { return Object.prototype.toString.call(o) === '[object Object]'; @@ -17,8 +17,18 @@ function sleep(timeout = 1000) { return new Promise(resolve => setTimeout(resolve, timeout)); } +function exec(command, args, options) { + const win32 = process.platform === 'win32'; + + const cmd = win32 ? 'cmd' : command; + const cmdArgs = win32 ? ['/c'].concat(command, args) : args; + + return cp.spawn(cmd, cmdArgs, options || {}); +} + module.exports = { isObject, spinnerStart, - sleep + sleep, + exec };