Skip to content
Permalink
Browse files

feat: support read egg.require from package.json (#121)

  • Loading branch information
whxaxes authored and atian25 committed Jan 4, 2019
1 parent 0d553f6 commit 904103fe673e93bdf600f6eace4121cf4bf15d9b
@@ -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
@@ -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

@@ -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);

@@ -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

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

@@ -0,0 +1,9 @@
{
"name": "demo-app",
"egg": {
"framework": "aliyun-egg",
"require": [
"../require-script"
]
}
}
@@ -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();
});
});
@@ -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);
@@ -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()

0 comments on commit 904103f

Please sign in to comment.
You can’t perform that action at this time.