Skip to content

Commit baf6dc3

Browse files
committed
:enhance: 增强工具链.
1 parent 8a6fee8 commit baf6dc3

File tree

12 files changed

+305
-45
lines changed

12 files changed

+305
-45
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
coverage
33
.vscode
44
.circleci
5+
*.d.ts

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ module.exports = {
88
"eslint-config-2o3t"
99
],
1010
parserOptions: {
11-
parser: "babel-eslint"
11+
parser: "babel-eslint",
1212
},
1313
}

index.d.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import fs from 'fs-extra';
2+
import chalk from 'chalk';
3+
import cheerio from 'cheerio';
4+
import semver from 'semver';
5+
import semverRegex from 'semver-regex';
6+
import _ from 'lodash';
7+
import ora from 'ora';
8+
import dedent from 'dedent';
9+
import globby from 'globby';
10+
import globParent from 'glob-parent';
11+
import isGlob from 'is-glob';
12+
import npa from 'npm-package-arg';
13+
import parseGitUrl from 'git-url-parse';
14+
15+
export function tryRequire( id: string, req?: Object): any | null;
16+
export function assert(value: any, message?: string | Error): void;
17+
18+
import * as moduleAlias from './src/moduleAlias';
19+
import * as getPadLength from './src/getPadLength';
20+
import * as injectHtml from './src/injectHtml';
21+
import * as loadFile from './src/loadFile';
22+
import * as logger from './src/logger';
23+
import * as smartMerge from './src/smartMerge';
24+
import * as virtualFile from './src/virtualFile';
25+
26+
export {
27+
moduleAlias,
28+
getPadLength,
29+
injectHtml,
30+
loadFile,
31+
logger,
32+
smartMerge,
33+
virtualFile,
34+
};
35+
36+
export {
37+
assert,
38+
fs,
39+
chalk,
40+
cheerio,
41+
semver,
42+
semverRegex,
43+
_,
44+
ora,
45+
dedent,
46+
globby,
47+
globParent,
48+
isGlob,
49+
npa,
50+
parseGitUrl,
51+
}

index.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
11
'use strict';
22

