create-platformatic 1.17.0 → 1.18.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.
@@ -1,132 +0,0 @@
1
- import { writeFile, readFile, appendFile } from 'fs/promises'
2
- import { join } from 'path'
3
- import * as desm from 'desm'
4
- import { addPrefixToEnv } from '../utils.mjs'
5
- import { getTsConfig } from '../get-tsconfig.mjs'
6
- import { generatePlugins } from '../create-plugins.mjs'
7
- import { createDynamicWorkspaceGHAction, createStaticWorkspaceGHAction } from '../ghaction.mjs'
8
- import { createGitRepository } from '../create-git-repository.mjs'
9
-
10
- const TS_OUT_DIR = 'dist'
11
-
12
- function generateConfig (isRuntimeContext, version, typescript, envPrefix) {
13
- const plugins = {
14
- paths: [
15
- { path: './plugins', encapsulate: false },
16
- './routes'
17
- ]
18
- }
19
-
20
- const config = {
21
- $schema: `https://platformatic.dev/schemas/v${version}/service`,
22
- service: {
23
- openapi: true
24
- },
25
- watch: true,
26
- plugins
27
- }
28
-
29
- if (!isRuntimeContext) {
30
- config.server = {
31
- hostname: '{PLT_SERVER_HOSTNAME}',
32
- port: '{PORT}',
33
- logger: {
34
- level: '{PLT_SERVER_LOGGER_LEVEL}'
35
- }
36
- }
37
- }
38
-
39
- if (typescript === true) {
40
- config.plugins.typescript = `{PLT_${envPrefix}TYPESCRIPT}`
41
- }
42
-
43
- return config
44
- }
45
-
46
- function generateEnv (isRuntimeContext, hostname, port, typescript, envPrefix) {
47
- let env = ''
48
-
49
- if (!isRuntimeContext) {
50
- env += `\
51
- PLT_SERVER_HOSTNAME=${hostname}
52
- PORT=${port}
53
- PLT_SERVER_LOGGER_LEVEL=info
54
- `
55
- }
56
-
57
- if (typescript === true) {
58
- env += `\
59
-
60
- # Set to false to disable automatic typescript compilation.
61
- # Changing this setting is needed for production
62
- PLT_${envPrefix}TYPESCRIPT=true
63
- `
64
- }
65
-
66
- return env
67
- }
68
-
69
- async function createService (params, logger, currentDir = process.cwd(), version) {
70
- const {
71
- isRuntimeContext,
72
- hostname,
73
- port,
74
- typescript = false,
75
- staticWorkspaceGitHubAction,
76
- dynamicWorkspaceGitHubAction,
77
- runtimeContext,
78
- initGitRepository
79
- } = params
80
-
81
- const serviceEnv = {
82
- PLT_SERVER_LOGGER_LEVEL: 'info',
83
- PORT: port,
84
- PLT_SERVER_HOSTNAME: hostname
85
- }
86
- if (typescript) {
87
- serviceEnv.PLT_TYPESCRIPT = true
88
- }
89
-
90
- if (!version) {
91
- const pkg = await readFile(desm.join(import.meta.url, '..', '..', 'package.json'))
92
- version = JSON.parse(pkg).version
93
- }
94
- const envPrefix = runtimeContext !== undefined ? `${runtimeContext.envPrefix}_` : ''
95
- const config = generateConfig(isRuntimeContext, version, typescript, envPrefix)
96
- await writeFile(join(currentDir, 'platformatic.service.json'), JSON.stringify(config, null, 2))
97
- logger.info('Configuration file platformatic.service.json successfully created.')
98
-
99
- const env = generateEnv(isRuntimeContext, hostname, port, typescript, envPrefix)
100
- await appendFile(join(currentDir, '.env'), env)
101
- await writeFile(join(currentDir, '.env.sample'), env)
102
-
103
- if (typescript === true) {
104
- const tsConfigFileName = join(currentDir, 'tsconfig.json')
105
- const tsConfig = getTsConfig(TS_OUT_DIR)
106
- await writeFile(tsConfigFileName, JSON.stringify(tsConfig, null, 2))
107
- logger.info(
108
- `Typescript configuration file ${tsConfigFileName} successfully created.`
109
- )
110
- }
111
-
112
- if (!isRuntimeContext) {
113
- if (staticWorkspaceGitHubAction) {
114
- await createStaticWorkspaceGHAction(logger, serviceEnv, './platformatic.service.json', currentDir, typescript)
115
- }
116
- if (dynamicWorkspaceGitHubAction) {
117
- await createDynamicWorkspaceGHAction(logger, serviceEnv, './platformatic.service.json', currentDir, typescript)
118
- }
119
- }
120
-
121
- await generatePlugins(logger, currentDir, typescript, 'service')
122
-
123
- if (isRuntimeContext) {
124
- return addPrefixToEnv(serviceEnv, runtimeContext.envPrefix)
125
- }
126
- if (initGitRepository) {
127
- await createGitRepository(logger, currentDir)
128
- }
129
- return serviceEnv
130
- }
131
-
132
- export default createService