Skip to content

Commit 4d265f0

Browse files
committedJun 1, 2015
[fixed] Use babel api to avoid command line conflicts between Linux and Windows
1 parent fc378de commit 4d265f0

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed
 

‎package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"scripts": {
1111
"build": "babel-node tools/build-cli.js",
1212
"test-watch": "karma start",
13-
"test": "npm run lint && npm run build && karma start --single-run && _mocha --compilers js:babel-core/register './test/server/*Spec.js'",
13+
"test": "npm run lint && npm run build && karma start --single-run && _mocha --compilers js:babel-core/register test/server/*Spec.js",
1414
"lint": "eslint ./",
1515
"docs-build": "babel-node tools/build-cli.js --docs-only",
1616
"docs": "docs/dev-run",
@@ -60,6 +60,7 @@
6060
"file-loader": "^0.8.1",
6161
"fs-extra": "^0.18.0",
6262
"fs-promise": "^0.3.1",
63+
"glob": "^5.0.10",
6364
"http-proxy": "^1.11.1",
6465
"ip": "^0.3.2",
6566
"json-loader": "^0.5.1",
@@ -81,6 +82,7 @@
8182
"mocha": "^2.2.1",
8283
"node-libs-browser": "^0.5.2",
8384
"nodemon": "^1.3.7",
85+
"output-file-sync": "^1.1.0",
8486
"phantomjs": "^1.9.17",
8587
"portfinder": "^0.4.0",
8688
"react": "^0.13.1",

‎tools/amd/build.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { copy } from '../fs-utils';
55
import { exec } from '../exec';
66
import generateFactories from '../generateFactories';
77
import { repoRoot, srcRoot, bowerRoot } from '../constants';
8+
import { buildFolder } from '../buildBabel';
89

910
const packagePath = path.join(repoRoot, 'package.json');
1011
const bowerTemplate = path.join(__dirname, 'bower.json');
@@ -13,7 +14,7 @@ const bowerJson = path.join(bowerRoot, 'bower.json');
1314
const readme = path.join(__dirname, 'README.md');
1415
const license = path.join(repoRoot, 'LICENSE');
1516

16-
const babelOptions = '--modules amd';
17+
const babelOptions = {modules: 'amd'};
1718

1819
const libDestination = path.join(bowerRoot, 'lib');
1920
const factoriesDestination = path.join(libDestination, 'factories');
@@ -37,7 +38,7 @@ export default function BuildBower() {
3738
.then(() => Promise.all([
3839
bowerConfig(),
3940
generateFactories(factoriesDestination, babelOptions),
40-
exec(`babel ${babelOptions} ${srcRoot} --out-dir ${libDestination}`),
41+
buildFolder(srcRoot, libDestination, babelOptions),
4142
copy(readme, bowerRoot),
4243
copy(license, bowerRoot)
4344
]))

‎tools/buildBabel.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { transform } from 'babel-core';
2+
import resolveRc from 'babel-core/lib/babel/tools/resolve-rc';
3+
import * as babelUtil from 'babel-core/lib/babel/util';
4+
import glob from 'glob';
5+
import fs from 'fs';
6+
import path from 'path';
7+
import outputFileSync from 'output-file-sync';
8+
9+
export function buildContent(content, filename, destination, babelOptions={}) {
10+
const result = transform(content, resolveRc(filename, babelOptions));
11+
outputFileSync(destination, result.code, {encoding: 'utf8'});
12+
}
13+
14+
export function buildFile(filename, destination, babelOptions={}) {
15+
const content = fs.readFileSync(filename, {encoding: 'utf8'});
16+
if(babelUtil.canCompile(filename)) {
17+
// Get file basename without the extension (in case not .js)
18+
let outputName = path.basename(filename, path.extname(filename));
19+
// append the file basename with extension .js
20+
let outputPath = path.join(destination, outputName + '.js');
21+
// console.log('%s => %s', filename, outputPath);
22+
buildContent(content, filename, outputPath, babelOptions);
23+
}
24+
}
25+
26+
export function buildFolder(folderPath, destination, babelOptions={}, firstFolder=true) {
27+
let stats = fs.statSync(folderPath);
28+
29+
if(stats.isFile()) {
30+
buildFile(folderPath, destination, babelOptions);
31+
} else if(stats.isDirectory()) {
32+
let outputPath = firstFolder ? destination : path.join(destination, path.basename(folderPath));
33+
let files = fs.readdirSync(folderPath).map(file => path.join(folderPath, file));
34+
files.forEach(filename => buildFolder(filename, outputPath, babelOptions, false));
35+
}
36+
}
37+
38+
export function buildGlob(filesGlob, destination, babelOptions={}) {
39+
let files = glob.sync(filesGlob);
40+
if (!files.length) {
41+
files = [filesGlob];
42+
}
43+
files.forEach(filename => buildFolder(filename, destination, babelOptions, true));
44+
}
45+

‎tools/generateFactories.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import _ from 'lodash';
22
import path from 'path';
33
import fsp from 'fs-promise';
4-
import { exec } from './exec';
54
import { srcRoot } from './constants';
65
import components from './public-components';
6+
import { buildContent } from './buildBabel';
77

88
const templatePath = path.join(srcRoot, 'templates');
99
const factoryTemplatePath = path.join(templatePath, 'factory.js.template');
1010
const indexTemplatePath = path.join(templatePath, 'factory.index.js.template');
1111

12-
export default function generateFactories(destination, babelOptions='') {
12+
export default function generateFactories(destination, babelOptions={}) {
1313

1414
let generateCompiledFile = function (file, content) {
1515
let outpath = path.join(destination, `${file}.js`);
16-
return exec(`babel ${babelOptions} --out-file ${outpath} <<EOF\n ${content}`);
16+
buildContent(content, __dirname, outpath, babelOptions);
1717
};
1818

1919
return Promise.all([

‎tools/lib/build.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'path';
44
import fsp from 'fs-promise';
55
import { srcRoot, libRoot } from '../constants';
66
import generateFactories from '../generateFactories';
7+
import { buildFolder } from '../buildBabel';
78

89
const factoryDestination = path.join(libRoot, 'factories');
910

@@ -14,7 +15,7 @@ export default function BuildCommonJs() {
1415
.then(() => fsp.mkdirs(factoryDestination))
1516
.then(() => Promise.all([
1617
generateFactories(factoryDestination),
17-
exec(`babel ${srcRoot} --out-dir ${libRoot}`)
18+
buildFolder(srcRoot, libRoot)
1819
]))
1920
.then(() => console.log('Built: '.cyan + 'npm module'.green));
2021
}

0 commit comments

Comments
 (0)
Please sign in to comment.