Files
test-repo/scripts/release/utils.js
T

85 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-06-05 16:37:52 -03:00
"use strict";
require("readline").emitKeypressEvents(process.stdin);
const chalk = require("chalk");
const fs = require("fs");
2018-06-05 19:10:27 -03:00
const execa = require("execa");
2018-06-05 16:37:52 -03:00
const stringWidth = require("string-width");
const OK = chalk.bgGreen.black(" DONE ");
const FAIL = chalk.bgRed.black(" FAIL ");
function fitTerminal(input) {
const columns = Math.min(process.stdout.columns, 80);
const WIDTH = columns - stringWidth(OK) + 1;
if (input.length < WIDTH) {
2020-01-19 06:48:04 +08:00
input += chalk.dim(".").repeat(WIDTH - input.length - 1);
2018-06-05 16:37:52 -03:00
}
return input;
}
function logPromise(name, promise) {
process.stdout.write(fitTerminal(name));
return promise
2020-03-21 22:07:52 +02:00
.then((result) => {
2018-06-05 16:37:52 -03:00
process.stdout.write(`${OK}\n`);
return result;
})
2020-03-21 22:07:52 +02:00
.catch((err) => {
2018-06-05 16:37:52 -03:00
process.stdout.write(`${FAIL}\n`);
throw err;
});
}
function runYarn(script) {
2018-06-18 15:16:40 -03:00
if (typeof script === "string") {
script = [script];
}
2020-03-21 22:07:52 +02:00
return execa("yarn", ["--silent"].concat(script)).catch((error) => {
2019-11-17 18:36:21 +08:00
throw new Error(`\`yarn ${script}\` failed\n${error.stdout}`);
2018-06-05 16:37:52 -03:00
});
}
function waitForEnter() {
process.stdin.setRawMode(true);
return new Promise((resolve, reject) => {
process.stdin.on("keypress", listener);
process.stdin.resume();
function listener(ch, key) {
if (key.name === "return") {
process.stdin.setRawMode(false);
process.stdin.removeListener("keypress", listener);
process.stdin.pause();
resolve();
} else if (key.ctrl && key.name === "c") {
2019-11-17 18:36:21 +08:00
reject(new Error("Process terminated by the user"));
2018-06-05 16:37:52 -03:00
}
}
});
}
function readJson(filename) {
return JSON.parse(fs.readFileSync(filename, "utf-8"));
}
function writeJson(filename, content) {
fs.writeFileSync(filename, JSON.stringify(content, null, 2) + "\n");
}
function processFile(filename, fn) {
const content = fs.readFileSync(filename, "utf-8");
fs.writeFileSync(filename, fn(content));
}
2018-06-05 16:37:52 -03:00
module.exports = {
runYarn,
logPromise,
processFile,
2018-06-05 16:37:52 -03:00
readJson,
writeJson,
2020-03-21 22:07:52 +02:00
waitForEnter,
2018-06-05 16:37:52 -03:00
};