Skip to content

Commit

Permalink
Merge pull request react-bootstrap#852 from AlexKVal/factories
Browse files Browse the repository at this point in the history
Simplify factories generation.
  • Loading branch information
AlexKVal committed Jun 22, 2015
2 parents 7f65599 + c8a8194 commit 1bc3253
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
5 changes: 2 additions & 3 deletions tools/amd/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const babelOptions = {
};

const libDestination = path.join(bowerRoot, 'lib');
const factoriesDestination = path.join(libDestination, 'factories');

function bowerConfig() {
return Promise.all([
Expand All @@ -37,10 +36,10 @@ export default function BuildBower() {
console.log('Building: '.cyan + 'bower module'.green);

return exec(`rimraf ${bowerRoot}`)
.then(() => fsp.mkdirs(factoriesDestination))
.then(() => fsp.mkdirs(libDestination))
.then(() => Promise.all([
bowerConfig(),
generateFactories(factoriesDestination, babelOptions),
generateFactories(libDestination, babelOptions),
buildFolder(srcRoot, libDestination, babelOptions),
copy(readme, bowerRoot),
copy(license, bowerRoot)
Expand Down
30 changes: 11 additions & 19 deletions tools/generateFactories.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
import _ from 'lodash';
import path from 'path';
import fsp from 'fs-promise';
import fs from 'fs';
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={}) {

let generateCompiledFile = function (file, content) {
let outpath = path.join(destination, `${file}.js`);
function generateCompiledFile(file, content) {
const outpath = path.join(destination, 'factories', `${file}.js`);
buildContent(content, __dirname, outpath, babelOptions);
};
}

const indexTemplate = fs.readFileSync(path.join(srcRoot, 'templates', 'factory.index.js.template'));
const factoryTemplate = fs.readFileSync(path.join(srcRoot, 'templates', 'factory.js.template'));

return Promise.all([
fsp.readFile(factoryTemplatePath)
.then(template => {
Promise.all(components.map(name => {
generateCompiledFile(name, _.template(template)({name}));
}));
}),
fsp.readFile(indexTemplatePath)
.then(template => _.template(template)({components}))
.then(content => generateCompiledFile('index', content))
]);
generateCompiledFile( 'index', _.template(indexTemplate)({components}) );

return Promise.all(
components.map( name => generateCompiledFile( name, _.template(factoryTemplate)({name}) ))
);
}
7 changes: 2 additions & 5 deletions tools/lib/build.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import 'colors';
import { exec } from '../exec';
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');

export default function BuildCommonJs() {
console.log('Building: '.cyan + 'npm module'.green);

return exec(`rimraf ${libRoot}`)
.then(() => fsp.mkdirs(factoryDestination))
.then(() => fsp.mkdirs(libRoot))
.then(() => Promise.all([
generateFactories(factoryDestination),
generateFactories(libRoot),
buildFolder(srcRoot, libRoot)
]))
.then(() => console.log('Built: '.cyan + 'npm module'.green));
Expand Down

0 comments on commit 1bc3253

Please sign in to comment.