create-platformatic 1.0.0 → 1.1.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,13 +1,14 @@
1
1
  import { writeFile, readFile, appendFile } from 'fs/promises'
2
2
  import { join } from 'path'
3
3
  import * as desm from 'desm'
4
- import { findServiceConfigFile, isFileAccessible } from '../utils.mjs'
4
+ import { addPrefixToEnv, findServiceConfigFile, isFileAccessible } from '../utils.mjs'
5
5
  import { getTsConfig } from '../get-tsconfig.mjs'
6
6
  import { generatePlugins } from '../create-plugins.mjs'
7
+ import { createDynamicWorkspaceGHAction, createStaticWorkspaceGHAction } from '../ghaction.mjs'
7
8
 
8
9
  const TS_OUT_DIR = 'dist'
9
10
 
10
- function generateConfig (isRuntimeContext, version, typescript) {
11
+ function generateConfig (isRuntimeContext, version, typescript, envPrefix) {
11
12
  const plugins = {
12
13
  paths: [
13
14
  { path: './plugins', encapsulate: false },
@@ -34,13 +35,13 @@ function generateConfig (isRuntimeContext, version, typescript) {
34
35
  }
35
36
 
36
37
  if (typescript === true) {
37
- config.plugins.typescript = '{PLT_TYPESCRIPT}'
38
+ config.plugins.typescript = `{PLT_${envPrefix}TYPESCRIPT}`
38
39
  }
39
40
 
40
41
  return config
41
42
  }
42
43
 
43
- function generateEnv (isRuntimeContext, hostname, port, typescript) {
44
+ function generateEnv (isRuntimeContext, hostname, port, typescript, envPrefix) {
44
45
  let env = ''
45
46
 
46
47
  if (!isRuntimeContext) {
@@ -56,7 +57,7 @@ PLT_SERVER_LOGGER_LEVEL=info
56
57
 
57
58
  # Set to false to disable automatic typescript compilation.
58
59
  # Changing this setting is needed for production
59
- PLT_TYPESCRIPT=true
60
+ PLT_${envPrefix}TYPESCRIPT=true
60
61
  `
61
62
  }
62
63
 
@@ -64,20 +65,37 @@ PLT_TYPESCRIPT=true
64
65
  }
65
66
 
66
67
  async function createService (params, logger, currentDir = process.cwd(), version) {
67
- const { isRuntimeContext, hostname, port, typescript = false } = params
68
+ const {
69
+ isRuntimeContext,
70
+ hostname,
71
+ port,
72
+ typescript = false,
73
+ staticWorkspaceGitHubAction,
74
+ dynamicWorkspaceGitHubAction,
75
+ runtimeContext
76
+ } = params
77
+
78
+ const serviceEnv = {
79
+ PLT_SERVER_LOGGER_LEVEL: 'info',
80
+ PORT: port,
81
+ PLT_SERVER_HOSTNAME: hostname
82
+ }
83
+ if (typescript) {
84
+ serviceEnv.PLT_TYPESCRIPT = true
85
+ }
68
86
 
69
87
  if (!version) {
70
88
  const pkg = await readFile(desm.join(import.meta.url, '..', '..', 'package.json'))
71
89
  version = JSON.parse(pkg).version
72
90
  }
73
91
  const accessibleConfigFilename = await findServiceConfigFile(currentDir)
74
-
92
+ const envPrefix = runtimeContext !== undefined ? `${runtimeContext.envPrefix}_` : ''
75
93
  if (accessibleConfigFilename === undefined) {
76
- const config = generateConfig(isRuntimeContext, version, typescript)
94
+ const config = generateConfig(isRuntimeContext, version, typescript, envPrefix)
77
95
  await writeFile(join(currentDir, 'platformatic.service.json'), JSON.stringify(config, null, 2))
78
96
  logger.info('Configuration file platformatic.service.json successfully created.')
79
97
 
80
- const env = generateEnv(isRuntimeContext, hostname, port, typescript)
98
+ const env = generateEnv(isRuntimeContext, hostname, port, typescript, envPrefix)
81
99
  const envFileExists = await isFileAccessible('.env', currentDir)
82
100
  await appendFile(join(currentDir, '.env'), env)
83
101
  await writeFile(join(currentDir, '.env.sample'), env)
@@ -107,17 +125,22 @@ async function createService (params, logger, currentDir = process.cwd(), versio
107
125
  }
108
126
  }
109
127
 
128
+ if (!isRuntimeContext) {
129
+ if (staticWorkspaceGitHubAction) {
130
+ await createStaticWorkspaceGHAction(logger, serviceEnv, './platformatic.service.json', currentDir, typescript)
131
+ }
132
+ if (dynamicWorkspaceGitHubAction) {
133
+ await createDynamicWorkspaceGHAction(logger, serviceEnv, './platformatic.service.json', currentDir, typescript)
134
+ }
135
+ }
136
+
110
137
  await generatePlugins(logger, currentDir, typescript, 'service')
111
138
 
112
- const output = {
113
- PLT_SERVER_LOGGER_LEVEL: 'info',
114
- PORT: port,
115
- PLT_SERVER_HOSTNAME: hostname
116
- }
117
- if (typescript) {
118
- output.PLT_TYPESCRIPT = true
139
+ if (isRuntimeContext) {
140
+ return addPrefixToEnv(serviceEnv, runtimeContext.envPrefix)
119
141
  }
120
- return output
142
+
143
+ return serviceEnv
121
144
  }
122
145
 
123
146
  export default createService
package/src/utils.mjs CHANGED
@@ -91,3 +91,14 @@ export const isCurrentVersionSupported = (currentVersion) => {
91
91
  }
92
92
  return false
93
93
  }
94
+
95
+ export function convertServiceNameToPrefix (serviceName) {
96
+ return serviceName.replace(/-/g, '_').toUpperCase()
97
+ }
98
+ export function addPrefixToEnv (env, prefix) {
99
+ const output = {}
100
+ Object.entries(env).forEach(([key, value]) => {
101
+ output[`${prefix}_${key}`] = value
102
+ })
103
+ return output
104
+ }