@platformatic/basic 2.25.0 → 2.26.0-alpha.1

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/index.js CHANGED
@@ -37,7 +37,7 @@ function isImportFailedError (error, pkg) {
37
37
  return match?.[1] === pkg || error.requireStack?.[0].endsWith(importStackablePackageMarker)
38
38
  }
39
39
 
40
- async function importStackablePackage (opts, pkg) {
40
+ async function importStackablePackage (directory, pkg) {
41
41
  try {
42
42
  try {
43
43
  // Try regular import
@@ -48,7 +48,7 @@ async function importStackablePackage (opts, pkg) {
48
48
  }
49
49
 
50
50
  // Scope to the service
51
- const require = createRequire(resolve(opts.context.directory, importStackablePackageMarker))
51
+ const require = createRequire(resolve(directory, importStackablePackageMarker))
52
52
  const imported = require.resolve(pkg)
53
53
  return await importFile(imported)
54
54
  }
@@ -57,16 +57,15 @@ async function importStackablePackage (opts, pkg) {
57
57
  throw e
58
58
  }
59
59
 
60
- const serviceDirectory = relative(workerData.dirname, opts.context.directory)
60
+ const serviceDirectory = workerData ? relative(workerData.dirname, directory) : directory
61
61
  throw new Error(
62
62
  `Unable to import package '${pkg}'. Please add it as a dependency in the package.json file in the folder ${serviceDirectory}.`
63
63
  )
64
64
  }
65
65
  }
66
66
 
67
- async function buildStackable (opts) {
68
- const root = opts.context.directory
69
- let toImport = '@platformatic/node'
67
+ export async function importStackableAndConfig (root, config) {
68
+ let moduleName = '@platformatic/node'
70
69
  let autodetectDescription = 'is using a generic Node.js application'
71
70
 
72
71
  let rootPackageJson
@@ -76,14 +75,12 @@ async function buildStackable (opts) {
76
75
  rootPackageJson = {}
77
76
  }
78
77
 
79
- const hadConfig = opts.config
80
-
81
- if (!hadConfig) {
78
+ if (!config) {
82
79
  for (const candidate of configCandidates) {
83
80
  const candidatePath = resolve(root, candidate)
84
81
 
85
82
  if (existsSync(candidatePath)) {
86
- opts.config = candidatePath
83
+ config = candidatePath
87
84
  break
88
85
  }
89
86
  }
@@ -93,19 +90,27 @@ async function buildStackable (opts) {
93
90
 
94
91
  if (dependencies?.next || devDependencies?.next) {
95
92
  autodetectDescription = 'is using Next.js'
96
- toImport = '@platformatic/next'
93
+ moduleName = '@platformatic/next'
97
94
  } else if (dependencies?.['@remix-run/dev'] || devDependencies?.['@remix-run/dev']) {
98
95
  autodetectDescription = 'is using Remix'
99
- toImport = '@platformatic/remix'
96
+ moduleName = '@platformatic/remix'
100
97
  } else if (dependencies?.vite || devDependencies?.vite) {
101
98
  autodetectDescription = 'is using Vite'
102
- toImport = '@platformatic/vite'
99
+ moduleName = '@platformatic/vite'
103
100
  } else if (dependencies?.astro || devDependencies?.astro) {
104
101
  autodetectDescription = 'is using Astro'
105
- toImport = '@platformatic/astro'
102
+ moduleName = '@platformatic/astro'
106
103
  }
107
104
 
108
- const imported = await importStackablePackage(opts, toImport)
105
+ const stackable = await importStackablePackage(root, moduleName)
106
+
107
+ return { stackable, config, autodetectDescription, moduleName }
108
+ }
109
+
110
+ async function buildStackable (opts) {
111
+ const hadConfig = !!opts.config
112
+ const { stackable, config, autodetectDescription, moduleName } = await importStackableAndConfig(opts.context.directory, opts.config)
113
+ opts.config = config
109
114
 
110
115
  const serviceRoot = relative(process.cwd(), opts.context.directory)
111
116
  if (
@@ -122,12 +127,12 @@ async function buildStackable (opts) {
122
127
  [
123
128
  `Platformatic has auto-detected that service "${opts.context.serviceId}" ${autodetectDescription}.\n`,
124
129
  `We suggest you create a platformatic.json or watt.json file in the folder ${serviceRoot} with the "$schema" `,
125
- `property set to "https://schemas.platformatic.dev/${toImport}/${packageJson.version}.json".`
130
+ `property set to "https://schemas.platformatic.dev/${moduleName}/${packageJson.version}.json".`
126
131
  ].join('')
127
132
  )
128
133
  }
129
134
 
130
- return imported.buildStackable(opts)
135
+ return stackable.buildStackable(opts)
131
136
  }
132
137
 
133
138
  /* c8 ignore next 3 */
package/lib/base.js CHANGED
@@ -160,25 +160,31 @@ export class BaseStackable {
160
160
  }
161
161
  }
162
162
 
163
- async buildWithCommand (command, basePath, loader, scripts) {
163
+ async buildWithCommand (command, basePath, opts = {}) {
164
+ const { loader, scripts, context, disableChildManager } = opts
165
+
164
166
  if (Array.isArray(command)) {
165
167
  command = command.join(' ')
166
168
  }
167
169
 
168
170
  this.logger.debug(`Executing "${command}" ...`)
169
171
 
170
- const context = await this.#getChildManagerContext(basePath)
171
- this.childManager = new ChildManager({
172
- logger: this.logger,
173
- loader,
174
- scripts,
175
- context: { ...context, isBuilding: true }
176
- })
172
+ const baseContext = await this.getChildManagerContext(basePath)
173
+ this.childManager = disableChildManager
174
+ ? null
175
+ : new ChildManager({
176
+ logger: this.logger,
177
+ loader,
178
+ scripts,
179
+ context: { ...baseContext, isBuilding: true, ...context }
180
+ })
177
181
 
178
182
  try {
179
- await this.childManager.inject()
183
+ await this.childManager?.inject()
180
184
 
181
185
  const subprocess = this.spawn(command)
186
+ subprocess.stdout.setEncoding('utf8')
187
+ subprocess.stderr.setEncoding('utf8')
182
188
 
183
189
  // Wait for the process to be started
184
190
  await new Promise((resolve, reject) => {
@@ -205,8 +211,8 @@ export class BaseStackable {
205
211
  throw error
206
212
  }
207
213
  } finally {
208
- await this.childManager.eject()
209
- await this.childManager.close()
214
+ await this.childManager?.eject()
215
+ await this.childManager?.close()
210
216
  }
211
217
  }
212
218
 
@@ -214,7 +220,7 @@ export class BaseStackable {
214
220
  const config = this.configManager.current
215
221
  const basePath = config.application?.basePath ? cleanBasePath(config.application?.basePath) : ''
216
222
 
217
- const context = await this.#getChildManagerContext(basePath)
223
+ const context = await this.getChildManagerContext(basePath)
218
224
  this.childManager = new ChildManager({
219
225
  logger: this.logger,
220
226
  loader,
@@ -387,7 +393,7 @@ export class BaseStackable {
387
393
  }
388
394
  }
389
395
 
390
- async #getChildManagerContext (basePath) {
396
+ async getChildManagerContext (basePath) {
391
397
  const meta = await this.getMeta()
392
398
 
393
399
  return {
@@ -405,7 +411,8 @@ export class BaseStackable {
405
411
  /* c8 ignore next 2 */
406
412
  port: (this.isEntrypoint ? this.serverConfig?.port || 0 : undefined) ?? true,
407
413
  host: (this.isEntrypoint ? this.serverConfig?.hostname : undefined) ?? true,
408
- telemetryConfig: this.telemetryConfig
414
+ telemetryConfig: this.telemetryConfig,
415
+ interceptLogging: typeof workerData?.loggingPort !== 'undefined'
409
416
  }
410
417
  }
411
418
  }
@@ -205,7 +205,7 @@ export class ChildProcess extends ITC {
205
205
  }
206
206
  }
207
207
 
208
- if (isMainThread) {
208
+ if (isMainThread && globalThis.platformatic.interceptLogging) {
209
209
  pinoOptions.transport = {
210
210
  target: new URL('./child-transport.js', import.meta.url).toString()
211
211
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/basic",
3
- "version": "2.25.0",
3
+ "version": "2.26.0-alpha.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -23,11 +23,11 @@
23
23
  "split2": "^4.2.0",
24
24
  "undici": "^7.0.0",
25
25
  "ws": "^8.18.0",
26
- "@platformatic/config": "2.25.0",
27
- "@platformatic/metrics": "2.25.0",
28
- "@platformatic/itc": "2.25.0",
29
- "@platformatic/telemetry": "2.25.0",
30
- "@platformatic/utils": "2.25.0"
26
+ "@platformatic/config": "2.26.0-alpha.1",
27
+ "@platformatic/metrics": "2.26.0-alpha.1",
28
+ "@platformatic/telemetry": "2.26.0-alpha.1",
29
+ "@platformatic/utils": "2.26.0-alpha.1",
30
+ "@platformatic/itc": "2.26.0-alpha.1"
31
31
  },
32
32
  "devDependencies": {
33
33
  "borp": "^0.19.0",
package/schema.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$id": "https://schemas.platformatic.dev/@platformatic/basic/2.25.0.json",
2
+ "$id": "https://schemas.platformatic.dev/@platformatic/basic/2.26.0-alpha.1.json",
3
3
  "$schema": "http://json-schema.org/draft-07/schema#",
4
4
  "title": "Platformatic Stackable",
5
5
  "type": "object",