All files / src/install-from-boilerplate copy-boilerplate.js

0% Statements 0/31
0% Branches 0/8
0% Functions 0/2
0% Lines 0/31

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                                                                                                                                             
const fs = require('fs-extra');
const path = require('node:path');
const Logger = require('../utils/logger');
 
/**
 * Copies the boilerplate files to the consuming project root.
 * If a template is specified (non-default), the template's spec/ directory
 * and specs.json overwrite those from the default boilerplate.
 * The assets/ and static/ directories always come from the original boilerplate.
 *
 * @param {Object|null} template - Template object from config-templates.js, or null/undefined for default.
 */
function copyBoilerplate(template) {
    const sourceDir = path.join(__dirname, './', 'boilerplate');
    const projectRoot = process.cwd();
 
    // Step 1: Copy everything from the default boilerplate
    const items = fs.readdirSync(sourceDir);
    for (const item of items) {
        const srcPath = path.join(sourceDir, item);
        const destPath = path.join(projectRoot, item);
        fs.cpSync(srcPath, destPath, { recursive: true });
    }
 
    // Rename the copied gitignore file to .gitignore
    const gitignorePath = path.join(projectRoot, 'gitignore');
    const gitignoreDestPath = path.join(projectRoot, '.gitignore');
    fs.renameSync(gitignorePath, gitignoreDestPath);
 
    Logger.success('Copied spec-up-t-boilerplate to current directory');
 
    // Step 2: If a non-default template was selected, overlay template-specific files
    if (template?.directory) {
        applyTemplate(template, projectRoot);
    }
}
 
/**
 * Overlays template-specific spec/ and specs.json onto the project root,
 * replacing the default boilerplate versions.
 *
 * @param {Object} template - Template object with a directory property.
 * @param {string} projectRoot - Absolute path to the consuming project root.
 */
function applyTemplate(template, projectRoot) {
    const templateDir = path.join(__dirname, 'boilerplate-templates', template.directory);
 
    if (!fs.existsSync(templateDir)) {
        Logger.error(`Template directory not found: ${template.directory}`);
        return;
    }
 
    // Replace spec/ directory with the template's version
    const destSpecDir = path.join(projectRoot, 'spec');
    const templateSpecDir = path.join(templateDir, 'spec');
 
    if (fs.existsSync(templateSpecDir)) {
        fs.rmSync(destSpecDir, { recursive: true, force: true });
        fs.cpSync(templateSpecDir, destSpecDir, { recursive: true });
        Logger.success(`Applied template spec/ from: ${template.name}`);
    }
 
    // Replace specs.json with the template's version
    const templateSpecsJson = path.join(templateDir, 'specs.json');
    if (fs.existsSync(templateSpecsJson)) {
        fs.cpSync(templateSpecsJson, path.join(projectRoot, 'specs.json'));
        Logger.success(`Applied template specs.json from: ${template.name}`);
    }
}
 
module.exports = copyBoilerplate;