Modernize scripts.

This commit is contained in:
cpojer 2017-05-22 11:51:24 +01:00
parent bc03c8d231
commit a7983ebcc8
8 changed files with 132 additions and 136 deletions

View File

@ -33,7 +33,7 @@
"scripts": {
"build-clean": "rm -rf ./packages/*/build",
"build": "node ./scripts/build.js",
"clean-all": "rm -rf ./node_modules; rm -rf ./packages/*/node_modules; yarn run build-clean",
"clean-all": "rm -rf ./node_modules && rm -rf ./packages/*/node_modules && yarn run build-clean",
"jest-coverage": "yarn run jest -- --coverage",
"lint": "eslint . --cache",
"postinstall": "node ./scripts/postinstall.js && node ./scripts/build.js",
@ -41,7 +41,7 @@
"test-ci": "yarn run typecheck && yarn run lint && yarn run build && yarn run jest-coverage -- -i && node scripts/mapCoverage.js && codecov",
"test": "yarn run typecheck && yarn run lint && yarn run build && yarn run jest && yarn run test-examples",
"typecheck": "flow check",
"watch": "yarn run build; node ./scripts/watch.js"
"watch": "yarn run build --silent && node ./scripts/watch.js"
},
"jest": {
"automock": true,

View File

@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const fs = require('fs');
const path = require('path');
@ -14,7 +13,8 @@ const PACKAGES_DIR = path.resolve(__dirname, '../packages');
// Get absolute paths of all directories under packages/*
module.exports = function getPackages() {
return fs.readdirSync(PACKAGES_DIR)
return fs
.readdirSync(PACKAGES_DIR)
.map(file => path.resolve(PACKAGES_DIR, file))
.filter(f => fs.lstatSync(path.resolve(f)).isDirectory());
};

31
scripts/_runCommand.js Normal file
View File

@ -0,0 +1,31 @@
/**
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
const spawn = require('child_process').spawnSync;
const chalk = require('chalk');
module.exports = function runCommand(cmd, args, cwd) {
if (!cwd) {
cwd = __dirname;
}
const displayArgs = args.length > 25
? args.slice(0, 25) + '...'
: args.join(' ');
console.log(chalk.dim('$ cd ' + cwd + `\n$ ${cmd} ${displayArgs}\n`));
const result = spawn(cmd, args, {
cwd,
stdio: 'inherit',
});
if (result.error || result.status !== 0) {
const message = 'Error running command.';
const error = new Error(message);
error.stack = message;
throw error;
}
};

View File

@ -1,63 +0,0 @@
/**
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const chalk = require('chalk');
const spawnSync = require('child_process').spawnSync;
/**
* @example `runCommands('npm test', packageDirectory)`
* @example `runCommands(['npm test', 'npm publish'], packageDirectory)`
* @param {boolean} options.suppressOutput don't print output to stdout
*/
module.exports = function runCommands(commands, cwd, options) {
options || (options = {});
if (!cwd) {
cwd = __dirname;
}
[].concat(commands).forEach(commandAndArgs => {
const cmd = commandAndArgs.split(' ')[0];
const args = commandAndArgs.split(' ').slice(1);
let msg = chalk.green('-> ') + chalk.underline.bold('running:') +
' ' + chalk.bold.cyan(cmd + ' ' + args.join(' '));
if (cwd) {
msg += ' cwd: ' + chalk.underline.bold(cwd);
}
const spawnOpts = {cwd};
if (options.suppressOutput) {
msg += chalk.red(' (output supressed)');
} else {
Object.assign(spawnOpts, {stdio: [0, 1, 1]});
}
console.log(msg);
const result = spawnSync(cmd, args, spawnOpts);
if (result.error || result.status !== 0) {
result.error && console.log(chalk.red(result.error));
// print output if it was suppressed
if (options.suppressOutput) {
result.stdout && console.log(result.stdout.toString());
result.stderr && console.log(chalk.red(result.stderr.toString()));
}
console.log(chalk.red(
`-> failed running: ${cmd + ' ' + args.join(' ')}`
));
process.exit(1);
}
});
};

View File

@ -6,8 +6,6 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
/**
* script to build (transpile) files.
* By default it transpiles all files for all packages and writes them
@ -19,25 +17,34 @@
* node ./scripts/build.js /user/c/metro-bundler/packages/metro-abc/src/abc.js
*/
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const mkdirp = require('mkdirp');
const babel = require('babel-core');
const chalk = require('chalk');
const fs = require('fs');
const getPackages = require('./_getPackages');
const glob = require('glob');
const micromatch = require('micromatch');
const path = require('path');
const spawnSync = require('child_process').spawnSync;
const getPackages = require('./_getPackages');
const SRC_DIR = 'src';
const BUILD_DIR = 'build';
const BUILD_ES5_DIR = 'build-es5';
const JS_FILES_PATTERN = '**/*.js';
const IGNORE_PATTERN = '**/__tests__/**';
const PACKAGES_DIR = path.resolve(__dirname, '../packages');
const babelOptions = JSON.parse(fs.readFileSync(
path.resolve(__dirname, '..', '.babelrc'),
'utf8'
));
babelOptions.babelrc = false;
const babelNodeOptions = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '..', '.babelrc'), 'utf8')
);
babelNodeOptions.babelrc = false;
const babelEs5Options = Object.assign(
{},
babelNodeOptions,
{presets: 'env'},
{plugins: [...babelNodeOptions.plugins, 'transform-runtime']}
);
const fixedWidth = str => {
const WIDTH = 80;
@ -49,54 +56,83 @@ const fixedWidth = str => {
return strs.slice(0, -1).concat(lastString).join('\n');
};
function getPackageName(file) {
return path.relative(PACKAGES_DIR, file).split(path.sep)[0];
}
function getBuildPath(file, buildFolder) {
const pkgName = getPackageName(file);
const pkgSrcPath = path.resolve(PACKAGES_DIR, pkgName, SRC_DIR);
const pkgBuildPath = path.resolve(PACKAGES_DIR, pkgName, buildFolder);
const relativeToSrcPath = path.relative(pkgSrcPath, file);
return path.resolve(pkgBuildPath, relativeToSrcPath);
}
function buildPackage(p) {
const srcDir = path.resolve(p, SRC_DIR);
const pattern = path.resolve(srcDir, '**/*');
const files = glob.sync(pattern, {nodir: true});
process.stdout.write(
fixedWidth(`${path.basename(p)}\n`)
);
process.stdout.write(fixedWidth(`${path.basename(p)}\n`));
files.forEach(file => buildFile(file, true));
process.stdout.write(`[ ${chalk.green('OK')} ]\n`);
}
function buildFile(file, silent) {
const packageName = path.relative(PACKAGES_DIR, file).split(path.sep)[0];
const packageSrcPath = path.resolve(PACKAGES_DIR, packageName, 'src');
const packageBuildPath = path.resolve(PACKAGES_DIR, packageName, 'build');
const relativeToSrcPath = path.relative(packageSrcPath, file);
const destPath = path.resolve(packageBuildPath, relativeToSrcPath);
buildFileFor(file, silent, 'node');
spawnSync('mkdir', ['-p', path.dirname(destPath)]);
const pkgJsonPath = path.resolve(
PACKAGES_DIR,
getPackageName(file),
'package.json'
);
const {browser} = require(pkgJsonPath);
if (browser) {
if (browser.indexOf(BUILD_ES5_DIR) !== 0) {
throw new Error(
`browser field for ${pkgJsonPath} should start with "${BUILD_ES5_DIR}"`
);
}
buildFileFor(file, silent, 'es5');
}
}
function buildFileFor(file, silent, env) {
const buildDir = env === 'es5' ? BUILD_ES5_DIR : BUILD_DIR;
const destPath = getBuildPath(file, buildDir);
const babelOptions = env === 'es5' ? babelEs5Options : babelNodeOptions;
mkdirp.sync(path.dirname(destPath));
if (micromatch.isMatch(file, IGNORE_PATTERN)) {
silent || process.stdout.write(
chalk.dim(' \u2022 ') +
path.relative(PACKAGES_DIR, file) +
' (ignore)\n'
);
silent ||
process.stdout.write(
chalk.dim(' \u2022 ') +
path.relative(PACKAGES_DIR, file) +
' (ignore)\n'
);
} else if (!micromatch.isMatch(file, JS_FILES_PATTERN)) {
fs.createReadStream(file).pipe(fs.createWriteStream(destPath));
silent || process.stdout.write(
chalk.red(' \u2022 ') +
path.relative(PACKAGES_DIR, file) +
chalk.red(' \u21D2 ') +
path.relative(PACKAGES_DIR, destPath) +
' (copy)' +
'\n'
);
silent ||
process.stdout.write(
chalk.red(' \u2022 ') +
path.relative(PACKAGES_DIR, file) +
chalk.red(' \u21D2 ') +
path.relative(PACKAGES_DIR, destPath) +
' (copy)' +
'\n'
);
} else {
const transformed = babel.transformFileSync(file, babelOptions).code;
spawnSync('mkdir', ['-p', path.dirname(destPath)]);
fs.writeFileSync(destPath, transformed);
silent || process.stdout.write(
chalk.green(' \u2022 ') +
path.relative(PACKAGES_DIR, file) +
chalk.green(' \u21D2 ') +
path.relative(PACKAGES_DIR, destPath) +
'\n'
);
silent ||
process.stdout.write(
chalk.green(' \u2022 ') +
path.relative(PACKAGES_DIR, file) +
chalk.green(' \u21D2 ') +
path.relative(PACKAGES_DIR, destPath) +
'\n'
);
}
}

View File

@ -6,8 +6,6 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
/**
* Because we have a build step, sometimes we can test files from both
* `packages/metro-whatever/build/*` and `packages/metro-whatever/src/*`
@ -29,20 +27,22 @@
*/
const createReporter = require('istanbul-api').createReporter;
const coverage = require('../coverage/coverage-final.json');
const istanbulCoverage = require('istanbul-lib-coverage');
const coverage = require('../coverage/coverage-final.json');
const map = istanbulCoverage.createCoverageMap();
const reporter = createReporter();
const mapFileCoverage = fileCoverage => {
fileCoverage.path = fileCoverage.path
.replace(/(.*packages\/.*\/)(build)(\/.*)/, '$1src$3');
fileCoverage.path = fileCoverage.path.replace(
/(.*packages\/.*\/)(build)(\/.*)/,
'$1src$3'
);
return fileCoverage;
};
Object.keys(coverage).forEach(
filename => map.addFileCoverage(mapFileCoverage(coverage[filename]))
Object.keys(coverage).forEach(filename =>
map.addFileCoverage(mapFileCoverage(coverage[filename]))
);
reporter.addAll(['json', 'lcov', 'text']);

View File

@ -5,17 +5,14 @@
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const path = require('path');
const isWindows = process.platform === 'win32';
const runCommands = require('./_runCommands');
const runCommand = require('./_runCommand');
console.log(`Setting up metro-bundler's development environment...`);
const isWindows = process.platform === 'win32';
const lerna = isWindows ? 'lerna.cmd' : 'lerna';
const lernaCmd = path.resolve(
__dirname,
'../node_modules/.bin/' + lerna + ' bootstrap'
);
const lernaCmd = path.resolve(__dirname, '../node_modules/.bin/' + lerna);
const args = process.env.CI ? ['bootstrap', '--concurrency=1'] : ['bootstrap'];
runCommands(lernaCmd, path.resolve(__dirname, '..'));
runCommand(lernaCmd, args, path.resolve(__dirname, '..'));

View File

@ -6,17 +6,15 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
/**
* Watch files for changes and rebuild (copy from 'src/' to `build/`) if changed
*/
const fs = require('fs');
const getPackages = require('./_getPackages');
const execSync = require('child_process').execSync;
const chalk = require('chalk');
const {execSync} = require('child_process');
const path = require('path');
const chalk = require('chalk');
const getPackages = require('./_getPackages');
const BUILD_CMD = `node ${path.resolve(__dirname, './build.js')}`;
@ -37,10 +35,7 @@ getPackages().forEach(p => {
fs.watch(path.resolve(p, 'src'), {recursive: true}, (event, filename) => {
const filePath = path.resolve(srcDir, filename);
if (
(event === 'change' || event === 'rename') &&
exists(filePath)
) {
if ((event === 'change' || event === 'rename') && exists(filePath)) {
console.log(chalk.green('->'), `${event}: ${filename}`);
rebuild(filePath);
} else {
@ -49,9 +44,9 @@ getPackages().forEach(p => {
fs.unlinkSync(buildFile);
process.stdout.write(
chalk.red(' \u2022 ') +
path.relative(path.resolve(srcDir, '..', '..'), buildFile) +
' (deleted)' +
'\n'
path.relative(path.resolve(srcDir, '..', '..'), buildFile) +
' (deleted)' +
'\n'
);
} catch (e) {}
}