bundle-checker
Version:
CLI tool to generate stats on the size of files between two git branches
179 lines (178 loc) • 7.96 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
const fs_1 = __importDefault(require("fs"));
const globby_1 = __importDefault(require("globby"));
const ora_1 = __importDefault(require("ora"));
const path = __importStar(require("path"));
const ramda_1 = require("ramda");
const size_limit_1 = __importDefault(require("size-limit"));
const util = __importStar(require("util"));
const utils_1 = require("./utils");
const exec = util.promisify(child_process_1.exec);
const { error } = console;
class BundleChecker {
constructor(params) {
this.workDir = '';
this.originalCwd = '';
this.spinner = ora_1.default(`Bundle checker`);
this.generateWorkDirName = () => `/tmp/bundler-checker/${new Date().getTime()}`;
this.safeDeleteFolder = (dir) => __awaiter(this, void 0, void 0, function* () { return exec(`rm -Rf ${dir === '/' ? '' : dir}`); });
this.inputParams = params;
this.originalCwd = process.cwd();
}
compare() {
return __awaiter(this, void 0, void 0, function* () {
let report = {
currentBranchReport: {},
targetBranchReport: {}
};
const { currentBranch, targetBranch } = this.inputParams;
try {
yield this.init();
// --- TARGET BRANCH
this.spinner.indent = 4;
this.spinner.info(`Branch: ${targetBranch}`);
yield this.buildBranch(targetBranch);
const targetBranchReport = utils_1.normalizeSlugsInFileNames(yield this.getSingleBranchReport());
this.spinner.indent = 0;
// --- CURRENT BRANCH
this.spinner.indent = 4;
this.spinner.info(`Branch: ${currentBranch}`);
yield this.cleanDist();
yield this.buildBranch(currentBranch);
const currentBranchReport = utils_1.normalizeSlugsInFileNames(yield this.getSingleBranchReport());
report = {
currentBranchReport,
targetBranchReport
};
}
catch (e) {
this.spinner.fail(e);
error(e);
}
yield this.destroy();
return report;
});
}
init() {
return __awaiter(this, void 0, void 0, function* () {
this.workDir = yield fs_1.default.realpathSync(process.cwd());
if (this.inputParams.gitRepository) {
this.workDir = this.generateWorkDirName();
yield exec(`mkdir -p ${this.workDir}`);
this.workDir = yield fs_1.default.realpathSync(this.workDir);
process.chdir(this.workDir);
const { stdout } = yield exec(`pwd`);
this.spinner.info(`Working Directory: ${stdout.trim()}`);
yield this.cloneRepo(this.inputParams.gitRepository);
}
});
}
destroy() {
return __awaiter(this, void 0, void 0, function* () {
if (this.inputParams.gitRepository) {
process.chdir(this.originalCwd);
yield this.safeDeleteFolder(path.resolve(this.workDir));
}
});
}
cloneRepo(gitRepository) {
return __awaiter(this, void 0, void 0, function* () {
this.spinner.start(`Cloning ${gitRepository}`);
const { GITHUB_TOKEN } = process.env;
// If there's a GITHUB_TOKEN env variable, use it to clone the repository
const authenticatedGitRepositoryUrl = Boolean(GITHUB_TOKEN)
? gitRepository.replace(`https://github.com/`, `https://${GITHUB_TOKEN}@github.com/`)
: gitRepository;
yield exec(`git clone ${authenticatedGitRepositoryUrl} .`);
this.spinner.succeed();
});
}
buildBranch(branch) {
return __awaiter(this, void 0, void 0, function* () {
this.spinner.start(`Checkout`);
yield exec(`git reset --hard`);
yield exec(`git clean -f`);
yield exec(`git checkout ${branch}`);
this.spinner.succeed().start(`Install`);
yield exec(this.inputParams.installScript, { maxBuffer: 20000 * 1024 });
this.spinner.succeed().start(`Building`);
yield exec(this.inputParams.buildScript, { maxBuffer: 20000 * 1024 });
this.spinner.succeed();
});
}
/**
* From whatever the current branch is, returns a list of each files that are matched by `buildFilesPatterns`
*/
getSingleBranchReport() {
return __awaiter(this, void 0, void 0, function* () {
this.spinner.start(`Calculating sizes of files matching: \`${this.inputParams.buildFilesPatterns}\``);
const targetedFiles = yield this.getTargetedFiles(this.inputParams.buildFilesPatterns);
const fileSizes = (yield Promise.all(targetedFiles.map(this.safeGetSize)));
const filePaths = targetedFiles.map(file => file.replace(this.workDir, ''));
this.spinner.succeed();
return ramda_1.zipObj(filePaths, fileSizes);
});
}
getTargetedFiles(regex) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield globby_1.default(regex.map(item => path.resolve(item)));
}
catch (_a) {
return [];
}
});
}
cleanDist() {
return __awaiter(this, void 0, void 0, function* () {
this.spinner.start(`Cleaning dist`);
(yield this.getTargetedFiles(this.inputParams.buildFilesPatterns)).map(fs_1.default.unlinkSync);
this.spinner.succeed();
});
}
safeGetSize(files) {
return __awaiter(this, void 0, void 0, function* () {
try {
// Todo: add `gzip: false` in the options, since we're only intereseted in parsed size
return (yield size_limit_1.default(files, { webpack: false })).parsed;
}
catch (_a) {
return 0;
}
});
}
}
exports.default = BundleChecker;