Skip to content

Commit 820aecd

Browse files
committed
:fix: 修复部分bug等。
1 parent 415c278 commit 820aecd

File tree

9 files changed

+164
-58
lines changed

9 files changed

+164
-58
lines changed

index.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference types="node"/>
2+
13
import fs from 'fs-extra';
24
import chalk from 'chalk';
35
import cheerio from 'cheerio';
@@ -11,6 +13,8 @@ import globParent from 'glob-parent';
1113
import isGlob from 'is-glob';
1214
import npa from 'npm-package-arg';
1315
import parseGitUrl from 'git-url-parse';
16+
import multimatch from 'multimatch';
17+
import stringifyObject from 'stringify-object';
1418

1519
export function tryRequire( id: string, req?: Object): any | null;
1620
export function assert(value: any, message?: string | Error): void;
@@ -28,9 +32,9 @@ export {
2832
getPadLength,
2933
injectHtml,
3034
loadFile,
31-
logger,
3235
smartMerge,
3336
virtualFile,
37+
logger,
3438
};
3539

3640
export {
@@ -48,4 +52,6 @@ export {
4852
isGlob,
4953
npa,
5054
parseGitUrl,
55+
multimatch,
56+
stringifyObject,
5157
}

index.js

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

3+
const output = {};
4+
5+
function defineProperty(key, from, to = output) {
6+
if (to[key]) throw new Error(`"${key}" already exsits!`);
7+
// lazy
8+
Object.defineProperty(to, key, {
9+
get() {
10+
return require(from[key]);
11+
},
12+
});
13+
}
14+
315
const internal = [
416
'moduleAlias',
517
'getPadLength',
@@ -9,11 +21,13 @@ const internal = [
921
'smartMerge',
1022
'virtualFile',
1123
].reduce((obj, key) => {
12-
obj[key] = require(`./src/${key}`);
24+
obj[key] = `./src/${key}`;
1325
return obj;
1426
}, {});
1527

16-
internal.assert = internal.logger.assert.bind(internal.logger);
28+
Object.keys(internal).forEach(key => {
29+
defineProperty(key, internal);
30+
});
1731

1832
const thirdParty = {
1933
fs: 'fs-extra',
@@ -30,15 +44,25 @@ const thirdParty = {
3044
isGlob: 'is-glob',
3145
npa: 'npm-package-arg',
3246
parseGitUrl: 'git-url-parse',
47+
multimatch: 'multimatch',
48+
stringifyObject: 'stringify-object',
3349
};
3450

3551
Object.keys(thirdParty).forEach(key => {
36-
// lazy
37-
Object.defineProperty(internal, key, {
38-
get() {
39-
return require(thirdParty[key]);
40-
},
52+
defineProperty(key, thirdParty);
53+
});
54+
55+
const alias = {
56+
assert: () => {
57+
return output.logger.assert.bind(output.logger);
58+
},
59+
};
60+
61+
Object.keys(alias).forEach(key => {
62+
if (output[key]) throw new Error(`"${key}" already exsits!`);
63+
Object.defineProperty(output, key, {
64+
get: alias[key],
4165
});
4266
});
4367

44-
module.exports = internal;
68+
module.exports = output;

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@micro-app/shared-utils",
3-
"version": "0.1.0",
3+
"version": "0.1.3",
44
"description": "[Shared] shared utilities for micro-app.",
55
"main": "index.js",
66
"scripts": {
@@ -18,6 +18,7 @@
1818
},
1919
"files": [
2020
"src",
21+
"index.d.ts",
2122
"index.js"
2223
],
2324
"keywords": [
@@ -39,14 +40,14 @@
3940
]
4041
},
4142
"devDependencies": {
42-
"@types/jest": "^24.0.19",
43+
"@types/jest": "^24.0.23",
4344
"babel-eslint": "^10.0.3",
4445
"coveralls": "^3.0.7",
4546
"eslint": "^5.16.0",
4647
"eslint-config-2o3t": "^1.1.17",
47-
"husky": "^3.0.9",
48+
"husky": "^3.1.0",
4849
"jest": "^24.9.0",
49-
"lint-staged": "^9.4.2",
50+
"lint-staged": "^9.4.3",
5051
"webpack-merge": "^4.2.2"
5152
},
5253
"dependencies": {
@@ -58,16 +59,18 @@
5859
"git-url-parse": "^11.1.2",
5960
"glob-parent": "^5.1.0",
6061
"globby": "^10.0.1",
61-
"import-fresh": "^3.1.0",
62+
"import-fresh": "^3.2.1",
6263
"is-glob": "^4.0.1",
6364
"lodash": "^4.17.15",
65+
"multimatch": "^4.0.0",
6466
"npm-package-arg": "^6.1.1",
6567
"npmlog": "^4.1.2",
6668
"ora": "^3.4.0",
6769
"parse-json": "^5.0.0",
6870
"semver": "^6.3.0",
6971
"semver-regex": "^3.1.0",
7072
"stream-to-string": "^1.2.0",
73+
"stringify-object": "^3.3.0",
7174
"try-require": "^1.2.1",
7275
"yaml": "^1.7.2"
7376
},

src/loadFile/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const fs = require('fs-extra');
44
const path = require('path');
5+
const _ = require('lodash');
56

67
const loaders = require('./loaders');
78
const logger = require('../logger');
@@ -46,7 +47,11 @@ function load(root, filename) {
4647
}
4748

4849
function loadFile(root, filename, { before, after } = {}) {
49-
if (!root || !filename) {
50+
if (!_.isEmpty(root) && _.isEmpty(filename)) {
51+
filename = path.basename(root);
52+
root = path.dirname(root);
53+
}
54+
if (_.isEmpty(root) || _.isEmpty(filename)) {
5055
return null;
5156
}
5257
if (!isSupport(filename)) {

src/loadFile/loadFile.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@ describe('loadFile', () => {
4242
expect(file).not.toBeNull();
4343
});
4444

45+
it('json', () => {
46+
const file = loadFile(process.cwd() + '/package.json');
47+
48+
expect(file).not.toBeNull();
49+
});
50+
4551
});

src/logger/index.js

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ const chalk = require('chalk');
66
const utils = require('util');
77
const ora = require('ora');
88
const os = require('os');
9+
const stringifyObject = require('stringify-object');
10+
const _ = require('lodash');
911

1012
npmlog.heading = require('../constants').SCOPE_NAME || '@micro-app';
1113

12-
if (process.env.MICRO_APP_LOGGER_LEVEL) {
13-
npmlog.level = process.env.MICRO_APP_LOGGER_LEVEL === 'true' ? 'silly' : process.env.MICRO_APP_LOGGER_LEVEL;
14-
}
1514
// npmlog.prefixStyle = {};
1615
const CUSTOM_LEVEL = {
1716
success: {
@@ -48,11 +47,21 @@ const getStdoutMethod = function(type) {
4847
return process.stdout.write.bind(process.stdout);
4948
};
5049

50+
function formatObject(message) {
51+
if (_.isString(message)) {
52+
return message;
53+
}
54+
return stringifyObject(message, {
55+
indent: ' ',
56+
singleQuotes: false,
57+
inlineCharacterLimit: 12,
58+
});
59+
}
60+
61+
// TODO 优化输出
5162
function dyeMessage(type, message) {
63+
message = formatObject(message);
5264
switch (type.toLowerCase()) {
53-
case 'debug': {
54-
return chalk.visible(message);
55-
}
5665
case 'warn': {
5766
return chalk.yellowBright(message);
5867
}
@@ -107,7 +116,6 @@ class Logger {
107116
}
108117

109118
debug() {
110-
// if (!process.env.MICRO_APP_DEBUG_LOGGER) return; // 是否开启
111119
return this.getMethod('debug')(...arguments);
112120
}
113121
warn() {
@@ -162,24 +170,26 @@ class Logger {
162170
}
163171

164172
getMethod(type) {
173+
if ([ 'debug' ].includes(type)) { // 不再需要 debug
174+
type = 'verbose';
175+
}
165176
const logger = this.getNpmlogMethod(type);
177+
if (typeof logger !== 'function') {
178+
return logger;
179+
}
166180
return function(...args) {
167181
if (args.length <= 1) {
168182
return logger(false, ...args.map(arg => dyeMessage(type, arg)));
169183
}
170184
return logger(args[0], ...args.splice(1).map(arg => dyeMessage(type, arg)));
171185
};
172-
// const logger = getStdoutMethod(type);
173-
// return function(...args) {
174-
// return logger(format[type].call(format, ...args));
175-
// };
176186
}
177187

178188
getNpmlogMethod(type) {
179-
if ([ 'debug' ].includes(type)) {
180-
type = 'verbose';
189+
if (typeof this.npmlog[type] === 'function') {
190+
return this.npmlog[type].bind(this.npmlog);
181191
}
182-
return this.npmlog[type].bind(this.npmlog);
192+
return this.npmlog[type];
183193
}
184194

185195
setAlias(type, value) {
@@ -194,15 +204,15 @@ class Logger {
194204
return this.aliasMap.get(type);
195205
}
196206

207+
addCustomToString(key, value) {
208+
this.customFormatMap.set(key, value);
209+
}
210+
197211
newGroup(name, ...args) {
198212
const newLog = this.npmlog.newGroup(name, ...args);
199213
return factroy(newLog, { alias: this.aliasMap, customFormat: this.customFormatMap });
200214
}
201215

202-
addCustomToString(key, value) {
203-
this.customFormatMap.set(key, value);
204-
}
205-
206216
get format() { // 兼容 toString
207217
return new Proxy(this, {
208218
get(target, prop) {
@@ -215,14 +225,21 @@ class Logger {
215225
function factroy(log, opts = {}) {
216226
return new Proxy(new Logger(log, opts), {
217227
get(target, prop) {
218-
if (!(prop in target) && typeof prop === 'string' && (prop in npmlog)) {
228+
if (prop in target) {
229+
return target[prop];
230+
}
231+
if ([ 'silly', 'verbose', 'info', 'timing', 'http', 'notice', 'warn', 'error', 'silent' ].includes(prop)) {
219232
return target.getMethod(prop);
220233
}
221234
const alias = target.getAlias(prop);
222235
if (alias) {
223236
return target.getMethod(alias);
224237
}
225-
return target[prop];
238+
return log[prop];
239+
},
240+
set(target, prop, value) {
241+
log[prop] = value;
242+
return true;
226243
},
227244
});
228245
}
@@ -231,3 +248,4 @@ module.exports = factroy(npmlog, { customFormat: Object.entries(format) });
231248

232249
module.exports.getStdoutMethod = getStdoutMethod;
233250

251+
module.exports.Logger = Logger;

src/logger/logger.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,10 @@ describe('Logger', () => {
5353

5454
});
5555

56+
57+
it('logger debug', () => {
58+
logger.level = 'silly';
59+
logger.debug('abc');
60+
});
61+
5662
});

test/npmlog.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,32 @@ const logger = require('../src/logger');
1212
// bg: 'red',
1313
// };
1414

15-
logger.npmlog.level = 'verbose';
15+
// logger.npmlog.level = 'verbose';
16+
logger.level = 'silly';
17+
console.warn(logger.level);
18+
console.warn(logger.npmlog.level);
1619

1720
logger.info('abc', 'haha..');
1821
logger.error('abc', 'haha..');
1922
logger.warn('abc', 'haha..');
2023
logger.warn('', 'haha..');
2124
logger.success('haha..');
2225
logger.debug('111haha..');
26+
logger.verbose('111haha..');
2327
logger.noise(false, 'haha..');
2428

2529
// logger.throw(new Error('abbdd'), 'abc..');
2630

2731
// console.log(npmlog.record)
2832
// console.log(npmlog.level)
29-
logger.assert(1 === 2, 'not eq!!');
33+
// logger.assert(1 === 2, 'not eq!!');
34+
35+
36+
logger.debug('1 abc');
37+
38+
const nl = logger.newGroup('abc');
39+
nl.error('ccc');
40+
nl.debug('cccd');
41+
nl.success('cccd');
42+
nl.debug('111 scccd');
43+
nl.silly('cccd');

0 commit comments

Comments
 (0)