Skip to content

Commit c0bf273

Browse files
feat: write the default environment files
Currently writing the default prod, dev and local files, as explained in the React starter kit. closes #16
1 parent 9af74ea commit c0bf273

3 files changed

Lines changed: 81 additions & 2 deletions

File tree

src/config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
// todo: remove when more items are exported
2-
// eslint-disable-next-line import/prefer-default-export
31
export const DEFAULT_STARTER = 'graasp/graasp-app-starter-react';
2+
3+
// environments
4+
export const LOCAL = 'local';
5+
export const DEV = 'dev';
6+
export const PROD = 'prod';

src/initStarter.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fs from 'fs-extra';
55
import HostedGitInfo from 'hosted-git-info';
66
import { sync as existsSync } from 'fs-exists-cached';
77
import { DEFAULT_STARTER } from './config';
8+
import writeEnvFiles from './writeEnvFiles';
89

910
// use execa to spawn a better child process
1011
const spawn = (cmd, opts = { stdio: 'inherit' }) => {
@@ -114,6 +115,8 @@ const clone = async (hostInfo, rootPath) => {
114115

115116
await install(rootPath);
116117

118+
await writeEnvFiles(rootPath);
119+
117120
await commit(rootPath);
118121
};
119122

src/writeEnvFiles.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)