mirror of https://github.com/status-im/metro.git
Modernize scripts.
This commit is contained in:
parent
bc03c8d231
commit
a7983ebcc8
|
@ -33,7 +33,7 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build-clean": "rm -rf ./packages/*/build",
|
"build-clean": "rm -rf ./packages/*/build",
|
||||||
"build": "node ./scripts/build.js",
|
"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",
|
"jest-coverage": "yarn run jest -- --coverage",
|
||||||
"lint": "eslint . --cache",
|
"lint": "eslint . --cache",
|
||||||
"postinstall": "node ./scripts/postinstall.js && node ./scripts/build.js",
|
"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-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",
|
"test": "yarn run typecheck && yarn run lint && yarn run build && yarn run jest && yarn run test-examples",
|
||||||
"typecheck": "flow check",
|
"typecheck": "flow check",
|
||||||
"watch": "yarn run build; node ./scripts/watch.js"
|
"watch": "yarn run build --silent && node ./scripts/watch.js"
|
||||||
},
|
},
|
||||||
"jest": {
|
"jest": {
|
||||||
"automock": true,
|
"automock": true,
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
* LICENSE file in the root directory of this source tree. An additional grant
|
* 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.
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
*/
|
*/
|
||||||
'use strict';
|
|
||||||
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
@ -14,7 +13,8 @@ const PACKAGES_DIR = path.resolve(__dirname, '../packages');
|
||||||
|
|
||||||
// Get absolute paths of all directories under packages/*
|
// Get absolute paths of all directories under packages/*
|
||||||
module.exports = function getPackages() {
|
module.exports = function getPackages() {
|
||||||
return fs.readdirSync(PACKAGES_DIR)
|
return fs
|
||||||
|
.readdirSync(PACKAGES_DIR)
|
||||||
.map(file => path.resolve(PACKAGES_DIR, file))
|
.map(file => path.resolve(PACKAGES_DIR, file))
|
||||||
.filter(f => fs.lstatSync(path.resolve(f)).isDirectory());
|
.filter(f => fs.lstatSync(path.resolve(f)).isDirectory());
|
||||||
};
|
};
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
};
|
|
@ -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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
120
scripts/build.js
120
scripts/build.js
|
@ -6,8 +6,6 @@
|
||||||
* of patent rights can be found in the PATENTS file in the same directory.
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* script to build (transpile) files.
|
* script to build (transpile) files.
|
||||||
* By default it transpiles all files for all packages and writes them
|
* 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
|
* 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 babel = require('babel-core');
|
||||||
const chalk = require('chalk');
|
const chalk = require('chalk');
|
||||||
const fs = require('fs');
|
|
||||||
const getPackages = require('./_getPackages');
|
|
||||||
const glob = require('glob');
|
|
||||||
const micromatch = require('micromatch');
|
const micromatch = require('micromatch');
|
||||||
const path = require('path');
|
|
||||||
const spawnSync = require('child_process').spawnSync;
|
const getPackages = require('./_getPackages');
|
||||||
|
|
||||||
const SRC_DIR = 'src';
|
const SRC_DIR = 'src';
|
||||||
|
const BUILD_DIR = 'build';
|
||||||
|
const BUILD_ES5_DIR = 'build-es5';
|
||||||
const JS_FILES_PATTERN = '**/*.js';
|
const JS_FILES_PATTERN = '**/*.js';
|
||||||
const IGNORE_PATTERN = '**/__tests__/**';
|
const IGNORE_PATTERN = '**/__tests__/**';
|
||||||
const PACKAGES_DIR = path.resolve(__dirname, '../packages');
|
const PACKAGES_DIR = path.resolve(__dirname, '../packages');
|
||||||
|
|
||||||
const babelOptions = JSON.parse(fs.readFileSync(
|
const babelNodeOptions = JSON.parse(
|
||||||
path.resolve(__dirname, '..', '.babelrc'),
|
fs.readFileSync(path.resolve(__dirname, '..', '.babelrc'), 'utf8')
|
||||||
'utf8'
|
);
|
||||||
));
|
babelNodeOptions.babelrc = false;
|
||||||
babelOptions.babelrc = false;
|
const babelEs5Options = Object.assign(
|
||||||
|
{},
|
||||||
|
babelNodeOptions,
|
||||||
|
{presets: 'env'},
|
||||||
|
{plugins: [...babelNodeOptions.plugins, 'transform-runtime']}
|
||||||
|
);
|
||||||
|
|
||||||
const fixedWidth = str => {
|
const fixedWidth = str => {
|
||||||
const WIDTH = 80;
|
const WIDTH = 80;
|
||||||
|
@ -49,54 +56,83 @@ const fixedWidth = str => {
|
||||||
return strs.slice(0, -1).concat(lastString).join('\n');
|
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) {
|
function buildPackage(p) {
|
||||||
const srcDir = path.resolve(p, SRC_DIR);
|
const srcDir = path.resolve(p, SRC_DIR);
|
||||||
const pattern = path.resolve(srcDir, '**/*');
|
const pattern = path.resolve(srcDir, '**/*');
|
||||||
const files = glob.sync(pattern, {nodir: true});
|
const files = glob.sync(pattern, {nodir: true});
|
||||||
|
|
||||||
process.stdout.write(
|
process.stdout.write(fixedWidth(`${path.basename(p)}\n`));
|
||||||
fixedWidth(`${path.basename(p)}\n`)
|
|
||||||
);
|
|
||||||
|
|
||||||
files.forEach(file => buildFile(file, true));
|
files.forEach(file => buildFile(file, true));
|
||||||
process.stdout.write(`[ ${chalk.green('OK')} ]\n`);
|
process.stdout.write(`[ ${chalk.green('OK')} ]\n`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildFile(file, silent) {
|
function buildFile(file, silent) {
|
||||||
const packageName = path.relative(PACKAGES_DIR, file).split(path.sep)[0];
|
buildFileFor(file, silent, 'node');
|
||||||
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);
|
|
||||||
|
|
||||||
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)) {
|
if (micromatch.isMatch(file, IGNORE_PATTERN)) {
|
||||||
silent || process.stdout.write(
|
silent ||
|
||||||
chalk.dim(' \u2022 ') +
|
process.stdout.write(
|
||||||
path.relative(PACKAGES_DIR, file) +
|
chalk.dim(' \u2022 ') +
|
||||||
' (ignore)\n'
|
path.relative(PACKAGES_DIR, file) +
|
||||||
);
|
' (ignore)\n'
|
||||||
|
);
|
||||||
} else if (!micromatch.isMatch(file, JS_FILES_PATTERN)) {
|
} else if (!micromatch.isMatch(file, JS_FILES_PATTERN)) {
|
||||||
fs.createReadStream(file).pipe(fs.createWriteStream(destPath));
|
fs.createReadStream(file).pipe(fs.createWriteStream(destPath));
|
||||||
silent || process.stdout.write(
|
silent ||
|
||||||
chalk.red(' \u2022 ') +
|
process.stdout.write(
|
||||||
path.relative(PACKAGES_DIR, file) +
|
chalk.red(' \u2022 ') +
|
||||||
chalk.red(' \u21D2 ') +
|
path.relative(PACKAGES_DIR, file) +
|
||||||
path.relative(PACKAGES_DIR, destPath) +
|
chalk.red(' \u21D2 ') +
|
||||||
' (copy)' +
|
path.relative(PACKAGES_DIR, destPath) +
|
||||||
'\n'
|
' (copy)' +
|
||||||
);
|
'\n'
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
const transformed = babel.transformFileSync(file, babelOptions).code;
|
const transformed = babel.transformFileSync(file, babelOptions).code;
|
||||||
spawnSync('mkdir', ['-p', path.dirname(destPath)]);
|
|
||||||
fs.writeFileSync(destPath, transformed);
|
fs.writeFileSync(destPath, transformed);
|
||||||
silent || process.stdout.write(
|
silent ||
|
||||||
chalk.green(' \u2022 ') +
|
process.stdout.write(
|
||||||
path.relative(PACKAGES_DIR, file) +
|
chalk.green(' \u2022 ') +
|
||||||
chalk.green(' \u21D2 ') +
|
path.relative(PACKAGES_DIR, file) +
|
||||||
path.relative(PACKAGES_DIR, destPath) +
|
chalk.green(' \u21D2 ') +
|
||||||
'\n'
|
path.relative(PACKAGES_DIR, destPath) +
|
||||||
);
|
'\n'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,6 @@
|
||||||
* of patent rights can be found in the PATENTS file in the same directory.
|
* 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
|
* Because we have a build step, sometimes we can test files from both
|
||||||
* `packages/metro-whatever/build/*` and `packages/metro-whatever/src/*`
|
* `packages/metro-whatever/build/*` and `packages/metro-whatever/src/*`
|
||||||
|
@ -29,20 +27,22 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const createReporter = require('istanbul-api').createReporter;
|
const createReporter = require('istanbul-api').createReporter;
|
||||||
const coverage = require('../coverage/coverage-final.json');
|
|
||||||
const istanbulCoverage = require('istanbul-lib-coverage');
|
const istanbulCoverage = require('istanbul-lib-coverage');
|
||||||
|
const coverage = require('../coverage/coverage-final.json');
|
||||||
|
|
||||||
const map = istanbulCoverage.createCoverageMap();
|
const map = istanbulCoverage.createCoverageMap();
|
||||||
const reporter = createReporter();
|
const reporter = createReporter();
|
||||||
|
|
||||||
const mapFileCoverage = fileCoverage => {
|
const mapFileCoverage = fileCoverage => {
|
||||||
fileCoverage.path = fileCoverage.path
|
fileCoverage.path = fileCoverage.path.replace(
|
||||||
.replace(/(.*packages\/.*\/)(build)(\/.*)/, '$1src$3');
|
/(.*packages\/.*\/)(build)(\/.*)/,
|
||||||
|
'$1src$3'
|
||||||
|
);
|
||||||
return fileCoverage;
|
return fileCoverage;
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.keys(coverage).forEach(
|
Object.keys(coverage).forEach(filename =>
|
||||||
filename => map.addFileCoverage(mapFileCoverage(coverage[filename]))
|
map.addFileCoverage(mapFileCoverage(coverage[filename]))
|
||||||
);
|
);
|
||||||
|
|
||||||
reporter.addAll(['json', 'lcov', 'text']);
|
reporter.addAll(['json', 'lcov', 'text']);
|
||||||
|
|
|
@ -5,17 +5,14 @@
|
||||||
* LICENSE file in the root directory of this source tree. An additional grant
|
* 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.
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
*/
|
*/
|
||||||
'use strict';
|
|
||||||
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const isWindows = process.platform === 'win32';
|
const runCommand = require('./_runCommand');
|
||||||
const runCommands = require('./_runCommands');
|
|
||||||
|
|
||||||
console.log(`Setting up metro-bundler's development environment...`);
|
console.log(`Setting up metro-bundler's development environment...`);
|
||||||
|
const isWindows = process.platform === 'win32';
|
||||||
const lerna = isWindows ? 'lerna.cmd' : 'lerna';
|
const lerna = isWindows ? 'lerna.cmd' : 'lerna';
|
||||||
const lernaCmd = path.resolve(
|
const lernaCmd = path.resolve(__dirname, '../node_modules/.bin/' + lerna);
|
||||||
__dirname,
|
const args = process.env.CI ? ['bootstrap', '--concurrency=1'] : ['bootstrap'];
|
||||||
'../node_modules/.bin/' + lerna + ' bootstrap'
|
|
||||||
);
|
|
||||||
|
|
||||||
runCommands(lernaCmd, path.resolve(__dirname, '..'));
|
runCommand(lernaCmd, args, path.resolve(__dirname, '..'));
|
||||||
|
|
|
@ -6,17 +6,15 @@
|
||||||
* of patent rights can be found in the PATENTS file in the same directory.
|
* 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
|
* Watch files for changes and rebuild (copy from 'src/' to `build/`) if changed
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const getPackages = require('./_getPackages');
|
const {execSync} = require('child_process');
|
||||||
const execSync = require('child_process').execSync;
|
|
||||||
const chalk = require('chalk');
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const chalk = require('chalk');
|
||||||
|
const getPackages = require('./_getPackages');
|
||||||
|
|
||||||
const BUILD_CMD = `node ${path.resolve(__dirname, './build.js')}`;
|
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) => {
|
fs.watch(path.resolve(p, 'src'), {recursive: true}, (event, filename) => {
|
||||||
const filePath = path.resolve(srcDir, filename);
|
const filePath = path.resolve(srcDir, filename);
|
||||||
|
|
||||||
if (
|
if ((event === 'change' || event === 'rename') && exists(filePath)) {
|
||||||
(event === 'change' || event === 'rename') &&
|
|
||||||
exists(filePath)
|
|
||||||
) {
|
|
||||||
console.log(chalk.green('->'), `${event}: ${filename}`);
|
console.log(chalk.green('->'), `${event}: ${filename}`);
|
||||||
rebuild(filePath);
|
rebuild(filePath);
|
||||||
} else {
|
} else {
|
||||||
|
@ -49,9 +44,9 @@ getPackages().forEach(p => {
|
||||||
fs.unlinkSync(buildFile);
|
fs.unlinkSync(buildFile);
|
||||||
process.stdout.write(
|
process.stdout.write(
|
||||||
chalk.red(' \u2022 ') +
|
chalk.red(' \u2022 ') +
|
||||||
path.relative(path.resolve(srcDir, '..', '..'), buildFile) +
|
path.relative(path.resolve(srcDir, '..', '..'), buildFile) +
|
||||||
' (deleted)' +
|
' (deleted)' +
|
||||||
'\n'
|
'\n'
|
||||||
);
|
);
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue