@platformatic/service 1.10.0 → 1.12.0

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/help/compile.txt CHANGED
@@ -7,6 +7,8 @@ Compile typescript plugins.
7
7
  As a result of executing this command, Platformatic Service will compile typescript
8
8
  plugins in the `outDir` directory.
9
9
 
10
+ Using the `--clean` flag, the outDir directory will be removed before the new compilation process starts.
11
+
10
12
  If not specified, the configuration will be loaded from any of the following, in the current directory.
11
13
 
12
14
  * `platformatic.service.json`, or
package/index.d.ts CHANGED
@@ -38,8 +38,11 @@ interface SchemaExport {
38
38
  schema: JSONSchemaType<PlatformaticServiceConfig>
39
39
  }
40
40
 
41
+ interface TSCompilerOptions {
42
+ clean: boolean
43
+ }
41
44
  interface TSCompiler {
42
- compile: (cwd: string, config: object, originalLogger: FastifyBaseLogger) => Promise<boolean>
45
+ compile: (cwd: string, config: object, originalLogger: FastifyBaseLogger, options: TSCompilerOptions) => Promise<boolean>
43
46
  }
44
47
  export const schema: SchemaExport
45
48
 
package/lib/compile.js CHANGED
@@ -5,6 +5,7 @@ const pino = require('pino')
5
5
  const pretty = require('pino-pretty')
6
6
  const { loadConfig } = require('@platformatic/config')
7
7
  const { isFileAccessible } = require('./utils.js')
8
+ const { readFile, rm } = require('fs/promises')
8
9
 
9
10
  async function getTSCExecutablePath (cwd) {
10
11
  const typescriptPath = require.resolve('typescript')
@@ -62,7 +63,7 @@ async function setup (cwd, config, logger) {
62
63
  return { execa, logger, tscExecutablePath, tsConfigPath, tsConfigExists }
63
64
  }
64
65
 
65
- async function compile (cwd, config, originalLogger) {
66
+ async function compile (cwd, config, originalLogger, options) {
66
67
  const { execa, logger, tscExecutablePath, tsConfigPath, tsConfigExists } = await setup(cwd, config, originalLogger)
67
68
  /* c8 ignore next 3 */
68
69
  if (!tscExecutablePath || !tsConfigExists) {
@@ -75,7 +76,18 @@ async function compile (cwd, config, originalLogger) {
75
76
  ...process.env
76
77
  }
77
78
  delete env.NODE_V8_COVERAGE
78
-
79
+ // somehow c8 does not pick up these lines even if there is a specific test
80
+ /* c8 ignore 10 */
81
+ if (options.clean) {
82
+ // delete outdir directory
83
+ const tsConfigContents = JSON.parse(await readFile(tsConfigPath, 'utf8'))
84
+ const outDir = tsConfigContents.compilerOptions.outDir
85
+ if (outDir) {
86
+ const outDirFullPath = join(dirname(tsConfigPath), outDir)
87
+ originalLogger.info(`Removing build directory ${outDirFullPath}`)
88
+ await rm(outDirFullPath, { recursive: true })
89
+ }
90
+ }
79
91
  await execa(tscExecutablePath, tsFlags, { cwd, env })
80
92
  logger.info('Typescript compilation completed successfully.')
81
93
  return true
@@ -101,8 +113,17 @@ function buildCompileCmd (app) {
101
113
  console.error(err)
102
114
  process.exit(1)
103
115
  }
116
+ const compileOptions = {
117
+ clean: _args.includes('--clean')
118
+ }
119
+ const logger = pino(
120
+ pretty({
121
+ translateTime: 'SYS:HH:MM:ss',
122
+ ignore: 'hostname,pid'
123
+ })
124
+ )
104
125
 
105
- if (!await compile(fullPath, config)) {
126
+ if (!await compile(fullPath, config, logger, compileOptions)) {
106
127
  process.exit(1)
107
128
  }
108
129
  }
package/lib/gen-types.mjs CHANGED
@@ -28,8 +28,6 @@ async function generateTypes (_args) {
28
28
  ignore: 'hostname,pid'
29
29
  }))
30
30
  const { configManager, args } = await loadConfig({}, _args, platformaticService)
31
-
32
- console.log('antanis', new Error().stack)
33
31
  await configManager.parseAndValidate()
34
32
  const config = configManager.current
35
33
 
@@ -19,7 +19,7 @@ async function setupOpenAPI (app, opts) {
19
19
  }
20
20
  }, typeof opts === 'object' ? opts : {})
