Skip to content

Commit

Permalink
feat: add project options in midway-bin
Browse files Browse the repository at this point in the history
  • Loading branch information
skmdev committed Apr 10, 2019
1 parent 12a7753 commit c635057
Show file tree
Hide file tree
Showing 20 changed files with 207 additions and 9 deletions.
54 changes: 46 additions & 8 deletions packages/midway-bin/lib/cmd/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class BuildCommand extends Command {
type: 'boolean',
alias: 'c',
},
project: {
description: 'project file location',
type: 'string',
alias: 'p',
default: 'tsconfig.json',
},
};
}

Expand All @@ -29,22 +35,41 @@ class BuildCommand extends Command {
const { cwd, argv } = context;

const tscCli = require.resolve('typescript/bin/tsc');
if (!fs.existsSync(path.join(cwd, 'tsconfig.json'))) {
if (!fs.existsSync(path.join(cwd, argv.project))) {
console.log(`[midway-bin] tsconfig.json not found in ${cwd}\n`);
return;
}

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

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

const args = [];

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

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

// if projectFile extended and without outDir,
// get setting from its parent
if (tsConfig && tsConfig.extends) {
if (
!tsConfig.compilerOptions ||
(tsConfig.compilerOptions && !tsConfig.compilerOptions.outDir)
) {
yield this.cleanDir(cwd, tsConfig.extends);
return;
}
}

if (tsConfig && tsConfig.compilerOptions) {
const outDir = tsConfig.compilerOptions.outDir;
if (outDir) {
Expand All @@ -53,8 +78,21 @@ class BuildCommand extends Command {
}
}

* copyFiles(cwd) {
const tsConfig = require(path.join(cwd, 'tsconfig.json'));
* copyFiles(cwd, projectFile) {
const tsConfig = require(path.join(cwd, projectFile));

// if projectFile extended and without outDir,
// get setting from its parent
if (tsConfig && tsConfig.extends) {
if (
!tsConfig.compilerOptions ||
(tsConfig.compilerOptions && !tsConfig.compilerOptions.outDir)
) {
yield this.copyFiles(cwd, tsConfig.extends);
return;
}
}

if (tsConfig && tsConfig.compilerOptions) {
const outDir = tsConfig.compilerOptions.outDir;
if (outDir && fs.existsSync(path.join(cwd, 'package.json'))) {
Expand Down
2 changes: 1 addition & 1 deletion packages/midway-bin/lib/cmd/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CleanCommand extends Command {
}, error => {
if (error) {
console.error(`[midway-bin] exec error: ${error}`);
reject();
reject(error);
return;
}
console.log('[midway-bin] clean midway temporary files complete!');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import * as path from 'path';
console.log(path.extname(__filename));
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "ES2017",
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"stripInternal": true,
"pretty": true,
"declaration": true,
"sourceMap": true,
"outDir": "dist",
"lib": ["es2017", "dom"]
},
"exclude": [
"dist",
"node_modules",
"test"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "ts-dir-with-assets",
"version": "0.0.1",
"dependencies": {

},
"midway-bin-build": {
"include": [
"public",
"view",
"resource.json",
"lib/*.json",
"lib/*.text",
["pattern/**", "!pattern/**/*.js"]
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import * as path from 'path';
console.log(path.extname(__filename));
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert(1);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert(1);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert(123);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"a": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "ES2017",
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"stripInternal": true,
"pretty": true,
"declaration": true,
"sourceMap": true,
"outDir": "dist",
"lib": ["es2017", "dom"]
},
"exclude": [
"dist",
"node_modules",
"test"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.json"
}
70 changes: 70 additions & 0 deletions packages/midway-bin/test/lib/cmd/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,75 @@ describe('test/lib/cmd/build.test.js', () => {
assert(fs.existsSync(path.join(cwd, 'dist/pattern/sub/sub_ignore.css')));
yield rimraf(path.join(cwd, 'dist'));
});
});

describe('test/lib/cmd/build.test.js - with another tsconfig', () => {
const midwayBin = require.resolve('../../../bin/midway-bin.js');

afterEach(mm.restore);

it('should warn message', function*() {
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();
});

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

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

it('should copy assets file to dist dir with another tsconfig', function*() {
const cwd = path.join(
__dirname,
'../../fixtures/ts-dir-with-assets-and-another-tsconfig'
);

const child = coffee.fork(
midwayBin,
['build', '-c', '-p', 'tsconfig.prod.json'],
{ cwd }
);
yield 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')));
assert(fs.existsSync(path.join(cwd, 'dist/public/test.js')));
assert(fs.existsSync(path.join(cwd, 'dist/resource.json')));
assert(fs.existsSync(path.join(cwd, 'dist/lib/b.json')));
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'));
});
});

0 comments on commit c635057

Please sign in to comment.