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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

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 fs = require("fs");
2020-06-26 20:30:21 +08:00
const chalk = require("chalk");
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
.then((result) => {
process.stdout.write(`${OK}\n`);
return result;
})
.catch((err) => {
process.stdout.write(`${FAIL}\n`);
throw err;
});
}
function runYarn(script) {
2021-01-26 09:46:14 +08:00
return execa("yarn", [
"--silent",
...(Array.isArray(script) ? script : [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,
waitForEnter,
};