import fs from "fs/promises"; import { mkdir_to, exec_i, copy, glob } from "../lib/builderator.js"; import { log } from "../lib/logging.js"; import path from "path"; export const GIT_BASE = "https://git.learnjsthehardway.com/learn-javascript-the-hard-way/"; export const BANDO_GIT = "bandolier-template"; export const description = "Generates projects using the Bandolier educational framework."; export const options = [ ["--using ", "repository to dupe and setup", BANDO_GIT], ["--template ", "templates directory to use for setups", "commands/templates"], ["--keep-git", "for patches and analysis, you can keep the original .git", false], ]; export const argument = ["", "name of the project directory (must not exist)"]; export const main = async (target, opts) => { const git_url = `${GIT_BASE}${opts.using}.git`; log.info(`Cloning from ${git_url}`); try { await fs.access(target); log.error(`Target ${target} exists. Won't install into it.`); process.exit(1); } catch(error) { // access is stupid, it throws an exception but we want the negative response as a positive outcome exec_i(`git clone --depth 1 ${git_url} ${target}`); } // normalize the temp directory const temp_dir = path.join(opts.template, ''); const temp_files = glob(path.join(target, temp_dir, "/**/*")); log.info("Copying template files..."); // copy all of the template files to the destination for(let f of temp_files) { // kind of dumb way to trim the middle of the path out const f_normal = path.join(f, ''); // this fixes glob's using / on windows // use simple string replace to remove the template part const copy_to = f_normal.replace(`${temp_dir}`, '').replace('\\\\', '\\'); mkdir_to(copy_to); copy(f_normal, copy_to); // we can add a template thing here log.debug(`${f_normal} ---> ${copy_to}`); } // remove the .git directory if(opts.keepGit) { log.warn("Keeping the .git directory, so be sure to delete it if you want to do your own work."); } else { if(process.platform === "win32") { exec_i(`rd /s /q ${path.join(target, ".git")}`); } else { exec_i(`rm -rf ${path.join(target, ".git")}`); } } }