3-
[
3+
const internal = [
44
'moduleAlias',
55
'getPadLength',
66
'injectHtml',
77
'loadFile',
88
'logger',
99
'smartMerge',
1010
'virtualFile',
11-
].forEach(m => {
12-
Object.assign(exports, {
13-
[m]: require(`./src/${m}`),
11+
].reduce((obj, key) => {
12+
obj[key] = require(`./src/${key}`);
13+
return obj;
14+
}, {});
15+
16+
internal.assert = internal.logger.assert.bind(internal.logger);
17+
18+
const thirdParty = {
19+
fs: 'fs-extra',
20+
chalk: 'chalk',
21+
cheerio: 'cheerio',
22+
semver: 'semver',
23+
semverRegex: 'semver-regex',
24+
_: 'lodash',
25+
tryRequire: 'try-require',
26+
ora: 'ora',
27+
dedent: 'dedent',
28+
globby: 'globby',
29+
globParent: 'glob-parent',
30+
isGlob: 'is-glob',
31+
npa: 'npm-package-arg',
32+
parseGitUrl: 'git-url-parse',
33+
};
34+
35+
Object.keys(thirdParty).forEach(key => {
36+
// lazy
37+
Object.defineProperty(internal, key, {
38+
get() {
39+
return require(thirdParty[key]);
40+
},
1441
});
1542
});
1643

17-
exports.assert = require('assert');
18-
exports.fs = require('fs-extra');
19-
exports.chalk = require('chalk');
20-
exports.cheerio = require('cheerio');
21-
exports.semver = require('semver');
22-
exports.semverRegex = require('semver-regex');
23-
exports._ = require('lodash');
24-
exports.tryRequire = require('try-require');
25-
exports.ora = require('ora');
44+
module.exports = internal;

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,23 @@
5353
"assert": "^2.0.0",
5454
"chalk": "^2.4.2",
5555
"cheerio": "^1.0.0-rc.3",
56+
"dedent": "^0.7.0",
5657
"fs-extra": "^8.1.0",
58+
"git-url-parse": "^11.1.2",
59+
"glob-parent": "^5.1.0",
60+
"globby": "^10.0.1",
61+
"import-fresh": "^3.1.0",
62+
"is-glob": "^4.0.1",
5763
"lodash": "^4.17.15",
64+
"npm-package-arg": "^6.1.1",
5865
"npmlog": "^4.1.2",
5966
"ora": "^3.4.0",
67+
"parse-json": "^5.0.0",
6068
"semver": "^6.3.0",
6169
"semver-regex": "^3.1.0",
6270
"stream-to-string": "^1.2.0",
63-
"try-require": "^1.2.1"
71+
"try-require": "^1.2.1",
72+
"yaml": "^1.7.2"
6473
},
6574
"engines": {
6675
"node": ">=8"

src/loadFile/index.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
'use strict';
22

3-
const tryRequire = require('try-require');
43
const fs = require('fs-extra');
54
const path = require('path');
65

6+
const loaders = require('./loaders');
7+
const logger = require('../logger');
8+
9+
const DEFAULT_LOADERS = Object.freeze({
10+
'.js': loaders.loadJs,
11+
'.json': loaders.loadJson,
12+
'.yaml': loaders.loadYaml,
13+
'.yml': loaders.loadYaml,
14+
// noExt: loaders.loadYaml,
15+
});
16+
17+
function loaderExt(filename) {
18+
return path.extname(filename) || 'noExt';
19+
}
20+
721
function isSupport(filename) {
8-
return [ '.js', '.json' ].some(ext => {
9-
return filename.endsWith(ext);
22+
const _ext = loaderExt(filename);
23+
return Object.keys(DEFAULT_LOADERS).some(ext => {
24+
return ext === _ext;
1025
});
1126
}
1227

@@ -18,14 +33,24 @@ function load(root, filename) {
1833
if (!fs.statSync(filePath).isFile()) {
1934
return null;
2035
}
21-
return tryRequire(filePath);
36+
const loaderKey = loaderExt(filename);
37+
const loader = DEFAULT_LOADERS[loaderKey];
38+
if (!loader) {
39+
logger.throw(`No loader specified for extension "${loaderKey}", so "${filename}" is invalid`);
40+
}
41+
try {
42+
return loader(filePath);
43+
} catch (err) {
44+
logger.throw(err);
45+
}
2246
}
2347

2448
function loadFile(root, filename, { before, after } = {}) {
2549
if (!root || !filename) {
2650
return null;
2751
}
2852
if (!isSupport(filename)) {
53+
logger.warn(`Not Support extension, so "${filename}" is invalid`);
2954
return null;
3055
}
3156
if (before && !before(root, filename)) {

src/loadFile/loadFile.test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,31 @@ describe('loadFile', () => {
1313
});
1414

1515
it('not exist', () => {
16-
const file = loadFile(__dirname, 'abc.jsx');
16+
const file = loadFile(process.cwd(), 'abc.jsx');
1717

1818
expect(file).toBeNull();
1919
});
2020

2121
it('not file', () => {
22-
const file = loadFile(__dirname, '../');
22+
const file = loadFile(process.cwd(), './');
2323

2424
expect(file).toBeNull();
2525
});
2626

2727
it('success', () => {
28-
const file = loadFile(__dirname, '../../test/a.js');
28+
const file = loadFile(process.cwd(), 'test/a.js');
29+
30+
expect(file).not.toBeNull();
31+
});
32+
33+
it('json', () => {
34+
const file = loadFile(process.cwd(), 'package.json');
35+
36+
expect(file).not.toBeNull();
37+
});
38+
39+
it('yaml', () => {
40+
const file = loadFile(process.cwd(), 'test/a.yaml');
2941

3042
expect(file).not.toBeNull();
3143
});

src/loadFile/loaders.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
const fs = require('fs-extra');
4+
const _ = require('lodash');
5+
6+
const loadFile = function loadFile(filepath) {
7+
const content = fs.readFileSync(filepath, 'utf8');
8+
return content;
9+
};
10+
11+
let importFresh;
12+
const loadJs = function loadJs(filepath) {
13+
if (!importFresh) {
14+
importFresh = require('import-fresh');
15+
}
16+
17+
const result = importFresh(filepath);
18+
return result;
19+
};
20+
21+
let parseJson;
22+
const loadJson = function loadJson(filepath, content) {
23+
if (!parseJson) {
24+
parseJson = require('parse-json');
25+
}
26+
if (_.isUndefined(content)) {
27+
content = loadFile(filepath);
28+
}
29+
try {
30+
const result = parseJson(content);
31+
return result;
32+
} catch (error) {
33+
error.message = `JSON Error in ${filepath}:\n${error.message}`;
34+
throw error;
35+
}
36+
};
37+
38+
let yaml;
39+
const loadYaml = function loadYaml(filepath, content) {
40+
if (!yaml) {
41+
yaml = require('yaml');
42+
}
43+
if (_.isUndefined(content)) {
44+
content = loadFile(filepath);
45+
}
46+
try {
47+
const result = yaml.parse(content, { prettyErrors: true });
48+
return result;
49+
} catch (error) {
50+
error.message = `YAML Error in ${filepath}:\n${error.message}`;
51+
throw error;
52+
}
53+
};
54+
55+
module.exports = { loadJs, loadJson, loadYaml };

src/logger/index.js

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

33
const npmlog = require('npmlog');
4-
4+
const assert = require('assert');
55
const chalk = require('chalk');
66
const utils = require('util');
77
const ora = require('ora');
@@ -14,21 +14,15 @@ if (process.env.MICRO_APP_LOGGER_LEVEL) {
1414
}
1515
// npmlog.prefixStyle = {};
1616
const CUSTOM_LEVEL = {
17-
debug: {
18-
disp: '*DEBUG*',
19-
bg: 'magenta',
20-
},
21-
info: {
22-
fg: 'brightGreen',
23-
},
2417
success: {
18+
level: 3001,
2519
fg: 'brightWhite',
2620
bg: 'green',
2721
bold: true,
2822
},
2923
noise: {
30-
fg: 'blue',
3124
level: 10000,
25+
bg: 'magenta',
3226
beep: true,
3327
},
3428
};
@@ -55,6 +49,9 @@ const getStdoutMethod = function(type) {
5549
};
5650

5751
const getNpmlogMethod = function(type) {
52+
if ([ 'debug' ].includes(type)) {
53+
type = 'verbose';
54+
}
5855
return npmlog[type].bind(npmlog);
5956
};
6057

@@ -98,7 +95,7 @@ const getMethod = function(type) {
9895
const logger = {
9996
toString,
10097
debug() {
101-
if (!process.env.MICRO_APP_DEBUG_LOGGER) return; // 是否开启
98+
// if (!process.env.MICRO_APP_DEBUG_LOGGER) return; // 是否开启
10299
return getMethod('debug')(...arguments);
103100
},
104101
warn() {
@@ -143,6 +140,14 @@ const logger = {
143140
}
144141
process.exit(1);
145142
},
143+
144+
assert(...args) {
145+
try {
146+
assert(...args);
147+
} catch (err) {
148+
this.throw('[assert]', err.message);
149+
}
150+
},
146151
};
147152
module.exports = new Proxy(logger, {
148153
get(target, prop) {

test/a.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- name: 'abc'

0 commit comments

Comments
 (0)