Files
react-native-community-cli/scripts/build.js
T
Michał Pierzchała 61bc2882f5 chore: setup monorepo with Lerna (#109)
* chore: setup monorepo with lerna

* chore: move flow to root

* fix: lint

* chore: add watch command
2019-01-23 18:04:01 +01:00

133 lines
3.9 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* script to build (transpile) files.
* By default it transpiles js files for all packages and writes them
* into `build/` directory.
* Non-js files not matching IGNORE_PATTERN will be copied without transpiling.
*
* Example:
* node ./scripts/build.js
* node ./scripts/build.js /users/123/jest/packages/jest-111/src/111.js
*
* NOTE: this script is node@4 compatible
*/
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 micromatch = require('micromatch');
const prettier = require('prettier');
const stringLength = require('string-length');
const { PACKAGES_DIR, getPackages } = require('./helpers');
const OK = chalk.reset.inverse.bold.green(' DONE ');
const SRC_DIR = 'src';
const BUILD_DIR = 'build';
const JS_FILES_PATTERN = '**/*.js';
const IGNORE_PATTERN = '**/__{tests,mocks,fixtures}__/**';
const transformOptions = require('../babel.config.js');
const prettierConfig = prettier.resolveConfig.sync(__filename);
prettierConfig.trailingComma = 'none';
prettierConfig.parser = 'babel';
const adjustToTerminalWidth = str => {
const columns = process.stdout.columns || 80;
const WIDTH = columns - stringLength(OK) + 1;
const strs = str.match(new RegExp(`(.{1,${WIDTH}})`, 'g'));
let lastString = strs[strs.length - 1];
if (lastString.length < WIDTH) {
lastString += Array(WIDTH - lastString.length).join(chalk.dim('.'));
}
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 buildNodePackage(p) {
const srcDir = path.resolve(p, SRC_DIR);
const pattern = path.resolve(srcDir, '**/*');
const files = glob.sync(pattern, {
nodir: true,
});
process.stdout.write(adjustToTerminalWidth(`${path.basename(p)}\n`));
files.forEach(file => buildFile(file, true));
process.stdout.write(`${OK}\n`);
}
function buildFile(file, silent) {
const destPath = getBuildPath(file, BUILD_DIR);
if (micromatch.isMatch(file, IGNORE_PATTERN)) {
silent ||
process.stdout.write(
`${chalk.dim(' \u2022 ') +
path.relative(PACKAGES_DIR, file)} (ignore)\n`
);
return;
}
mkdirp.sync(path.dirname(destPath), '777');
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`
);
} else {
const options = Object.assign({}, transformOptions);
const transformed = babel.transformFileSync(file, options).code;
const prettyCode = prettier.format(transformed, prettierConfig);
fs.writeFileSync(destPath, prettyCode);
silent ||
process.stdout.write(
`${chalk.green(' \u2022 ') +
path.relative(PACKAGES_DIR, file) +
chalk.green(' \u21D2 ') +
path.relative(PACKAGES_DIR, destPath)}\n`
);
}
}
const files = process.argv.slice(2);
if (files.length) {
files.forEach(buildFile);
} else {
const packages = getPackages();
process.stdout.write(chalk.inverse(' Building packages \n'));
packages.forEach(buildNodePackage);
process.stdout.write('\n');
}