Skip to content

Commit

Permalink
feat: write the default environment files
Browse files Browse the repository at this point in the history
Currently writing the default prod, dev and local files, as explained
in the React starter kit.

closes #16
  • Loading branch information
juancarlosfarah committed Feb 6, 2019
1 parent 9af74ea commit c0bf273
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// todo: remove when more items are exported
// eslint-disable-next-line import/prefer-default-export
export const DEFAULT_STARTER = 'graasp/graasp-app-starter-react';

// environments
export const LOCAL = 'local';
export const DEV = 'dev';
export const PROD = 'prod';
3 changes: 3 additions & 0 deletions src/initStarter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs from 'fs-extra';
import HostedGitInfo from 'hosted-git-info';
import { sync as existsSync } from 'fs-exists-cached';
import { DEFAULT_STARTER } from './config';
import writeEnvFiles from './writeEnvFiles';

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

await install(rootPath);

await writeEnvFiles(rootPath);

await commit(rootPath);
};

Expand Down
73 changes: 73 additions & 0 deletions src/writeEnvFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import fs from 'fs-extra';
import path from 'path';
import { DEV, LOCAL, PROD } from './config';

const writeRemoteEnvFile = async (
env,
rootPath,
{
graaspDeveloperId = '',
graaspAppId = '',
awsAccessKeyId = '',
awsSecretAccessKey = '',
} = {},
) => {
const host = env === PROD ? 'apps.graasp.eu' : 'apps.dev.graasp.eu';
const bucket = `graasp-apps-${env}`;
const string = `
REACT_APP_GRAASP_DEVELOPER_ID=${graaspDeveloperId}
REACT_APP_GRAASP_APP_ID=${graaspAppId}
REACT_APP_GRAASP_DOMAIN=graasp.eu
REACT_APP_HOST=${host}
REACT_APP_VERSION=latest
REACT_APP_BASE=//$REACT_APP_HOST/$REACT_APP_GRAASP_DEVELOPER_ID/$REACT_APP_GRAASP_APP_ID/$REACT_APP_VERSION/
NODE_ENV=production
BUCKET=${bucket}
AWS_DEFAULT_REGION=us-east-1
AWS_ACCESS_KEY_ID=${awsAccessKeyId}
AWS_SECRET_ACCESS_KEY=${awsSecretAccessKey}
`;
try {
await fs.writeFile(path.join(rootPath, `.env.${env}`), string, 'utf8');
} catch (e) {
console.error(e);
}
};

const writeLocalEnvFile = async (env, rootPath) => {
const string = `
REACT_APP_GRAASP_DEVELOPER_ID=
REACT_APP_GRAASP_APP_ID=
REACT_APP_GRAASP_DOMAIN=localhost
REACT_APP_HOST=
REACT_APP_VERSION=
REACT_APP_BASE=
`;
try {
await fs.writeFile(path.join(rootPath, '.env.local'), string, 'utf8');
} catch (e) {
console.error(e);
}
};


const writeEnvFile = async (env, rootPath) => {
switch (env) {
case LOCAL:
return writeLocalEnvFile(env, rootPath);
case DEV:
case PROD:
return writeRemoteEnvFile(env, rootPath);
default:
return false;
}
};


const writeEnvFiles = async (rootPath) => {
console.log('writing environment files...');
await Promise.all([LOCAL, DEV, PROD].map(env => writeEnvFile(env, rootPath)));
console.log('wrote environment files');
};

export default writeEnvFiles;

0 comments on commit c0bf273

Please sign in to comment.