Skip to content

Commit

Permalink
Rewrite set-env command to node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pziemkowski committed Sep 5, 2023
1 parent d2a6140 commit 54abf39
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"nx-cloud": "16.0.5",
"prettier": "^2.6.2",
"react-refresh": "^0.10.0",
"string-argv": "^0.3.2",
"tailwindcss": "^3.3.2",
"tailwindcss-animate": "^1.0.5",
"ts-jest": "29.1.0",
Expand Down
5 changes: 1 addition & 4 deletions packages/internal/core/custom-task-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ async function loadStageEnv(shouldValidate = true) {
}
}

const noValidateTasks = [
'tools:bootstrap-infra',
'infra-shared:bootstrap'
]
const noValidateTasks = ['tools:bootstrap-infra', 'infra-shared:bootstrap'];

module.exports = async (...args) => {
const taskId = args[0]?.[0]?.id;
Expand Down
3 changes: 2 additions & 1 deletion packages/internal/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"devDependencies": {
"env-cmd": "^10.1.0",
"envalid": "^7.3.1",
"dotenv": "^16.1.4"
"dotenv": "^16.1.4",
"lookpath": "^1.2.2"
}
}
11 changes: 10 additions & 1 deletion packages/internal/core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"executor": "nx:run-commands",
"options": {
"color": true,
"commands": ["nx docker-compose:up", "nx run webapp:start"],
"commands": ["nx run core:docker-compose:up", "nx run webapp:start"],
"parallel": false
}
},
Expand All @@ -70,6 +70,15 @@
"options": {
"lintFilePatterns": ["packages/internal/core/**/*.ts"]
}
},
"set-env": {
"executor": "nx:run-commands",
"options": {
"color": true,
"commands": ["node ./scripts/set-env.js {args.env}"],
"cwd": "packages/internal/core",
"parallel": false
}
}
},
"tags": []
Expand Down
19 changes: 19 additions & 0 deletions packages/internal/core/scripts/lib/runCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { spawn } = require('node:child_process');

function runCommand(command, args) {
return new Promise((resolve, reject) => {
const cmd = spawn(command, args, { stdio: 'inherit' });

cmd.on('close', (code) => {
if (code !== 0) {
reject(
new Error(`"${command} ${args.join(' ')}" failed with code ${code}`)
);
} else {
resolve();
}
});
});
}

module.exports = { runCommand };
37 changes: 37 additions & 0 deletions packages/internal/core/scripts/set-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env node

const os = require('os');
const { lookpath } = require('lookpath');

const { runCommand } = require('./lib/runCommand');

const ENV_STAGE = process.argv[2];
const AWS_VAULT_PROFILE = process.env.AWS_VAULT_PROFILE;

function getCmd() {
if (os.platform() === 'win32') {
return 'cmd.exe';
}
return process.env.SHELL;
}

(async () => {
try {
if (!ENV_STAGE || ENV_STAGE === 'undefined' || ENV_STAGE === 'local') {
console.error('Please set --env argument');
process.exit(1);
}

process.env.ENV_STAGE = ENV_STAGE;

const cmd = getCmd();
if (await lookpath('aws-vault')) {
await runCommand('aws-vault', ['exec', AWS_VAULT_PROFILE, '--', cmd]);
} else {
await runCommand(cmd, []);
}
} catch (err) {
console.error(err);
process.exit(1);
}
})();
Binary file modified patches/[email protected]
Binary file not shown.
31 changes: 24 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 54abf39

Please sign in to comment.