Skip to content

Commit

Permalink
feat(dotenvcreate): refactor dotenvcreate script to use yargs for arg…
Browse files Browse the repository at this point in the history
…ument parsing and update ENTRYPOINT in Dockerfile
  • Loading branch information
merrcury committed Dec 31, 2024
1 parent c41676f commit b97bddc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/actions/docker/build-api/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ runs:
--output=type=image,name=$REGISTRY/$REPOSITORY,push-by-digest=true,name-canonical=true
run: |
set -x
cp scripts/dotenvcreate.mjs apps/api/src/dotenvcreate.mjs
cd apps/api && pnpm run docker:build
- name: Tag and test
Expand Down
2 changes: 1 addition & 1 deletion apps/api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ RUN --mount=type=cache,id=pnpm-store-api,target=/root/.pnpm-store\
ENV NEW_RELIC_NO_CONFIG_FILE=true

WORKDIR /usr/src/app/apps/api
ENTRYPOINT [ "sh", "-c", "node dist/dotenvcreate.mjs novu/api $NOVU_REGION $NOVU_ENTERPRISE $NODE_ENV && pm2-runtime start dist/main.js" ]
ENTRYPOINT [ "sh", "-c", "node dist/dotenvcreate.mjs -s=novu/api -r=$NOVU_REGION -e=$NOVU_ENTERPRISE -v=$NODE_ENV && pm2-runtime start dist/main.js" ]

Check warning on line 73 in apps/api/Dockerfile

View workflow job for this annotation

GitHub Actions / Spell check

Unknown word (dotenvcreate)
38 changes: 32 additions & 6 deletions apps/api/src/dotenvcreate.mjs → scripts/dotenvcreate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,38 @@ import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-sec
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

console.time('dotenvcreate');

const secretName = process.argv[2];
const region = process.argv[3];
const enterprise = process.argv[4];
const env = process.argv[5];
const { argv } = yargs(hideBin(process.argv))
.option('secretName', {
alias: 's',
type: 'string',
description: 'The name of the secret',
demandOption: true,
})
.option('region', {
alias: 'r',
type: 'string',
description: 'The region',
demandOption: true,
})
.option('enterprise', {
alias: 'e',
type: 'string',
description: 'The enterprise value',
demandOption: true,
})
.option('env', {
alias: 'v',
type: 'string',
description: 'The environment',
demandOption: true,
});

const { secretName, region, enterprise, env } = argv;

if (!enterprise || enterprise.toLowerCase() === 'false') {
console.log('Enterprise value is false or not provided. Exiting script.');
Expand Down Expand Up @@ -47,9 +72,10 @@ async function getSecretValue(secretName) {
function escapeValue(value) {
// If the value contains special characters or spaces, quote it
if (value && /[ \t"=$]/.test(value)) {
// Escape existing double quotes and wrap the value in quotes
return `"${value.replace(/"/g, '\\"')}"`;
// Escape backslashes and double quotes, then wrap the value in quotes
return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
}

return value;
}

Expand Down

0 comments on commit b97bddc

Please sign in to comment.