All files / picobel build.utils.js

24% Statements 6/25
0% Branches 0/6
10% Functions 1/10
25% Lines 6/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45      1x                   1x                                                   1x 1x 1x 1x    
import readline from "readline";
import fs from "fs";
 
export const parseArgs = rawArgs => {
    const [a, b, ...relevant] = rawArgs;
    return relevant
        .map(arg => {
            const [key, value] = arg.split("=");
            return { [key.replace(/-/g, "")]: value || true };
        })
        .reduce((args, arg) => ({ ...args, ...arg }), {});
};
 
export const watchFiles = (files, onChange) => {
    console.log(`Press "q" to exit`);
    readline.emitKeypressEvents(process.stdin);
    process.stdin.setRawMode(true);
 
    process.stdin.on("keypress", async (str, key) => {
        if (key.name === "q") {
            console.log("stopped watching");
            process.exit();
        }
    });
 
    // All filepaths in `files` dirs:
    const allFiles = files.flatMap(dir => {
        return fs.readdirSync(dir).map(file => `${dir}/${file}`);
    });
 
    allFiles.forEach(file => {
        fs.watch(file, (eventType, filename) => {
            if (filename) {
                onChange(filename);
            }
        });
    });
};
 
export const getPackageVersion = () => {
    const packageJson = fs.readFileSync("./package.json");
    const { version } = JSON.parse(packageJson);
    return version;
};