Skip to content

Commit 5d859f8

Browse files
committed
feat: add CONSTANTS & isDocker, art test file
1 parent 9829f4f commit 5d859f8

26 files changed

+122
-88
lines changed

.github/workflows/deploy.yml

Lines changed: 0 additions & 27 deletions
This file was deleted.

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export * as onExit from 'signal-exit';
2727
export * as hash from 'hash-sum';
2828
export * as path from 'path';
2929
export * as tryRequire from 'try-require';
30+
export * as isDocker from 'is-docker';
3031

3132
export function assert(value: any, message?: string | Error): void;
3233

@@ -42,3 +43,4 @@ export * as Env from './src/Env';
4243
export * as validateSchema from './src/validateSchema';
4344
export * as loadFile from './src/loadFile';
4445
export * as pluginResolution from './src/pluginResolution';
46+
export * as CONSTANTS from './src/constants';

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const internal = [
2525
'Env',
2626
'validateSchema',
2727
'pluginResolution',
28+
'CONSTANTS',
2829
].reduce((obj, key) => {
2930
obj[key] = `./src/${key}`;
3031
return obj;
@@ -62,6 +63,7 @@ const thirdParty = {
6263
onExit: 'signal-exit',
6364
hash: 'hash-sum',
6465
path: 'path',
66+
isDocker: 'is-docker',
6567
};
6668

6769
Object.keys(thirdParty).forEach(key => {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"hash-sum": "^2.0.0",
7272
"import-fresh": "^3.2.1",
7373
"inquirer": "^7.1.0",
74+
"is-docker": "^2.0.0",
7475
"is-glob": "^4.0.1",
7576
"lodash": "^4.17.15",
7677
"lru-cache": "^5.1.1",

src/Env/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const { execSync } = require('child_process');
4-
const fs = require('fs');
4+
const fs = require('fs-extra');
55
const path = require('path');
66
const LRU = require('lru-cache');
77
const semver = require('semver');

src/constants/index.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
11
'use strict';
22

3-
module.exports = {
4-
NAME: 'Micro App',
5-
SCOPE_NAME: '@micro-app',
6-
INJECT_ID: '_MICRO_APP_INJECT_',
7-
};
3+
const logger = require('../logger');
4+
5+
class Constants {
6+
7+
constructor() {
8+
this.NAME = 'Micro App';
9+
this.SCOPE_NAME = '@micro-app';
10+
this.INJECT_ID = '_MICRO_APP_INJECT_';
11+
}
12+
13+
set(key, value) {
14+
this[key] = value;
15+
}
16+
17+
get(key) {
18+
return this[key] || null;
19+
}
20+
21+
freeze(key, value) {
22+
Object.defineProperty(this, key, {
23+
get() { return value; },
24+
set() {
25+
logger.warn('[freeze]', `"${key}" be freezed, Not Allow Change!`);
26+
},
27+
enumerable: true,
28+
});
29+
}
30+
}
31+
32+
module.exports = new Constants();

src/injectHtml.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const stream = require('stream');
77
const CONSTANTS = require('./constants');
88

99
module.exports = async function injectHtml(ctx) {
10+
const INJECT_ID = CONSTANTS.get('INJECT_ID');
1011
// 处理文件注入一些信息
1112
let ok = false;
1213
if (ctx && ctx.method === 'GET' && ctx.type === 'text/html') {
@@ -22,7 +23,6 @@ module.exports = async function injectHtml(ctx) {
2223
ctx.body = body;
2324
try {
2425
const $ = cheerio.load(ctx.body);
25-
const { INJECT_ID } = CONSTANTS;
2626
if ($(`body>#${INJECT_ID}`).length <= 0) {
2727
const INJECT_MICRO_APP = `{
2828
cache: {},
@@ -54,7 +54,6 @@ module.exports = async function injectHtml(ctx) {
5454
function func(key, value) {
5555
if (ok && key) {
5656
const $ = cheerio.load(ctx.body);
57-
const { INJECT_ID } = CONSTANTS;
5857
$(`body>#${INJECT_ID}`).append(`<textarea id="${INJECT_ID}_${key}" style="display: none;">${encodeURIComponent(JSON.stringify(value))}</textarea>`);
5958
ctx.body = $.html();
6059
}

src/logger/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ const format = {
122122
// },
123123
logo(...arrs) {
124124
const message = utils.format(...(arrs || []));
125-
const { NAME } = CONSTANTS;
126-
return `${chalk.bgHex('#662F88')(` ${NAME} `)} ${message}`;
125+
const namespace = CONSTANTS.get('NAME');
126+
return `${chalk.bgHex('#662F88')(` ${namespace} `)} ${message}`;
127127
},
128128
json(...arrs) {
129129
const message = utils.format(...(arrs || []).map(item => formatObject(item)));

src/moduleAlias/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ module.exports = {
2424
add,
2525
addPaths,
2626
addPath: addPaths,
27+
Module: moduleAlias,
2728
};

src/moduleAlias/index.test.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)