All files / src/install-from-boilerplate should-auto-run-custom-update.js

100% Statements 9/9
83.33% Branches 10/12
100% Functions 1/1
100% Lines 9/9

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                                              9x 2x     7x 1x       6x 1x     5x 5x         2x  
/**
 * @file should-auto-run-custom-update.js
 * @description Decides whether custom-update.js should execute as a side effect
 * of being loaded. Kept as its own module so the 2.0.0 bridge can be tested
 * without touching a consuming repo.
 *
 * Auto-run is required for existing package.json scripts:
 *   node -e "require('spec-up-t/src/install-from-boilerplate/custom-update.js')"
 * On Node 22 that path has require.main == null. Running the file as a script
 * has require.main === module.
 *
 * Do not auto-run when another real module loaded this file (the CLI, Jest).
 * Those callers invoke the exported function themselves.
 */
 
/**
 * @param {NodeModule|null|undefined} requireMain - The value of require.main
 *   at load time.
 * @param {NodeModule} thisModule - The custom-update module (module).
 * @param {NodeJS.ProcessEnv} [env=process.env]
 * @returns {boolean}
 */
function shouldAutoRunCustomUpdate(requireMain, thisModule, env = process.env) {
    if (env.SPEC_UP_T_SKIP_CUSTOM_UPDATE === 'true') {
        return false;
    }
 
    if (requireMain === thisModule) {
        return true;
    }
 
    // node -e "require('...custom-update.js')" on Node 22.
    if (requireMain == null) {
        return true;
    }
 
    const filename = requireMain.filename || '';
    return filename === '[eval]'
        || filename.endsWith('/[eval]')
        || filename.endsWith('\\[eval]');
}
 
module.exports = shouldAutoRunCustomUpdate;