|
| 1 | +import fs from 'fs-extra'; |
| 2 | +import path from 'path'; |
| 3 | +import { DEV, LOCAL, PROD } from './config'; |
| 4 | + |
| 5 | +const writeRemoteEnvFile = async ( |
| 6 | + env, |
| 7 | + rootPath, |
| 8 | + { |
| 9 | + graaspDeveloperId = '', |
| 10 | + graaspAppId = '', |
| 11 | + awsAccessKeyId = '', |
| 12 | + awsSecretAccessKey = '', |
| 13 | + } = {}, |
| 14 | +) => { |
| 15 | + const host = env === PROD ? 'apps.graasp.eu' : 'apps.dev.graasp.eu'; |
| 16 | + const bucket = `graasp-apps-${env}`; |
| 17 | + const string = ` |
| 18 | + REACT_APP_GRAASP_DEVELOPER_ID=${graaspDeveloperId} |
| 19 | + REACT_APP_GRAASP_APP_ID=${graaspAppId} |
| 20 | + REACT_APP_GRAASP_DOMAIN=graasp.eu |
| 21 | + REACT_APP_HOST=${host} |
| 22 | + REACT_APP_VERSION=latest |
| 23 | + REACT_APP_BASE=//$REACT_APP_HOST/$REACT_APP_GRAASP_DEVELOPER_ID/$REACT_APP_GRAASP_APP_ID/$REACT_APP_VERSION/ |
| 24 | + NODE_ENV=production |
| 25 | + BUCKET=${bucket} |
| 26 | + AWS_DEFAULT_REGION=us-east-1 |
| 27 | + AWS_ACCESS_KEY_ID=${awsAccessKeyId} |
| 28 | + AWS_SECRET_ACCESS_KEY=${awsSecretAccessKey} |
| 29 | + `; |
| 30 | + try { |
| 31 | + await fs.writeFile(path.join(rootPath, `.env.${env}`), string, 'utf8'); |
| 32 | + } catch (e) { |
| 33 | + console.error(e); |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +const writeLocalEnvFile = async (env, rootPath) => { |
| 38 | + const string = ` |
| 39 | + REACT_APP_GRAASP_DEVELOPER_ID= |
| 40 | + REACT_APP_GRAASP_APP_ID= |
| 41 | + REACT_APP_GRAASP_DOMAIN=localhost |
| 42 | + REACT_APP_HOST= |
| 43 | + REACT_APP_VERSION= |
| 44 | + REACT_APP_BASE= |
| 45 | + `; |
| 46 | + try { |
| 47 | + await fs.writeFile(path.join(rootPath, '.env.local'), string, 'utf8'); |
| 48 | + } catch (e) { |
| 49 | + console.error(e); |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | + |
| 54 | +const writeEnvFile = async (env, rootPath) => { |
| 55 | + switch (env) { |
| 56 | + case LOCAL: |
| 57 | + return writeLocalEnvFile(env, rootPath); |
| 58 | + case DEV: |
| 59 | + case PROD: |
| 60 | + return writeRemoteEnvFile(env, rootPath); |
| 61 | + default: |
| 62 | + return false; |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | + |
| 67 | +const writeEnvFiles = async (rootPath) => { |
| 68 | + console.log('writing environment files...'); |
| 69 | + await Promise.all([LOCAL, DEV, PROD].map(env => writeEnvFile(env, rootPath))); |
| 70 | + console.log('wrote environment files'); |
| 71 | +}; |
| 72 | + |
| 73 | +export default writeEnvFiles; |
0 commit comments