@pecometer/peco-cli
Version:
Pecometer Command Line Interface Application with monorepo support
5 lines • 2.23 kB
JavaScript
;
/**
* @author Sean Hutchinson
* @copyright Pecometer Software Ltd
*/Object.defineProperty(exports,"__esModule",{value:!0}),exports.GenerateEnv=void 0;const path=require("node:path"),base_command_1=require("../classes/base-command"),file_1=require("../classes/file"),dotenv_config_1=require("../helpers/dotenv-config");class GenerateEnv extends base_command_1.CBaseCommand{constructor(){super(...arguments),this.description="Generate a PecoTS .env file in the current directory",this._fileHelper=new file_1.CFileHelper,this._cwdPath=process.cwd(),this._envFileIPlaceholders=dotenv_config_1.DOT_ENV_CONFIG}async execute(){return this.notice("Generating DOTENV file."),await this._fileHelper.exists(path.join(this._cwdPath,".env"))&&(this.warn("File already exists, are you sure you wish to replace it Y/N (N): "),!await this.waitForYesNo())?(this.notice("Aborting Generate DOTENV file.",!1),!0):(this.outputNewLine(),!!await this._processFile()&&(this.notice("DOTENV file created successfully."),!0))}async _processFile(){let dataToWrite=(await this._fileHelper.readFile(path.join(__dirname,"..","data","full_env.data"))).toString();if(this._envFileIPlaceholders)for(let i=0,n=this._envFileIPlaceholders.length;i<n;i++){const placeholderGroup=this._envFileIPlaceholders[i];let result=!0;if(placeholderGroup.prompt&&(this.info(`${placeholderGroup.prompt} Y/N (N): `,!1),result=await this.waitForYesNo()),result)for(let k=0,m=placeholderGroup.placeholders.length;k<m;k++){const placeholder=placeholderGroup.placeholders[k];let placeholderResult=null;placeholder.prompt?(this.info(`${placeholder.prompt}: `,!1),placeholderResult=await this.waitForPrompt()):placeholder.value&&(placeholderResult=await placeholder.value(null));const regex=new RegExp(`{{${placeholder.name}}}`,"gi");dataToWrite=dataToWrite.replace(regex,""!==placeholderResult?placeholderResult:void 0!==placeholder.default?placeholder.default:""),placeholder.replace&&placeholder.with&&(dataToWrite=dataToWrite.replace(placeholder.replace,placeholder.with))}}return!!await this._fileHelper.writeFile(path.join(this._cwdPath,".env"),dataToWrite)||(this.error("An error has occurred trying to create the DOTENV file."),!1)}}exports.GenerateEnv=GenerateEnv;