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

100% Statements 27/27
92.85% Branches 13/14
100% Functions 2/2
100% Lines 27/27

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 632x 2x 2x                   5x 5x 5x   5x                     4x   4x 1x     3x 3x   3x 1x     3x   3x 6x   6x 3x 3x 3x       3x 2x 2x   1x       2x 2x  
const fs = require('node:fs');
const path = require('node:path');
const Logger = require('../utils/logger');
 
/**
 * Reads the packaged consumer dependency list and pins spec-up-t to the
 * version of this running package. No network call — the file is shipped
 * inside spec-up-t so custom-update works offline.
 *
 * @returns {Object} Map of package name to version string
 */
function loadCanonicalDependencies() {
    const packagedPath = path.join(__dirname, 'package.spec-up-t.json');
    const packaged = JSON.parse(fs.readFileSync(packagedPath, 'utf8'));
    const self = require('../../package.json');
 
    return {
        ...(packaged.dependencies || {}),
        'spec-up-t': self.version
    };
}
 
/**
 * Updates dependencies in the consuming repo's package.json from the
 * packaged canonical list, pinning spec-up-t to this package's version.
 */
async function updateDependencies() {
    const consumerPackagePath = path.resolve(process.cwd(), 'package.json');
 
    if (!fs.existsSync(consumerPackagePath)) {
        throw new Error(`package.json not found at: ${consumerPackagePath}`);
    }
 
    const specUpPackageDependencies = loadCanonicalDependencies();
    const consumerPackage = JSON.parse(fs.readFileSync(consumerPackagePath, 'utf8'));
 
    if (!consumerPackage.dependencies) {
        consumerPackage.dependencies = {};
    }
 
    let updatedCount = 0;
 
    for (const [packageName, version] of Object.entries(specUpPackageDependencies)) {
        const currentVersion = consumerPackage.dependencies[packageName];
 
        if (currentVersion !== version) {
            consumerPackage.dependencies[packageName] = version;
            updatedCount++;
            Logger.info(`Updated ${packageName}: ${currentVersion || 'not installed'} -> ${version}`);
        }
    }
 
    if (updatedCount > 0) {
        fs.writeFileSync(consumerPackagePath, JSON.stringify(consumerPackage, null, 2) + '\n', 'utf8');
        Logger.success(`Successfully updated ${updatedCount} dependenc${updatedCount === 1 ? 'y' : 'ies'} in package.json`);
    } else {
        Logger.info('All dependencies are already up to date');
    }
}
 
module.exports = updateDependencies;
module.exports.loadCanonicalDependencies = loadCanonicalDependencies;