create-platformatic 0.47.6 → 1.0.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/package.json +4 -4
- package/src/composer/create-composer-cli.mjs +11 -6
- package/src/composer/create-composer.mjs +20 -18
- package/src/db/create-db-cli.mjs +21 -18
- package/src/db/create-db.mjs +39 -16
- package/src/runtime/create-runtime-cli.mjs +10 -2
- package/src/runtime/create-runtime.mjs +38 -69
- package/src/service/create-service-cli.mjs +8 -3
- package/src/service/create-service.mjs +21 -12
- package/src/utils.mjs +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-platformatic",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Create platformatic-db interactive tool",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"semver": "^7.5.1",
|
|
35
35
|
"undici": "^5.22.1",
|
|
36
36
|
"which": "^3.0.1",
|
|
37
|
-
"@platformatic/config": "0.
|
|
37
|
+
"@platformatic/config": "1.0.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"ajv": "^8.12.0",
|
|
@@ -47,8 +47,8 @@
|
|
|
47
47
|
"tap": "^16.3.6",
|
|
48
48
|
"typescript": "~5.2.0",
|
|
49
49
|
"yaml": "^2.3.1",
|
|
50
|
-
"@platformatic/
|
|
51
|
-
"@platformatic/
|
|
50
|
+
"@platformatic/service": "1.0.0",
|
|
51
|
+
"@platformatic/db": "1.0.0"
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
54
|
"test": "standard | snazzy && cross-env NODE_OPTIONS=\"--loader=esmock --no-warnings\" c8 tap --no-coverage test/*test.mjs test/*/*test.mjs",
|
|
@@ -67,13 +67,19 @@ const createPlatformaticComposer = async (_args, opts) => {
|
|
|
67
67
|
const projectDir = opts.dir || await askDir(logger, '.')
|
|
68
68
|
const isRuntimeContext = opts.isRuntimeContext || false
|
|
69
69
|
|
|
70
|
-
const toAsk = [
|
|
70
|
+
const toAsk = []
|
|
71
|
+
|
|
72
|
+
if (!isRuntimeContext) {
|
|
73
|
+
toAsk.push(getPort(args.port))
|
|
74
|
+
}
|
|
71
75
|
|
|
72
76
|
if (isRuntimeContext) {
|
|
73
77
|
const servicesNames = opts.runtimeContext.servicesNames.filter(
|
|
74
78
|
(serviceName) => serviceName !== opts.serviceName
|
|
75
79
|
)
|
|
76
|
-
|
|
80
|
+
if (servicesNames.length > 0) {
|
|
81
|
+
toAsk.push(getServicesToCompose(servicesNames))
|
|
82
|
+
}
|
|
77
83
|
}
|
|
78
84
|
|
|
79
85
|
if (!opts.skipPackageJson) {
|
|
@@ -90,18 +96,17 @@ const createPlatformaticComposer = async (_args, opts) => {
|
|
|
90
96
|
await mkdir(projectDir, { recursive: true })
|
|
91
97
|
|
|
92
98
|
const params = {
|
|
99
|
+
isRuntimeContext,
|
|
93
100
|
hostname: args.hostname,
|
|
94
101
|
port,
|
|
95
|
-
|
|
102
|
+
servicesToCompose
|
|
96
103
|
}
|
|
97
104
|
|
|
98
105
|
const env = await createComposer(
|
|
99
106
|
params,
|
|
100
107
|
logger,
|
|
101
108
|
projectDir,
|
|
102
|
-
version
|
|
103
|
-
isRuntimeContext,
|
|
104
|
-
servicesToCompose
|
|
109
|
+
version
|
|
105
110
|
)
|
|
106
111
|
|
|
107
112
|
const fastifyVersion = await getDependencyVersion('fastify')
|
|
@@ -3,16 +3,9 @@ import { findComposerConfigFile, isFileAccessible } from '../utils.mjs'
|
|
|
3
3
|
import { join } from 'path'
|
|
4
4
|
import * as desm from 'desm'
|
|
5
5
|
|
|
6
|
-
function generateConfig (
|
|
6
|
+
function generateConfig (isRuntimeContext, version, servicesToCompose) {
|
|
7
7
|
const config = {
|
|
8
8
|
$schema: `https://platformatic.dev/schemas/v${version}/composer`,
|
|
9
|
-
server: {
|
|
10
|
-
hostname: '{PLT_SERVER_HOSTNAME}',
|
|
11
|
-
port: '{PORT}',
|
|
12
|
-
logger: {
|
|
13
|
-
level: '{PLT_SERVER_LOGGER_LEVEL}'
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
9
|
composer: {
|
|
17
10
|
services: [{
|
|
18
11
|
id: 'example',
|
|
@@ -26,6 +19,16 @@ function generateConfig (version, isRuntimeContext, servicesToCompose) {
|
|
|
26
19
|
watch: true
|
|
27
20
|
}
|
|
28
21
|
|
|
22
|
+
if (!isRuntimeContext) {
|
|
23
|
+
config.server = {
|
|
24
|
+
hostname: '{PLT_SERVER_HOSTNAME}',
|
|
25
|
+
port: '{PORT}',
|
|
26
|
+
logger: {
|
|
27
|
+
level: '{PLT_SERVER_LOGGER_LEVEL}'
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
29
32
|
if (isRuntimeContext) {
|
|
30
33
|
config.composer.services = servicesToCompose.map((serviceName) => {
|
|
31
34
|
return {
|
|
@@ -42,14 +45,13 @@ function generateConfig (version, isRuntimeContext, servicesToCompose) {
|
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
function generateEnv (isRuntimeContext, hostname, port) {
|
|
45
|
-
let env =
|
|
46
|
-
PLT_SERVER_HOSTNAME=${hostname}
|
|
47
|
-
PORT=${port}
|
|
48
|
-
PLT_SERVER_LOGGER_LEVEL=info
|
|
49
|
-
`
|
|
48
|
+
let env = ''
|
|
50
49
|
|
|
51
50
|
if (!isRuntimeContext) {
|
|
52
51
|
env += `\
|
|
52
|
+
PLT_SERVER_HOSTNAME=${hostname}
|
|
53
|
+
PORT=${port}
|
|
54
|
+
PLT_SERVER_LOGGER_LEVEL=info
|
|
53
55
|
PLT_EXAMPLE_ORIGIN=
|
|
54
56
|
`
|
|
55
57
|
}
|
|
@@ -58,13 +60,13 @@ PLT_EXAMPLE_ORIGIN=
|
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
async function createComposer (
|
|
61
|
-
|
|
63
|
+
params,
|
|
62
64
|
logger,
|
|
63
65
|
currentDir = process.cwd(),
|
|
64
|
-
version
|
|
65
|
-
isRuntimeContext = false,
|
|
66
|
-
servicesToCompose = []
|
|
66
|
+
version
|
|
67
67
|
) {
|
|
68
|
+
const { isRuntimeContext, hostname, port, servicesToCompose = [] } = params
|
|
69
|
+
|
|
68
70
|
if (!version) {
|
|
69
71
|
const pkg = await readFile(desm.join(import.meta.url, '..', '..', 'package.json'))
|
|
70
72
|
version = JSON.parse(pkg).version
|
|
@@ -72,7 +74,7 @@ async function createComposer (
|
|
|
72
74
|
const accessibleConfigFilename = await findComposerConfigFile(currentDir)
|
|
73
75
|
|
|
74
76
|
if (accessibleConfigFilename === undefined) {
|
|
75
|
-
const config = generateConfig(
|
|
77
|
+
const config = generateConfig(isRuntimeContext, version, servicesToCompose)
|
|
76
78
|
await writeFile(join(currentDir, 'platformatic.composer.json'), JSON.stringify(config, null, 2))
|
|
77
79
|
logger.info('Configuration file platformatic.composer.json successfully created.')
|
|
78
80
|
|
package/src/db/create-db-cli.mjs
CHANGED
|
@@ -55,10 +55,7 @@ export function parseDBArgs (_args) {
|
|
|
55
55
|
default: {
|
|
56
56
|
hostname: '127.0.0.1',
|
|
57
57
|
database: 'sqlite',
|
|
58
|
-
migrations: 'migrations'
|
|
59
|
-
plugin: true,
|
|
60
|
-
types: true,
|
|
61
|
-
typescript: false
|
|
58
|
+
migrations: 'migrations'
|
|
62
59
|
},
|
|
63
60
|
alias: {
|
|
64
61
|
h: 'hostname',
|
|
@@ -83,6 +80,7 @@ const createPlatformaticDB = async (_args, opts) => {
|
|
|
83
80
|
const version = await getVersion()
|
|
84
81
|
const pkgManager = getPkgManager()
|
|
85
82
|
const projectDir = opts.dir || await askDir(logger, '.')
|
|
83
|
+
const isRuntimeContext = opts.isRuntimeContext || false
|
|
86
84
|
|
|
87
85
|
const { database } = await inquirer.prompt({
|
|
88
86
|
type: 'list',
|
|
@@ -125,23 +123,28 @@ const createPlatformaticDB = async (_args, opts) => {
|
|
|
125
123
|
break
|
|
126
124
|
}
|
|
127
125
|
}
|
|
128
|
-
|
|
129
|
-
const wizardOptions = await inquirer.prompt([{
|
|
126
|
+
const wizardPrompts = [{
|
|
130
127
|
type: 'list',
|
|
131
128
|
name: 'defaultMigrations',
|
|
132
129
|
message: 'Do you want to create default migrations?',
|
|
133
130
|
default: true,
|
|
134
131
|
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
132
|
+
}]
|
|
133
|
+
|
|
134
|
+
if (args.plugin === false) {
|
|
135
|
+
wizardPrompts.push({
|
|
136
|
+
type: 'list',
|
|
137
|
+
name: 'generatePlugin',
|
|
138
|
+
message: 'Do you want to create a plugin?',
|
|
139
|
+
default: true,
|
|
140
|
+
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
const wizardOptions = await inquirer.prompt(wizardPrompts, getUseTypescript(args.typescript))
|
|
144
|
+
if (!isRuntimeContext) {
|
|
145
|
+
const { port } = await inquirer.prompt([getPort(args.port)])
|
|
146
|
+
wizardOptions.port = port
|
|
147
|
+
}
|
|
145
148
|
|
|
146
149
|
// Create the project directory
|
|
147
150
|
await mkdir(projectDir, { recursive: true })
|
|
@@ -151,6 +154,7 @@ const createPlatformaticDB = async (_args, opts) => {
|
|
|
151
154
|
const useTypes = args.types || generatePlugin // we set this always to true if we want to generate a plugin
|
|
152
155
|
|
|
153
156
|
const params = {
|
|
157
|
+
isRuntimeContext,
|
|
154
158
|
hostname: args.hostname,
|
|
155
159
|
port: wizardOptions.port,
|
|
156
160
|
database,
|
|
@@ -158,8 +162,7 @@ const createPlatformaticDB = async (_args, opts) => {
|
|
|
158
162
|
migrations: wizardOptions.defaultMigrations ? args.migrations : '',
|
|
159
163
|
plugin: generatePlugin,
|
|
160
164
|
types: useTypes,
|
|
161
|
-
typescript: useTypescript
|
|
162
|
-
isRuntime: opts.isRuntime
|
|
165
|
+
typescript: useTypescript
|
|
163
166
|
}
|
|
164
167
|
|
|
165
168
|
const env = await createDB(params, logger, projectDir, version)
|
package/src/db/create-db.mjs
CHANGED
|
@@ -249,16 +249,9 @@ test('movies', async (t) => {
|
|
|
249
249
|
})
|
|
250
250
|
`
|
|
251
251
|
|
|
252
|
-
function generateConfig (migrations, plugin, types, typescript, version) {
|
|
252
|
+
function generateConfig (isRuntimeContext, migrations, plugin, types, typescript, version) {
|
|
253
253
|
const config = {
|
|
254
254
|
$schema: `https://platformatic.dev/schemas/v${version}/db`,
|
|
255
|
-
server: {
|
|
256
|
-
hostname: '{PLT_SERVER_HOSTNAME}',
|
|
257
|
-
port: '{PORT}',
|
|
258
|
-
logger: {
|
|
259
|
-
level: '{PLT_SERVER_LOGGER_LEVEL}'
|
|
260
|
-
}
|
|
261
|
-
},
|
|
262
255
|
db: {
|
|
263
256
|
connectionString: '{DATABASE_URL}',
|
|
264
257
|
graphql: true,
|
|
@@ -270,6 +263,16 @@ function generateConfig (migrations, plugin, types, typescript, version) {
|
|
|
270
263
|
}
|
|
271
264
|
}
|
|
272
265
|
|
|
266
|
+
if (!isRuntimeContext) {
|
|
267
|
+
config.server = {
|
|
268
|
+
hostname: '{PLT_SERVER_HOSTNAME}',
|
|
269
|
+
port: '{PORT}',
|
|
270
|
+
logger: {
|
|
271
|
+
level: '{PLT_SERVER_LOGGER_LEVEL}'
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
273
276
|
if (migrations) {
|
|
274
277
|
config.migrations = {
|
|
275
278
|
dir: migrations
|
|
@@ -300,20 +303,27 @@ function generateConfig (migrations, plugin, types, typescript, version) {
|
|
|
300
303
|
return config
|
|
301
304
|
}
|
|
302
305
|
|
|
303
|
-
function generateEnv (hostname, port, connectionString, typescript) {
|
|
306
|
+
function generateEnv (isRuntimeContext, hostname, port, connectionString, typescript) {
|
|
304
307
|
let env = `\
|
|
308
|
+
DATABASE_URL=${connectionString}
|
|
309
|
+
|
|
310
|
+
`
|
|
311
|
+
|
|
312
|
+
if (!isRuntimeContext) {
|
|
313
|
+
env += `\
|
|
305
314
|
PLT_SERVER_HOSTNAME=${hostname}
|
|
306
315
|
PORT=${port}
|
|
307
316
|
PLT_SERVER_LOGGER_LEVEL=info
|
|
308
|
-
|
|
309
|
-
`
|
|
317
|
+
|
|
318
|
+
`
|
|
319
|
+
}
|
|
310
320
|
|
|
311
321
|
if (typescript === true) {
|
|
312
322
|
env += `\
|
|
313
|
-
|
|
314
323
|
# Set to false to disable automatic typescript compilation.
|
|
315
324
|
# Changing this setting is needed for production
|
|
316
325
|
PLT_TYPESCRIPT=true
|
|
326
|
+
|
|
317
327
|
`
|
|
318
328
|
}
|
|
319
329
|
|
|
@@ -324,18 +334,31 @@ export function getConnectionString (database) {
|
|
|
324
334
|
return connectionStrings[database]
|
|
325
335
|
}
|
|
326
336
|
|
|
327
|
-
export async function createDB (
|
|
337
|
+
export async function createDB (params, logger, currentDir, version) {
|
|
338
|
+
let {
|
|
339
|
+
isRuntimeContext,
|
|
340
|
+
hostname,
|
|
341
|
+
port,
|
|
342
|
+
database = 'sqlite',
|
|
343
|
+
migrations = 'migrations',
|
|
344
|
+
plugin = true,
|
|
345
|
+
types = true,
|
|
346
|
+
typescript = false,
|
|
347
|
+
connectionString
|
|
348
|
+
} = params
|
|
349
|
+
|
|
328
350
|
connectionString = connectionString || getConnectionString(database)
|
|
329
351
|
const createMigrations = !!migrations // If we don't define a migrations folder, we don't create it
|
|
330
352
|
const accessibleConfigFilename = await findDBConfigFile(currentDir)
|
|
331
353
|
if (accessibleConfigFilename === undefined) {
|
|
332
|
-
const config = generateConfig(migrations, plugin, types, typescript, version)
|
|
354
|
+
const config = generateConfig(isRuntimeContext, migrations, plugin, types, typescript, version)
|
|
333
355
|
await writeFile(join(currentDir, 'platformatic.db.json'), JSON.stringify(config, null, 2))
|
|
334
356
|
logger.info('Configuration file platformatic.db.json successfully created.')
|
|
335
|
-
const env = generateEnv(hostname, port, connectionString, typescript)
|
|
357
|
+
const env = generateEnv(isRuntimeContext, hostname, port, connectionString, typescript)
|
|
358
|
+
const envSample = generateEnv(isRuntimeContext, hostname, port, getConnectionString(database), typescript)
|
|
336
359
|
const envFileExists = await isFileAccessible('.env', currentDir)
|
|
337
360
|
await appendFile(join(currentDir, '.env'), env)
|
|
338
|
-
await writeFile(join(currentDir, '.env.sample'),
|
|
361
|
+
await writeFile(join(currentDir, '.env.sample'), envSample)
|
|
339
362
|
/* c8 ignore next 5 */
|
|
340
363
|
if (envFileExists) {
|
|
341
364
|
logger.info('Environment file .env found, appending new environment variables to existing .env file.')
|
|
@@ -12,7 +12,7 @@ import ora from 'ora'
|
|
|
12
12
|
import createRuntime from './create-runtime.mjs'
|
|
13
13
|
import askDir from '../ask-dir.mjs'
|
|
14
14
|
import { askDynamicWorkspaceCreateGHAction, askStaticWorkspaceGHAction } from '../ghaction.mjs'
|
|
15
|
-
import { getOverwriteReadme, getRunPackageManagerInstall } from '../cli-options.mjs'
|
|
15
|
+
import { getPort, getOverwriteReadme, getRunPackageManagerInstall } from '../cli-options.mjs'
|
|
16
16
|
import generateName from 'boring-name-generator'
|
|
17
17
|
import { chooseKind } from '../index.mjs'
|
|
18
18
|
|
|
@@ -112,7 +112,15 @@ export async function createPlatformaticRuntime (_args) {
|
|
|
112
112
|
entrypoint = names[0]
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
const
|
|
115
|
+
const { port: entrypointPort } = await inquirer.prompt([getPort()])
|
|
116
|
+
|
|
117
|
+
const params = {
|
|
118
|
+
servicesDir,
|
|
119
|
+
entrypoint,
|
|
120
|
+
entrypointPort
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const env = await createRuntime(params, logger, projectDir, version)
|
|
116
124
|
|
|
117
125
|
await askStaticWorkspaceGHAction(logger, env, 'service', false, projectDir)
|
|
118
126
|
await askDynamicWorkspaceCreateGHAction(logger, env, 'service', false, projectDir)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { readFile,
|
|
2
|
-
import { findRuntimeConfigFile } from '../utils.mjs'
|
|
1
|
+
import { readFile, appendFile, writeFile } from 'fs/promises'
|
|
2
|
+
import { findConfigFile, findRuntimeConfigFile } from '../utils.mjs'
|
|
3
3
|
import { join, relative, isAbsolute } from 'path'
|
|
4
4
|
import * as desm from 'desm'
|
|
5
5
|
|
|
@@ -18,7 +18,9 @@ function generateConfig (version, path, entrypoint) {
|
|
|
18
18
|
return config
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
async function createRuntime (logger, currentDir = process.cwd(), version
|
|
21
|
+
async function createRuntime (params, logger, currentDir = process.cwd(), version) {
|
|
22
|
+
const { servicesDir, entrypoint, entrypointPort } = params
|
|
23
|
+
|
|
22
24
|
if (!version) {
|
|
23
25
|
const pkg = await readFile(desm.join(import.meta.url, '..', '..', 'package.json'))
|
|
24
26
|
version = JSON.parse(pkg).version
|
|
@@ -33,83 +35,50 @@ async function createRuntime (logger, currentDir = process.cwd(), version, servi
|
|
|
33
35
|
} else {
|
|
34
36
|
logger.info(`Configuration file ${accessibleConfigFilename} found, skipping creation of configuration file.`)
|
|
35
37
|
}
|
|
36
|
-
if (servicesDir) {
|
|
37
|
-
const servicesDirFullPath = isAbsolute(servicesDir) ? servicesDir : join(currentDir, servicesDir)
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
if (servicesDir && entrypoint && entrypointPort) {
|
|
40
|
+
const servicesDirFullPath = isAbsolute(servicesDir)
|
|
41
|
+
? servicesDir
|
|
42
|
+
: join(currentDir, servicesDir)
|
|
43
|
+
|
|
44
|
+
const entrypointPath = join(servicesDirFullPath, entrypoint)
|
|
45
|
+
await updateEntrypointConfig(logger, entrypointPath)
|
|
46
|
+
await updateEntrypointEnv(entrypointPort, logger, entrypointPath)
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
return {}
|
|
49
50
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
async function getAllServices (servicesDir, entrypoint = null) {
|
|
57
|
-
const services = (await readdir(servicesDir))
|
|
58
|
-
if (entrypoint) {
|
|
59
|
-
return services.filter(dir => dir !== entrypoint)
|
|
60
|
-
}
|
|
61
|
-
return services
|
|
62
|
-
}
|
|
63
|
-
async function cleanServicesConfig (logger, servicesDir, entrypoint) {
|
|
64
|
-
const services = await getAllServices(servicesDir, entrypoint)
|
|
65
|
-
for (const svc of services) {
|
|
66
|
-
const serviceDir = join(servicesDir, svc)
|
|
67
|
-
const configFile = await findConfigFile(serviceDir)
|
|
68
|
-
if (!configFile) {
|
|
69
|
-
logger.warn(`Cannot find config file in ${serviceDir}`)
|
|
70
|
-
} else {
|
|
71
|
-
console.log(`Found config file ${configFile}`)
|
|
72
|
-
const configFilePath = join(serviceDir, configFile)
|
|
73
|
-
const config = JSON.parse(await readFile(configFilePath, 'utf8'))
|
|
74
|
-
delete config.server
|
|
75
|
-
await writeFile(configFilePath, JSON.stringify(config, null, 2))
|
|
76
|
-
}
|
|
51
|
+
|
|
52
|
+
async function updateEntrypointConfig (logger, currentDir) {
|
|
53
|
+
const accessibleConfigFilename = await findConfigFile(currentDir)
|
|
54
|
+
if (accessibleConfigFilename === undefined) {
|
|
55
|
+
logger.error('Cannot find an entrypoint configuration file.')
|
|
56
|
+
return
|
|
77
57
|
}
|
|
78
|
-
}
|
|
79
58
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
} else {
|
|
89
|
-
// read .env file
|
|
90
|
-
const lines = envFile.split('\n')
|
|
91
|
-
lines.forEach((line) => {
|
|
92
|
-
// copy to main env file only if line _doesn't_ match
|
|
93
|
-
// i.e any other config or comments
|
|
94
|
-
if (!line.match(/(PLT_LOGGER_LEVEL|PORT|PLT_SERVER_HOSTNAME)=/)) {
|
|
95
|
-
mainEnvFile += `\n${line}`
|
|
96
|
-
}
|
|
97
|
-
})
|
|
98
|
-
}
|
|
99
|
-
try {
|
|
100
|
-
await unlink(join(servicesDir, svc, '.env.sample'))
|
|
101
|
-
} catch (err) {
|
|
102
|
-
// do nothing
|
|
59
|
+
const configPath = join(currentDir, accessibleConfigFilename)
|
|
60
|
+
const config = JSON.parse(await readFile(configPath, 'utf8'))
|
|
61
|
+
|
|
62
|
+
config.server = {
|
|
63
|
+
hostname: '{PLT_SERVER_HOSTNAME}',
|
|
64
|
+
port: '{PORT}',
|
|
65
|
+
logger: {
|
|
66
|
+
level: '{PLT_SERVER_LOGGER_LEVEL}'
|
|
103
67
|
}
|
|
104
68
|
}
|
|
105
|
-
|
|
69
|
+
|
|
70
|
+
await writeFile(configPath, JSON.stringify(config, null, 2))
|
|
106
71
|
}
|
|
107
72
|
|
|
108
|
-
async function
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
73
|
+
async function updateEntrypointEnv (port, logger, currentDir) {
|
|
74
|
+
const env = `\
|
|
75
|
+
PLT_SERVER_HOSTNAME=127.0.0.1
|
|
76
|
+
PORT=${port}
|
|
77
|
+
PLT_SERVER_LOGGER_LEVEL=info
|
|
78
|
+
`
|
|
79
|
+
|
|
80
|
+
await appendFile(join(currentDir, '.env'), env)
|
|
81
|
+
await writeFile(join(currentDir, '.env.sample'), env)
|
|
113
82
|
}
|
|
114
83
|
|
|
115
84
|
export default createRuntime
|
|
@@ -55,8 +55,13 @@ const createPlatformaticService = async (_args, opts = {}) => {
|
|
|
55
55
|
const pkgManager = getPkgManager()
|
|
56
56
|
|
|
57
57
|
const projectDir = opts.dir || await askDir(logger, '.')
|
|
58
|
+
const isRuntimeContext = opts.isRuntimeContext || false
|
|
58
59
|
|
|
59
|
-
const toAsk = [getUseTypescript(args.typescript)
|
|
60
|
+
const toAsk = [getUseTypescript(args.typescript)]
|
|
61
|
+
|
|
62
|
+
if (!isRuntimeContext) {
|
|
63
|
+
toAsk.push(getPort(args.port))
|
|
64
|
+
}
|
|
60
65
|
|
|
61
66
|
if (!opts.skipPackageJson) {
|
|
62
67
|
toAsk.unshift(getRunPackageManagerInstall(pkgManager))
|
|
@@ -68,10 +73,10 @@ const createPlatformaticService = async (_args, opts = {}) => {
|
|
|
68
73
|
await mkdir(projectDir, { recursive: true })
|
|
69
74
|
|
|
70
75
|
const params = {
|
|
76
|
+
isRuntimeContext,
|
|
71
77
|
hostname: args.hostname,
|
|
72
78
|
port,
|
|
73
|
-
typescript: useTypescript
|
|
74
|
-
isRuntime: opts.isRuntime
|
|
79
|
+
typescript: useTypescript
|
|
75
80
|
}
|
|
76
81
|
|
|
77
82
|
const env = await createService(params, logger, projectDir, version)
|
|
@@ -7,7 +7,7 @@ import { generatePlugins } from '../create-plugins.mjs'
|
|
|
7
7
|
|
|
8
8
|
const TS_OUT_DIR = 'dist'
|
|
9
9
|
|
|
10
|
-
function generateConfig (version, typescript) {
|
|
10
|
+
function generateConfig (isRuntimeContext, version, typescript) {
|
|
11
11
|
const plugins = {
|
|
12
12
|
paths: [
|
|
13
13
|
{ path: './plugins', encapsulate: false },
|
|
@@ -17,17 +17,20 @@ function generateConfig (version, typescript) {
|
|
|
17
17
|
|
|
18
18
|
const config = {
|
|
19
19
|
$schema: `https://platformatic.dev/schemas/v${version}/service`,
|
|
20
|
-
|
|
20
|
+
service: {
|
|
21
|
+
openapi: true
|
|
22
|
+
},
|
|
23
|
+
plugins
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!isRuntimeContext) {
|
|
27
|
+
config.server = {
|
|
21
28
|
hostname: '{PLT_SERVER_HOSTNAME}',
|
|
22
29
|
port: '{PORT}',
|
|
23
30
|
logger: {
|
|
24
31
|
level: '{PLT_SERVER_LOGGER_LEVEL}'
|
|
25
32
|
}
|
|
26
|
-
}
|
|
27
|
-
service: {
|
|
28
|
-
openapi: true
|
|
29
|
-
},
|
|
30
|
-
plugins
|
|
33
|
+
}
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
if (typescript === true) {
|
|
@@ -37,12 +40,16 @@ function generateConfig (version, typescript) {
|
|
|
37
40
|
return config
|
|
38
41
|
}
|
|
39
42
|
|
|
40
|
-
function generateEnv (hostname, port, typescript) {
|
|
41
|
-
let env =
|
|
43
|
+
function generateEnv (isRuntimeContext, hostname, port, typescript) {
|
|
44
|
+
let env = ''
|
|
45
|
+
|
|
46
|
+
if (!isRuntimeContext) {
|
|
47
|
+
env += `\
|
|
42
48
|
PLT_SERVER_HOSTNAME=${hostname}
|
|
43
49
|
PORT=${port}
|
|
44
50
|
PLT_SERVER_LOGGER_LEVEL=info
|
|
45
51
|
`
|
|
52
|
+
}
|
|
46
53
|
|
|
47
54
|
if (typescript === true) {
|
|
48
55
|
env += `\
|
|
@@ -56,7 +63,9 @@ PLT_TYPESCRIPT=true
|
|
|
56
63
|
return env
|
|
57
64
|
}
|
|
58
65
|
|
|
59
|
-
async function createService (
|
|
66
|
+
async function createService (params, logger, currentDir = process.cwd(), version) {
|
|
67
|
+
const { isRuntimeContext, hostname, port, typescript = false } = params
|
|
68
|
+
|
|
60
69
|
if (!version) {
|
|
61
70
|
const pkg = await readFile(desm.join(import.meta.url, '..', '..', 'package.json'))
|
|
62
71
|
version = JSON.parse(pkg).version
|
|
@@ -64,11 +73,11 @@ async function createService ({ hostname, port, typescript = false }, logger, cu
|
|
|
64
73
|
const accessibleConfigFilename = await findServiceConfigFile(currentDir)
|
|
65
74
|
|
|
66
75
|
if (accessibleConfigFilename === undefined) {
|
|
67
|
-
const config = generateConfig(version, typescript)
|
|
76
|
+
const config = generateConfig(isRuntimeContext, version, typescript)
|
|
68
77
|
await writeFile(join(currentDir, 'platformatic.service.json'), JSON.stringify(config, null, 2))
|
|
69
78
|
logger.info('Configuration file platformatic.service.json successfully created.')
|
|
70
79
|
|
|
71
|
-
const env = generateEnv(hostname, port, typescript)
|
|
80
|
+
const env = generateEnv(isRuntimeContext, hostname, port, typescript)
|
|
72
81
|
const envFileExists = await isFileAccessible('.env', currentDir)
|
|
73
82
|
await appendFile(join(currentDir, '.env'), env)
|
|
74
83
|
await writeFile(join(currentDir, '.env.sample'), env)
|
package/src/utils.mjs
CHANGED
|
@@ -61,6 +61,7 @@ export async function isDirectoryWriteable (directory) {
|
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
export const findConfigFile = async (directory) => (ConfigManager.findConfigFile(directory))
|
|
64
65
|
export const findDBConfigFile = async (directory) => (ConfigManager.findConfigFile(directory, 'db'))
|
|
65
66
|
export const findServiceConfigFile = async (directory) => (ConfigManager.findConfigFile(directory, 'service'))
|
|
66
67
|
export const findComposerConfigFile = async (directory) => (ConfigManager.findConfigFile(directory, 'composer'))
|