Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
81 changes: 0 additions & 81 deletions config/default.js

This file was deleted.

39 changes: 26 additions & 13 deletions libs/Config/base/BaseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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) {
Expand All @@ -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}" !`);
}
}
}
Expand All @@ -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() {
Expand All @@ -77,7 +90,7 @@ class BaseConfig {

get path() {
const config = this.config;
return config[symbols.PATH] || '';
return config[Symbols.PATH] || '';
}

get nodeModules() {
Expand Down Expand Up @@ -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() {
Expand Down
8 changes: 5 additions & 3 deletions libs/Config/base/BaseConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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();
Expand All @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion libs/Config/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {

}));
Expand All @@ -34,6 +35,7 @@ describe('MicroAppConfig', () => {
});

it('new constructor Config', () => {
const defaultConfig = loadFile(__dirname, '../Constants/default.js');
const config = new MicroAppConfig(Object.assign({}, defaultConfig, {

}));
Expand All @@ -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' ],
Expand Down
81 changes: 81 additions & 0 deletions libs/Constants/default.js
Original file line number Diff line number Diff line change
@@ -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: [ // 插件集
// [
// '', {},
// ],
// ],
};
2 changes: 1 addition & 1 deletion config/constants.js → libs/Constants/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const pkg = require('../package.json');
const pkg = require('../../package.json');

module.exports = {
NAME: 'Micro App',
Expand Down
1 change: 1 addition & 0 deletions config/symbols.js → libs/Constants/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 成功
};
2 changes: 1 addition & 1 deletion libs/Service/base/BaseAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
2 changes: 1 addition & 1 deletion libs/Service/base/BaseService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"license": "MIT",
"lint-staged": {
"*.js": [
"prettier --write",
"npm run lint:fix",
"git add"
]
},
Expand Down
5 changes: 4 additions & 1 deletion plugins/commands/show/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});

Expand Down
Loading