Skip to content

Commit

Permalink
[fixed] Use babel api to avoid command line conflicts between Linux a…
Browse files Browse the repository at this point in the history
…nd Windows
  • Loading branch information
Cellule committed Jun 1, 2015
1 parent fc378de commit 4d265f0
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts": {
"build": "babel-node tools/build-cli.js",
"test-watch": "karma start",
"test": "npm run lint && npm run build && karma start --single-run && _mocha --compilers js:babel-core/register './test/server/*Spec.js'",
"test": "npm run lint && npm run build && karma start --single-run && _mocha --compilers js:babel-core/register test/server/*Spec.js",
"lint": "eslint ./",
"docs-build": "babel-node tools/build-cli.js --docs-only",
"docs": "docs/dev-run",
Expand Down Expand Up @@ -60,6 +60,7 @@
"file-loader": "^0.8.1",
"fs-extra": "^0.18.0",
"fs-promise": "^0.3.1",
"glob": "^5.0.10",
"http-proxy": "^1.11.1",
"ip": "^0.3.2",
"json-loader": "^0.5.1",
Expand All @@ -81,6 +82,7 @@
"mocha": "^2.2.1",
"node-libs-browser": "^0.5.2",
"nodemon": "^1.3.7",
"output-file-sync": "^1.1.0",
"phantomjs": "^1.9.17",
"portfinder": "^0.4.0",
"react": "^0.13.1",
Expand Down
5 changes: 3 additions & 2 deletions tools/amd/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { copy } from '../fs-utils';
import { exec } from '../exec';
import generateFactories from '../generateFactories';
import { repoRoot, srcRoot, bowerRoot } from '../constants';
import { buildFolder } from '../buildBabel';

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

const babelOptions = '--modules amd';
const babelOptions = {modules: 'amd'};

const libDestination = path.join(bowerRoot, 'lib');
const factoriesDestination = path.join(libDestination, 'factories');
Expand All @@ -37,7 +38,7 @@ export default function BuildBower() {
.then(() => Promise.all([
bowerConfig(),
generateFactories(factoriesDestination, babelOptions),
exec(`babel ${babelOptions} ${srcRoot} --out-dir ${libDestination}`),
buildFolder(srcRoot, libDestination, babelOptions),
copy(readme, bowerRoot),
copy(license, bowerRoot)
]))
Expand Down
45 changes: 45 additions & 0 deletions tools/buildBabel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { transform } from 'babel-core';
import resolveRc from 'babel-core/lib/babel/tools/resolve-rc';
import * as babelUtil from 'babel-core/lib/babel/util';
import glob from 'glob';
import fs from 'fs';
import path from 'path';
import outputFileSync from 'output-file-sync';

export function buildContent(content, filename, destination, babelOptions={}) {
const result = transform(content, resolveRc(filename, babelOptions));
outputFileSync(destination, result.code, {encoding: 'utf8'});
}

export function buildFile(filename, destination, babelOptions={}) {
const content = fs.readFileSync(filename, {encoding: 'utf8'});
if(babelUtil.canCompile(filename)) {
// Get file basename without the extension (in case not .js)
let outputName = path.basename(filename, path.extname(filename));
// append the file basename with extension .js
let outputPath = path.join(destination, outputName + '.js');
// console.log('%s => %s', filename, outputPath);
buildContent(content, filename, outputPath, babelOptions);
}
}

export function buildFolder(folderPath, destination, babelOptions={}, firstFolder=true) {
let stats = fs.statSync(folderPath);

if(stats.isFile()) {
buildFile(folderPath, destination, babelOptions);
} else if(stats.isDirectory()) {
let outputPath = firstFolder ? destination : path.join(destination, path.basename(folderPath));
let files = fs.readdirSync(folderPath).map(file => path.join(folderPath, file));
files.forEach(filename => buildFolder(filename, outputPath, babelOptions, false));
}
}

export function buildGlob(filesGlob, destination, babelOptions={}) {
let files = glob.sync(filesGlob);
if (!files.length) {
files = [filesGlob];
}
files.forEach(filename => buildFolder(filename, destination, babelOptions, true));
}

6 changes: 3 additions & 3 deletions tools/generateFactories.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import _ from 'lodash';
import path from 'path';
import fsp from 'fs-promise';
import { exec } from './exec';
import { srcRoot } from './constants';
import components from './public-components';
import { buildContent } from './buildBabel';

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

export default function generateFactories(destination, babelOptions='') {
export default function generateFactories(destination, babelOptions={}) {

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

return Promise.all([
Expand Down
3 changes: 2 additions & 1 deletion tools/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path';
import fsp from 'fs-promise';
import { srcRoot, libRoot } from '../constants';
import generateFactories from '../generateFactories';
import { buildFolder } from '../buildBabel';

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

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

0 comments on commit 4d265f0

Please sign in to comment.