This is only for testing npm init for installing other things. https://learnjsthehardway.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

51 lines
1.9 KiB

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 BANDO_GIT = "https://git.learnjsthehardway.com/learn-javascript-the-hard-way/bandolier-template.git";
export const description = "Generates projects using the Bandolier educational framework.";
export const options = [
["--repo", "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 = ["<target>", "name of the project directory (must not exist)"];
export const main = async (target, opts) => {
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 ${BANDO_GIT} ${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 {
exec_i(`rm -rf ${path.join(target, ".git")}`);
}
}