From 4d265f08e4c08cf8db2d19672452a37e204cd2a4 Mon Sep 17 00:00:00 2001 From: Michael Ferris Date: Fri, 29 May 2015 18:56:21 -0400 Subject: [PATCH] [fixed] Use babel api to avoid command line conflicts between Linux and Windows --- package.json | 4 +++- tools/amd/build.js | 5 +++-- tools/buildBabel.js | 45 ++++++++++++++++++++++++++++++++++++++ tools/generateFactories.js | 6 ++--- tools/lib/build.js | 3 ++- 5 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 tools/buildBabel.js diff --git a/package.json b/package.json index ce93b3d8d7..c8aa57ce7b 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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", diff --git a/tools/amd/build.js b/tools/amd/build.js index 4544c74522..d52aedf7e5 100644 --- a/tools/amd/build.js +++ b/tools/amd/build.js @@ -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'); @@ -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'); @@ -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) ])) diff --git a/tools/buildBabel.js b/tools/buildBabel.js new file mode 100644 index 0000000000..e2ef5c66e2 --- /dev/null +++ b/tools/buildBabel.js @@ -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)); +} + diff --git a/tools/generateFactories.js b/tools/generateFactories.js index aac7a25344..edc05137c1 100644 --- a/tools/generateFactories.js +++ b/tools/generateFactories.js @@ -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} < 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)); }