Skip to content

Commit

Permalink
feat(cwp-templates): generate JWT_SECRET, S3_BUCKET, and MONGODB_NAME…
Browse files Browse the repository at this point in the history
… for all environments (#1103)
  • Loading branch information
Pavel910 authored Jul 5, 2020
1 parent 042119e commit 210f8f3
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 29 deletions.
27 changes: 17 additions & 10 deletions packages/cwp-template-cms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const loadJsonFile = require("load-json-file");
const writeJsonFile = require("write-json-file");
const { v4: uuidv4 } = require("uuid");

const s3BucketName = (projectId, appName) => {
return `${projectId}-${appName.toLowerCase().replace(/_/g, "-")}-files`;
const s3BucketName = (projectId, appName, env) => {
return `${projectId}-${appName.toLowerCase().replace(/_/g, "-")}-${env}`;
};

module.exports = async ({ appName, root }) => {
Expand All @@ -30,18 +30,25 @@ module.exports = async ({ appName, root }) => {
.split("-")
.shift();

const jwtSecret = crypto
.randomBytes(128)
.toString("base64")
.slice(0, 60);

apiEnv.default["JWT_SECRET"] = jwtSecret;
apiEnv.default["S3_BUCKET"] = s3BucketName(projectId, appName);
const jwtSecret = () =>
crypto
.randomBytes(128)
.toString("base64")
.slice(0, 60);

apiEnv.local["JWT_SECRET"] = jwtSecret();
apiEnv.dev["JWT_SECRET"] = jwtSecret();
apiEnv.prod["JWT_SECRET"] = jwtSecret();
apiEnv.local["S3_BUCKET"] = s3BucketName(projectId, appName, "local");
apiEnv.dev["S3_BUCKET"] = s3BucketName(projectId, appName, "dev");
apiEnv.prod["S3_BUCKET"] = s3BucketName(projectId, appName, "prod");
await writeJsonFile(apiEnvJson, apiEnv);

const baseEnvPath = path.join(root, ".env.json");
const baseEnv = await loadJsonFile(baseEnvPath);
baseEnv.default["MONGODB_NAME"] = appName;
baseEnv.local["MONGODB_NAME"] = `${appName}-local`;
baseEnv.dev["MONGODB_NAME"] = `${appName}-dev`;
baseEnv.prod["MONGODB_NAME"] = `${appName}-prod`;
await writeJsonFile(baseEnvPath, baseEnv);

let webinyRoot = fs.readFileSync(path.join(root, "webiny.root.js"), "utf-8");
Expand Down
16 changes: 13 additions & 3 deletions packages/cwp-template-cms/template/api/example.env.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
{
"default": {
"GRAPHQL_INTROSPECTION": true,
"GRAPHQL_PLAYGROUND": true,
"S3_BUCKET": "[BUCKET]",
"JWT_SECRET": "[JWT_SECRET]"
"GRAPHQL_PLAYGROUND": true
},
"local": {
"JWT_SECRET": "[JWT_SECRET]",
"S3_BUCKET": "[BUCKET]"
},
"dev": {
"JWT_SECRET": "[JWT_SECRET]",
"S3_BUCKET": "[BUCKET]"
},
"prod": {
"JWT_SECRET": "[JWT_SECRET]",
"S3_BUCKET": "[BUCKET]"
}
}
14 changes: 12 additions & 2 deletions packages/cwp-template-cms/template/example.env.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
{
"default": {
"AWS_PROFILE": "default",
"AWS_REGION": "us-east-1",
"AWS_REGION": "us-east-1"
},
"local": {
"MONGODB_SERVER": "[MONGODB_SERVER]",
"MONGODB_NAME": "webiny"
"MONGODB_NAME": "[MONGODB_NAME]"
},
"dev": {
"MONGODB_SERVER": "[MONGODB_SERVER]",
"MONGODB_NAME": "[MONGODB_NAME]"
},
"prod": {
"MONGODB_SERVER": "[MONGODB_SERVER]",
"MONGODB_NAME": "[MONGODB_NAME]"
}
}
25 changes: 16 additions & 9 deletions packages/cwp-template-full/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const writeJsonFile = require("write-json-file");
const { v4: uuidv4 } = require("uuid");
const execa = require("execa");

const s3BucketName = (projectId, appName) => {
return `${projectId}-${appName.toLowerCase().replace(/_/g, "-")}-files`;
const s3BucketName = (projectId, appName, env) => {
return `${projectId}-${appName.toLowerCase().replace(/_/g, "-")}-${env}`;
};

module.exports = async ({ appName, root }) => {
Expand All @@ -34,18 +34,25 @@ module.exports = async ({ appName, root }) => {
.split("-")
.shift();

const jwtSecret = crypto
.randomBytes(128)
.toString("base64")
.slice(0, 60);
const jwtSecret = () =>
crypto
.randomBytes(128)
.toString("base64")
.slice(0, 60);

apiEnv.default["JWT_SECRET"] = jwtSecret;
apiEnv.default["S3_BUCKET"] = s3BucketName(projectId, appName);
apiEnv.local["JWT_SECRET"] = jwtSecret();
apiEnv.dev["JWT_SECRET"] = jwtSecret();
apiEnv.prod["JWT_SECRET"] = jwtSecret();
apiEnv.local["S3_BUCKET"] = s3BucketName(projectId, appName, "local");
apiEnv.dev["S3_BUCKET"] = s3BucketName(projectId, appName, "dev");
apiEnv.prod["S3_BUCKET"] = s3BucketName(projectId, appName, "prod");
await writeJsonFile(apiEnvJson, apiEnv);

const baseEnvPath = path.join(root, ".env.json");
const baseEnv = await loadJsonFile(baseEnvPath);
baseEnv.default["MONGODB_NAME"] = appName;
baseEnv.local["MONGODB_NAME"] = `${appName}-local`;
baseEnv.dev["MONGODB_NAME"] = `${appName}-dev`;
baseEnv.prod["MONGODB_NAME"] = `${appName}-prod`;
await writeJsonFile(baseEnvPath, baseEnv);

let webinyRoot = fs.readFileSync(path.join(root, "webiny.root.js"), "utf-8");
Expand Down
16 changes: 13 additions & 3 deletions packages/cwp-template-full/template/api/example.env.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
{
"default": {
"GRAPHQL_INTROSPECTION": true,
"GRAPHQL_PLAYGROUND": true,
"S3_BUCKET": "[BUCKET]",
"JWT_SECRET": "[JWT_SECRET]"
"GRAPHQL_PLAYGROUND": true
},
"local": {
"JWT_SECRET": "[JWT_SECRET]",
"S3_BUCKET": "[BUCKET]"
},
"dev": {
"JWT_SECRET": "[JWT_SECRET]",
"S3_BUCKET": "[BUCKET]"
},
"prod": {
"JWT_SECRET": "[JWT_SECRET]",
"S3_BUCKET": "[BUCKET]"
}
}
14 changes: 12 additions & 2 deletions packages/cwp-template-full/template/example.env.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
{
"default": {
"AWS_PROFILE": "default",
"AWS_REGION": "us-east-1",
"AWS_REGION": "us-east-1"
},
"local": {
"MONGODB_SERVER": "[MONGODB_SERVER]",
"MONGODB_NAME": "webiny"
"MONGODB_NAME": "[MONGODB_NAME]"
},
"dev": {
"MONGODB_SERVER": "[MONGODB_SERVER]",
"MONGODB_NAME": "[MONGODB_NAME]"
},
"prod": {
"MONGODB_SERVER": "[MONGODB_SERVER]",
"MONGODB_NAME": "[MONGODB_NAME]"
}
}

0 comments on commit 210f8f3

Please sign in to comment.