Skip to content

Commit 1a7f09c

Browse files
authored
feat: support pkgInfo.eggScriptsConfig.require (#47)
1 parent a68ac67 commit 1a7f09c

File tree

15 files changed

+267
-47
lines changed

15 files changed

+267
-47
lines changed

.travis.yml

-24
This file was deleted.

appveyor.yml

-15
This file was deleted.

lib/command.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ class Command extends BaseCommand {
2626
type: 'boolean',
2727
alias: [ 'ts', 'typescript' ],
2828
},
29+
30+
require: {
31+
description: 'inject to execArgv --require',
32+
type: 'array',
33+
alias: 'r',
34+
},
2935
};
3036

3137
this.logger = new Logger({
@@ -38,22 +44,45 @@ class Command extends BaseCommand {
3844
const context = super.context;
3945
const { argv, execArgvObj, cwd } = context;
4046

41-
// read `egg.typescript` from package.json
4247
let baseDir = argv._[0] || cwd;
4348
if (!path.isAbsolute(baseDir)) baseDir = path.join(cwd, baseDir);
4449
const pkgFile = path.join(baseDir, 'package.json');
4550
if (fs.existsSync(pkgFile)) {
4651
const pkgInfo = require(pkgFile);
47-
if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript) {
52+
const eggInfo = pkgInfo.egg;
53+
54+
// read `egg.typescript` from package.json
55+
if (eggInfo && eggInfo.typescript) {
4856
argv.sourcemap = true;
4957
}
5058

59+
// read `eggScriptsConfig.require` from package.json
60+
const eggScriptsConfig = pkgInfo.eggScriptsConfig;
61+
let requireFiles = Array.isArray(argv.require) ? argv.require : [];
62+
if (eggScriptsConfig && Array.isArray(eggScriptsConfig.require)) {
63+
requireFiles = requireFiles.concat(eggScriptsConfig.require);
64+
}
65+
execArgvObj.require = execArgvObj.require || [];
66+
requireFiles
67+
.filter(injectScript => injectScript)
68+
.forEach(injectScript => {
69+
let requirePath = '';
70+
if (path.isAbsolute(injectScript) || injectScript.startsWith(`.${path.sep}`)) {
71+
requirePath = path.resolve(baseDir, injectScript);
72+
} else {
73+
requirePath = injectScript;
74+
}
75+
execArgvObj.require.push(requirePath);
76+
});
77+
5178
// read argv from eggScriptsConfig in package.json
52-
if (pkgInfo && pkgInfo.eggScriptsConfig && typeof pkgInfo.eggScriptsConfig === 'object') {
79+
if (eggScriptsConfig && typeof eggScriptsConfig === 'object') {
5380
for (const key in pkgInfo.eggScriptsConfig) {
5481
if (argv[key] == null) argv[key] = pkgInfo.eggScriptsConfig[key];
5582
}
5683
}
84+
85+
delete argv.require;
5786
}
5887

5988
// execArgv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
exports.keys = '123456';
4+
5+
exports.logger = {
6+
level: 'WARN',
7+
consoleLevel: 'WARN',
8+
};
+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
'use strict';
2+
3+
module.exports = {
4+
// enable plugins
5+
6+
/**
7+
* app global Error Handling
8+
* @member {Object} Plugin#onerror
9+
* @property {Boolean} enable - `true` by default
10+
*/
11+
onerror: {
12+
enable: false,
13+
package: 'egg-onerror',
14+
path: 'xxxxx',
15+
},
16+
17+
/**
18+
* session
19+
* @member {Object} Plugin#session
20+
* @property {Boolean} enable - `true` by default
21+
* @since 1.0.0
22+
*/
23+
session: {
24+
enable: false,
25+
package: 'egg-session',
26+
path: 'xxxxx',
27+
},
28+
29+
/**
30+
* i18n
31+
* @member {Object} Plugin#i18n
32+
* @property {Boolean} enable - `true` by default
33+
* @since 1.0.0
34+
*/
35+
i18n: {
36+
enable: false,
37+
package: 'egg-i18n',
38+
path: 'xxxxx',
39+
},
40+
41+
/**
42+
* file and dir watcher
43+
* @member {Object} Plugin#watcher
44+
* @property {Boolean} enable - `true` by default
45+
* @since 1.0.0
46+
*/
47+
watcher: {
48+
enable: false,
49+
package: 'egg-watcher',
50+
path: 'xxxxx',
51+
},
52+
53+
/**
54+
* multipart
55+
* @member {Object} Plugin#multipart
56+
* @property {Boolean} enable - `true` by default
57+
* @since 1.0.0
58+
*/
59+
multipart: {
60+
enable: false,
61+
package: 'egg-multipart',
62+
path: 'xxxxx',
63+
},
64+
65+
/**
66+
* security middlewares and extends
67+
* @member {Object} Plugin#security
68+
* @property {Boolean} enable - `true` by default
69+
* @since 1.0.0
70+
*/
71+
security: {
72+
enable: false,
73+
package: 'egg-security',
74+
path: 'xxxxx',
75+
},
76+
77+
/**
78+
* local development helper
79+
* @member {Object} Plugin#development
80+
* @property {Boolean} enable - `true` by default
81+
* @since 1.0.0
82+
*/
83+
development: {
84+
enable: false,
85+
package: 'egg-development',
86+
path: 'xxxxx',
87+
},
88+
89+
/**
90+
* logger file rotator
91+
* @member {Object} Plugin#logrotator
92+
* @property {Boolean} enable - `true` by default
93+
* @since 1.0.0
94+
*/
95+
logrotator: {
96+
enable: false,
97+
package: 'egg-logrotator',
98+
path: 'xxxxx',
99+
},
100+
101+
/**
102+
* schedule tasks
103+
* @member {Object} Plugin#schedule
104+
* @property {Boolean} enable - `true` by default
105+
* @since 2.7.0
106+
*/
107+
schedule: {
108+
enable: false,
109+
package: 'egg-schedule',
110+
path: 'xxxxx',
111+
},
112+
113+
/**
114+
* `app/public` dir static serve
115+
* @member {Object} Plugin#static
116+
* @property {Boolean} enable - `true` by default
117+
* @since 1.0.0
118+
*/
119+
static: {
120+
enable: false,
121+
package: 'egg-static',
122+
path: 'xxxxx',
123+
},
124+
125+
/**
126+
* jsonp support for egg
127+
* @member {Function} Plugin#jsonp
128+
* @property {Boolean} enable - `true` by default
129+
* @since 1.0.0
130+
*/
131+
jsonp: {
132+
enable: false,
133+
package: 'egg-jsonp',
134+
path: 'xxxxx',
135+
},
136+
137+
/**
138+
* view plugin
139+
* @member {Function} Plugin#view
140+
* @property {Boolean} enable - `true` by default
141+
* @since 1.0.0
142+
*/
143+
view: {
144+
enable: false,
145+
package: 'egg-view',
146+
path: 'xxxxx',
147+
},
148+
};

test/fixtures/pkg-config/inject.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
console.log('@@@ inject relative js by pkgInfo');

test/fixtures/pkg-config/inject2.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
console.log('@@@ inject by cli');

test/fixtures/pkg-config/node_modules/custom-framework/index.js

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/pkg-config/node_modules/custom-framework/package.json

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/pkg-config/node_modules/inject/index.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/pkg-config/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.0",
4+
"egg": {
5+
"framework": "custom-framework"
6+
},
7+
"eggScriptsConfig": {
8+
"require": [
9+
"./inject.js",
10+
"inject"
11+
]
12+
}
13+
}

test/fixtures/ts-pkg/app/controller/home.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default class AppController extends Controller {
44
public index() {
55
try {
66
throw new Error('some err');
7-
} catch (err) {
7+
} catch (err: any) {
88
this.ctx.logger.error(err);
99
this.ctx.body = {
1010
msg: err.message,

test/fixtures/ts/app/controller/home.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default class AppController extends Controller {
44
public index() {
55
try {
66
throw new Error('some err');
7-
} catch (err) {
7+
} catch (err: any) {
88
this.ctx.logger.error(err);
99
this.ctx.body = {
1010
msg: err.message,

test/start.test.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,34 @@ describe('test/start.test.js', () => {
3030
afterEach(mm.restore);
3131

3232
describe('start without daemon', () => {
33+
describe('read pkgInfo', () => {
34+
let app;
35+
let fixturePath;
36+
37+
before(function* () {
38+
fixturePath = path.join(__dirname, 'fixtures/pkg-config');
39+
yield utils.cleanup(fixturePath);
40+
});
41+
42+
after(function* () {
43+
app.proc.kill('SIGTERM');
44+
yield utils.cleanup(fixturePath);
45+
});
46+
47+
it('should --require', function* () {
48+
app = coffee.fork(eggBin, [ 'start', '--workers=1', '--require=./inject2' ], { cwd: fixturePath });
49+
app.debug();
50+
app.expect('code', 0);
51+
52+
yield sleep(waitTime);
53+
54+
assert(app.stderr === '');
55+
assert(app.stdout.match(/@@@ inject relative js by pkgInfo/));
56+
assert(app.stdout.match(/@@@ inject node_modules by pkgInfo/));
57+
assert(app.stdout.match(/@@@ inject by cli/));
58+
});
59+
});
60+
3361
describe('full path', () => {
3462
let app;
3563

@@ -551,7 +579,11 @@ describe('test/start.test.js', () => {
551579
const exitEvent = awaitEvent(app.proc, 'exit');
552580
app.proc.kill('SIGTERM');
553581
const code = yield exitEvent;
554-
assert(code === 0);
582+
if (isWin) {
583+
assert(code === null);
584+
} else {
585+
assert(code === 0);
586+
}
555587
});
556588
});
557589
});

test/stop.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('test/stop.test.js', () => {
1818
const timeoutPath = path.join(__dirname, 'fixtures/stop-timeout');
1919
const homePath = path.join(__dirname, 'fixtures/home');
2020
const logDir = path.join(homePath, 'logs');
21-
const waitTime = '10s';
21+
const waitTime = '15s';
2222

2323
before(function* () {
2424
yield mkdirp(homePath);
@@ -271,7 +271,7 @@ describe('test/stop.test.js', () => {
271271
describe('stop all with timeout', function() {
272272
let app;
273273
let killer;
274-
this.timeout(12000);
274+
this.timeout(17000);
275275
beforeEach(function* () {
276276
yield utils.cleanup(timeoutPath);
277277
app = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=stop-timeout', timeoutPath ]);

0 commit comments

Comments
 (0)