21
21
  app.log.trace({ openapi: openapiConfig })
22
- await app.register(Swagger, {
22
+ const swaggerOptions = {
23
23
  exposeRoute: openapiConfig.exposeRoute,
24
24
  openapi: {
25
25
  ...openapiConfig
@@ -31,7 +31,15 @@ async function setupOpenAPI (app, opts) {
31
31
  return json.$id || `def-${i}`
32
32
  }
33
33
  }
34
- })
34
+ }
35
+
36
+ if (opts.path) {
37
+ swaggerOptions.mode = 'static'
38
+ swaggerOptions.specification = {
39
+ path: opts.path
40
+ }
41
+ }
42
+ await app.register(Swagger, swaggerOptions)
35
43
 
36
44
  const { default: theme } = await import('@platformatic/swagger-ui-theme')
37
45
  app.register(SwaggerUI, {
@@ -3,11 +3,10 @@
3
3
  const { join, resolve } = require('path')
4
4
  const { readFile } = require('fs/promises')
5
5
  const fp = require('fastify-plugin')
6
+ const wrapper = require('./sandbox-wrapper')
6
7
 
7
8
  const { getJSPluginPath, isFileAccessible } = require('../utils')
8
9
 
9
- const wrapperPath = join(__dirname, 'sandbox-wrapper.js')
10
-
11
10
  async function loadPlugins (app) {
12
11
  const configManager = app.platformatic.configManager
13
12
  const config = configManager.current
@@ -45,7 +44,7 @@ async function loadPlugins (app) {
45
44
  })
46
45
  }
47
46
 
48
- await app.register(require(wrapperPath), { paths: config.plugins.paths })
47
+ await app.register(wrapper, { packages: config.plugins.packages, paths: config.plugins.paths })
49
48
  }
50
49
 
51
50
  module.exports = fp(loadPlugins)
@@ -2,14 +2,26 @@
2
2
 
3
3
  const fp = require('fastify-plugin')
4
4
  const autoload = require('@fastify/autoload')
5
- const { stat } = require('fs').promises
5
+ const { stat } = require('node:fs').promises
6
+ const { createRequire } = require('node:module')
7
+ const { join } = require('node:path')
8
+ const { pathToFileURL } = require('node:url')
6
9
 
