Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: support read egg.require from package.json #120

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 10 additions & 0 deletions test/fixtures/egg-require/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"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();
});
});