Skip to content

Commit 8a6fee8

Browse files
committed
🎨 整理结构, 修改默认logger, 增加prompt.
1 parent 9e7f4ce commit 8a6fee8

File tree

18 files changed

+245
-24
lines changed

18 files changed

+245
-24
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ Micro APP 依赖工具库.
99
[![NPM Version][npm-img]][npm-url]
1010
[![NPM Download][download-img]][download-url]
1111

12-
[Coverage-img]: https://coveralls.io/repos/github/MicrosApp/MicroApp-Shared-Utils/badge.svg?branch=master
13-
[Coverage-url]: https://coveralls.io/github/MicrosApp/MicroApp-Shared-Utils?branch=master
14-
[CircleCI-img]: https://circleci.com/gh/MicrosApp/MicroApp-Shared-Utils/tree/master.svg?style=svg
15-
[CircleCI-url]: https://circleci.com/gh/MicrosApp/MicroApp-Shared-Utils/tree/master
12+
[Coverage-img]: https://coveralls.io/repos/github/MicroAppJS/MicroApp-Shared-Utils/badge.svg?branch=master
13+
[Coverage-url]: https://coveralls.io/github/MicroAppJS/MicroApp-Shared-Utils?branch=master
14+
[CircleCI-img]: https://circleci.com/gh/MicroAppJS/MicroApp-Shared-Utils/tree/master.svg?style=svg
15+
[CircleCI-url]: https://circleci.com/gh/MicroAppJS/MicroApp-Shared-Utils/tree/master
1616
[npm-img]: https://img.shields.io/npm/v/@micro-app/shared-utils.svg?style=flat-square
1717
[npm-url]: https://npmjs.org/package/@micro-app/shared-utils
1818
[download-img]: https://img.shields.io/npm/dm/@micro-app/shared-utils.svg?style=flat-square

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
"lint:fix": "npm run lint -- --fix",
99
"test": "jest"
1010
},
11-
"homepage": "https://github.com/MicrosApp/MicroApp-Shared-Utils",
11+
"homepage": "https://github.com/MicroAppJS/MicroApp-Shared-Utils",
1212
"repository": {
1313
"type": "git",
14-
"url": "git+https://github.com/MicrosApp/MicroApp-Shared-Utils.git"
14+
"url": "git+https://github.com/MicroAppJS/MicroApp-Shared-Utils.git"
1515
},
1616
"bugs": {
17-
"url": "https://github.com/MicrosApp/MicroApp-Shared-Utils/issues"
17+
"url": "https://github.com/MicroAppJS/MicroApp-Shared-Utils/issues"
1818
},
1919
"files": [
2020
"src",
@@ -55,6 +55,7 @@
5555
"cheerio": "^1.0.0-rc.3",
5656
"fs-extra": "^8.1.0",
5757
"lodash": "^4.17.15",
58+
"npmlog": "^4.1.2",
5859
"ora": "^3.4.0",
5960
"semver": "^6.3.0",
6061
"semver-regex": "^3.1.0",

src/constants/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

33
module.exports = {
4+
SCOPE_NAME: '@micro-app',
45
INJECT_ID: '_MICRO_APP_INJECT_',
56
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/* global expect */
44

5-
const { getPadLength } = require('../');
5+
const { getPadLength } = require('../../');
66

77
describe('getPadLength', () => {
88

File renamed without changes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/* global expect */
44

5-
const { loadFile } = require('../');
5+
const { loadFile } = require('../..');
66

77
describe('loadFile', () => {
88

@@ -25,7 +25,7 @@ describe('loadFile', () => {
2525
});
2626

2727
it('success', () => {
28-
const file = loadFile(__dirname, 'a.js');
28+
const file = loadFile(__dirname, '../../test/a.js');
2929

3030
expect(file).not.toBeNull();
3131
});

src/logger.js renamed to src/logger/index.js

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,47 @@
11
'use strict';
22

3+
const npmlog = require('npmlog');
4+
35
const chalk = require('chalk');
46
const utils = require('util');
57
const ora = require('ora');
68
const os = require('os');
79

10+
npmlog.heading = require('../constants').SCOPE_NAME || '@micro-app';
11+
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+
}
15+
// npmlog.prefixStyle = {};
16+
const CUSTOM_LEVEL = {
17+
debug: {
18+
disp: '*DEBUG*',
19+
bg: 'magenta',
20+
},
21+
info: {
22+
fg: 'brightGreen',
23+
},
24+
success: {
25+
fg: 'brightWhite',
26+
bg: 'green',
27+
bold: true,
28+
},
29+
noise: {
30+
fg: 'blue',
31+
level: 10000,
32+
beep: true,
33+
},
34+
};
35+
Object.keys(CUSTOM_LEVEL).forEach(key => {
36+
const style = CUSTOM_LEVEL[key];
37+
const disp = style.disp || (key.length > 4 ? key.substr(0, 4) : key).toUpperCase();
38+
npmlog.addLevel(key, style.level || 'info', style, disp);
39+
});
40+
841
const getStdoutMethod = function(type) {
42+
if ([ 'debug', 'success' ].includes(type)) {
43+
type = 'log';
44+
}
945
if (!process) {
1046
if (type) {
1147
return console[type].bind(console);
@@ -18,6 +54,10 @@ const getStdoutMethod = function(type) {
1854
return process.stdout.write.bind(process.stdout);
1955
};
2056

57+
const getNpmlogMethod = function(type) {
58+
return npmlog[type].bind(npmlog);
59+
};
60+
2161
const toString = {
2262
debug() {
2363
const message = utils.format(...(arguments || []));
@@ -41,23 +81,37 @@ const toString = {
4181
},
4282
};
4383

44-
module.exports = {
84+
const getMethod = function(type) {
85+
const logger = getNpmlogMethod(type);
86+
return function(...args) {
87+
if (args.length <= 1) {
88+
return logger(false, ...args);
89+
}
90+
return logger(...args);
91+
};
92+
// const logger = getStdoutMethod(type);
93+
// return function(...args) {
94+
// return logger(toString[type].call(toString, ...args));
95+
// };
96+
};
97+
98+
const logger = {
4599
toString,
46100
debug() {
47101
if (!process.env.MICRO_APP_DEBUG_LOGGER) return; // 是否开启
48-
return getStdoutMethod('log')(toString.debug.call(toString, ...arguments));
102+
return getMethod('debug')(...arguments);
49103
},
50104
warn() {
51-
return getStdoutMethod('warn')(toString.warn.call(toString, ...arguments));
105+
return getMethod('warn')(...arguments);
52106
},
53107
error() {
54-
return getStdoutMethod('error')(toString.error.call(toString, ...arguments));
108+
return getMethod('error')(...arguments);
55109
},
56110
info() {
57-
return getStdoutMethod('info')(toString.info.call(toString, ...arguments));
111+
return getMethod('info')(...arguments);
58112
},
59113
success() {
60-
return getStdoutMethod('log')(toString.success.call(toString, ...arguments));
114+
return getMethod('success')(...arguments);
61115
},
62116

63117
/**
@@ -90,5 +144,16 @@ module.exports = {
90144
process.exit(1);
91145
},
92146
};
147+
module.exports = new Proxy(logger, {
148+
get(target, prop) {
149+
if (!(prop in target) && typeof prop === 'string') {
150+
return getMethod(prop).bind(target);
151+
}
152+
return target[prop];
153+
},
154+
});
93155

156+
module.exports.npmlog = npmlog;
94157
module.exports.getStdoutMethod = getStdoutMethod;
158+
module.exports.getNpmlogMethod = getNpmlogMethod;
159+
module.exports.getMethod = getMethod;
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/* global expect */
44

5-
const { logger } = require('../');
5+
const { logger } = require('../..');
66

77
describe('Logger', () => {
88

@@ -46,4 +46,11 @@ describe('Logger', () => {
4646

4747
});
4848

49+
it('logger npmlog', () => {
50+
51+
const npmlog = require('npmlog');
52+
npmlog.info('abcddd');
53+
54+
});
55+
4956
});

src/prompt/index.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
const chalk = require('chalk');
4+
const inquirer = require('inquirer');
5+
const logger = require('../logger');
6+
const npmlog = logger.npmlog;
7+
8+
const SCOPE_NAME = require('../constants').SCOPE_NAME || '@micro-app';
9+
const prefix = `[${chalk.green('?')}] ${chalk.bgBlack(chalk.whiteBright(SCOPE_NAME))}`;
10+
11+
function createDefaultOptions(options = {}) {
12+
return Object.assign({
13+
prefix,
14+
}, options);
15+
}
16+
17+
function confirm(message) {
18+
npmlog.pause();
19+
20+
return inquirer
21+
.prompt([
22+
createDefaultOptions({
23+
type: 'expand',
24+
name: 'confirm',
25+
message,
26+
default: 2, // default to help in order to avoid clicking straight through
27+
choices: [
28+
{ key: 'y', name: 'Yes', value: true },
29+
{ key: 'n', name: 'No', value: false },
30+
],
31+
}),
32+
])
33+
.then(answers => {
34+
npmlog.resume();
35+
36+
return answers.confirm;
37+
});
38+
}
39+
40+
function select(message, { choices, filter, validate } = {}) {
41+
if (!choices || choices.length <= 0) {
42+
logger.throw('prompt', 'select choices is empty!');
43+
}
44+
45+
npmlog.pause();
46+
47+
return inquirer
48+
.prompt([
49+
createDefaultOptions({
50+
type: 'list',
51+
name: 'prompt',
52+
message,
53+
choices,
54+
pageSize: choices.length,
55+
filter,
56+
validate,
57+
}),
58+
])
59+
.then(answers => {
60+
npmlog.resume();
61+
62+
return answers.prompt;
63+
});
64+
}
65+
66+
function input(message, { filter, validate } = {}) {
67+
npmlog.pause();
68+
69+
return inquirer
70+
.prompt([
71+
createDefaultOptions({
72+
type: 'input',
73+
name: 'input',
74+
message,
75+
filter,
76+
validate,
77+
}),
78+
])
79+
.then(answers => {
80+
npmlog.resume();
81+
82+
return answers.input;
83+
});
84+
}
85+
86+
exports.confirm = confirm;
87+
exports.select = select;
88+
exports.input = input;

0 commit comments

Comments
 (0)