All files / npm-pack-all index.js

97.96% Statements 48/49
85% Branches 17/20
100% Functions 7/7
97.96% Lines 48/49

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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113  4x 4x 4x   4x   4x 4x 4x   4x 4x   4x                 4x 4x     4x   4x   4x 4x     4x     4x 4x   4x     4x 4x           4x 4x 4x     4x     4x 4x         4x 4x 1x 1x 1x       4x 4x 4x       4x 2x 2x 2x 2x     2x           2x                 4x 16x   4x       4x 16x      
#! /usr/bin/env node
const fs = require(`fs`);
const path = require(`path`);
const shell = require(`shelljs`);
 
const { CliError, safetyDecorator, shellExecDecorator } = require(path.join(__dirname, `./utils/utils.index`));
 
const cp = safetyDecorator(shell.cp);
const mv = safetyDecorator(shell.mv);
const exec = shellExecDecorator(shell.exec);
 
const FILES_TO_BACKUP = [`package.json`, `package-lock.json`, `yarn.lock`, `.npmignore`];
const TMP_DIRECTORY = path.join(process.cwd(), `.npm-pack-all-tmp`);
 
console.info(`
.------..------..------..------..------..------..------..------..------..------..------..------.
|N.--. ||P.--. ||M.--. ||-.--. ||P.--. ||A.--. ||C.--. ||K.--. ||-.--. ||A.--. ||L.--. ||L.--. |
| :(): || :/\\: || (\\/) || (\\/) || :/\\: || (\\/) || :/\\: || :/\\: || (\\/) || (\\/) || :/\\: || :/\\: |
| ()() || (__) || :\\/: || :\\/: || (__) || :\\/: || :\\/: || :\\/: || :\\/: || :\\/: || (__) || (__) |
| '--'N|| '--'P|| '--'M|| '--'-|| '--'P|| '--'A|| '--'C|| '--'K|| '--'-|| '--'A|| '--'L|| '--'L|
\`------'\`------'\`------'\`------'\`------'\`------'\`------'\`------'\`------'\`------'\`------'\`------'\n`);
 
// parse cli args
const cliArgs = require(`minimist`)(process.argv.slice(2));
Iif (typeof cliArgs.output !== `string` && cliArgs.output) {
    throw new CliError(`--output`, cliArgs.output, `The \`--output\` flag requires a string filename`);
}
console.info(`CLI Args: ${JSON.stringify(cliArgs, null, 4)}\n`);
 
const packageJson = require(path.join(process.cwd(), `package.json`));
 
shell.config.fatal = true; // error out if a shell command errors out
shell.config.silent = true;
 
// create temp directory
createTempDirectory(TMP_DIRECTORY);
 
// copy existing package.json and lock files (keep linting, etc in tact)
console.info(`Saving existing package.json and lock files`);
copyFiles(process.cwd(), TMP_DIRECTORY, FILES_TO_BACKUP);
 
setBundledDependencies(packageJson);
 
// pack with npm
console.info(`\nPacking source code${!cliArgs[`dev-deps`] ? `` : `, development`} and production dependencies...`);
exec(`npm -s pack`, {
    maxBuffer: 1024 * 1024 * 10,
    timeout: cliArgs.timeout || 3 * 60 * 1000 // 3 min timeout
});
 
// restoring package.json and lock files back to project root
console.info(`Restoring original package.json and lock files`);
moveFiles(TMP_DIRECTORY, process.cwd(), FILES_TO_BACKUP);
shell.rm(`-Rf`, TMP_DIRECTORY);
// shell.rm(`-Rf`, `.npmignore`);
 
setArtifactName(cliArgs);
 
function createTempDirectory(dir) {
    shell.rm(`-Rf`, dir);
    shell.mkdir("-p", dir);
}
 
function setBundledDependencies(pj) {
    // prune - get rid of devDependencies
    pj.bundledDependencies = Object.keys(pj.dependencies || {});
    if (cliArgs[`dev-deps`]) {
        console.info(`Detected --dev-deps\n\nInstalling all modules...`);
        console.warn(`To save time and space, you may want to think about only packaging production dependencies`);
        pj.bundledDependencies = pj.bundledDependencies.concat(Object.keys(pj.devDependencies));
    }
 
    // put dependencies into bundledDependencies in package.json
    console.info(`Adding dependencies${cliArgs["dev-deps"] ? " and devDependencies" : ""} to bundledDependencies:`);
    console.info(JSON.stringify(pj.bundledDependencies, null, 4));
    fs.writeFileSync(path.join(process.cwd(), `package.json`), JSON.stringify(pj, null, 4));
}
 
function setArtifactName(args) {
    if (args.output) {
        const outputDir = path.parse(path.join(process.cwd(), cliArgs.output)).dir;
        Eif (outputDir && !fs.existsSync(outputDir)) {
            console.info(`Creating directory ${outputDir}`);
            shell.mkdir(`-p`, outputDir);
        }
 
        console.info(
            `Moving ${path.join(process.cwd(), `${packageJson.name}-${packageJson.version}.tgz`)} to ${path.join(
                process.cwd(),
                cliArgs.output
            )}`
        );
        shell.mv(
            `-f`,
            path.join(process.cwd(), `${packageJson.name}-${packageJson.version}.tgz`),
            path.join(process.cwd(), cliArgs.output)
        );
    }
}
 
function copyFiles(from, to, files) {
    files.forEach(file => {
        cp(`-Rf`, path.join(from, file), path.join(to, file));
    });
    fs.writeFileSync(".npmignore", ".npm-pack-all-tmp");
}
 
function moveFiles(from, to, files) {
    files.forEach(file => {
        mv(`-f`, path.join(from, file), path.join(to, file));
    });
}