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

90.9% Statements 30/33
83.33% Branches 10/12
66.66% Functions 2/3
90.9% Lines 30/33

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                                1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             4x 3x   3x 1x     2x 3x 1x   2x 2x                   4x     4x   4x   4x         4x   4x   1x     1x             1x  
/**
 * @file custom-update.js
 * @description Upgrade a consuming Spec-Up-T repository in place.
 *
 * This is not a postinstall hook. First-time install is install.js.
 * This file is invoked later by:
 *   - existing scripts: node -e "require('spec-up-t/src/install-from-boilerplate/custom-update.js')"
 *   - npm run custom-update (after 2.0.0 rewrites the script)
 *   - npx spec-up-t@2 custom-update
 *   - node path/to/custom-update.js
 *
 * 2.0.0 still auto-runs on require() from `node -e` so GitHubUi / old
 * package.json scripts keep working. The CLI requires this module without
 * auto-running, then calls the exported function.
 */
 
const fs = require('fs-extra');
const { configScriptsKeys, configOverwriteScriptsKeys } = require('./config-scripts-keys');
const addScriptsKeys = require('./add-scripts-keys');
const copySystemFiles = require('./copy-system-files');
const { gitIgnoreEntries } = require('./config-gitignore-entries');
const { updateGitignore } = require('./add-gitignore-entries');
const updateDependencies = require('./update-dependencies');
const installConsumerDependencies = require('./install-consumer-dependencies');
const migrateVersionsToSnapshots = require('./migrate-versions-to-snapshots');
const renameBuildDirToLegacy = require('./rename-docs-to-legacy');
const shouldAutoRunCustomUpdate = require('./should-auto-run-custom-update');
const Logger = require('../utils/logger');
 
/**
 * Migrates snapshots and tracked build dirs for every spec in specs.json.
 * @throws {Error} When specs.json is missing, unreadable, or has no specs.
 */
function migrateAllSpecs() {
    const config = fs.readJsonSync('specs.json');
    const specs = config && Array.isArray(config.specs) ? config.specs : [];
 
    if (specs.length === 0) {
        throw new Error('specs.json has no specs array');
    }
 
    for (const spec of specs) {
        if (!spec || !spec.output_path) {
            throw new Error('A spec in specs.json is missing output_path');
        }
        migrateVersionsToSnapshots(spec.output_path);
        renameBuildDirToLegacy(spec.output_path);
    }
}
 
/**
 * Runs the in-place upgrade of the consuming repository (process.cwd()).
 */
async function customUpdate() {
    // Copy/replace boilerplate system files, replace workflows 1:1, and
    // remove stale files (e.g. menu-wrapper.sh) before touching package.json.
    copySystemFiles();
 
    // Must complete before updateDependencies — both write package.json.
    addScriptsKeys(configScriptsKeys, configOverwriteScriptsKeys);
 
    await updateGitignore(gitIgnoreEntries.gitignorePath, gitIgnoreEntries.filesToAdd);
 
    await updateDependencies();
 
    // Install what was just written. Must live in this function (not only in
    // the npm-script string) so the first 2.0.0 run via node -e require()
    // still installs.
    installConsumerDependencies();
 
    migrateAllSpecs();
 
    Logger.success('Custom update done');
}
 
Iif (shouldAutoRunCustomUpdate(require.main, module, process.env)) {
    customUpdate().catch((error) => {
        Logger.error('Custom update failed:', error);
        process.exitCode = 1;
    });
}
 
module.exports = customUpdate;