@platformatic/watt-extra 1.14.0 → 1.14.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli.js CHANGED
@@ -1,12 +1,12 @@
1
1
  #!/usr/bin/env node
2
2
  import commist from 'commist'
3
- import minimist from 'minimist'
4
- import { resolve, join, dirname } from 'node:path'
3
+ import { join, dirname } from 'node:path'
5
4
  import { fileURLToPath } from 'node:url'
6
5
  import helpMeInit from 'help-me'
7
6
  import { readFileSync } from 'node:fs'
8
7
  import { start, logger } from './index.js'
9
8
  import { getSimpleBanner } from './lib/banner.js'
9
+ import { applyStartArgs } from './lib/start-args.js'
10
10
 
11
11
  const __filename = fileURLToPath(import.meta.url)
12
12
  const __dirname = dirname(__filename)
@@ -35,46 +35,15 @@ async function startCommand (argv) {
35
35
  logger.info(`WattExtra v${pkg.version}`)
36
36
  }
37
37
 
38
- const args = minimist(argv, {
39
- alias: {
40
- h: 'help',
41
- l: 'log-level',
42
- i: 'icc-url',
43
- a: 'app-name',
44
- d: 'app-dir'
45
- },
46
- boolean: ['help'],
47
- string: ['log-level', 'icc-url', 'app-name', 'app-dir'],
48
- default: {
49
- 'log-level': 'info',
50
- 'app-dir': process.cwd()
51
- }
52
- })
38
+ const { args, help } = applyStartArgs(argv)
53
39
 
54
40
  logger.debug({ args, argv }, 'Start command arguments')
55
41
 
56
- if (args.help) {
42
+ if (help) {
57
43
  helpMe.toStdout('start')
58
44
  return true
59
45
  }
60
46
 
61
- // Set environment variables based on CLI options
62
- if (args['log-level']) {
63
- process.env.PLT_LOG_LEVEL = args['log-level']
64
- }
65
-
66
- if (args['icc-url']) {
67
- process.env.PLT_ICC_URL = args['icc-url']
68
- }
69
-
70
- if (args['app-name']) {
71
- process.env.PLT_APP_NAME = args['app-name']
72
- }
73
-
74
- if (args['app-dir']) {
75
- process.env.PLT_APP_DIR = resolve(args['app-dir']) // Ensure the path is absolute
76
- }
77
-
78
47
  await start()
79
48
  return true
80
49
  }
@@ -0,0 +1,47 @@
1
+ import minimist from 'minimist'
2
+ import { resolve } from 'node:path'
3
+
4
+ // Parse start command argv and apply CLI options to env.
5
+ // Precedence for PLT_LOG_LEVEL: --log-level flag > pre-set env > 'info' default.
6
+ // Do NOT add a minimist default for `log-level`: that would make args['log-level']
7
+ // always truthy and unconditionally clobber an env-provided value.
8
+ export function applyStartArgs (argv, env = process.env) {
9
+ const args = minimist(argv, {
10
+ alias: {
11
+ h: 'help',
12
+ l: 'log-level',
13
+ i: 'icc-url',
14
+ a: 'app-name',
15
+ d: 'app-dir'
16
+ },
17
+ boolean: ['help'],
18
+ string: ['log-level', 'icc-url', 'app-name', 'app-dir'],
19
+ default: {
20
+ 'app-dir': process.cwd()
21
+ }
22
+ })
23
+
24
+ if (args.help) {
25
+ return { args, help: true }
26
+ }
27
+
28
+ if (args['log-level']) {
29
+ env.PLT_LOG_LEVEL = args['log-level']
30
+ } else if (!env.PLT_LOG_LEVEL) {
31
+ env.PLT_LOG_LEVEL = 'info'
32
+ }
33
+
34
+ if (args['icc-url']) {
35
+ env.PLT_ICC_URL = args['icc-url']
36
+ }
37
+
38
+ if (args['app-name']) {
39
+ env.PLT_APP_NAME = args['app-name']
40
+ }
41
+
42
+ if (args['app-dir']) {
43
+ env.PLT_APP_DIR = resolve(args['app-dir']) // Ensure the path is absolute
44
+ }
45
+
46
+ return { args, help: false }
47
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/watt-extra",
3
- "version": "1.14.0",
3
+ "version": "1.14.3",
4
4
  "description": "The Platformatic runtime manager",
5
5
  "type": "module",
6
6
  "scripts": {
package/test/cli.test.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import assert from 'node:assert'
2
2
  import { test } from 'node:test'
3
3
  import { spawn } from 'node:child_process'
4
- import { join, dirname } from 'node:path'
4
+ import { join, resolve, dirname } from 'node:path'
5
5
  import { fileURLToPath } from 'node:url'
6
+ import { applyStartArgs } from '../lib/start-args.js'
6
7
 
7
8
  const __filename = fileURLToPath(import.meta.url)
8
9
  const __dirname = dirname(__filename)
@@ -90,3 +91,51 @@ test('CLI should output JSON log without banner in non-TTY mode (version command
90
91
  'Non-TTY output should include JSON log with version'
91
92
  )
92
93
  })
