chore: add deployment and maintenance tooling
This commit is contained in:
1 parent
7989da96d0
commit
b689fa6a46
9 files changed
+2585
No files matched your search
@@ -0,0 +1,72 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
const MACHINE_ENV_PATHS = [
|
||||
process.env.MachineEnvFile,
|
||||
process.env.MACHINE_ENV_FILE,
|
||||
'/source/global-env-vars.txt',
|
||||
process.env.HOME
|
||||
? path.join(process.env.HOME, 'source/global-env-vars.txt')
|
||||
: '',
|
||||
].filter(Boolean);
|
||||
|
||||
const PROJECT_ENV_PATHS = [
|
||||
'project.env',
|
||||
'/app/project.env',
|
||||
'apps/project.env',
|
||||
];
|
||||
|
||||
function stripQuotes(value) {
|
||||
const trimmed = value.trim();
|
||||
if (
|
||||
(trimmed.startsWith('"') && trimmed.endsWith('"')) ||
|
||||
(trimmed.startsWith("'") && trimmed.endsWith("'"))
|
||||
) {
|
||||
return trimmed.slice(1, -1);
|
||||
}
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
function loadEnvFile(filePath, options = {}) {
|
||||
const { override = false } = options;
|
||||
const content = fs.readFileSync(filePath, 'utf8');
|
||||
for (const line of content.split(/\r?\n/u)) {
|
||||
if (!line.trim() || /^\s*#/u.test(line)) continue;
|
||||
|
||||
const match = line.match(/^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$/u);
|
||||
if (!match) continue;
|
||||
|
||||
if (!override && process.env[match[1]] !== undefined) continue;
|
||||
process.env[match[1]] = stripQuotes(match[2]);
|
||||
}
|
||||
}
|
||||
|
||||
const machineEnvFile = MACHINE_ENV_PATHS.find((candidate) =>
|
||||
fs.existsSync(candidate),
|
||||
);
|
||||
if (machineEnvFile) {
|
||||
loadEnvFile(machineEnvFile, { override: true });
|
||||
}
|
||||
|
||||
for (const projectEnvFile of PROJECT_ENV_PATHS) {
|
||||
if (fs.existsSync(projectEnvFile)) {
|
||||
loadEnvFile(projectEnvFile);
|
||||
}
|
||||
}
|
||||
|
||||
const missing = [
|
||||
'INFISICAL_CLIENT_ID',
|
||||
'INFISICAL_CLIENT_SECRET',
|
||||
'INFISICAL_PROJECT_ID',
|
||||
].filter((key) => !process.env[key]);
|
||||
|
||||
if (missing.length > 0) {
|
||||
console.error(
|
||||
`Missing required environment variables: ${missing.join(', ')}`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
require('./dist/main.js');
|
||||
@@ -0,0 +1,68 @@
|
||||
param(
|
||||
[switch]$UsePm2,
|
||||
[string]$Pm2Name = "lolympus-tournament-support"
|
||||
)
|
||||
|
||||
if (-not $env:NODE_ENV) {
|
||||
$env:NODE_ENV = "dev"
|
||||
}
|
||||
|
||||
$MainScript = Join-Path $PSScriptRoot "dist/main.js"
|
||||
|
||||
if ($isMacOS -or $isLinux) {
|
||||
$env:MachineEnvFile = "$env:HOME/source/global-env-vars.txt"
|
||||
} elseif ($isWindows) {
|
||||
$env:MachineEnvFile = "$env:USERPROFILE/source/global-env-vars.txt"
|
||||
}
|
||||
|
||||
function Set-EnvFromFile {
|
||||
param([string]$Path)
|
||||
|
||||
if (-not (Test-Path $Path)) {
|
||||
throw "File not found: $Path"
|
||||
}
|
||||
|
||||
foreach ($line in Get-Content $Path) {
|
||||
if ([string]::IsNullOrWhiteSpace($line)) { continue }
|
||||
if ($line -match '^\s*#') { continue }
|
||||
|
||||
if ($line -match '^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$') {
|
||||
$key = $matches[1]
|
||||
$value = $matches[2].Trim()
|
||||
|
||||
if (
|
||||
($value.StartsWith('"') -and $value.EndsWith('"')) -or
|
||||
($value.StartsWith("'") -and $value.EndsWith("'"))
|
||||
) {
|
||||
$value = $value.Substring(1, $value.Length - 2)
|
||||
}
|
||||
|
||||
[System.Environment]::SetEnvironmentVariable($key, $value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Test-Path $env:MachineEnvFile) {
|
||||
Set-EnvFromFile $env:MachineEnvFile
|
||||
}
|
||||
|
||||
if (-not $env:INFISICAL_CLIENT_ID) {
|
||||
Write-Error "INFISICAL_CLIENT_ID is not set."
|
||||
exit 1
|
||||
}
|
||||
|
||||
if (-not $env:INFISICAL_CLIENT_SECRET) {
|
||||
Write-Error "INFISICAL_CLIENT_SECRET is not set."
|
||||
exit 1
|
||||
}
|
||||
|
||||
if (-not $env:INFISICAL_PROJECT_ID) {
|
||||
Write-Error "INFISICAL_PROJECT_ID is not set."
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ($UsePm2) {
|
||||
pm2 start $MainScript --name $Pm2Name --update-env
|
||||
} else {
|
||||
node $MainScript
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
INFISICAL_PROJECT_ID=26a80d59-a424-4e33-adfc-2b6e84d6e25f
|
||||
|
||||
SERVICE_NAME=lolympus-tournament-support
|
||||
VERSION=0.1
|
||||
Reference in new issue
Block a user