diff --git a/.circleci/config.yml b/.circleci/config.yml index 1ea4bdd..12d2396 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,4 +23,6 @@ jobs: paths: - ./node_modules - run: yarn run test --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage + - run: rm -rf ./micro-app.config.js + - run: yarn run test --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage diff --git a/LICENSE b/LICENSE index b38446e..e392fb4 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019 zyao89 +Copyright (c) 2019-present, Zyao89 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/config/default.js b/config/default.js deleted file mode 100644 index 2191550..0000000 --- a/config/default.js +++ /dev/null @@ -1,81 +0,0 @@ -'use strict'; - -module.exports = { - name: '', // 名称, 为空则使用 package.json 中的 name - description: '', // 描述 - version: '0.0.1', // 版本 - type: '', // types 类型 - strict: true, // 是否为严格模式? 默认为 true - 强依赖 - - entry: { // 入口 - }, - - htmls: [ - // { - // template: '', - // }, - ], // 模版 - - staticPath: [], // String | Array - - dlls: [ - // { // dll 基本配置, [ 只支持子模块中使用 ] - // disabled: false, - // context: '', - // manifest: '', - // filepath: '', - // }, - ], - - alias: { // 共享别名 - // api: './client/api.js', // 默认为前后端通用 - // service: { - // link: './server/service.js', - // type: 'server', // 只支持后端 - // }, - }, - - micros: [ // 需要注入的子模块 - // 'test' - ], - - // 服务端配置 - server: { - // hooks: '', // 服务端 hook 路径 - // entry: '', // 服务端入口 - // host: ‘’, // 服务 IP - // port: 8888, // 服务端口号 - // options: { }, // 服务端注入附加参数 - - // proxy: { // 服务代理 - // '/api': { - // target: 'http://127.0.0.1', // target host - // changeOrigin: true, // needed for virtual hosted sites - // ws: true, // proxy websockets - // }, - // }, - }, - - // 一些插件配置项 (弃用) - // plugin: { - // ReplaceFileNotExists: { - // debug: false, // 开启log - // warnHint: 'Not Found', - // loader: '', // 路径 - // resource: '', // 路径 - // test: /^@micros\//i, // 匹配规则 - // }, - // SpeedMeasurePlugin: { - // disabled: true, - // }, - // HappyPack: { - // disabled: true, - // }, - // }, - - plugins: [ // 插件集 - // [ - // '', {}, - // ], - ], -}; diff --git a/libs/Config/base/BaseConfig.js b/libs/Config/base/BaseConfig.js index 4b914bb..9ce4037 100644 --- a/libs/Config/base/BaseConfig.js +++ b/libs/Config/base/BaseConfig.js @@ -5,13 +5,13 @@ const fs = require('fs-extra'); const tryRequire = require('try-require'); const _ = require('lodash'); -const symbols = require('../../../config/symbols'); -const CONSTANTS = require('../../../config/constants'); +const Symbols = require('../../Constants/symbols'); +const CONSTANTS = require('../../Constants'); const logger = require('../../../src/utils/logger'); const getPadLength = require('../../../src/utils/getPadLength'); // 默认配置 -const DEFAULT_CONFIG = require('../../../config/default'); +// const DEFAULT_CONFIG = require('../../Constants/default'); const validate = require('../schema'); const SCHEMA = require('../schema/configSchema'); @@ -21,11 +21,16 @@ const ORIGNAL_CONFIG = Symbol('@BaseConfig#ORIGNAL_CONFIG'); class BaseConfig { + /** + * Creates an instance of BaseConfig. + * @param {DEFAULT_CONFIG} config config + * @memberof BaseConfig + */ constructor(config /* , opts = {} */) { // 校验 config this._validateSchema(config); - this[INIT](config); - this[ORIGNAL_CONFIG] = config || DEFAULT_CONFIG; + this[ORIGNAL_CONFIG] = config; + this[INIT](); } _validateSchema(config) { @@ -41,18 +46,26 @@ class BaseConfig { } } - [INIT](config) { - if (config) { + [INIT]() { + if (!this.config[Symbols.LOAD_SUCCESS]) { + // 文件未加载成功. + logger.error(`Not Found "${CONSTANTS.CONFIG_NAME}"`); + } + if (this.root) { try { - const packagePath = path.join(config[symbols.ROOT], CONSTANTS.PACKAGE_JSON); + const packagePath = path.resolve(this.root, CONSTANTS.PACKAGE_JSON); if (fs.existsSync(packagePath)) { this._packagePath = packagePath; this._package = require(packagePath); + if (!this.config[Symbols.LOAD_SUCCESS]) { + // 文件未加载成功. + // TODO 可以从 package.json 中查询配置文件 + } } } catch (error) { this._packagePath = ''; this._package = {}; - logger.warn('Not Fount "package.json" !'); + logger.warn(`Not Fount "${CONSTANTS.PACKAGE_JSON}" !`); } } } @@ -63,12 +76,12 @@ class BaseConfig { get root() { const config = this.config; - return config[symbols.ROOT] || ''; + return config[Symbols.ROOT] || ''; } get originalRoot() { const config = this.config; - return config[symbols.ORIGINAL_ROOT] || this.root || ''; + return config[Symbols.ORIGINAL_ROOT] || this.root || ''; } get hasSoftLink() { @@ -77,7 +90,7 @@ class BaseConfig { get path() { const config = this.config; - return config[symbols.PATH] || ''; + return config[Symbols.PATH] || ''; } get nodeModules() { @@ -116,7 +129,7 @@ class BaseConfig { get key() { const config = this.config; const reg = new RegExp(`^${CONSTANTS.SCOPE_NAME}\/?`, 'ig'); - return config[symbols.KEY] || this.packageName.replace(reg, '') || ''; + return config[Symbols.KEY] || this.packageName.replace(reg, '') || ''; } get name() { diff --git a/libs/Config/base/BaseConfig.test.js b/libs/Config/base/BaseConfig.test.js index 247ea0b..4f2f8b8 100644 --- a/libs/Config/base/BaseConfig.test.js +++ b/libs/Config/base/BaseConfig.test.js @@ -3,7 +3,7 @@ /* global expect */ const BaseConfig = require('./BaseConfig'); -const defaultConfig = require('../../../micro-app.config'); +const loadFile = require('../../../src/utils/loadFile'); const testConfig = { shared: { @@ -28,7 +28,8 @@ const testConfig = { describe('BaseConfig', () => { it('new constructor', () => { - const config = new BaseConfig(Object.assign({}, defaultConfig, testConfig)); + const defaultConfig = loadFile(__dirname, '../../Constants/default.js'); + const config = new BaseConfig(Object.assign(defaultConfig, testConfig)); expect(config.config).not.toBeNull(); expect(config.root).not.toBeUndefined(); @@ -52,7 +53,8 @@ describe('BaseConfig', () => { }); it('config inspect', () => { - const config = new BaseConfig(Object.assign({}, defaultConfig, testConfig)); + const defaultConfig = loadFile(__dirname, '../../Constants/default.js'); + const config = new BaseConfig(Object.assign(defaultConfig, testConfig)); expect(config.inspect).not.toBeNull(); expect(config.inspect).not.toBeUndefined(); diff --git a/libs/Config/index.test.js b/libs/Config/index.test.js index b08ec4d..fc0b0e1 100644 --- a/libs/Config/index.test.js +++ b/libs/Config/index.test.js @@ -3,11 +3,12 @@ /* global expect */ const MicroAppConfig = require('./index'); -const defaultConfig = require('../../micro-app.config'); +const loadFile = require('../../src/utils/loadFile'); describe('MicroAppConfig', () => { it('new constructor', () => { + const defaultConfig = loadFile(__dirname, '../Constants/default.js'); const config = new MicroAppConfig(Object.assign({}, defaultConfig, { })); @@ -34,6 +35,7 @@ describe('MicroAppConfig', () => { }); it('new constructor Config', () => { + const defaultConfig = loadFile(__dirname, '../Constants/default.js'); const config = new MicroAppConfig(Object.assign({}, defaultConfig, { })); @@ -55,6 +57,7 @@ describe('MicroAppConfig', () => { }); it('new constructor others', () => { + const defaultConfig = loadFile(__dirname, '../Constants/default.js'); const config = new MicroAppConfig(Object.assign({}, defaultConfig, { entry: { main: [ './test/index.js' ], diff --git a/libs/Constants/default.js b/libs/Constants/default.js new file mode 100644 index 0000000..1422648 --- /dev/null +++ b/libs/Constants/default.js @@ -0,0 +1,81 @@ +'use strict'; + +module.exports = { + name: '', // 名称, 为空则使用 package.json 中的 name + description: '', // 描述 + version: '0.0.1', // 版本 + type: '', // types 类型 + strict: true, // 是否为严格模式? 默认为 true - 强依赖 + + // entry: { // 入口 + // }, + + // htmls: [ + // { + // template: '', + // }, + // ], // 模版 + + // staticPath: [], // String | Array + + // dlls: [ + // { // dll 基本配置, [ 只支持子模块中使用 ] + // disabled: false, + // context: '', + // manifest: '', + // filepath: '', + // }, + // ], + + alias: { // 共享别名 + // api: './client/api.js', // 默认为前后端通用 + // service: { + // link: './server/service.js', + // type: 'server', // 只支持后端 + // }, + }, + + // micros: [ // 需要注入的子模块 + // 'test' + // ], + + // 服务端配置 + // server: { + // hooks: '', // 服务端 hook 路径 + // entry: '', // 服务端入口 + // host: ‘’, // 服务 IP + // port: 8888, // 服务端口号 + // options: { }, // 服务端注入附加参数 + + // proxy: { // 服务代理 + // '/api': { + // target: 'http://127.0.0.1', // target host + // changeOrigin: true, // needed for virtual hosted sites + // ws: true, // proxy websockets + // }, + // }, + // }, + + // 一些插件配置项 (弃用) + // plugin: { + // ReplaceFileNotExists: { + // debug: false, // 开启log + // warnHint: 'Not Found', + // loader: '', // 路径 + // resource: '', // 路径 + // test: /^@micros\//i, // 匹配规则 + // }, + // SpeedMeasurePlugin: { + // disabled: true, + // }, + // HappyPack: { + // disabled: true, + // }, + // }, + + // plugins: [ // 插件集 + // [ + // '', {}, + // ], + // ], +}; diff --git a/config/constants.js b/libs/Constants/index.js similarity index 90% rename from config/constants.js rename to libs/Constants/index.js index 75c610f..b83c171 100644 --- a/config/constants.js +++ b/libs/Constants/index.js @@ -1,6 +1,6 @@ 'use strict'; -const pkg = require('../package.json'); +const pkg = require('../../package.json'); module.exports = { NAME: 'Micro App', diff --git a/config/symbols.js b/libs/Constants/symbols.js similarity index 71% rename from config/symbols.js rename to libs/Constants/symbols.js index a6d03b7..085f82e 100644 --- a/config/symbols.js +++ b/libs/Constants/symbols.js @@ -5,4 +5,5 @@ module.exports = { PATH: Symbol('@MicroAppConfig#PATH'), ROOT: Symbol('@MicroAppConfig#ROOT'), ORIGINAL_ROOT: Symbol('@MicroAppConfig#ORIGINAL_ROOT'), + LOAD_SUCCESS: Symbol('@MicroAppConfig#LOAD_SUCCESS'), // 是否加载 config 成功 }; diff --git a/libs/Service/base/BaseAPI.js b/libs/Service/base/BaseAPI.js index b6998f7..a69e878 100644 --- a/libs/Service/base/BaseAPI.js +++ b/libs/Service/base/BaseAPI.js @@ -2,7 +2,7 @@ const semver = require('semver'); const logger = require('../../../src/utils/logger'); -const CONSTANTS = require('../../../config/constants'); +const CONSTANTS = require('../../../libs/Constants'); class BaseAPI { diff --git a/libs/Service/base/BaseService.js b/libs/Service/base/BaseService.js index 0c9fc2b..badc56c 100644 --- a/libs/Service/base/BaseService.js +++ b/libs/Service/base/BaseService.js @@ -4,7 +4,7 @@ const assert = require('assert'); const _ = require('lodash'); const semverRegex = require('semver-regex'); -const CONSTANTS = require('../../../config/constants'); +const CONSTANTS = require('../../../libs/Constants'); const requireMicro = require('../../../src/utils/requireMicro'); const loadFile = require('../../../src/utils/loadFile'); diff --git a/package.json b/package.json index 81d73b4..5e7a4d9 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "license": "MIT", "lint-staged": { "*.js": [ - "prettier --write", + "npm run lint:fix", "git add" ] }, diff --git a/plugins/commands/show/index.js b/plugins/commands/show/index.js index baf2ffb..f1ef533 100644 --- a/plugins/commands/show/index.js +++ b/plugins/commands/show/index.js @@ -130,12 +130,15 @@ Examples: return obj; }, {})); case 'info': - default: api.logger.logo(`${chalk.green('Show Details')}:`); return showAliasList(Object.keys(info).reduce((obj, key) => { obj[key] = { description: JSON.stringify(info[key]) }; return obj; }, {})); + default: + // const envinfo = require('envinfo'); + // TODO 这里应该支持扩展. + return; } }); diff --git a/src/index.js b/src/index.js index 7c449cb..3ef76c4 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ 'use strict'; -const CONSTANTS = require('../config/constants'); +const CONSTANTS = require('../libs/Constants'); const Service = require('../libs/Service'); const logger = require('../src/utils/logger'); const requireMicro = require('../src/utils/requireMicro'); diff --git a/src/utils/injectHtml.js b/src/utils/injectHtml.js index 6cee4f3..8adea45 100644 --- a/src/utils/injectHtml.js +++ b/src/utils/injectHtml.js @@ -4,7 +4,7 @@ const toString = require('stream-to-string'); const cheerio = require('cheerio'); const stream = require('stream'); -const CONSTANTS = require('../../config/constants'); +const CONSTANTS = require('../../libs/Constants'); module.exports = async function injectHtml(ctx) { // 处理文件注入一些信息 diff --git a/src/utils/loadFile.js b/src/utils/loadFile.js index 195efdc..dbd60b6 100644 --- a/src/utils/loadFile.js +++ b/src/utils/loadFile.js @@ -3,7 +3,7 @@ const tryRequire = require('try-require'); const fs = require('fs-extra'); const path = require('path'); -const symbols = require('../../config/symbols'); +const symbols = require('../../libs/Constants/symbols'); function isSupport(filename) { return [ '.js', '.json' ].some(ext => { @@ -11,17 +11,32 @@ function isSupport(filename) { }); } -function load(filePath) { - // const str = fs.readFileSync(filePath, 'utf8'); +function load(root, filename) { + const filePath = path.resolve(root, filename); + if (!fs.existsSync(filePath)) { + return null; + } + if (!fs.statSync(filePath).isFile()) { + return null; + } const file = tryRequire(filePath); if (file) { + file[symbols.LOAD_SUCCESS] = true; + return extraConfig(file, root, filename); + } + return null; +} + +function extraConfig(file, root, filename) { + if (file && root && filename) { + const filePath = path.resolve(root, filename); file[symbols.ROOT] = path.dirname(filePath); file[symbols.PATH] = filePath; } return file; } -module.exports = function loadFile(root, filename) { +function loadFile(root, filename) { if (!root || !filename) { return null; } @@ -34,12 +49,8 @@ module.exports = function loadFile(root, filename) { if (!fs.statSync(root).isDirectory()) { return null; } - const filePath = path.join(root, filename); - if (!fs.existsSync(filePath)) { - return null; - } - if (!fs.statSync(filePath).isFile()) { - return null; - } - return load(filePath); -}; + return load(root, filename); +} + +module.exports = loadFile; +module.exports.extraConfig = extraConfig; diff --git a/src/utils/logger.js b/src/utils/logger.js index 23cc6ea..f115260 100644 --- a/src/utils/logger.js +++ b/src/utils/logger.js @@ -4,7 +4,7 @@ const chalk = require('chalk').default; const utils = require('util'); const ora = require('ora'); -const CONSTANTS = require('../../config/constants'); +const CONSTANTS = require('../../libs/Constants'); const getStdoutMethod = function(type) { if (!process) { diff --git a/src/utils/requireMicro.js b/src/utils/requireMicro.js index 109ca52..63839fd 100644 --- a/src/utils/requireMicro.js +++ b/src/utils/requireMicro.js @@ -3,40 +3,43 @@ const path = require('path'); const fs = require('fs-extra'); -const CONSTANTS = require('../../config/constants'); -const loadFile = require('./loadFile'); +const CONSTANTS = require('../../libs/Constants'); const MicroAppConfig = require('../../libs/Config'); -const symbols = require('../../config/symbols'); +const symbols = require('../../libs/Constants/symbols'); +const loadFile = require('./loadFile'); const SELF_CONFIG = Symbol('@MicroAppConfig#SELF_CONFIG'); const configCache = {}; -const self = function() { +function self() { const { ROOT, CONFIG_NAME } = CONSTANTS; if (configCache[SELF_CONFIG]) { return configCache[SELF_CONFIG]; } - const microConfig = loadFile(ROOT, CONFIG_NAME); + let microConfig = loadFile(ROOT, CONFIG_NAME); + if (!microConfig) { // 可忽略配置文件. + microConfig = loadFile.extraConfig({}, ROOT, CONFIG_NAME); + } if (microConfig) { const _microAppConfig = new MicroAppConfig(microConfig); configCache[SELF_CONFIG] = _microAppConfig; return _microAppConfig; } return null; -}; +} -const isExists = function(p) { +function isExists(p) { try { return fs.existsSync(p) && fs.statSync(p).isDirectory(); } catch (error) { return false; } -}; +} // TODO global 可优化 // @custom 开发模式软链接 // 当 global.MicroAppConfig.microsExtraConfig 存在时, 才会开启软链功能 -const fixedDevLink = function(id, micPath) { +function fixedDevLink(id, micPath) { const MicroAppConfig = global.MicroAppConfig || {}; const microsExtraConfig = MicroAppConfig.microsExtraConfig || {}; const extralConfig = microsExtraConfig[id]; @@ -44,9 +47,9 @@ const fixedDevLink = function(id, micPath) { return extralConfig.link; } return micPath; -}; +} -const requireMicro = function(id) { +function requireMicro(id) { const { ROOT, SCOPE_NAME, CONFIG_NAME, NODE_MODULES_NAME } = CONSTANTS; const name = `${SCOPE_NAME}/${id}`; if (configCache[name]) { @@ -66,7 +69,7 @@ const requireMicro = function(id) { } } else { originalMicPath = path.join(ROOT, NODE_MODULES_NAME, name); - if (originalMicPath) { + if (isExists(originalMicPath)) { const micPath = fixedDevLink(id, originalMicPath); const microConfig = loadFile(micPath, CONFIG_NAME); if (microConfig) { @@ -79,7 +82,7 @@ const requireMicro = function(id) { } } return null; -}; +} module.exports = requireMicro; diff --git a/test/validate.js b/test/validate.js index 14ea8a4..52f0551 100644 --- a/test/validate.js +++ b/test/validate.js @@ -4,7 +4,7 @@ const _ = require('lodash'); const validate = require('../libs/Config/schema'); const getPadLength = require('../src/utils/getPadLength'); -const result = validate(require('../libs/Config/schema/configSchema'), require('../config/default')); +const result = validate(require('../libs/Config/schema/configSchema'), require('../libs/Constants/default')); const padLength = getPadLength(result.map(item => { return { name: item.keyword }; }));