UNPKG

@sp-packages/copyrc

Version:

A lightweight CLI tool to automate copying template files during project setup or runtime.

2 lines (1 loc) 941 B
"use strict";import a from"fs";import i from"path";import{createInterface as p}from"readline";import{Printer as n}from"../utils/logger.js";import{existsSync as f,writeFileSync as l}from"fs";import{DEFAULT_CONFIG as m}from"../constants.js";function u(t){const r=p({input:process.stdin,output:process.stdout});return new Promise(s=>r.question(t,e=>{r.close(),s(e.trim().toLowerCase())}))}export async function readConfig(t){const r="copyrc.json",s=["copyrc.json",".copyrc.json"];let e=t?i.resolve(t):null;if(!e)for(const o of s){const c=i.resolve(o);if(f(c)){e=c;break}}(!e||!f(e))&&(n.error("Config file not found"),n.error(`Please create a ${r} or specify a config file with --config <path>`),await u(`Generate a default ${r} and proceed? (y/n)`)==="y"?(l(r,JSON.stringify(m)),e=i.resolve(r)):process.exit(1));try{const o=a.readFileSync(e,"utf-8");return JSON.parse(o)}catch(o){n.error(`Error reading config file: ${e}`,o),process.exit(1)}}