Skip to content

Commit

Permalink
build(platform): Improve consistency (yarn), and async (#84)
Browse files Browse the repository at this point in the history
- Add CircleCI override to use latest yarn for deterministic builds
- Refactor some overly obtuse functions
- Use async throughout
- Add commitizen-friendly badge
  • Loading branch information
bfricka authored and MikeRyanDev committed Jul 16, 2017
1 parent 8804156 commit b9776ea
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 81 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Reactive libraries for Angular

## Support

[![Join the chat at https://gitter.im/ngrx/store](https://badges.gitter.im/ngrx/store.svg)](https://gitter.im/ngrx/store?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Join the chat at https://gitter.im/ngrx/store](https://badges.gitter.im/ngrx/store.svg)](https://gitter.im/ngrx/store?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)


## Packages

Expand Down
72 changes: 36 additions & 36 deletions build/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import * as util from './util';
* Cleans the top level dist folder. All npm-ready packages are created
* in the dist folder.
*/
export async function removeDistFolder(config: Config) {
const args = ['./dist'];

return await util.exec('rimraf', args);
export function removeDistFolder(config: Config) {
return util.exec('rimraf', ['./dist']);
}

/**
Expand All @@ -22,8 +20,8 @@ export async function compilePackagesWithNgc(config: Config) {
const testPkgs = util.getTestingPackages(config);

await _compilePackagesWithNgc(storePkg);
await mapPackages(restPkgs, _compilePackagesWithNgc);
await mapPackages(testPkgs, _compilePackagesWithNgc);
await mapAsync(restPkgs, _compilePackagesWithNgc);
await mapAsync(testPkgs, _compilePackagesWithNgc);
}

async function _compilePackagesWithNgc(pkg: string) {
Expand All @@ -32,8 +30,10 @@ async function _compilePackagesWithNgc(pkg: string) {
const entryTypeDefinition = `export * from './${pkg}/index';`;
const entryMetadata = `{"__symbolic":"module","version":3,"metadata":{},"exports":[{"from":"./${pkg}/index"}]}`;

util.writeFile(`./dist/packages/${pkg}.d.ts`, entryTypeDefinition);
util.writeFile(`./dist/packages/${pkg}.metadata.json`, entryMetadata);
await Promise.all([
util.writeFile(`./dist/packages/${pkg}.d.ts`, entryTypeDefinition),
util.writeFile(`./dist/packages/${pkg}.metadata.json`, entryMetadata),
]);
}

/**
Expand All @@ -43,7 +43,7 @@ async function _compilePackagesWithNgc(pkg: string) {
export async function bundleFesms(config: Config) {
const pkgs = util.getAllPackages(config);

await mapPackages(pkgs, async pkg => {
await mapAsync(pkgs, async pkg => {
const topLevelName = util.getTopLevelName(pkg);

await util.exec('rollup', [
Expand All @@ -64,14 +64,13 @@ export async function downLevelFesmsToES5(config: Config) {
const packages = util.getAllPackages(config);
const tscArgs = ['--target es5', '--module es2015', '--noLib', '--sourceMap'];

await mapPackages(packages, async pkg => {
await mapAsync(packages, async pkg => {
const topLevelName = util.getTopLevelName(pkg);

const file = `./dist/${topLevelName}/${config.scope}/${pkg}.js`;
const target = `./dist/${topLevelName}/${config.scope}/${pkg}.es5.ts`;

util.copy(file, target);

await util.copy(file, target);
await util.ignoreErrors(util.exec('tsc', [target, ...tscArgs]));
await util.mapSources(target.replace('.ts', '.js'));
await util.remove(target);
Expand All @@ -84,7 +83,7 @@ export async function downLevelFesmsToES5(config: Config) {
* Re-runs Rollup on the downleveled ES5 to produce a UMD bundle
*/
export async function createUmdBundles(config: Config) {
await mapPackages(util.getAllPackages(config), async pkg => {
await mapAsync(util.getAllPackages(config), async pkg => {
const topLevelName = util.getTopLevelName(pkg);
const destinationName = util.getDestinationName(pkg);

Expand All @@ -106,26 +105,25 @@ export async function cleanTypeScriptFiles(config: Config) {
const dtsFilesFlob = './dist/packages/**/*.d.ts';
const filesToRemove = await util.getListOfFiles(tsFilesGlob, dtsFilesFlob);

for (let file of filesToRemove) {
util.remove(file);
}
await mapAsync(filesToRemove, util.remove);
}

/**
* Renames the index files in each package to the name
* of the package.
*/
export async function renamePackageEntryFiles(config: Config) {
await mapPackages(util.getAllPackages(config), async pkg => {
await mapAsync(util.getAllPackages(config), async pkg => {
const bottomLevelName = util.getBottomLevelName(pkg);

const files = await util.getListOfFiles(`./dist/packages/${pkg}/index.**`);

for (let file of files) {
await mapAsync(files, async file => {
const target = file.replace('index', bottomLevelName);
util.copy(file, target);
util.remove(file);
}

await util.copy(file, target);
await util.remove(file);
});
});
}

Expand All @@ -150,10 +148,10 @@ export async function copyTypeDefinitionFiles(config: Config) {
`./dist/packages/?(${packages.join('|')})/**/*`
);

for (let file of files) {
await mapAsync(files, async file => {
const target = file.replace('packages/', '');
util.copy(file, target);
}
await util.copy(file, target);
});

await util.removeRecursively(`./dist/packages/?(${packages.join('|')})`);
}
Expand All @@ -164,7 +162,7 @@ export async function copyTypeDefinitionFiles(config: Config) {
export async function minifyUmdBundles(config: Config) {
const uglifyArgs = ['-c', '-m', '--screw-ie8', '--comments'];

await mapPackages(util.getAllPackages(config), async pkg => {
await mapAsync(util.getAllPackages(config), async pkg => {
const topLevelName = util.getTopLevelName(pkg);
const destinationName = util.getDestinationName(pkg);
const file = `./dist/${topLevelName}/bundles/${destinationName}.umd.js`;
Expand All @@ -187,24 +185,26 @@ export async function minifyUmdBundles(config: Config) {
export async function copyDocs(config: Config) {
const packages = util.getTopLevelPackages(config);

for (let pkg of packages) {
await mapAsync(packages, async pkg => {
const source = `./modules/${pkg}`;
const target = `./dist/${pkg}`;

util.copy(`${source}/README.md`, `${target}/README.md`);
util.copy('./LICENSE', `${target}/LICENSE`);
}
await Promise.all([
util.copy(`${source}/README.md`, `${target}/README.md`),
util.copy('./LICENSE', `${target}/LICENSE`),
]);
});
}

export async function copyPackageJsonFiles(config: Config) {
const packages = util.getAllPackages(config);

for (let pkg of packages) {
await mapAsync(packages, async pkg => {
const source = `./modules/${pkg}`;
const target = `./dist/${pkg}`;

util.copy(`${source}/package.json`, `${target}/package.json`);
}
await util.copy(`${source}/package.json`, `${target}/package.json`);
});
}

/**
Expand Down Expand Up @@ -254,9 +254,9 @@ export async function publishToRepo(config: Config) {
}
}

export function mapPackages(
packages: string[],
mapFn: (pkg: string, i: number) => Promise<any>
export function mapAsync<T>(
list: T[],
mapFn: (v: T, i: number) => Promise<any>
) {
return Promise.all(packages.map(mapFn));
return Promise.all(list.map(mapFn));
}
85 changes: 43 additions & 42 deletions build/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,35 @@ import * as path from 'path';
import * as rimraf from 'rimraf';
import { Config } from './config';

export function copy(target: string, destination: string) {
fsExtra.copySync(target, destination);
}

export function remove(target: string) {
fsExtra.removeSync(target);
}
export type RunnerFn = (config: Config) => Promise<any>;
export type TaskDef = [string, RunnerFn];
export type BaseFn = (command: string) => string;

export function writeFile(target: string, contents: string) {
fs.writeFileSync(target, contents);
export function copy(target: string, destination: string): Promise<void> {
return new Promise((resolve, reject) => {
fsExtra.copy(target, destination, err => {
if (err) return reject(err);
resolve();
});
});
}

export function mkdir(target: string) {
fs.mkdirSync(target);
export function remove(target: string): Promise<void> {
return new Promise((resolve, reject) => {
fsExtra.remove(target, err => {
if (err) return reject(err);
resolve();
});
});
}

export function rmdir(target: string) {
fs.rmdirSync(target);
export function writeFile(target: string, contents: string) {
return new Promise((resolve, reject) => {
fs.writeFile(target, contents, err => {
if (err) return reject(err);
resolve();
});
});
}

export function getListOfFiles(
Expand Down Expand Up @@ -58,10 +69,10 @@ export function removeRecursively(glob: string) {
export function exec(
command: string,
args: string[],
base: (command: string) => string = fromNpm
base: BaseFn = fromNpm
): Promise<string> {
return new Promise((resolve, reject) => {
cp.exec(base(command) + ' ' + args.join(' '), (err, stdout, stderr) => {
cp.exec(base(command) + ' ' + args.join(' '), (err, stdout) => {
if (err) {
return reject(err);
}
Expand All @@ -76,37 +87,25 @@ export function cmd(command: string, args: string[]): Promise<string> {
}

export function git(args: string[]): Promise<string> {
return exec('git', args, (command: string) => command);
return cmd('git', args);
}

export async function ignoreErrors<T>(promise: Promise<T>): Promise<T | null> {
try {
const result = await promise;
return result;
} catch (err) {
return null;
}
export function ignoreErrors<T>(promise: Promise<T>): Promise<T | null> {
return promise.catch(() => null);
}

export function fromNpm(command: string) {
return path.normalize(`./node_modules/.bin/${command}`);
return baseDir(`./node_modules/.bin/${command}`);
}

export function getPackageFilePath(pkg: string, filename: string) {
return path.resolve(process.cwd(), `./modules/${pkg}/${filename}`);
return baseDir(`./modules/${pkg}/${filename}`);
}

const sorcery = require('sorcery');
export function mapSources(file: string) {
return new Promise((resolve, reject) => {
sorcery
.load(file)
.then((chain: any) => {
chain.write();
resolve();
})
.catch(reject);
});
export async function mapSources(file: string) {
const chain = await sorcery.load(file);
chain.write();
}

const ora = require('ora');
Expand All @@ -126,9 +125,7 @@ async function runTask(name: string, taskFn: () => Promise<any>) {
}
}

export function createBuilder(
tasks: [string, (config: Config) => Promise<any>][]
) {
export function createBuilder(tasks: TaskDef[]) {
return async function(config: Config) {
for (let [name, runner] of tasks) {
await runTask(name, () => runner(config));
Expand Down Expand Up @@ -157,12 +154,12 @@ export function getTestingPackages(config: Config) {
}

export function getAllPackages(config: Config) {
return flatMap(config.packages, packageDescription => {
if (packageDescription.hasTestingModule) {
return [packageDescription.name, `${packageDescription.name}/testing`];
return flatMap(config.packages, ({ name, hasTestingModule }) => {
if (hasTestingModule) {
return [name, `${name}/testing`];
}

return [packageDescription.name];
return [name];
});
}

Expand All @@ -177,3 +174,7 @@ export function getTopLevelName(packageName: string) {
export function getBottomLevelName(packageName: string) {
return packageName.includes('/testing') ? 'testing' : packageName;
}

export function baseDir(...dirs: string[]): string {
return path.resolve(__dirname, '../', ...dirs);
}
15 changes: 13 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
machine:
environment:
PATH: "${PATH}:${HOME}/${CIRCLE_PROJECT_REPONAME}/node_modules/.bin"
node:
version: 6.9.5
test:

dependencies:
pre:
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 0.28.1
cache_directories:
- ~/.cache/yarn
override:
- npm install -g yarn
- yarn
- yarn run bootstrap -- --concurrency=1

test:
override:
- yarn run ci

deployment:
builds:
owner: ngrx
Expand Down

0 comments on commit b9776ea

Please sign in to comment.