Convert build.sh into build.js using shelljs

For better cross platform compatibility.
master
Simon Lydell 2017-06-28 22:34:59 +02:00
parent ba51d2dfa8
commit 20a0723fc1
4 changed files with 142 additions and 76 deletions

View File

@ -50,6 +50,7 @@
"rollup-plugin-node-globals": "1.1.0",
"rollup-plugin-node-resolve": "2.0.0",
"rollup-plugin-replace": "1.1.1",
"shelljs": "0.7.8",
"sw-toolbox": "3.6.0",
"uglify-es": "3.0.15",
"webpack": "2.6.1"
@ -58,7 +59,7 @@
"test": "jest",
"test-integration": "jest tests_integration",
"lint": "EFF_NO_LINK_RULES=true eslint . --format 'node_modules/eslint-friendly-formatter'",
"build": "./scripts/build/build.sh"
"build": "./scripts/build/build.js"
},
"jest": {
"setupFiles": [

126
scripts/build/build.js Executable file
View File

@ -0,0 +1,126 @@
#!/usr/bin/env node
"use strict";
const path = require("path");
const shell = require("shelljs");
const pkg = require("../../package.json");
const rootDir = path.join(__dirname, "..", "..");
const parsers = [
"babylon",
"flow",
"typescript",
"graphql",
"postcss",
"parse5",
"json"
];
process.env.PATH += path.delimiter + path.join(rootDir, "node_modules", ".bin");
function pipe(string) {
return new shell.ShellString(string);
}
shell.set("-e");
shell.cd(rootDir);
shell.rm("-Rf", "dist/", "docs/lib/");
shell.mkdir("-p", "docs/lib/");
// --- Lib ---
shell.echo("Bundling lib index...");
shell.exec("rollup -c scripts/build/rollup.index.config.js");
shell.echo("Bundling lib bin...");
shell.exec("rollup -c scripts/build/rollup.bin.config.js");
shell.chmod("+x", "./dist/bin/prettier.js");
for (const parser of parsers) {
if (parser === "postcss") {
continue;
}
shell.echo(`Bundling lib ${parser}...`);
shell.exec(
`rollup -c scripts/build/rollup.parser.config.js --environment parser:${parser}`
);
}
shell.echo("Bundling lib postcss...");
// PostCSS has dependency cycles and won't work correctly with rollup :(
shell.exec(
"webpack --hide-modules src/parser-postcss.js dist/parser-postcss.js"
);
// Prepend module.exports =
const content = shell.cat("dist/parser-postcss.js").stdout;
pipe(`module.exports = ${content}`).to("dist/parser-postcss.js");
shell.echo();
// --- Docs ---
shell.echo("Bundling docs index...");
shell.cp("dist/index.js", "docs/lib/index.js");
shell.exec(
"node_modules/babel-cli/bin/babel.js dist/index.js --out-file docs/lib/index.js --presets=es2015"
);
shell.echo("Bundling docs babylon...");
shell.exec(
"rollup -c scripts/build/rollup.docs.config.js --environment filepath:parser-babylon.js"
);
shell.exec(
"node_modules/babel-cli/bin/babel.js docs/lib/parser-babylon.js --out-file docs/lib/parser-babylon.js --presets=es2015"
);
for (const parser of parsers) {
if (parser === "babylon") {
continue;
}
shell.echo(`Bundling docs ${parser}...`);
shell.exec(
`rollup -c scripts/build/rollup.docs.config.js --environment filepath:parser-${parser}.js`
);
}
shell.echo();
// --- Misc ---
shell.echo("Remove eval");
shell.sed(
"-i",
/eval\("require"\)/,
"require",
"dist/index.js",
"dist/bin/prettier.js"
);
shell.echo("Create prettier-version.js");
pipe(`prettierVersion = "${pkg.version}";`).to("docs/lib/prettier-version.js");
shell.echo("Copy sw-toolbox.js to docs");
shell.cp("node_modules/sw-toolbox/sw-toolbox.js", "docs/lib/sw-toolbox.js");
shell.cp(
"node_modules/sw-toolbox/companion.js",
"docs/lib/sw-toolbox-companion.js"
);
shell.echo("Copy package.json");
const pkgWithoutDependencies = Object.assign({}, pkg);
delete pkgWithoutDependencies.dependencies;
pipe(JSON.stringify(pkgWithoutDependencies, null, 2)).to("dist/package.json");
shell.echo("Copy README.md");
shell.cp("README.md", "dist/README.md");
shell.echo("Done!");
shell.echo();
shell.echo("How to test against dist:");
shell.echo(" 1) yarn test --prod");
shell.echo();
shell.echo("How to publish:");
shell.echo(" 1) IMPORTANT!!! Go to dist/");
shell.echo(" 2) npm publish");

View File

@ -1,75 +0,0 @@
#!/bin/bash
set -e
cd "$(dirname "$0")";
cd ../..;
rm -Rf dist/ docs/lib/
mkdir -p docs/lib/
## --- Lib ---
echo 'Bundling lib index...';
node_modules/.bin/rollup -c scripts/build/rollup.index.config.js
echo 'Bundling lib bin...';
node_modules/.bin/rollup -c scripts/build/rollup.bin.config.js
chmod +x ./dist/bin/prettier.js
for parser in babylon flow graphql typescript parse5 json; do
echo "Bundling lib $parser...";
node_modules/.bin/rollup -c scripts/build/rollup.parser.config.js --environment parser:$parser
done
echo 'Bundling lib postcss...';
# PostCSS has dependency cycles and won't work correctly with rollup :(
./node_modules/.bin/webpack --hide-modules src/parser-postcss.js dist/parser-postcss.js
# Prepend module.exports =
echo "module.exports =" > dist/parser-postcss.js.tmp
cat dist/parser-postcss.js >> dist/parser-postcss.js.tmp
mv dist/parser-postcss.js.tmp dist/parser-postcss.js
echo;
## --- Docs ---
echo 'Bundling docs index...';
cp dist/index.js docs/lib/index.js
node_modules/babel-cli/bin/babel.js dist/index.js --out-file docs/lib/index.js --presets=es2015
echo 'Bundling docs babylon...';
node_modules/.bin/rollup -c scripts/build/rollup.docs.config.js --environment filepath:parser-babylon.js
node_modules/babel-cli/bin/babel.js docs/lib/parser-babylon.js --out-file docs/lib/parser-babylon.js --presets=es2015
for parser in flow graphql typescript postcss parse5 json; do
echo "Bundling docs $parser...";
node_modules/.bin/rollup -c scripts/build/rollup.docs.config.js --environment filepath:parser-$parser.js
done
echo;
## --- Misc ---
echo 'Remove eval'
perl -pi -e 's/eval\("require"\)/require/g' dist/index.js dist/bin/prettier.js
echo 'Create prettier-version.js'
node -p '`prettierVersion = "${require(".").version}";`' > docs/lib/prettier-version.js
echo 'Copy sw-toolbox.js to docs'
cp node_modules/sw-toolbox/sw-toolbox.js docs/lib/sw-toolbox.js
cp node_modules/sw-toolbox/companion.js docs/lib/sw-toolbox-companion.js
echo 'Copy package.json'
node -p "pkg = require('./package.json'), delete pkg.dependencies, JSON.stringify(pkg, null, 2)" > dist/package.json
echo 'Copy README.md'
cp README.md dist/README.md
echo 'Done!'
echo;
echo 'How to test against dist:'
echo ' 1) yarn test --prod'
echo;
echo 'How to publish:'
echo ' 1) IMPORTANT!!! Go to dist/'
echo ' 2) npm publish'

View File

@ -2997,6 +2997,12 @@ readdirp@^2.0.0:
readable-stream "^2.0.2"
set-immediate-shim "^1.0.1"
rechoir@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/rechoir/-/rechoir-0.6.2.tgz#85204b54dba82d5742e28c96756ef43af50e3384"
dependencies:
resolve "^1.1.6"
regenerate@^1.2.1:
version "1.3.2"
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.3.2.tgz#d1941c67bad437e1be76433add5b385f95b19260"
@ -3278,6 +3284,14 @@ shebang-regex@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3"
shelljs@0.7.8:
version "0.7.8"
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.7.8.tgz#decbcf874b0d1e5fb72e14b164a9683048e9acb3"
dependencies:
glob "^7.0.0"
interpret "^1.0.0"
rechoir "^0.6.2"
shellwords@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.0.tgz#66afd47b6a12932d9071cbfd98a52e785cd0ba14"