2019-05-14 22:25:46 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
const resolve = require("path").resolve;
|
|
|
|
const spawn = require("child_process").spawn;
|
|
|
|
|
2019-08-25 06:39:20 +00:00
|
|
|
const { dirnames } = require("./local");
|
2019-08-25 20:20:34 +00:00
|
|
|
const { loadPackage, savePackage } = require("./local");
|
2019-08-25 06:39:20 +00:00
|
|
|
const { loadJson, saveJson } = require("./utils");
|
2019-05-14 22:25:46 +00:00
|
|
|
|
|
|
|
function run(progname, args, ignoreErrorStream) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const proc = spawn(progname, args);
|
|
|
|
|
|
|
|
let stdout = Buffer.from([]);
|
|
|
|
let stderr = Buffer.from([]);
|
|
|
|
|
|
|
|
proc.stdout.on("data", (data) => {
|
|
|
|
stdout = Buffer.concat([ stdout, data ]);
|
|
|
|
});
|
|
|
|
|
|
|
|
proc.stderr.on("data", (data) => {
|
|
|
|
stderr = Buffer.concat([ stdout, data ]);
|
|
|
|
});
|
|
|
|
|
|
|
|
proc.on("error", (error) => {
|
|
|
|
console.log("ERROR");
|
|
|
|
console.log(stderr.toString());
|
|
|
|
error.stderr = stderr.toString();
|
|
|
|
error.stdout = stdout.toString();
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
proc.on("close", (code) => {
|
|
|
|
if ((stderr.length && !ignoreErrorStream) || code !== 0) {
|
|
|
|
console.log("ERROR");
|
|
|
|
console.log(stderr.toString());
|
|
|
|
|
2019-11-24 15:02:22 +00:00
|
|
|
let error = new Error(`stderr not empty: ${ progname } ${ JSON.stringify(args) }`);
|
2019-05-14 22:25:46 +00:00
|
|
|
error.stderr = stderr.toString();
|
|
|
|
error.stdout = stdout.toString();
|
|
|
|
error.statusCode = code;
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve(stdout.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-25 06:39:20 +00:00
|
|
|
function setupConfig(outDir, moduleType, targetType) {
|
2019-08-25 20:20:34 +00:00
|
|
|
|
2019-08-25 06:39:20 +00:00
|
|
|
// Configure the tsconfit.package.json...
|
|
|
|
const path = resolve(__dirname, "../tsconfig.package.json");
|
|
|
|
const content = loadJson(path);
|
|
|
|
content.compilerOptions.module = moduleType;
|
|
|
|
content.compilerOptions.target = targetType;
|
|
|
|
saveJson(path, content);
|
|
|
|
|
2020-04-18 09:23:36 +00:00
|
|
|
// Configure the browser field for every pacakge, copying the
|
|
|
|
// browser.umd filed for UMD and browser.esm for ESM
|
2019-08-25 06:39:20 +00:00
|
|
|
dirnames.forEach((dirname) => {
|
2019-08-25 20:20:34 +00:00
|
|
|
let info = loadPackage(dirname);
|
|
|
|
|
|
|
|
if (info._ethers_nobuild) { return; }
|
|
|
|
|
2020-04-18 09:23:36 +00:00
|
|
|
if (targetType === "es2015") {
|
|
|
|
if (info["browser.esm"]) {
|
|
|
|
info.browser = info["browser.esm"];
|
2019-08-25 20:20:34 +00:00
|
|
|
}
|
2020-04-18 09:23:36 +00:00
|
|
|
} else if (targetType === "es5") {
|
|
|
|
if (info["browser.umd"]) {
|
|
|
|
info.browser = info["browser.umd"];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error("unsupported target");
|
|
|
|
}
|
2019-08-25 20:20:34 +00:00
|
|
|
savePackage(dirname, info);
|
2019-08-25 06:39:20 +00:00
|
|
|
|
|
|
|
let path = resolve(__dirname, "../packages", dirname, "tsconfig.json");
|
|
|
|
let content = loadJson(path);
|
|
|
|
content.compilerOptions.outDir = outDir;
|
|
|
|
saveJson(path, content);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupBuild(buildModule) {
|
|
|
|
if (buildModule) {
|
|
|
|
setupConfig("./lib.esm/", "es2015", "es2015");
|
|
|
|
} else {
|
|
|
|
setupConfig("./lib/", "commonjs", "es5");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function runBuild(buildModule) {
|
|
|
|
setupBuild(buildModule);
|
|
|
|
|
|
|
|
// Compile
|
|
|
|
return run("npx", [ "tsc", "--build", resolve(__dirname, "../tsconfig.project.json"), "--force" ]);
|
2019-05-14 22:25:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function runDist() {
|
2019-11-24 15:02:22 +00:00
|
|
|
return run("npm", [ "run", "_dist" ], true);
|
2019-05-14 22:25:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
run: run,
|
|
|
|
runDist: runDist,
|
2019-08-25 06:39:20 +00:00
|
|
|
runBuild: runBuild,
|
|
|
|
setupBuild: setupBuild
|
2019-05-14 22:25:46 +00:00
|
|
|
};
|