@@ -8,63 +8,64 @@ import { getDeploymentName } from '../../domain/pm2/apps.js';
88
99export async function cmdEnv (
1010 cwd : string ,
11- options : { file ?: string ; config ?: string } ,
11+ options : { file ?: string ; config ?: string ; app ?: string } ,
1212) : Promise < void > {
1313 await runRemoteCommand (
1414 cwd ,
1515 async ( { config, executor } ) => {
16- const app = getActiveApp ( config ) ;
17- const envFile = options . file ?? app . envFile ;
18- const localEnvPath = resolve ( cwd , envFile ) ;
16+ const apps = options . app
17+ ? [ getActiveApp ( config , options . app ) ]
18+ : config . apps ;
1919
20- if ( ! ( await pathExists ( localEnvPath ) ) ) {
21- throw new Error ( `Environment file not found: ${ envFile } ` ) ;
22- }
23-
24- ui . info ( `Uploading ${ envFile } to server...` ) ;
20+ for ( const app of apps ) {
21+ const envFile = options . file ?? app . envFile ;
22+ const localEnvPath = resolve ( cwd , envFile ) ;
2523
26- const content = await readFile ( localEnvPath ) ;
27- const b64 = content . toString ( 'base64' ) ;
24+ if ( ! ( await pathExists ( localEnvPath ) ) ) {
25+ throw new Error ( `Environment file not found: ${ envFile } ` ) ;
26+ }
2827
29- // Store with the configured envFile name so the PM2 ecosystem reference
30- // (`shared/${envFile}`) and the workDir symlink target stay consistent.
31- // Maintain a `.env` alias too — older configs and any external scripts
32- // that read `shared/.env` keep working.
33- const sharedEnv = `${ config . remotePath } /shared/${ app . envFile } ` ;
34- const sharedEnvAlias = `${ config . remotePath } /shared/.env` ;
35- await executor . exec ( `mkdir -p "${ config . remotePath } /shared"` ) ;
36- await executor . exec ( `echo "${ b64 } " | base64 -d > "${ sharedEnv } "` ) ;
37- await executor . exec ( `chmod 600 "${ sharedEnv } "` ) ;
38- if ( app . envFile !== '.env' ) {
39- await executor . exec ( `ln -sf "${ sharedEnv } " "${ sharedEnvAlias } "` ) ;
40- }
41- ui . success ( `Uploaded to ${ sharedEnv } ` ) ;
28+ ui . info ( `Uploading ${ envFile } for app '${ app . name } '...` ) ;
4229
43- const linkResult = await executor . exec (
44- `if [ -d "${ config . remotePath } /current" ]; then ` +
45- `ln -sfn "${ sharedEnv } " "${ config . remotePath } /current/.env" && echo "linked"; ` +
46- `fi` ,
47- ) ;
48- if ( linkResult . stdout === 'linked' ) {
49- ui . success ( 'Linked shared .env to current release' ) ;
50- }
30+ const content = await readFile ( localEnvPath ) ;
31+ const b64 = content . toString ( 'base64' ) ;
32+ const appPath = `${ config . remotePath } /${ app . name } ` ;
33+ const sharedEnv = `${ appPath } /shared/${ app . envFile } ` ;
34+ const sharedEnvAlias = `${ appPath } /shared/.env` ;
35+ await executor . exec ( `mkdir -p "${ appPath } /shared"` ) ;
36+ await executor . exec ( `echo "${ b64 } " | base64 -d > "${ sharedEnv } "` ) ;
37+ await executor . exec ( `chmod 600 "${ sharedEnv } "` ) ;
38+ if ( app . envFile !== '.env' ) {
39+ await executor . exec ( `ln -sf "${ sharedEnv } " "${ sharedEnvAlias } "` ) ;
40+ }
41+ ui . success ( `Uploaded to ${ sharedEnv } ` ) ;
5142
52- const namespace = getDeploymentName ( config ) ;
53- if ( app . appType === 'backend' && namespace ) {
54- const nodeVersion = config . nodeVersion === 'lts' ? '24' : config . nodeVersion ;
55- const mise = `export PATH="$HOME/.local/bin:$HOME/.local/share/mise/shims:$PATH"` ;
56- const checkResult = await executor . exec (
57- `${ mise } ; mise exec node@${ nodeVersion } -- pm2 describe ${ namespace } 2>/dev/null && echo "running" || echo "stopped"` ,
43+ const linkResult = await executor . exec (
44+ `if [ -d "${ appPath } /current" ]; then ` +
45+ `ln -sfn "${ sharedEnv } " "${ appPath } /current/.env" && echo "linked"; ` +
46+ `fi` ,
5847 ) ;
48+ if ( linkResult . stdout === 'linked' ) {
49+ ui . success ( 'Linked shared .env to current release' ) ;
50+ }
5951
60- if ( checkResult . stdout . includes ( 'running' ) ) {
61- ui . info ( 'Reloading deployment to pick up environment variables...' ) ;
62- await executor . exec (
63- `${ mise } ; mise exec node@${ nodeVersion } -- pm2 reload ${ namespace } --update-env` ,
52+ const namespace = getDeploymentName ( { ...config , apps : [ app ] } as any ) ;
53+ if ( app . appType === 'backend' && namespace ) {
54+ const nodeVersion = config . nodeVersion === 'lts' ? '24' : config . nodeVersion ;
55+ const mise = `export PATH="$HOME/.local/bin:$HOME/.local/share/mise/shims:$PATH"` ;
56+ const checkResult = await executor . exec (
57+ `${ mise } ; mise exec node@${ nodeVersion } -- pm2 describe ${ namespace } 2>/dev/null && echo "running" || echo "stopped"` ,
6458 ) ;
65- ui . success ( 'Deployment reloaded with new environment variables' ) ;
66- } else {
67- ui . warn ( 'Deployment not running. Variables will be loaded on next deploy.' ) ;
59+
60+ if ( checkResult . stdout . includes ( 'running' ) ) {
61+ ui . info ( 'Reloading deployment to pick up environment variables...' ) ;
62+ await executor . exec (
63+ `${ mise } ; mise exec node@${ nodeVersion } -- pm2 reload ${ namespace } --update-env` ,
64+ ) ;
65+ ui . success ( 'Deployment reloaded with new environment variables' ) ;
66+ } else {
67+ ui . warn ( 'Deployment not running. Variables will be loaded on next deploy.' ) ;
68+ }
6869 }
6970 }
7071 } ,
0 commit comments