7
10
  module.exports = fp(async function (app, opts) {
8
- for (let plugin of opts.paths) {
11
+ // fake require next to the configManager dirname
12
+ const _require = createRequire(join(app.platformatic.configManager.dirname, 'package.json'))
13
+ for (const plugin of opts.packages || []) {
14
+ const name = typeof plugin === 'string' ? plugin : plugin.name
15
+ const url = pathToFileURL(_require.resolve(name))
16
+ const loaded = await import(url)
17
+ await app.register(loaded, plugin.options)
18
+ }
19
+
20
+ for (let plugin of opts.paths || []) {
9
21
  if (typeof plugin === 'string') {
10
22
  plugin = { path: plugin, encapsulate: true }
11
23
  }
12
- if ((await stat(plugin.path)).isDirectory()) {
24
+ if (plugin.path && (await stat(plugin.path)).isDirectory()) {
13
25
  const patternOptions = patternOptionsFromPlugin(plugin)
14
26
 
15
27
  app.register(autoload, {
@@ -27,7 +39,7 @@ module.exports = fp(async function (app, opts) {
27
39
  ...patternOptions
28
40
  })
29
41
  } else {
30
- let loaded = await import(`file://${plugin.path}`)
42
+ let loaded = await import(pathToFileURL(plugin.path))
31
43
  /* c8 ignore next 3 */
32
44
  if (loaded.__esModule === true || typeof loaded.default === 'function') {
33
45
  loaded = loaded.default
@@ -56,7 +68,11 @@ function patternOptionsFromPlugin (plugin) {
56
68
  const config = {}
57
69
 
58
70
  // Expected keys for autoload plugin options that expect regexp patterns
59
- const patternOptionKeys = ['ignorePattern', 'indexPattern', 'autoHooksPattern']
71
+ const patternOptionKeys = [
72
+ 'ignorePattern',
73
+ 'indexPattern',
74
+ 'autoHooksPattern'
75
+ ]
60
76
 
61
77
  for (const key of patternOptionKeys) {
62
78
  const pattern = plugin[key]
@@ -10,7 +10,7 @@ async function setupTsCompiler (app) {
10
10
  const config = configManager.current
11
11
  const workingDir = configManager.dirname
12
12
 
13
- await compiler.compile(workingDir, config, app.log)
13
+ await compiler.compile(workingDir, config, app.log, { clean: false })
14
14
  }
15
15
 
16
16
  module.exports = fp(setupTsCompiler)
package/lib/schema.js CHANGED
@@ -422,6 +422,26 @@ const plugins = {
422
422
  $id: '#plugins',
423
423
  type: 'object',
424
424
  properties: {
425
+ packages: {
426
+ type: 'array',
427
+ items: {
428
+ anyOf: [{
429
+ type: 'string'
430
+ }, {
431
+ type: 'object',
432
+ properties: {
433
+ name: {
434
+ type: 'string'
435
+ },
436
+ options: {
437
+ type: 'object',
438
+ additionalProperties: true
439
+ }
440
+ },
441
+ required: ['name']
442
+ }]
443
+ }
444
+ },
425
445
  paths: {
426
446
  type: 'array',
427
447
  items: {
@@ -517,7 +537,11 @@ const plugins = {
517
537
  }
518
538
  },
519
539
  additionalProperties: false,
520
- required: ['paths']
540
+ anyOf: [{
541
+ required: ['paths']
542
+ }, {
543
+ required: ['packages']
544
+ }]
521
545
  }
522
546
 
523
547
  const metrics = {
@@ -600,6 +624,11 @@ const openApiBase = {
600
624
  prefix: {
601
625
  type: 'string',
602
626
  description: 'Base URL for the OpenAPI'
627
+ },
628
+ path: {
629
+ type: 'string',
630
+ description: 'Path to an OpenAPI spec file',
631
+ resolvePath: true
603
632
  }
604
633
  }
605
634
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/service",
3
- "version": "1.10.0",
3
+ "version": "1.12.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -18,6 +18,7 @@
18
18
  "homepage": "https://github.com/platformatic/platformatic#readme",
19
19
  "devDependencies": {
20
20
  "@fastify/aws-lambda": "^3.3.0",
21
+ "@fastify/compress": "^6.5.0",
21
22
  "bindings": "^1.5.0",
22
23
  "c8": "^8.0.1",
23
24
  "glob": "^10.3.10",
@@ -68,11 +69,11 @@
68
69
  "pino-pretty": "^10.2.0",
69
70
  "rfdc": "^1.3.0",
70
71
  "ua-parser-js": "^1.0.36",
71
- "@platformatic/client": "1.10.0",
72
- "@platformatic/swagger-ui-theme": "1.10.0",
73
- "@platformatic/telemetry": "1.10.0",
74
- "@platformatic/config": "1.10.0",
75
- "@platformatic/utils": "1.10.0"
72
+ "@platformatic/client": "1.12.0",
73
+ "@platformatic/config": "1.12.0",
74
+ "@platformatic/swagger-ui-theme": "1.12.0",
75
+ "@platformatic/telemetry": "1.12.0",
76
+ "@platformatic/utils": "1.12.0"
76
77
  },
77
78
  "standard": {
78
79
  "ignore": [