Skip to content

Commit

Permalink
feat(midway-bin): use async-await instead of generator
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingsong authored and czy88840616 committed Nov 22, 2019
1 parent e518ab5 commit eed48f1
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 84 deletions.
20 changes: 10 additions & 10 deletions packages/midway-bin/lib/cmd/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class BuildCommand extends Command {
return 'build application automatically';
}

* run(context) {
async run(context) {
const { cwd, argv } = context;

const tscCli = require.resolve('typescript/bin/tsc');
Expand All @@ -46,21 +46,21 @@ class BuildCommand extends Command {
}

if (argv.clean) {
yield this.cleanDir(cwd, argv.project);
await this.cleanDir(cwd, argv.project);
}

yield this.copyFiles(cwd, argv.project, argv);
await this.copyFiles(cwd, argv.project, argv);

const args = [];

if (argv.project) {
args.push('-p');
args.push(argv.project);
}
yield this.helper.forkNode(tscCli, args, { cwd });
await this.helper.forkNode(tscCli, args, { cwd });
}

* cleanDir(cwd, projectFile) {
async cleanDir(cwd, projectFile) {
const tsConfig = require(path.join(cwd, projectFile));

// if projectFile extended and without outDir,
Expand All @@ -70,20 +70,20 @@ class BuildCommand extends Command {
!tsConfig.compilerOptions ||
(tsConfig.compilerOptions && !tsConfig.compilerOptions.outDir)
) {
yield this.cleanDir(cwd, tsConfig.extends);
await this.cleanDir(cwd, tsConfig.extends);
return;
}
}

if (tsConfig && tsConfig.compilerOptions) {
const outDir = tsConfig.compilerOptions.outDir;
if (outDir) {
yield rimraf(outDir);
await rimraf(outDir);
}
}
}

* copyFiles(cwd, projectFile, argv) {
async copyFiles(cwd, projectFile, argv) {
const tsConfig = require(path.join(cwd, projectFile));

// if projectFile extended and without outDir,
Expand All @@ -93,7 +93,7 @@ class BuildCommand extends Command {
!tsConfig.compilerOptions ||
(tsConfig.compilerOptions && !tsConfig.compilerOptions.outDir)
) {
yield this.copyFiles(cwd, tsConfig.extends, argv);
await this.copyFiles(cwd, tsConfig.extends, argv);
return;
}
}
Expand All @@ -111,7 +111,7 @@ class BuildCommand extends Command {
this.copyFile(srcDir, targetDir, cwd);
} else {
// 通配符的情况
const paths = yield globby([].concat(file), {
const paths = await globby([].concat(file), {
cwd: path.join(cwd, argv.srcDir),
});
for (const p of paths) {
Expand Down
10 changes: 5 additions & 5 deletions packages/midway-bin/lib/cmd/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ class CleanCommand extends Command {
return 'clean application temporary files';
}

* run(context) {
async run(context) {
const { cwd } = context;
if (!fs.existsSync(path.join(cwd, 'package.json'))) {
console.log(`[midway-bin] package.json not found in ${cwd}\n`);
return;
}
yield this.cleanDir(cwd);
await this.cleanDir(cwd);
}

* cleanDir(cwd) {
yield new Promise((resolve, reject) => {
async cleanDir(cwd) {
await new Promise((resolve, reject) => {
cp.exec('find . -type d -name \'logs\' -or -name \'run\' -or -name \'.nodejs-cache\' | xargs rm -rf', {
cwd,
}, error => {
Expand All @@ -43,7 +43,7 @@ class CleanCommand extends Command {
const pkg = require(path.join(cwd, 'package.json'));
if (pkg['midway-bin-clean'] && pkg['midway-bin-clean'].length) {
for (const file of pkg['midway-bin-clean']) {
yield rimraf(path.join(cwd, file));
await rimraf(path.join(cwd, file));
console.log(`[midway-bin] clean ${file} success!`);
}
console.log('[midway-bin] clean complete!');
Expand Down
4 changes: 2 additions & 2 deletions packages/midway-bin/lib/cmd/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class DebugCommand extends require('egg-bin').DebugCommand {
this.usage = 'Usage: midway-bin debug [dir] [options]';
}

* run(context) {
async run(context) {
if (!context.argv.framework) {
context.argv.framework = this.findFramework('midway') || this.findFramework('midway-mirror');
}
yield super.run(context);
await super.run(context);
}

findFramework(module) {
Expand Down
4 changes: 2 additions & 2 deletions packages/midway-bin/lib/cmd/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class DevCommand extends require('egg-bin/lib/cmd/dev') {
this.defaultPort = process.env.PORT || 7001;
}

* run(context) {
async run(context) {
if (!context.argv.framework) {
context.argv.framework = this.findFramework('midway') || this.findFramework('midway-mirror');
}
yield super.run(context);
await super.run(context);
}

findFramework(module) {
Expand Down
4 changes: 2 additions & 2 deletions packages/midway-bin/lib/cmd/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class DocCommand extends Command {
return 'generate typescript document by typedoc';
}

* run({ cwd, argv }) {
async run({ cwd, argv }) {
let args;
if (argv.options) {
// if has options args just ignore others
Expand All @@ -68,7 +68,7 @@ class DocCommand extends Command {
}

const docBin = require.resolve('typedoc/bin/typedoc');
yield this.helper.forkNode(docBin, args, { cwd });
await this.helper.forkNode(docBin, args, { cwd });
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/midway-bin/lib/cmd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class TestCommand extends require('egg-bin').TestCommand {
this.usage = 'Usage: midway-bin test [files] [options]';
}

* run(context) {
async run(context) {
if (!context.env.NODE_ENV) {
context.env.NODE_ENV = 'unittest';
}
yield super.run(context);
await super.run(context);
}
}

Expand Down
56 changes: 28 additions & 28 deletions packages/midway-bin/test/lib/cmd/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,36 @@ describe('test/lib/cmd/build.test.js', () => {

afterEach(mm.restore);

it('should warn message', function* () {
it('should warn message', async () => {
const cwd = path.join(__dirname, '../../fixtures/ts-dir-without-config');
const child = coffee
.fork(midwayBin, [ 'build' ], { cwd })
.expect('stdout', /tsconfig/);

yield child.expect('code', 0).end();
await child.expect('code', 0).end();
});

it('should build success', function* () {
it('should build success', async () => {
const cwd = path.join(__dirname, '../../fixtures/ts-dir');
yield rimraf(path.join(cwd, 'dist'));
await rimraf(path.join(cwd, 'dist'));
const child = coffee.fork(midwayBin, [ 'build' ], { cwd });
yield child.expect('code', 0).end();
await child.expect('code', 0).end();
assert(fs.existsSync(path.join(cwd, 'dist/a.js')));
yield rimraf(path.join(cwd, 'dist'));
await rimraf(path.join(cwd, 'dist'));
});

it('should auto clean dir before build', function* () {
it('should auto clean dir before build', async () => {
const cwd = path.join(__dirname, '../../fixtures/ts-dir');
const child = coffee.fork(midwayBin, [ 'build' ], { cwd });
yield child.expect('code', 0).end();
await child.expect('code', 0).end();
assert(fs.existsSync(path.join(cwd, 'dist/a.js')));
yield rimraf(path.join(cwd, 'dist'));
await rimraf(path.join(cwd, 'dist'));
});

it('should copy assets file to dist dir', function* () {
it('should copy assets file to dist dir', async () => {
const cwd = path.join(__dirname, '../../fixtures/ts-dir-with-assets');
const child = coffee.fork(midwayBin, [ 'build', '-c' ], { cwd });
yield child.expect('code', 0).end();
await child.expect('code', 0).end();
assert(fs.existsSync(path.join(cwd, 'dist/a.js')));
assert(fs.existsSync(path.join(cwd, 'dist/view/index.html')));
assert(fs.existsSync(path.join(cwd, 'dist/public/test.css')));
Expand All @@ -51,20 +51,20 @@ describe('test/lib/cmd/build.test.js', () => {
assert(fs.existsSync(path.join(cwd, 'dist/lib/a.text')));
assert(fs.existsSync(path.join(cwd, 'dist/pattern/ignore.css')));
assert(fs.existsSync(path.join(cwd, 'dist/pattern/sub/sub_ignore.css')));
yield rimraf(path.join(cwd, 'dist'));
await rimraf(path.join(cwd, 'dist'));
});

it('should copy assets file and ignore not exists directory', function* () {
it('should copy assets file and ignore not exists directory', async () => {
const cwd = path.join(
__dirname,
'../../fixtures/ts-dir-with-not-exists-file'
);
const child = coffee.fork(midwayBin, [ 'build', '-c' ], { cwd }).debug();
yield child.expect('code', 0).end();
await child.expect('code', 0).end();
assert(!fs.existsSync(path.join(cwd, 'dist/view/index.html')));
assert(fs.existsSync(path.join(cwd, 'dist/public/test.css')));
assert(fs.existsSync(path.join(cwd, 'dist/public/test.js')));
yield rimraf(path.join(cwd, 'dist'));
await rimraf(path.join(cwd, 'dist'));
});
});

Expand All @@ -73,48 +73,48 @@ describe('test/lib/cmd/build.test.js - with another tsconfig', () => {

afterEach(mm.restore);

it('should warn message', function* () {
it('should warn message', async () => {
const cwd = path.join(__dirname, '../../fixtures/ts-dir-without-config');
const child = coffee
.fork(midwayBin, [ 'build', '-p', 'tsconfig.prod.json' ], { cwd })
.expect('stdout', /tsconfig/);

yield child.expect('code', 0).end();
await child.expect('code', 0).end();
});

it('should build success with another tsconfig', function* () {
it('should build success with another tsconfig', async () => {
const cwd = path.join(
__dirname,
'../../fixtures/ts-dir-with-another-tsconfig'
);
yield rimraf(path.join(cwd, 'dist'));
await rimraf(path.join(cwd, 'dist'));
const child = coffee.fork(
midwayBin,
[ 'build', '-p', 'tsconfig.prod.json' ],
{ cwd }
);
yield child.expect('code', 0).end();
await child.expect('code', 0).end();
assert(fs.existsSync(path.join(cwd, 'dist/a.js')));
yield rimraf(path.join(cwd, 'dist'));
await rimraf(path.join(cwd, 'dist'));
});

it('should auto clean dir before build with another tsconfig', function* () {
it('should auto clean dir before build with another tsconfig', async () => {
const cwd = path.join(
__dirname,
'../../fixtures/ts-dir-with-another-tsconfig'
);
yield rimraf(path.join(cwd, 'dist'));
await rimraf(path.join(cwd, 'dist'));
const child = coffee.fork(
midwayBin,
[ 'build', '-p', 'tsconfig.prod.json' ],
{ cwd }
);
yield child.expect('code', 0).end();
await child.expect('code', 0).end();
assert(fs.existsSync(path.join(cwd, 'dist/a.js')));
yield rimraf(path.join(cwd, 'dist'));
await rimraf(path.join(cwd, 'dist'));
});

it('should copy assets file to dist dir with another tsconfig', function* () {
it('should copy assets file to dist dir with another tsconfig', async () => {
const cwd = path.join(
__dirname,
'../../fixtures/ts-dir-with-assets-and-another-tsconfig'
Expand All @@ -125,7 +125,7 @@ describe('test/lib/cmd/build.test.js - with another tsconfig', () => {
[ 'build', '-c', '-p', 'tsconfig.prod.json' ],
{ cwd }
);
yield child.expect('code', 0).end();
await child.expect('code', 0).end();
assert(fs.existsSync(path.join(cwd, 'dist/a.js')));
assert(fs.existsSync(path.join(cwd, 'dist/view/index.html')));
assert(fs.existsSync(path.join(cwd, 'dist/public/test.css')));
Expand All @@ -135,6 +135,6 @@ describe('test/lib/cmd/build.test.js - with another tsconfig', () => {
assert(fs.existsSync(path.join(cwd, 'dist/lib/a.text')));
assert(fs.existsSync(path.join(cwd, 'dist/pattern/ignore.css')));
assert(fs.existsSync(path.join(cwd, 'dist/pattern/sub/sub_ignore.css')));
yield rimraf(path.join(cwd, 'dist'));
await rimraf(path.join(cwd, 'dist'));
});
});
18 changes: 9 additions & 9 deletions packages/midway-bin/test/lib/cmd/clean.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ describe('test/lib/cmd/clean.test.js', () => {

afterEach(mm.restore);

it('should clean dir', function *() {
it('should clean dir', async () => {
const cwd = path.join(__dirname, '../../fixtures/clean-dir');
yield mkdirp(path.join(cwd, 'logs/test.log'));
yield mkdirp(path.join(cwd, '.nodejs-cache/test.log'));
yield mkdirp(path.join(cwd, 'run/a.log'));
await mkdirp(path.join(cwd, 'logs/test.log'));
await mkdirp(path.join(cwd, '.nodejs-cache/test.log'));
await mkdirp(path.join(cwd, 'run/a.log'));

const child = coffee.fork(midwayBin, [ 'clean' ], { cwd });
yield child.expect('code', 0).end();
await child.expect('code', 0).end();
assert(fs.existsSync(path.join(cwd, 'a.ts')));
assert(!fs.existsSync(path.join(cwd, '.nodejs-cache')));
assert(!fs.existsSync(path.join(cwd, 'run')));
assert(!fs.existsSync(path.join(cwd, 'logs')));
});

it('should clean file with config', function *() {
it('should clean file with config', async () => {
const cwd = path.join(__dirname, '../../fixtures/clean-dir-config');
yield mkdirp(path.join(cwd, 'customDir/a.js'));
await mkdirp(path.join(cwd, 'customDir/a.js'));

const child = coffee.fork(midwayBin, [ 'clean'], { cwd });
yield child.expect('code', 0).end();
const child = coffee.fork(midwayBin, [ 'clean' ], { cwd });
await child.expect('code', 0).end();
assert(!fs.existsSync(path.join(cwd, 'customDir')));
});

Expand Down
20 changes: 10 additions & 10 deletions packages/midway-bin/test/lib/cmd/doc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@ describe('test/lib/cmd/doc.test.js', () => {

afterEach(mm.restore);

it('should generate doc use default value', function *() {
it('should generate doc use default value', async () => {
const cwd = path.join(__dirname, '../../fixtures/ts-dir');

const child = coffee.fork(midwayBin, [ 'doc'], { cwd });
yield child.expect('code', 0).end();
const child = coffee.fork(midwayBin, [ 'doc' ], { cwd });
await child.expect('code', 0).end();
assert(fs.existsSync(path.join(cwd, 'doc/index.html')));
yield rimraf(path.join(cwd, 'doc'));
await rimraf(path.join(cwd, 'doc'));
});

it('should generate doc use custom value', function *() {
it('should generate doc use custom value', async () => {
const cwd = path.join(__dirname, '../../fixtures/ts-dir');

const child = coffee.fork(midwayBin, [ 'doc', '-o', 'api' ], { cwd });
yield child.expect('code', 0).end();
await child.expect('code', 0).end();
assert(fs.existsSync(path.join(cwd, 'api/index.html')));
yield rimraf(path.join(cwd, 'api'));
await rimraf(path.join(cwd, 'api'));
});

it('should generate doc', function *() {
it('should generate doc', async () => {
const cwd = path.join(__dirname, '../../fixtures/ts-dir-doc-options');

const child = coffee.fork(midwayBin, [ 'doc', '--options', 'typedoc.js' ], { cwd });
yield child.expect('code', 0).end();
await child.expect('code', 0).end();
assert(fs.existsSync(path.join(cwd, 'docs/api/index.html')));
yield rimraf(path.join(cwd, 'docs'));
await rimraf(path.join(cwd, 'docs'));
});

});
Loading

0 comments on commit eed48f1

Please sign in to comment.