Skip to content

Commit

Permalink
feat: support read egg.require from package.json (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes authored and atian25 committed Jan 4, 2019
1 parent 0d553f6 commit 904103f
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test/fixtures/ts/node_modules/aliyun-egg/
!test/fixtures/test-files-stack/node_modules/
!test/fixtures/example/node_modules/
!test/fixtures/example-ts-error-stack/node_modules/
!test/fixtures/egg-require/node_modules/


**/run/*.json
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ $ egg-bin dev
- `--cluster` worker process number, skip this argvs will start only `1` worker, provide this without value will start `cpu` count worker.
- `--sticky` start a sticky cluster server, default to `false`.
- `--typescript` / `--ts` enable typescript support, default to `false`. Also support read from `package.json`'s `egg.typescript`.
- `--require` will add to `execArgv`, support multiple.
- `--require` will add to `execArgv`, support multiple. Also support read from `package.json`'s `egg.require`

### debug

Expand Down
2 changes: 1 addition & 1 deletion lib/cmd/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class DevCommand extends Command {
* formatArgs(context) {
const { cwd, argv } = context;
/* istanbul ignore next */
argv.baseDir = argv._[0] || argv.baseDir || cwd;
argv.baseDir = argv.baseDir || cwd;
/* istanbul ignore next */
if (!path.isAbsolute(argv.baseDir)) argv.baseDir = path.join(cwd, argv.baseDir);

Expand Down
26 changes: 15 additions & 11 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,26 @@ class Command extends BaseCommand {
// remove unuse args
argv.$0 = undefined;

// read package.json
let baseDir = argv.baseDir || cwd;
if (!path.isAbsolute(baseDir)) baseDir = path.join(cwd, baseDir);
const pkgFile = path.join(baseDir, 'package.json');
const pkgInfo = fs.existsSync(pkgFile) ? require(pkgFile) : null;
const eggInfo = pkgInfo && pkgInfo.egg;
execArgvObj.require = execArgvObj.require || [];

// read `egg.typescript` from package.json if not pass argv
if (argv.typescript === undefined) {
let baseDir = argv._[0] || argv.baseDir || cwd;
if (!path.isAbsolute(baseDir)) baseDir = path.join(cwd, baseDir);
const pkgFile = path.join(baseDir, 'package.json');
if (fs.existsSync(pkgFile)) {
const pkgInfo = require(pkgFile);
if (pkgInfo && pkgInfo.egg && pkgInfo.egg.typescript === true) {
argv.typescript = true;
}
}
if (argv.typescript === undefined && eggInfo && eggInfo.typescript === true) {
argv.typescript = true;
}

// read `egg.require` from package.json
if (eggInfo && eggInfo.require && Array.isArray(eggInfo.require)) {
execArgvObj.require = execArgvObj.require.concat(eggInfo.require);
}

// execArgv
if (argv.typescript) {
execArgvObj.require = execArgvObj.require || [];
execArgvObj.require.push(require.resolve('ts-node/register'));

// tell egg loader to load ts file
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/egg-require/node_modules/aliyun-egg/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/fixtures/egg-require/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "demo-app",
"egg": {
"framework": "aliyun-egg",
"require": [
"../require-script"
]
}
}
11 changes: 11 additions & 0 deletions test/lib/cmd/dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,15 @@ describe('test/lib/cmd/dev.test.js', () => {
.expect('code', 0)
.end();
});

it('should support egg.require', () => {
mm(process.env, 'NODE_ENV', 'development');
return coffee.fork(eggBin, [ 'dev' ], {
cwd: path.join(__dirname, '../../fixtures/egg-require'),
})
// .debug()
.expect('stdout', /hey, you require me by --require/)
.expect('code', 0)
.end();
});
});
2 changes: 1 addition & 1 deletion test/my-egg-bin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('test/my-egg-bin.test.js', () => {
.expect('stdout', /"baseDir":".\/dist"/)
.expect('stdout', /debugPort: 6666/)
.notExpect('stdout', /"argv: {.*debugBrk":true/)
.expect('debugOptions:', /{"debug":true,"debug-brk":5555,"inspect":6666,"inspect-brk":true}/)
.expect('stdout', /{"debug":true,"debug-brk":5555,"inspect":6666,"inspect-brk":true}/)
.expect('stdout', /execArgv: \["--debug","--debug-brk=5555","--expose_debug_as=v8debug","--inspect=6666","--inspect-brk","--es_staging","--harmony","--harmony_default_parameters"]/)
.expect('code', 0)
.end(done);
Expand Down
10 changes: 0 additions & 10 deletions test/ts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,6 @@ describe('test/ts.test.js', () => {
.end();
});

it('should start app with relative path', () => {
return coffee.fork(eggBin, [ 'dev', './example-ts-pkg' ], { cwd: path.dirname(cwd) })
// .debug()
.expect('stdout', /hi, egg, 12345/)
.expect('stdout', /ts env: true/)
.expect('stdout', /started/)
.expect('code', 0)
.end();
});

it('should test app', () => {
return coffee.fork(eggBin, [ 'test' ], { cwd })
// .debug()
Expand Down

0 comments on commit 904103f

Please sign in to comment.