iwa-cli
Version:
An ImmutableWebApp CLI using oclif and cosmiconfig
77 lines (76 loc) • 3.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs_1 = (0, tslib_1.__importDefault)(require("fs"));
const path_1 = (0, tslib_1.__importDefault)(require("path"));
const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
const command_1 = require("@oclif/command");
const helpers_1 = require("../helpers");
const sgf = require('staged-git-files');
function getHtmlFromInput(inputFilePath) {
const inputLocation = path_1.default.join(process.cwd(), inputFilePath);
return fs_1.default.readFileSync(inputLocation, {
encoding: 'utf-8',
});
}
class CheckCommand extends command_1.Command {
handleNoConfigFound(inputFilePath) {
this.log(chalk_1.default.cyanBright `No iwa config found in ${inputFilePath}!`);
}
handleConfigFoundAndStaged(inputFilePath) {
this.log(chalk_1.default.redBright `iwa config found in ${inputFilePath} file!`);
this.error(`Remove iwa config from ${inputFilePath}!`, { exit: 2 });
}
handleConfigFound(inputFilePath) {
this.log(chalk_1.default.cyanBright `iwa config found in ${inputFilePath} file!`);
}
checkInputForIwaConfig(inputFilePath, isStaged) {
const indexHtml = getHtmlFromInput(inputFilePath);
if (!indexHtml) {
this.log(chalk_1.default.redBright `Could not find ${inputFilePath} file!`);
}
if ((0, helpers_1.hasIWAConfig)(indexHtml)) {
if (isStaged) {
this.handleConfigFoundAndStaged(inputFilePath);
return;
}
this.handleConfigFound(inputFilePath);
return;
}
this.handleNoConfigFound(inputFilePath);
}
handleStagedFiles(stagedFiles, inputFilePath) {
const isInputFileStaged = stagedFiles === null || stagedFiles === void 0 ? void 0 : stagedFiles.find((file) => file.filename === inputFilePath);
if (isInputFileStaged) {
this.checkInputForIwaConfig(inputFilePath, true);
}
else {
this.log(chalk_1.default.cyanBright `${inputFilePath} is not staged. Not checking for IWA config.`);
}
}
async run() {
const { flags: { isStaged }, args } = this.parse(CheckCommand);
const { input } = args;
if (isStaged) {
const stagedFiles = await sgf();
this.handleStagedFiles(stagedFiles, input);
}
else {
this.checkInputForIwaConfig(input);
}
}
}
CheckCommand.aliases = ['check', 'c'];
CheckCommand.description = 'Checks HTML file for injected iwa config, useful in a pre-commit hook to prevent commiting injected iwa-config.';
CheckCommand.flags = {
isStaged: command_1.flags.boolean({
char: 's',
description: 'Checks only staged files, and will throw error if config found',
default: false,
}),
verbose: command_1.flags.boolean({ char: 'd' }),
version: command_1.flags.version({ char: 'v' }),
help: command_1.flags.help({ char: 'h' }),
};
CheckCommand.args = [{ name: 'input' }];
exports.default = CheckCommand;