From 5d652ab35310a665eeb49bb690b4443e91ffa5b0 Mon Sep 17 00:00:00 2001 From: lcxfs1991 Date: Tue, 5 Dec 2017 00:32:35 +0800 Subject: [PATCH] add readSteamerDefaultConfig --- CHANGELOG.md | 4 + README.md | 11 ++ index.js | 452 ++++++++++++++++++++++++++------------------------ package.json | 2 +- test/index.js | 59 ++++++- 5 files changed, 298 insertions(+), 230 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fb0f96..ec8f987 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,6 @@ +## 1.0.1 + - 优化测试用例 + - 新增 `readSteamerDefaultConfig` 方法 + ## 1.0.0 - 提供各类 `steamerjs` 插件所需的基础功能 \ No newline at end of file diff --git a/README.md b/README.md index cffd2ee..f333d34 100755 --- a/README.md +++ b/README.md @@ -76,6 +76,17 @@ - `option.overwrite`, `Boolean` 是否覆盖已经存在的配置文件 - `option.isGlobal`, `Boolean` 是否全局 +* readSteamerDefaultConfig + - `Function` + - 读取 `steamerjs` 配置与默认配置的并集 + - 默认值 + ```javascript + NPM: 'npm', + PLUGIN_PREFIX: 'steamer-plugin-', + KIT_PREFIX: 'steamer-kit-', + TEAM_PREFIX: 'steamer-team-' + ``` + * readSteamerConfig - `Function` - 读取 `steamerjs` 配置 diff --git a/index.js b/index.js index bdbefbd..fd28d97 100755 --- a/index.js +++ b/index.js @@ -1,292 +1,304 @@ -'use strict'; - -const os = require('os'), - path = require('path'), - fs = require('fs-extra'), - chalk = require('chalk'), - _ = require('lodash'), - spawn = require('cross-spawn'); +const os = require('os'); +const path = require('path'); +const fs = require('fs-extra'); +const chalk = require('chalk'); +const _ = require('lodash'); + +const DEFAULT_CONFIG = { + NPM: 'npm', + PLUGIN_PREFIX: 'steamer-plugin-', + KIT_PREFIX: 'steamer-kit-', + TEAM_PREFIX: 'steamer-team-' +}; class SteamerPlugin { - constructor() { - this.fs = fs; - this.chalk = chalk; - this._ = _; - this.pluginName = _.kebabCase(SteamerPlugin.name); - } + constructor() { + this.fs = fs; + this.chalk = chalk; + this._ = _; + this.pluginName = _.kebabCase(SteamerPlugin.name); + this.defaultConfig = DEFAULT_CONFIG; + } - init() { - this.warn('You do not write any ini logics.'); - } + init() { + this.warn('You do not write any ini logics.'); + } - help() { - this.warn('You do not add any help content.'); - } + help() { + this.warn('You do not add any help content.'); + } - version() { + version() { - } + } - /** + /** * get global node_modules path * @returns {String} */ - getGlobalModules() { - let env = process.env; + getGlobalModules() { + let env = process.env; - if (env.NODE_PATH && env.NODE_PATH !== 'undefined') { - return env.NODE_PATH; - } + if (env.NODE_PATH && env.NODE_PATH !== 'undefined') { + return env.NODE_PATH; + } - let globalModules = require('global-modules'); + let globalModules = require('global-modules'); - if (this.fs.existsSync(globalModules)) { - return globalModules; - } + if (this.fs.existsSync(globalModules)) { + return globalModules; + } - // No Longer Support Compatible Mode - return null; - } + // No Longer Support Compatible Mode + return null; + } - /** + /** * get global home dir * @returns {String} */ - getGlobalHome() { - return os.homedir() || process.cwd(); - } + getGlobalHome() { + return os.homedir() || process.cwd(); + } - /** + /** * Create config file * @param {Object} config [config object] * @param {Object} option [options] */ - createConfig(config = {}, option = {}) { - // config file path: [folder]./steamer/[filename].[extension] - var folder = (option.isGlobal) ? this.getGlobalHome() : (option.folder || process.cwd()), - filename = option.filename || this.pluginName, - extension = option.extension || 'js', - overwrite = option.overwrite || false; // overwrite the config file or not + createConfig(config = {}, option = {}) { + // config file path: [folder]./steamer/[filename].[extension] + let folder = (option.isGlobal) ? this.getGlobalHome() : (option.folder || process.cwd()), + filename = option.filename || this.pluginName, + extension = option.extension || 'js', + overwrite = option.overwrite || false; // overwrite the config file or not - var configFile = path.resolve(path.join(folder, '.steamer/' + filename + '.' + extension)); + let configFile = path.resolve(path.join(folder, '.steamer/' + filename + '.' + extension)); - if (!overwrite && this.fs.existsSync(configFile)) { - throw new Error(configFile + ' exists'); - } + if (!overwrite && this.fs.existsSync(configFile)) { + throw new Error(configFile + ' exists'); + } - this._writeFile(configFile, filename, config); - } + this._writeFile(configFile, filename, config); + } - /** + /** * read config file, local config extends global config * @param {Object} config [config object] * @param {Object} option [options] */ - readConfig(option = {}) { - let folder = option.folder || process.cwd(), - filename = option.filename || this.pluginName, - extension = option.extension || 'js', - isGlobal = option.isGlobal || false; + readConfig(option = {}) { + let folder = option.folder || process.cwd(), + filename = option.filename || this.pluginName, + extension = option.extension || 'js', + isGlobal = option.isGlobal || false; - let globalConfigFile = path.resolve(path.join(this.getGlobalHome(), '.steamer/' + filename + '.' + extension)), - globalConfig = this._readFile(globalConfigFile); + let globalConfigFile = path.resolve(path.join(this.getGlobalHome(), '.steamer/' + filename + '.' + extension)), + globalConfig = this._readFile(globalConfigFile); - if (isGlobal) { - return globalConfig; - } + if (isGlobal) { + return globalConfig; + } - let localConfigFile = path.resolve(path.join(folder, '.steamer/' + filename + '.' + extension)), - localConfig = this._readFile(localConfigFile); + let localConfigFile = path.resolve(path.join(folder, '.steamer/' + filename + '.' + extension)), + localConfig = this._readFile(localConfigFile); - return _.merge({}, globalConfig, localConfig); - } + return _.merge({}, globalConfig, localConfig); + } - /** + /** * read steamerjs config, local config extends global config * @return {Object} [steamer config] */ - readSteamerConfig(option = {}) { - let isGlobal = option.isGlobal || false; + readSteamerConfig(option = {}) { + let isGlobal = option.isGlobal || false; - let globalConfigFile = path.join(this.getGlobalHome(), '.steamer/steamer.js'), - globalConfig = this._readFile(globalConfigFile); + let globalConfigFile = path.join(this.getGlobalHome(), '.steamer/steamer.js'), + globalConfig = this._readFile(globalConfigFile); - if (isGlobal) { - return globalConfig; - } + if (isGlobal) { + return globalConfig; + } - let localConfigFile = path.join(process.cwd(), '.steamer/steamer.js'), - localConfig = this._readFile(localConfigFile); + let localConfigFile = path.join(process.cwd(), '.steamer/steamer.js'), + localConfig = this._readFile(localConfigFile); - return _.merge({}, globalConfig, localConfig); - } + return _.merge({}, globalConfig, localConfig); + } - /** + /** + * read steamerjs config default config merge with value from steamerSteamerConfig + */ + readSteamerDefaultConfig() { + return this._.merge({}, this.defaultConfig, this.readSteamerConfig()); + } + + /** * create steamerjs config */ - createSteamerConfig(config = {}, options = {}) { - let folder = (options.isGlobal) ? this.getGlobalHome() : process.cwd(), - overwrite = options.overwrite || false; - - let configFile = path.join(folder, '.steamer/steamer.js'); - - try { - if (!overwrite && this.fs.existsSync(configFile)) { - throw new Error(configFile + ' exists'); - } - this._writeFile(configFile, 'steamerjs', config); - } - catch (e) { - this.error(e.stack); - throw e; - } - } - - /** + createSteamerConfig(config = {}, options = {}) { + let folder = (options.isGlobal) ? this.getGlobalHome() : process.cwd(), + overwrite = options.overwrite || false; + + let configFile = path.join(folder, '.steamer/steamer.js'); + + try { + if (!overwrite && this.fs.existsSync(configFile)) { + throw new Error(configFile + ' exists'); + } + this._writeFile(configFile, 'steamerjs', config); + } + catch (e) { + this.error(e.stack); + throw e; + } + } + + /** * read config file * @param {String} filepath [file path] * @return {Object} [config object] */ - _readFile(filepath) { - let config = {}; - - try { - // 获取真实路径 - filepath = fs.realpathSync(filepath); - if (require.cache[filepath]) { - delete require.cache[filepath]; - } - - config = require(filepath) || {}; - config = config.config; - } - catch (e) { - return config; - } - - return config; - } - - /** + _readFile(filepath) { + let config = {}; + + try { + // 获取真实路径 + filepath = fs.realpathSync(filepath); + if (require.cache[filepath]) { + delete require.cache[filepath]; + } + + config = require(filepath) || {}; + config = config.config; + } + catch (e) { + return config; + } + + return config; + } + + /** * write config file * @param {String} filepath [config file path] * @param {Object|String} [config content] */ - _writeFile(filepath, plugin, config) { - let extension = path.extname(filepath); - let isJs = extension === '.js', - newConfig = { - plugin: plugin, - config: config - }, - contentPrefix = (isJs) ? 'module.exports = ' : '', - contentPostfix = (isJs) ? ';' : '', - content = contentPrefix + JSON.stringify(newConfig, null, 4) + contentPostfix; - - try { - this.fs.ensureFileSync(filepath); - this.fs.writeFileSync(filepath, content, 'utf-8'); - } - catch (e) { - throw e; - } - } - - /** + _writeFile(filepath, plugin, config) { + let extension = path.extname(filepath); + let isJs = extension === '.js', + newConfig = { + plugin: plugin, + config: config + }, + contentPrefix = (isJs) ? 'module.exports = ' : '', + contentPostfix = (isJs) ? ';' : '', + content = contentPrefix + JSON.stringify(newConfig, null, 4) + contentPostfix; + + try { + this.fs.ensureFileSync(filepath); + this.fs.writeFileSync(filepath, content, 'utf-8'); + } + catch (e) { + throw e; + } + } + + /** * log things * @param str * @param color * @returns {String} */ - log(str, color = 'white') { - str = str || ''; - str = _.isObject(str) ? JSON.stringify(str) : str; - let msg = chalk[color](str); + log(str, color = 'white') { + str = str || ''; + str = _.isObject(str) ? JSON.stringify(str) : str; + let msg = chalk[color](str); - console.info(msg); - return msg; - } + console.info(msg); + return msg; + } - /** + /** * print errors * @param str */ - error(str) { - this.log(str, 'red'); - } + error(str) { + this.log(str, 'red'); + } - /** + /** * print infos * @param str */ - info(str) { - this.log(str, 'cyan'); - } + info(str) { + this.log(str, 'cyan'); + } - /** + /** * print warnings * @param str */ - warn(str) { - this.log(str, 'yellow'); - } + warn(str) { + this.log(str, 'yellow'); + } - /** + /** * pring success info * @param str */ - success(str) { - this.log(str, 'green'); - } + success(str) { + this.log(str, 'green'); + } - /** + /** * print title message * @param {String} color [color name] * @return {String} [msg with color] */ - printTitle(str, color = 'white') { - var msg = '', - str = ' ' + str + ' ', - len = str.length, - maxLen = process.stdout.columns || 84; + printTitle(str, color = 'white') { + var msg = '', + str = ' ' + str + ' ', + len = str.length, + maxLen = process.stdout.columns || 84; - var padding = '='.repeat(Math.floor((maxLen - len) / 2)); + let padding = '='.repeat(Math.floor((maxLen - len) / 2)); - msg += padding + str + padding; + msg += padding + str + padding; - return this.log(msg, color); - } + return this.log(msg, color); + } - /** + /** * print end message * @param {String} color [color name] * @return {String} [msg with color] */ - printEnd(color) { - var msg = '', - color = color || 'white', - maxLen = process.stdout.columns || 84; + printEnd(color) { + var msg = '', + color = color || 'white', + maxLen = process.stdout.columns || 84; - msg += '='.repeat(maxLen); + msg += '='.repeat(maxLen); - return this.log(msg, color); - } + return this.log(msg, color); + } - /** + /** * print command usage * @param {String} description [description of the command] * @param {String} cmd [command name] * @return {String} [message] */ - printUsage(description, cmd) { - var msg = chalk.green('\nusage: \n'), - cmd = cmd || this.pluginName.replace(this.pluginPrefix, ''); + printUsage(description, cmd) { + var msg = chalk.green('\nusage: \n'), + cmd = cmd || this.pluginName.replace(this.pluginPrefix, ''); - msg += 'steamer ' + cmd + ' ' + description + '\n'; - this.info(msg); - } + msg += 'steamer ' + cmd + ' ' + description + '\n'; + this.info(msg); + } - /** + /** * print command option * @param {Array} options [array of options] - option {String} full option @@ -295,51 +307,51 @@ class SteamerPlugin { - description {String} option description * @return {String} [message] */ - printOption(options) { - var options = options || []; + printOption(options) { + var options = options || []; - var maxColumns = process.stdout.columns || 84, - maxOptionLength = 0; + let maxColumns = process.stdout.columns || 84, + maxOptionLength = 0; - var msg = chalk.green('options: \n'); + let msg = chalk.green('options: \n'); - options.map((item) => { - let option = item.option || '', - alias = item.alias || '', - value = item.value || ''; + options.map((item) => { + let option = item.option || '', + alias = item.alias || '', + value = item.value || ''; - let msg = ' --' + option; + let msg = ' --' + option; - msg += (alias) ? ', -' + alias : ''; - msg += (value) ? ' ' + value : ''; + msg += (alias) ? ', -' + alias : ''; + msg += (value) ? ' ' + value : ''; - item.msg = msg; + item.msg = msg; - let msgLen = msg.length; + let msgLen = msg.length; - maxOptionLength = (msgLen > maxOptionLength) ? msgLen : maxOptionLength; + maxOptionLength = (msgLen > maxOptionLength) ? msgLen : maxOptionLength; - return item; - }); + return item; + }); - options.map((item) => { - let length = item.msg.length; + options.map((item) => { + let length = item.msg.length; - let space = ' '.repeat(maxOptionLength - length); + let space = ' '.repeat(maxOptionLength - length); - item.msg = item.msg + space + ' '; + item.msg = item.msg + space + ' '; - return item; - }); + return item; + }); - options.map((item) => { - item.msg += item.description + '\n'; + options.map((item) => { + item.msg += item.description + '\n'; - msg += item.msg; - }); + msg += item.msg; + }); - this.info(msg); - }; -}; + this.info(msg); + } +} module.exports = SteamerPlugin; \ No newline at end of file diff --git a/package.json b/package.json index 3b64af4..eff3a6e 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "steamer-plugin", - "version": "1.0.0", + "version": "1.0.1", "description": "steamer plugin base class", "main": "index.js", "scripts": { diff --git a/test/index.js b/test/index.js index 5f03d8a..cbbeeef 100755 --- a/test/index.js +++ b/test/index.js @@ -1,12 +1,10 @@ -'use strict'; - -const path = require('path'), - os = require('os'), - fs = require('fs-extra'), - chalk = require('chalk'), - expect = require('chai').expect, - sinon = require('sinon'), - globalModules = require('global-modules'); +const path = require('path'); +const os = require('os'); +const fs = require('fs-extra'); +const chalk = require('chalk'); +const expect = require('chai').expect; +const sinon = require('sinon'); +const globalModules = require('global-modules'); const Plugin = require('../index'); @@ -201,6 +199,49 @@ describe('[config]', function() { plugin.createSteamerConfig(globalConfig, options); }); + + it.only('readSteamerDefaultConfig - 1', function() { + var plugin = new Plugin(); + + let stub1 = sinon.stub(plugin, 'readSteamerConfig').callsFake(() => { + return { + NPM: 'tnpm', + PLUGIN_PREFIX: 'at-plugin-', + KIT_PREFIX: 'at-kit-', + TEAM_PREFIX: 'at-team-' + }; + }); + + expect(plugin.readSteamerDefaultConfig()).deep.eql({ + NPM: 'tnpm', + PLUGIN_PREFIX: 'at-plugin-', + KIT_PREFIX: 'at-kit-', + TEAM_PREFIX: 'at-team-' + }); + + stub1.restore(); + }); + + it.only('readSteamerDefaultConfig - 2', function() { + var plugin = new Plugin(); + + let stub1 = sinon.stub(plugin, 'readSteamerConfig').callsFake(() => { + return { + GIT: 'github.com' + }; + }); + + expect(plugin.readSteamerDefaultConfig()).deep.eql({ + NPM: 'npm', + PLUGIN_PREFIX: 'steamer-plugin-', + KIT_PREFIX: 'steamer-kit-', + TEAM_PREFIX: 'steamer-team-', + GIT: 'github.com' + }); + + stub1.restore(); + }); + }); describe('[log]', function() {