94
+
95
+ test('applyStartArgs: --log-level flag overrides env-provided PLT_LOG_LEVEL', () => {
96
+ const env = { PLT_LOG_LEVEL: 'warn' }
97
+ applyStartArgs(['--log-level', 'debug'], env)
98
+ assert.strictEqual(env.PLT_LOG_LEVEL, 'debug')
99
+ })
100
+
101
+ test('applyStartArgs: -l short flag also overrides env', () => {
102
+ const env = { PLT_LOG_LEVEL: 'warn' }
103
+ applyStartArgs(['-l', 'trace'], env)
104
+ assert.strictEqual(env.PLT_LOG_LEVEL, 'trace')
105
+ })
106
+
107
+ // Regression test: cli.js used to have `default: { 'log-level': 'info' }` in its
108
+ // minimist config, which populated args['log-level'] with 'info' whenever the flag
109
+ // was not passed. The subsequent unconditional write to process.env.PLT_LOG_LEVEL
110
+ // then silently clobbered the value provided by the deployment environment (e.g.
111
+ // PLT_LOG_LEVEL=warn from k8s), forcing the runtime to log at info regardless.
112
+ test('applyStartArgs: preserves env-provided PLT_LOG_LEVEL when --log-level is not passed', () => {
113
+ const env = { PLT_LOG_LEVEL: 'warn' }
114
+ applyStartArgs([], env)
115
+ assert.strictEqual(env.PLT_LOG_LEVEL, 'warn')
116
+ })
117
+
118
+ test('applyStartArgs: defaults PLT_LOG_LEVEL to "info" when neither env nor flag is set', () => {
119
+ const env = {}
120
+ applyStartArgs([], env)
121
+ assert.strictEqual(env.PLT_LOG_LEVEL, 'info')
122
+ })
123
+
124
+ test('applyStartArgs: passes through other CLI options to env', () => {
125
+ const env = {}
126
+ applyStartArgs([
127
+ '--icc-url', 'http://icc.example',
128
+ '--app-name', 'my-app',
129
+ '--app-dir', 'test/fixtures/runtime-service'
130
+ ], env)
131
+ assert.strictEqual(env.PLT_ICC_URL, 'http://icc.example')
132
+ assert.strictEqual(env.PLT_APP_NAME, 'my-app')
133
+ assert.strictEqual(env.PLT_APP_DIR, resolve('test/fixtures/runtime-service'))
134
+ })
135
+
136
+ test('applyStartArgs: --help short-circuits without mutating env', () => {
137
+ const env = {}
138
+ const res = applyStartArgs(['--help'], env)
139
+ assert.strictEqual(res.help, true)
140
+ assert.strictEqual(env.PLT_LOG_LEVEL, undefined)
141
+ })