@platformatic/service 0.43.0 → 0.44.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/index.d.ts CHANGED
@@ -1,18 +1,17 @@
1
1
  /* eslint-disable @typescript-eslint/triple-slash-reference */
2
- /// <reference types="@platformatic/types" />
3
2
  /// <reference types="mercurius" />
4
3
  /// <reference types="@fastify/swagger" />
5
4
  import { FastifyInstance } from 'fastify'
6
5
  import ConfigManager from '@platformatic/config'
7
6
  import { PlatformaticService } from './config'
8
7
 
9
- declare module '@platformatic/types' {
10
- interface PlatformaticApp {
11
- configManager: ConfigManager<PlatformaticService>
12
- config: PlatformaticService
13
- }
8
+ export interface PlatformaticApp<T> {
9
+ configManager: ConfigManager<T>
10
+ config: T
14
11
  }
15
12
 
13
+ export type PlatformaticServiceConfig = PlatformaticService
14
+
16
15
  export function buildServer (opts: object, app?: object, ConfigManagerContructor?: object): Promise<FastifyInstance>
17
16
 
18
17
  declare module 'fastify' {
package/index.test-d.ts CHANGED
@@ -1,11 +1,17 @@
1
1
  import { expectType } from 'tsd'
2
2
  import { FastifyInstance } from 'fastify'
3
- import { buildServer } from '.'
3
+ import { buildServer, PlatformaticApp } from '.'
4
4
  import ConfigManager from '@platformatic/config'
5
5
  import { OpenAPI } from 'openapi-types'
6
6
  import type { MercuriusPlugin } from 'mercurius'
7
7
  import { PlatformaticService } from './config'
8
8
 
9
+ declare module 'fastify' {
10
+ interface FastifyInstance {
11
+ platformatic: PlatformaticApp<PlatformaticService>
12
+ }
13
+ }
14
+
9
15
  const server = await buildServer({
10
16
  })
11
17
 
@@ -0,0 +1,40 @@
1
+ import { join, dirname } from 'path'
2
+ import { createRequire } from 'module'
3
+ import { writeFile } from 'fs/promises'
4
+ import pino from 'pino'
5
+ import pretty from 'pino-pretty'
6
+ import { loadConfig } from '@platformatic/config'
7
+ import { platformaticService } from '../index.js'
8
+ import { checkForDependencies } from '@platformatic/utils'
9
+
10
+ const GLOBAL_TYPES_TEMPLATE = `
11
+ import { FastifyInstance } from 'fastify'
12
+ import { PlatformaticApp, PlatformaticServiceConfig } from '@platformatic/service'
13
+
14
+ declare module 'fastify' {
15
+ interface FastifyInstance {
16
+ platformatic: PlatformaticApp<PlatformaticServiceConfig>
17
+ }
18
+ }
19
+ `
20
+ async function execute ({ logger, configManager }) {
21
+ const fileNameOrThen = join(dirname(configManager.fullPath), 'global.d.ts')
22
+ await writeFile(fileNameOrThen, GLOBAL_TYPES_TEMPLATE)
23
+ }
24
+
25
+ async function generateTypes (_args) {
26
+ const logger = pino(pretty({
27
+ translateTime: 'SYS:HH:MM:ss',
28
+ ignore: 'hostname,pid'
29
+ }))
30
+
31
+ const { configManager, args } = await loadConfig({}, _args, platformaticService)
32
+
33
+ await configManager.parseAndValidate()
34
+ const config = configManager.current
35
+
36
+ await execute({ logger, configManager })
37
+ await checkForDependencies(logger, args, createRequire(import.meta.url), config, ['@platformatic/service'])
38
+ }
39
+
40
+ export { execute, generateTypes }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/service",
3
- "version": "0.43.0",
3
+ "version": "0.44.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -65,12 +65,11 @@
65
65
  "pino-pretty": "^10.0.0",
66
66
  "rfdc": "^1.3.0",
67
67
  "ua-parser-js": "^1.0.35",
68
- "@platformatic/client": "0.43.0",
69
- "@platformatic/config": "0.43.0",
70
- "@platformatic/swagger-ui-theme": "0.43.0",
71
- "@platformatic/types": "0.43.0",
72
- "@platformatic/utils": "0.43.0",
73
- "@platformatic/telemetry": "0.43.0"
68
+ "@platformatic/client": "0.44.0",
69
+ "@platformatic/config": "0.44.0",
70
+ "@platformatic/swagger-ui-theme": "0.44.0",
71
+ "@platformatic/utils": "0.44.0",
72
+ "@platformatic/telemetry": "0.44.0"
74
73
  },
75
74
  "standard": {
76
75
  "ignore": [
package/service.mjs CHANGED
@@ -6,7 +6,9 @@ import isMain from 'es-main'
6
6
  import helpMe from 'help-me'
7
7
  import { readFile } from 'fs/promises'
8
8
  import { join } from 'desm'
9
+ import { printAndExitLoadConfigError } from '@platformatic/config'
9
10
  import { generateJsonSchemaConfig } from './lib/gen-schema.js'
11
+ import { generateTypes } from './lib/gen-types.mjs'
10
12
 
11
13
  import { buildCompileCmd } from './lib/compile.js'
12
14
 
@@ -18,20 +20,30 @@ const help = helpMe({
18
20
  ext: '.txt'
19
21
  })
20
22
 
23
+ function wrapCommand (fn) {
24
+ return async function (...args) {
25
+ try {
26
+ return await fn(...args)
27
+ /* c8 ignore next 3 */
28
+ } catch (err) {
29
+ printAndExitLoadConfigError(err)
30
+ }
31
+ }
32
+ }
33
+
21
34
  const program = commist({ maxDistance: 2 })
22
35
 
23
36
  program.register('help', help.toStdout)
24
37
  program.register('help start', help.toStdout.bind(null, ['start']))
25
38
 
26
39
  program.register('start', (argv) => {
27
- start(platformaticService, argv).catch((err) => {
28
- /* c8 ignore next 2 */
29
- console.error(err)
30
- process.exit(1)
31
- })
40
+ /* c8 ignore next 1 */
41
+ start(platformaticService, argv).catch(printAndExitLoadConfigError)
32
42
  })
43
+
33
44
  program.register('compile', buildCompileCmd(platformaticService))
34
- program.register('schema config', generateJsonSchemaConfig)
45
+ program.register('types', wrapCommand(generateTypes))
46
+ program.register('schema config', wrapCommand(generateJsonSchemaConfig))
35
47
  program.register('schema', help.toStdout.bind(null, ['schema']))
36
48
 
37
49
  export async function runService (argv) {
@@ -498,3 +498,34 @@ t.test('should not compile typescript plugin with start without tsconfig', async
498
498
  t.equal(err.stdout.includes('No typescript configuration file was found, skipping compilation.'), true)
499
499
  }
500
500
  })
501
+
502
+ t.test('should compile ts app with config', async (t) => {
503
+ const testDir = path.join(urlDirname(import.meta.url), '..', 'fixtures', 'hello-ts-with-config')
504
+ const cwd = await getCWD(t)
505
+
506
+ await cp(testDir, cwd, { recursive: true })
507
+
508
+ const child = execa('node', [cliPath, 'compile'], { cwd })
509
+
510
+ t.teardown(exitOnTeardown(child))
511
+
512
+ const splitter = split()
513
+ child.stdout.pipe(splitter)
514
+
515
+ for await (const data of splitter) {
516
+ const sanitized = stripAnsi(data)
517
+ if (sanitized.includes('Typescript compilation completed successfully.')) {
518
+ const jsPluginPath = path.join(cwd, 'dist', 'plugin.js')
519
+ try {
520
+ await access(jsPluginPath)
521
+ } catch (err) {
522
+ t.fail(err)
523
+ }
524
+
525
+ t.pass()
526
+ return
527
+ }
528
+ }
529
+
530
+ t.fail('should compile typescript plugin with a compile command')
531
+ })
@@ -0,0 +1,30 @@
1
+ import { cliPath } from './helper.mjs'
2
+ import { test } from 'tap'
3
+ import { join } from 'desm'
4
+ import { execa } from 'execa'
5
+ import { readFile, unlink } from 'fs/promises'
6
+
7
+ const GLOBAL_TYPES_TEMPLATE = `
8
+ import { FastifyInstance } from 'fastify'
9
+ import { PlatformaticApp, PlatformaticServiceConfig } from '@platformatic/service'
10
+
11
+ declare module 'fastify' {
12
+ interface FastifyInstance {
13
+ platformatic: PlatformaticApp<PlatformaticServiceConfig>
14
+ }
15
+ }
16
+ `
17
+
18
+ test('generate global.d.ts', async ({ equal }) => {
19
+ const fileNameOrThen = join(import.meta.url, '..', '..', 'fixtures', 'hello', 'global.d.ts')
20
+ try {
21
+ await unlink(fileNameOrThen)
22
+ } catch {}
23
+
24
+ await execa('node', [cliPath, 'types', '-c', join(import.meta.url, '..', '..', 'fixtures', 'hello', 'platformatic.service.json')])
25
+
26
+ const data = await readFile(fileNameOrThen, 'utf-8')
27
+ await unlink(fileNameOrThen)
28
+
29
+ equal(data, GLOBAL_TYPES_TEMPLATE)
30
+ })
@@ -3,6 +3,7 @@ import { test } from 'tap'
3
3
  import { join } from 'desm'
4
4
  import { readFile } from 'fs/promises'
5
5
  import { execa } from 'execa'
6
+ import stripAnsi from 'strip-ansi'
6
7
 
7
8
  const version = JSON.parse(await readFile(join(import.meta.url, '..', '..', 'package.json'))).version
8
9
 
@@ -21,7 +22,7 @@ test('print validation errors', async ({ equal, plan }) => {
21
22
  await execa('node', [cliPath, 'start', '--config', join(import.meta.url, '..', 'fixtures', 'missing-property.config.json')])
22
23
  } catch (err) {
23
24
  equal(err.exitCode, 1)
24
- equal(err.stdout, `
25
+ equal(stripAnsi(err.stdout), `
25
26
  ┌─────────┬──────┬─────────────────────────────────────────────────────────────────────┐
26
27
  │ (index) │ path │ message │
27
28
  ├─────────┼──────┼─────────────────────────────────────────────────────────────────────┤
@@ -0,0 +1,8 @@
1
+ import { FastifyInstance } from 'fastify'
2
+ import { PlatformaticApp, PlatformaticServiceConfig } from '../../../index'
3
+
4
+ declare module 'fastify' {
5
+ interface FastifyInstance {
6
+ platformatic: PlatformaticApp<PlatformaticServiceConfig>
7
+ }
8
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "server": {
3
+ "hostname": "127.0.0.1",
4
+ "port": 0,
5
+ "logger": {
6
+ "level": "info"
7
+ },
8
+ "pluginTimeout": 30000
9
+ },
10
+ "plugins": {
11
+ "paths": ["./plugin.ts"],
12
+ "typescript": true
13
+ },
14
+ "metrics": false,
15
+ "watch": false
16
+ }
@@ -0,0 +1,8 @@
1
+ /// <reference path="./global.d.ts" />
2
+ import { FastifyInstance } from 'fastify'
3
+
4
+ export default async function (app: FastifyInstance) {
5
+ app.get('/', async () => {
6
+ return app.platformatic.config
7
+ })
8
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "esModuleInterop": true,
5
+ "target": "es2020",
6
+ "moduleResolution": "node",
7
+ "sourceMap": false,
8
+ "incremental": true,
9
+ "pretty": true,
10
+ "noEmitOnError": true,
11
+ "outDir": "dist"
12
+ },
13
+ "watchOptions": {
14
+ "watchFile": "fixedPollingInterval",
15
+ "watchDirectory": "fixedPollingInterval",
16
+ "fallbackPolling": "dynamicPriority",
17
+ "synchronousWatchDirectory": true,
18
+ "excludeDirectories": [
19
+ "**/node_modules",
20
+ "dist"
21
+ ]
22
+ }
23
+ }