From fd2bd12e3cf6f850ba8716d407353cf566562159 Mon Sep 17 00:00:00 2001 From: lcxfs1991 Date: Thu, 23 Jun 2016 19:37:30 +0800 Subject: [PATCH] add remove project feature --- README.md | 2 ++ bin/steamer.js | 18 ++++++--------- libs/SteamerErrWarning.js | 5 ++++- libs/SteamerInit.js | 28 ++++++++++++++++++++++++ libs/SteamerRemove.js | 45 ++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- template/steamer.config.js | 18 --------------- 7 files changed, 88 insertions(+), 31 deletions(-) create mode 100644 libs/SteamerInit.js create mode 100644 libs/SteamerRemove.js diff --git a/README.md b/README.md index 0cb7cc1..fd86b0c 100755 --- a/README.md +++ b/README.md @@ -91,6 +91,8 @@ module.exports = steamerConfig; steamer --init 初始化项目并生成初始steamer.config.js +steamer -- remove [project] 删除项目文件夹及对应steamer.config.js中projects的配置 + steamer --dev 启动开发命令 steamer --pub 启动发布命令 diff --git a/bin/steamer.js b/bin/steamer.js index d7d59bc..5f8632f 100755 --- a/bin/steamer.js +++ b/bin/steamer.js @@ -14,7 +14,9 @@ const Warning = require('../libs/SteamerErrWarning'), Logger = require('../libs/SteamerLogger'), Get = require('../libs/SteamerGet'), Install = require('../libs/SteamerInstall'), - DevOrPub = require('../libs/SteamerDevOrPub'); + DevOrPub = require('../libs/SteamerDevOrPub'), + Init = require('../libs/SteamerInit'), + Remove = require('../libs/SteamerRemove'); var steamerConfigPath = path.resolve('steamer.config.js'), steamerConfig = {}, @@ -44,16 +46,7 @@ function readConfig() { function init() { if (argv.init) { - // avoid overriding - let configSrcPath = "./node_modules/steamer/template/steamer.config.js", - configDestPath = path.resolve('steamer.config.js'); - if (fs.existsSync(configDestPath)) { - throw new Warning.FileExistErr("steamer.config.js"); - } - - let configStr = fs.readFileSync(configSrcPath); - fs.writeFileSync(configDestPath, configStr); - Logger.log('steamer.config.js is initiated'); + Init(); } steamerConfig = readConfig(); @@ -77,6 +70,9 @@ function init() { else if (argv.get) { Get(steamerConfig); } + else if (argv.remove) { + Remove(steamerConfig); + } else if (argv.init) { } diff --git a/libs/SteamerErrWarning.js b/libs/SteamerErrWarning.js index 3c1dc43..9f9ce1f 100755 --- a/libs/SteamerErrWarning.js +++ b/libs/SteamerErrWarning.js @@ -27,7 +27,10 @@ module.exports = { FileExistErr: function(file) { console.log((file + " has existed").red); }, + FolderNotExistErr: function(folder) { + console.log((folder + " has existed").red); + }, HasNoRepo: function(repo) { console.log(("steamer has no repo " + repo).red); - } + }, }; \ No newline at end of file diff --git a/libs/SteamerInit.js b/libs/SteamerInit.js new file mode 100644 index 0000000..04e5bee --- /dev/null +++ b/libs/SteamerInit.js @@ -0,0 +1,28 @@ +"use strict"; +/** + * init steamer.config.js + * by heyli 2016.06.23 + */ + +const path = require('path'), + fs = require('fs'), + Warning = require('../libs/SteamerErrWarning'), + Logger = require('../libs/SteamerLogger'); + +function initSteamerConfig() { + // avoid overriding + let configSrcPath = "./node_modules/steamer/template/steamer.config.js", + configDestPath = path.resolve('steamer.config.js'); + if (fs.existsSync(configDestPath)) { + throw new Warning.FileExistErr("steamer.config.js"); + } + + let configStr = fs.readFileSync(configSrcPath); + fs.writeFileSync(configDestPath, configStr); + Logger.log('steamer.config.js is initiated'); +} + + +module.exports = function() { + initSteamerConfig(); +}; \ No newline at end of file diff --git a/libs/SteamerRemove.js b/libs/SteamerRemove.js new file mode 100644 index 0000000..7e412a4 --- /dev/null +++ b/libs/SteamerRemove.js @@ -0,0 +1,45 @@ +"use strict"; +/** + * remove projects + * by heyli 2016.06.23 + */ + +const fs = require('fs-extra'), + path = require('path'), + argv = require('yargs').argv; + +const Warning = require('../libs/SteamerErrWarning'), + Logger = require('../libs/SteamerLogger'); + +var steamerConfig = {}, + projectConfig = {}; + +function RemoveProject(project) { + let folderPath = path.resolve(project); + + if (!fs.existsSync(folderPath)) { + throw new Warning.FolderNotExistErr(folderPath); + } + + fs.remove(folderPath); + + let config = require(path.resolve('steamer.config.js')); + + if (config.projects.hasOwnProperty(project)) { + delete config.projects[project]; + let configStr = 'var steamerConfig = ' + JSON.stringify(config, null, 4) + + '\n ' + 'module.exports = steamerConfig;'; + + fs.writeFileSync(path.resolve('steamer.config.js'), configStr); + } + + Logger.log("remove project " + project + " success"); +} + + +module.exports = function(steamerConfig) { + steamerConfig = steamerConfig; + projectConfig = steamerConfig.projects; + + RemoveProject(argv.remove); +}; \ No newline at end of file diff --git a/package.json b/package.json index 752bf6c..8f4f854 100755 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ "lodash": "^4.13.1", "yargs": "^4.7.1", "colors": "^1.1.2", - "requireg": "^0.1.5" + "requireg": "^0.1.5", + "fs-extra": "^0.30.0" }, "devDependencies": { diff --git a/template/steamer.config.js b/template/steamer.config.js index 132de66..e744ebd 100644 --- a/template/steamer.config.js +++ b/template/steamer.config.js @@ -2,24 +2,6 @@ var steamerConfig = { projects: { }, - steps: { - dev: { - start: function(config) { - - }, - finish: function(config) { - - } - }, - pub: { - start: function(config) { - - }, - finish: function(config) { - - } - }, - } }; module.exports = steamerConfig; \ No newline at end of file