typescript-assistant
Version:
Combines and integrates professional Typescript tools into your project
128 lines (125 loc) • 4.78 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findChangedFiles = findChangedFiles;
exports.updateDependencies = updateDependencies;
exports.pnpmLockFileChanged = pnpmLockFileChanged;
exports.npmLockFileChanged = npmLockFileChanged;
exports.filterTsFiles = filterTsFiles;
const child_process_1 = require("child_process");
const fs_1 = require("fs");
function findChangedFiles(refA, refB) {
if (refA === undefined) {
refA = "HEAD";
}
if (refB === undefined) {
refB = "";
}
let output = (0, child_process_1.execSync)(`git diff --name-only --diff-filter=ACMR ${refA} ${refB}`, { encoding: "utf-8" });
return output.split("\n").filter((fileName) => fileName.length > 0);
}
function updateDependencies(logger, previousHead) {
return __awaiter(this, void 0, void 0, function* () {
let pnpmInstalled = false;
try {
const child_process = yield Promise.resolve().then(() => require("child_process"));
child_process.execSync("pnpm --version", {
encoding: "utf-8",
stdio: "ignore",
});
pnpmInstalled = true;
}
catch (_a) {
// pnpm is not installed.
}
if (pnpmInstalled && pnpmLockFileChanged(previousHead, "HEAD")) {
logger.log("hooks", "Running pnpm install...");
yield pnpmInstall();
}
else {
if (npmLockFileChanged(previousHead, "HEAD")) {
logger.log("hooks", "Running npm install...");
npmInstall();
}
else {
logger.log("hooks", "No need to run (p)npm install");
}
}
});
}
function pnpmInstall() {
return __awaiter(this, void 0, void 0, function* () {
const child_process = yield Promise.resolve().then(() => require("child_process"));
try {
child_process.execSync("pnpm install", {
encoding: "utf-8",
stdio: [0, 1, 2],
});
}
catch (installError) {
// eslint-disable-next-line no-console
console.error("pnpm install failed");
// eslint-disable-next-line no-console
console.log("Retrying with npm install");
npmInstall();
}
});
}
function npmInstall() {
let scriptPath = `${process.cwd()}/build/npm-install.js`;
let currentDir = process.cwd().replace(/\\/g, "\\\\");
(0, fs_1.writeFileSync)(scriptPath, `
var fs = require('fs');
var tryNpmInstall = function() {
if (fs.existsSync('.git/index.lock')) {
return false;
}
console.log('Updating dependencies, please wait...');
const child_process = require('child_process');
try {
try {
child_process.execSync('npm install --no-package-lock', { encoding: 'UTF-8', stdio: [0, 1, 2] });
} catch (installError) {
console.error('Retrying npm install');
child_process.execSync('npm install --no-package-lock', { encoding: 'UTF-8', stdio: [0, 1, 2] });
}
} catch (secondError) {
console.error('npm install failed');
console.log('Press enter to continue');
process.stdin.once('data', function(){
process.exit(1);
});
}
process.exit(0);
}
fs.watch('.git', {persistent: true}, tryNpmInstall);
if (!tryNpmInstall()) {
console.log('waiting for git before running npm install');
}
`);
let install = (0, child_process_1.spawn)("node", ["./build/npm-install.js"], {
stdio: "ignore",
shell: true,
detached: true,
cwd: currentDir,
});
install.unref();
}
function pnpmLockFileChanged(refA, refB) {
return (findChangedFiles(refA, refB).filter((f) => f.indexOf("pnpm-lock.yaml") !== -1).length >= 1);
}
function npmLockFileChanged(refA, refB) {
return (findChangedFiles(refA, refB).filter((f) => f.indexOf("package-lock.json") !== -1).length >= 1);
}
function filterTsFiles(files) {
return files.filter((f) => f.slice(-3) === ".ts" && f.slice(-5) !== ".d.ts");
}
//# sourceMappingURL=helpers.js.map