UNPKG

template-maker

Version:

A CLI to generate project templates with alot of options!!

84 lines (67 loc) 2.12 kB
import chalk from "chalk" import fs from 'fs' import ncp from 'ncp' import path from 'path' import {promisify} from 'util' import execa from 'execa' import Listr from 'listr' import {projectInstall} from 'pkg-install' const access = promisify(fs.access) const copy = promisify(ncp) async function copyTemplates(options) { return copy(options.templateDirectory, options.targetDirectory, { clobber:false }) } async function initGit(options) { const result = await execa("git", ["init"], { cwd:options.targetDirectory }) if (result.failed) { return Promise.reject(new Error("Could Not Initialise Git Repo")) } } export async function createProject(options) { options = { ...options, targetDirectory:options.targetDirectory || process.cwd() } const currentFileUrl = import.meta.url; const templateDir = path.resolve( new URL(currentFileUrl).pathname, '../../templates', options.template.toLowerCase() ) console.log(templateDir) options.templateDirectory = templateDir try { await access(templateDir, fs.constants.R_OK) } catch { console.error("Invalid Template Name", chalk.red.bold("ERROR")) process.exit(1) } // console.log("Creating Project Files....") // await copyTemplates(options) const tasks = new Listr([ { title:"Generating Project Files...", task:() => copyTemplates(options) }, { title:"Initializing Git Repo...", task:() => initGit(options), enabled:() => options.git }, { title:"Installing Packages...", task:() => projectInstall({ cwd:options.targetDirectory }), skip:() => !options.runInstall ? "Pass --install or -i to automatically install dependencies" : undefined } ]) await tasks.run() console.log(chalk.green.bold("DONE")) console.log("We suggesst you begin by cd'ing into the project directory and type `npm start`!") return true }