|
| 1 | +import path from 'path'; |
| 2 | +import { writeFile, mkdir, access } from 'fs/promises'; |
| 3 | +import rimraf from 'rimraf'; |
| 4 | + |
| 5 | +import { errors } from './../errors/index.js'; |
| 6 | +import init from './init.js'; |
| 7 | +import { metadataExport } from './metadata.js'; |
| 8 | + |
| 9 | +import CLIErrorHandler from './../utils/CLIErrorHandler.js'; |
| 10 | + |
| 11 | +export const clone = async (dirName, url, options, program) => { |
| 12 | + await (async () => { |
| 13 | + const cwd = path.resolve(process.cwd()); |
| 14 | + console.log('Executing in cwd:'.green, `${cwd}`.yellow); |
| 15 | + |
| 16 | + const cloneFilePath = path.resolve(cwd, dirName); |
| 17 | + console.log(`Trying to clone app in:`.green, `${cloneFilePath}`.yellow); |
| 18 | + |
| 19 | + const { force, forceRemove } = options; |
| 20 | + const includeDemoApp = !options.clean; // Flip the clean flag. |
| 21 | + |
| 22 | + const folderExists = await access(cloneFilePath) |
| 23 | + .then((result) => { |
| 24 | + return true; |
| 25 | + }) |
| 26 | + .catch((error) => { |
| 27 | + // console.log(error); |
| 28 | + // throw new Error(errors.ERROR_FS_READ_ERROR); |
| 29 | + return false; |
| 30 | + }); |
| 31 | + if (folderExists) { |
| 32 | + if (force) { |
| 33 | + if (forceRemove) { |
| 34 | + if ( |
| 35 | + typeof cloneFilePath === 'string' && |
| 36 | + cloneFilePath !== '' && |
| 37 | + cloneFilePath !== '/' && |
| 38 | + cloneFilePath !== '/*' |
| 39 | + ) { |
| 40 | + // Sanity check !!! |
| 41 | + console.log(`Removing everything inside ${cloneFilePath}`.red); |
| 42 | + rimraf.sync(`${cloneFilePath}/*`); // Please be careful... |
| 43 | + } else { |
| 44 | + throw new Error(errors.ERROR_DANGEROUS_SANITY_CHECK_DID_NOT_PASS); |
| 45 | + } |
| 46 | + } |
| 47 | + } else { |
| 48 | + throw new Error(errors.ERROR_FOLDER_ALREADY_EXISTS); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Call Init |
| 53 | + await init(dirName, options, program, true); |
| 54 | + |
| 55 | + // Write configuration .env with endpoint and admin secret values |
| 56 | + const adminSecret = options?.adminSecret ?? ''; |
| 57 | + |
| 58 | + // !!! Important !!! Mutate options?.config to point inside hlapp |
| 59 | + // eslint-disable-next-line no-param-reassign |
| 60 | + options.config = `./${dirName}/`; |
| 61 | + |
| 62 | + const envTemplate = `# Remove "#" to uncomment the env values. |
| 63 | +ENV_LOCAL_HLAMBDA_ENDPOINT="${url}" |
| 64 | +ENV_LOCAL_HLAMBDA_ADMIN_SECRET="${adminSecret}" |
| 65 | +
|
| 66 | +# ENV_DEV_HLAMBDA_ENDPOINT="http://dev-server:8081" |
| 67 | +# ENV_DEV_HLAMBDA_ADMIN_SECRET="demo-dev" |
| 68 | +
|
| 69 | +# ENV_DEFAULT_ENVIRONMENT="local" |
| 70 | +`; |
| 71 | + |
| 72 | + await writeFile(`./${dirName}/.env`, envTemplate, 'utf-8') |
| 73 | + .then(() => { |
| 74 | + // console.log(`File write ${`./${dirName}/.env`} successfull!`.green); |
| 75 | + }) |
| 76 | + .catch(() => { |
| 77 | + console.log(`File write ${`./${dirName}/.env`} failed`.red); |
| 78 | + }); |
| 79 | + |
| 80 | + // Call metadata export |
| 81 | + await metadataExport(options, program); |
| 82 | + |
| 83 | + console.log(`Directory created.`.green); |
| 84 | + })() |
| 85 | + .then(() => {}) |
| 86 | + .catch(CLIErrorHandler(program)); |
| 87 | +}; |
| 88 | + |
| 89 | +export default clone; |
0 commit comments