create-platformatic 1.0.0 → 1.1.1
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/create-platformatic.mjs +1 -1
- package/package.json +19 -19
- package/src/ask-dir.mjs +2 -0
- package/src/cli-options.mjs +0 -10
- package/src/composer/create-composer-cli.mjs +44 -40
- package/src/composer/create-composer.mjs +38 -16
- package/src/create-package-json.mjs +2 -1
- package/src/create-readme.mjs +12 -0
- package/src/db/create-db-cli.mjs +84 -76
- package/src/db/create-db.mjs +42 -23
- package/src/ghaction.mjs +8 -41
- package/src/runtime/create-runtime-cli.mjs +58 -42
- package/src/runtime/create-runtime.mjs +72 -25
- package/src/service/create-service-cli.mjs +46 -39
- package/src/service/create-service.mjs +40 -17
- package/src/utils.mjs +11 -0
package/create-platformatic.mjs
CHANGED
|
@@ -24,7 +24,7 @@ if (isMain(import.meta)) {
|
|
|
24
24
|
})
|
|
25
25
|
|
|
26
26
|
if (args.version) {
|
|
27
|
-
console.log('v' + JSON.parse(await readFile(join(import.meta.url, 'package.json'))).version)
|
|
27
|
+
console.log('v' + JSON.parse(await readFile(join(import.meta.url, 'package.json'), 'utf8')).version)
|
|
28
28
|
process.exit(0)
|
|
29
29
|
}
|
|
30
30
|
await createPlatformatic(_args)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-platformatic",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Create platformatic-db interactive tool",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,39 +16,39 @@
|
|
|
16
16
|
"author": "Marco Piraccini <marco.piraccini@gmail.com>",
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"boring-name-generator": "^1.0.3",
|
|
19
|
-
"chalk": "^5.
|
|
19
|
+
"chalk": "^5.3.0",
|
|
20
20
|
"columnify": "^1.6.0",
|
|
21
21
|
"commist": "^3.2.0",
|
|
22
22
|
"desm": "^1.3.0",
|
|
23
|
-
"es-main": "^1.
|
|
24
|
-
"execa": "^8.0.
|
|
25
|
-
"fastify": "^4.
|
|
23
|
+
"es-main": "^1.3.0",
|
|
24
|
+
"execa": "^8.0.1",
|
|
25
|
+
"fastify": "^4.23.2",
|
|
26
26
|
"help-me": "^4.2.0",
|
|
27
|
-
"inquirer": "^9.2.
|
|
27
|
+
"inquirer": "^9.2.11",
|
|
28
28
|
"log-update": "^5.0.1",
|
|
29
29
|
"minimist": "^1.2.8",
|
|
30
30
|
"ora": "^6.3.1",
|
|
31
|
-
"pino": "^8.
|
|
32
|
-
"pino-pretty": "^10.
|
|
31
|
+
"pino": "^8.15.3",
|
|
32
|
+
"pino-pretty": "^10.2.0",
|
|
33
33
|
"pupa": "^3.1.0",
|
|
34
|
-
"semver": "^7.5.
|
|
35
|
-
"undici": "^5.
|
|
34
|
+
"semver": "^7.5.4",
|
|
35
|
+
"undici": "^5.25.4",
|
|
36
36
|
"which": "^3.0.1",
|
|
37
|
-
"@platformatic/config": "1.
|
|
37
|
+
"@platformatic/config": "1.1.1"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"ajv": "^8.12.0",
|
|
41
|
-
"c8": "^8.0.
|
|
41
|
+
"c8": "^8.0.1",
|
|
42
42
|
"cross-env": "^7.0.3",
|
|
43
|
-
"dotenv": "^16.1
|
|
44
|
-
"esmock": "^2.
|
|
43
|
+
"dotenv": "^16.3.1",
|
|
44
|
+
"esmock": "^2.5.1",
|
|
45
45
|
"snazzy": "^9.0.0",
|
|
46
46
|
"standard": "^17.1.0",
|
|
47
|
-
"tap": "^16.3.
|
|
48
|
-
"typescript": "~5.2.
|
|
49
|
-
"yaml": "^2.3.
|
|
50
|
-
"@platformatic/
|
|
51
|
-
"@platformatic/
|
|
47
|
+
"tap": "^16.3.9",
|
|
48
|
+
"typescript": "~5.2.2",
|
|
49
|
+
"yaml": "^2.3.2",
|
|
50
|
+
"@platformatic/db": "1.1.1",
|
|
51
|
+
"@platformatic/service": "1.1.1"
|
|
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",
|
package/src/ask-dir.mjs
CHANGED
package/src/cli-options.mjs
CHANGED
|
@@ -19,16 +19,6 @@ export const getUseTypescript = typescript => {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export const getOverwriteReadme = () => {
|
|
23
|
-
return {
|
|
24
|
-
type: 'list',
|
|
25
|
-
name: 'shouldReplace',
|
|
26
|
-
message: 'Do you want to overwrite the existing README.md?',
|
|
27
|
-
default: true,
|
|
28
|
-
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
22
|
let port = 3042
|
|
33
23
|
export const getPort = (nextPort) => {
|
|
34
24
|
if (nextPort === undefined) {
|
|
@@ -1,39 +1,20 @@
|
|
|
1
|
-
import { getVersion, getDependencyVersion
|
|
1
|
+
import { getVersion, getDependencyVersion } from '../utils.mjs'
|
|
2
2
|
import { createPackageJson } from '../create-package-json.mjs'
|
|
3
3
|
import { createGitignore } from '../create-gitignore.mjs'
|
|
4
4
|
import { getPkgManager } from '../get-pkg-manager.mjs'
|
|
5
5
|
import parseArgs from 'minimist'
|
|
6
|
-
import { join } from 'path'
|
|
7
6
|
import inquirer from 'inquirer'
|
|
8
|
-
import {
|
|
7
|
+
import { mkdir } from 'fs/promises'
|
|
9
8
|
import pino from 'pino'
|
|
10
9
|
import pretty from 'pino-pretty'
|
|
11
10
|
import { execa } from 'execa'
|
|
12
11
|
import ora from 'ora'
|
|
13
12
|
import createComposer from './create-composer.mjs'
|
|
14
13
|
import askDir from '../ask-dir.mjs'
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const readmeFileName = join(dir, 'README.md')
|
|
20
|
-
let isReadmeExists = await isFileAccessible(readmeFileName)
|
|
21
|
-
if (isReadmeExists) {
|
|
22
|
-
logger.debug(`${readmeFileName} found, asking to overwrite it.`)
|
|
23
|
-
const { shouldReplace } = await inquirer.prompt([getOverwriteReadme()])
|
|
24
|
-
isReadmeExists = !shouldReplace
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (isReadmeExists) {
|
|
28
|
-
logger.debug(`${readmeFileName} found, skipping creation of README.md file.`)
|
|
29
|
-
return
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const readmeFile = new URL('README.md', import.meta.url)
|
|
33
|
-
const readme = await readFile(readmeFile, 'utf-8')
|
|
34
|
-
await writeFile(readmeFileName, readme)
|
|
35
|
-
logger.debug(`${readmeFileName} successfully created.`)
|
|
36
|
-
}
|
|
14
|
+
import { getRunPackageManagerInstall, getPort } from '../cli-options.mjs'
|
|
15
|
+
import { createReadme } from '../create-readme.mjs'
|
|
16
|
+
import { stat } from 'node:fs/promises'
|
|
17
|
+
import { join } from 'path'
|
|
37
18
|
|
|
38
19
|
export const getServicesToCompose = (servicesNames) => {
|
|
39
20
|
return {
|
|
@@ -64,14 +45,22 @@ const createPlatformaticComposer = async (_args, opts) => {
|
|
|
64
45
|
const version = await getVersion()
|
|
65
46
|
const pkgManager = getPkgManager()
|
|
66
47
|
|
|
67
|
-
const projectDir = opts.dir || await askDir(logger, '.')
|
|
48
|
+
const projectDir = opts.dir || await askDir(logger, join('.', 'platformatic-composer'))
|
|
49
|
+
// checks directory
|
|
50
|
+
try {
|
|
51
|
+
await stat(projectDir)
|
|
52
|
+
logger.error(`Directory ${projectDir} already exists. Please choose another path.`)
|
|
53
|
+
process.exit(1)
|
|
54
|
+
} catch (err) {}
|
|
55
|
+
|
|
68
56
|
const isRuntimeContext = opts.isRuntimeContext || false
|
|
69
57
|
|
|
70
58
|
const toAsk = []
|
|
71
59
|
|
|
72
|
-
if
|
|
73
|
-
|
|
74
|
-
|
|
60
|
+
// Ask for port if not in runtime context
|
|
61
|
+
const portQuestion = getPort(args.port)
|
|
62
|
+
portQuestion.when = !isRuntimeContext
|
|
63
|
+
toAsk.push(portQuestion)
|
|
75
64
|
|
|
76
65
|
if (isRuntimeContext) {
|
|
77
66
|
const servicesNames = opts.runtimeContext.servicesNames.filter(
|
|
@@ -85,11 +74,28 @@ const createPlatformaticComposer = async (_args, opts) => {
|
|
|
85
74
|
if (!opts.skipPackageJson) {
|
|
86
75
|
toAsk.push(getRunPackageManagerInstall(pkgManager))
|
|
87
76
|
}
|
|
88
|
-
|
|
77
|
+
if (!opts.skipGitHubActions) {
|
|
78
|
+
toAsk.push({
|
|
79
|
+
type: 'list',
|
|
80
|
+
name: 'staticWorkspaceGitHubAction',
|
|
81
|
+
message: 'Do you want to create the github action to deploy this application to Platformatic Cloud?',
|
|
82
|
+
default: true,
|
|
83
|
+
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
type: 'list',
|
|
87
|
+
name: 'dynamicWorkspaceGitHubAction',
|
|
88
|
+
message: 'Do you want to enable PR Previews in your application?',
|
|
89
|
+
default: true,
|
|
90
|
+
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
91
|
+
})
|
|
92
|
+
}
|
|
89
93
|
const {
|
|
90
94
|
runPackageManagerInstall,
|
|
91
95
|
servicesToCompose,
|
|
92
|
-
port
|
|
96
|
+
port,
|
|
97
|
+
staticWorkspaceGitHubAction,
|
|
98
|
+
dynamicWorkspaceGitHubAction
|
|
93
99
|
} = await inquirer.prompt(toAsk)
|
|
94
100
|
|
|
95
101
|
// Create the project directory
|
|
@@ -99,10 +105,13 @@ const createPlatformaticComposer = async (_args, opts) => {
|
|
|
99
105
|
isRuntimeContext,
|
|
100
106
|
hostname: args.hostname,
|
|
101
107
|
port,
|
|
102
|
-
servicesToCompose
|
|
108
|
+
servicesToCompose,
|
|
109
|
+
staticWorkspaceGitHubAction,
|
|
110
|
+
dynamicWorkspaceGitHubAction,
|
|
111
|
+
runtimeContext: opts.runtimeContext
|
|
103
112
|
}
|
|
104
113
|
|
|
105
|
-
|
|
114
|
+
await createComposer(
|
|
106
115
|
params,
|
|
107
116
|
logger,
|
|
108
117
|
projectDir,
|
|
@@ -119,17 +128,12 @@ const createPlatformaticComposer = async (_args, opts) => {
|
|
|
119
128
|
if (!opts.skipGitignore) {
|
|
120
129
|
await createGitignore(logger, projectDir)
|
|
121
130
|
}
|
|
122
|
-
await createReadme(logger, projectDir)
|
|
131
|
+
await createReadme(logger, projectDir, 'composer')
|
|
123
132
|
|
|
124
133
|
if (runPackageManagerInstall) {
|
|
125
134
|
const spinner = ora('Installing dependencies...').start()
|
|
126
135
|
await execa(pkgManager, ['install'], { cwd: projectDir })
|
|
127
|
-
spinner.succeed(
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (!opts.skipGitHubActions) {
|
|
131
|
-
await askStaticWorkspaceGHAction(logger, env, 'composer', false, projectDir)
|
|
132
|
-
await askDynamicWorkspaceCreateGHAction(logger, env, 'composer', false, projectDir)
|
|
136
|
+
spinner.succeed()
|
|
133
137
|
}
|
|
134
138
|
}
|
|
135
139
|
|
|
@@ -2,21 +2,29 @@ import { readFile, writeFile, appendFile } from 'fs/promises'
|
|
|
2
2
|
import { findComposerConfigFile, isFileAccessible } from '../utils.mjs'
|
|
3
3
|
import { join } from 'path'
|
|
4
4
|
import * as desm from 'desm'
|
|
5
|
+
import { generatePlugins, generateRouteWithTypesSupport } from '../create-plugins.mjs'
|
|
6
|
+
import { createDynamicWorkspaceGHAction, createStaticWorkspaceGHAction } from '../ghaction.mjs'
|
|
5
7
|
|
|
6
|
-
function generateConfig (isRuntimeContext, version, servicesToCompose) {
|
|
8
|
+
function generateConfig (isRuntimeContext, version, servicesToCompose, envPrefix) {
|
|
7
9
|
const config = {
|
|
8
10
|
$schema: `https://platformatic.dev/schemas/v${version}/composer`,
|
|
9
11
|
composer: {
|
|
10
12
|
services: [{
|
|
11
13
|
id: 'example',
|
|
12
|
-
origin:
|
|
14
|
+
origin: `{PLT_${envPrefix}EXAMPLE_ORIGIN}`,
|
|
13
15
|
openapi: {
|
|
14
16
|
url: '/documentation/json'
|
|
15
17
|
}
|
|
16
18
|
}],
|
|
17
19
|
refreshTimeout: 1000
|
|
18
20
|
},
|
|
19
|
-
watch: true
|
|
21
|
+
watch: true,
|
|
22
|
+
plugins: {
|
|
23
|
+
paths: [
|
|
24
|
+
{ path: './plugins', encapsulate: false },
|
|
25
|
+
'./routes'
|
|
26
|
+
]
|
|
27
|
+
}
|
|
20
28
|
}
|
|
21
29
|
|
|
22
30
|
if (!isRuntimeContext) {
|
|
@@ -44,18 +52,18 @@ function generateConfig (isRuntimeContext, version, servicesToCompose) {
|
|
|
44
52
|
return config
|
|
45
53
|
}
|
|
46
54
|
|
|
47
|
-
function generateEnv (isRuntimeContext, hostname, port) {
|
|
55
|
+
function generateEnv (isRuntimeContext, hostname, port, envPrefix) {
|
|
48
56
|
let env = ''
|
|
49
57
|
|
|
50
58
|
if (!isRuntimeContext) {
|
|
51
59
|
env += `\
|
|
52
60
|
PLT_SERVER_HOSTNAME=${hostname}
|
|
53
61
|
PORT=${port}
|
|
54
|
-
PLT_SERVER_LOGGER_LEVEL=info
|
|
55
|
-
PLT_EXAMPLE_ORIGIN=
|
|
56
|
-
`
|
|
62
|
+
PLT_SERVER_LOGGER_LEVEL=info`
|
|
57
63
|
}
|
|
58
|
-
|
|
64
|
+
env += `
|
|
65
|
+
PLT_${envPrefix}EXAMPLE_ORIGIN=
|
|
66
|
+
`
|
|
59
67
|
return env
|
|
60
68
|
}
|
|
61
69
|
|
|
@@ -63,10 +71,17 @@ async function createComposer (
|
|
|
63
71
|
params,
|
|
64
72
|
logger,
|
|
65
73
|
currentDir = process.cwd(),
|
|
66
|
-
version
|
|
74
|
+
version,
|
|
75
|
+
staticWorkspaceGitHubAction,
|
|
76
|
+
dynamicWorkspaceGitHubAction
|
|
67
77
|
) {
|
|
68
|
-
const { isRuntimeContext, hostname, port, servicesToCompose = [] } = params
|
|
78
|
+
const { isRuntimeContext, hostname, port, servicesToCompose = [], runtimeContext } = params
|
|
69
79
|
|
|
80
|
+
const composerEnv = {
|
|
81
|
+
PLT_SERVER_LOGGER_LEVEL: 'info',
|
|
82
|
+
PORT: port,
|
|
83
|
+
PLT_SERVER_HOSTNAME: hostname
|
|
84
|
+
}
|
|
70
85
|
if (!version) {
|
|
71
86
|
const pkg = await readFile(desm.join(import.meta.url, '..', '..', 'package.json'))
|
|
72
87
|
version = JSON.parse(pkg).version
|
|
@@ -74,11 +89,13 @@ async function createComposer (
|
|
|
74
89
|
const accessibleConfigFilename = await findComposerConfigFile(currentDir)
|
|
75
90
|
|
|
76
91
|
if (accessibleConfigFilename === undefined) {
|
|
77
|
-
const
|
|
92
|
+
const envPrefix = runtimeContext !== undefined ? `${runtimeContext.envPrefix}_` : ''
|
|
93
|
+
|
|
94
|
+
const config = generateConfig(isRuntimeContext, version, servicesToCompose, envPrefix)
|
|
78
95
|
await writeFile(join(currentDir, 'platformatic.composer.json'), JSON.stringify(config, null, 2))
|
|
79
96
|
logger.info('Configuration file platformatic.composer.json successfully created.')
|
|
80
97
|
|
|
81
|
-
const env = generateEnv(isRuntimeContext, hostname, port)
|
|
98
|
+
const env = generateEnv(isRuntimeContext, hostname, port, envPrefix)
|
|
82
99
|
const envFileExists = await isFileAccessible('.env', currentDir)
|
|
83
100
|
await appendFile(join(currentDir, '.env'), env)
|
|
84
101
|
await writeFile(join(currentDir, '.env.sample'), env)
|
|
@@ -91,12 +108,17 @@ async function createComposer (
|
|
|
91
108
|
} else {
|
|
92
109
|
logger.info(`Configuration file ${accessibleConfigFilename} found, skipping creation of configuration file.`)
|
|
93
110
|
}
|
|
111
|
+
await generatePlugins(logger, currentDir, false, 'composer')
|
|
112
|
+
await generateRouteWithTypesSupport(logger, currentDir, false)
|
|
94
113
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
114
|
+
if (staticWorkspaceGitHubAction) {
|
|
115
|
+
await createStaticWorkspaceGHAction(logger, composerEnv, './platformatic.service.json', currentDir, false)
|
|
116
|
+
}
|
|
117
|
+
if (dynamicWorkspaceGitHubAction) {
|
|
118
|
+
await createDynamicWorkspaceGHAction(logger, composerEnv, './platformatic.service.json', currentDir, false)
|
|
99
119
|
}
|
|
120
|
+
|
|
121
|
+
return composerEnv
|
|
100
122
|
}
|
|
101
123
|
|
|
102
124
|
export default createComposer
|
|
@@ -14,7 +14,8 @@ const packageJsonTemplate = async (addTSBuild, fastifyVersion, platVersion) => {
|
|
|
14
14
|
|
|
15
15
|
const pkg = {
|
|
16
16
|
scripts: {
|
|
17
|
-
start: 'platformatic start'
|
|
17
|
+
start: 'platformatic start',
|
|
18
|
+
test: 'node --test test/**'
|
|
18
19
|
},
|
|
19
20
|
devDependencies: {
|
|
20
21
|
fastify: `^${fastifyVersion}`
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
import { readFile, writeFile } from 'fs/promises'
|
|
3
|
+
import { join as desmJoin } from 'desm'
|
|
4
|
+
import { join } from 'path'
|
|
5
|
+
|
|
6
|
+
export const createReadme = async (logger, dir = '.', type) => {
|
|
7
|
+
const readmeFileTarget = join(dir, 'README.md')
|
|
8
|
+
const readmeFileSource = desmJoin(import.meta.url, type, 'README.md')
|
|
9
|
+
const readme = await readFile(readmeFileSource, 'utf-8')
|
|
10
|
+
await writeFile(readmeFileTarget, readme)
|
|
11
|
+
logger.debug(`${readmeFileTarget} successfully created.`)
|
|
12
|
+
}
|
package/src/db/create-db-cli.mjs
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { getVersion, getDependencyVersion
|
|
1
|
+
import { getVersion, getDependencyVersion } from '../utils.mjs'
|
|
2
2
|
import { createPackageJson } from '../create-package-json.mjs'
|
|
3
3
|
import { createGitignore } from '../create-gitignore.mjs'
|
|
4
4
|
import { getPkgManager } from '../get-pkg-manager.mjs'
|
|
5
5
|
import parseArgs from 'minimist'
|
|
6
|
-
import { join } from 'path'
|
|
7
6
|
import inquirer from 'inquirer'
|
|
8
7
|
import which from 'which'
|
|
9
|
-
import {
|
|
8
|
+
import { mkdir, stat } from 'fs/promises'
|
|
10
9
|
import pino from 'pino'
|
|
11
10
|
import pretty from 'pino-pretty'
|
|
12
11
|
import { execa } from 'execa'
|
|
13
12
|
import ora from 'ora'
|
|
14
13
|
import { getConnectionString, createDB } from './create-db.mjs'
|
|
15
14
|
import askDir from '../ask-dir.mjs'
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
15
|
+
import { getRunPackageManagerInstall, getUseTypescript, getPort } from '../cli-options.mjs'
|
|
16
|
+
import { createReadme } from '../create-readme.mjs'
|
|
17
|
+
import { join } from 'node:path'
|
|
18
18
|
|
|
19
19
|
const databases = [{
|
|
20
20
|
value: 'sqlite',
|
|
@@ -30,26 +30,6 @@ const databases = [{
|
|
|
30
30
|
name: 'MariaDB'
|
|
31
31
|
}]
|
|
32
32
|
|
|
33
|
-
export const createReadme = async (logger, dir = '.') => {
|
|
34
|
-
const readmeFileName = join(dir, 'README.md')
|
|
35
|
-
let isReadmeExists = await isFileAccessible(readmeFileName)
|
|
36
|
-
if (isReadmeExists) {
|
|
37
|
-
logger.debug(`${readmeFileName} found, asking to overwrite it.`)
|
|
38
|
-
const { shouldReplace } = await inquirer.prompt([getOverwriteReadme()])
|
|
39
|
-
isReadmeExists = !shouldReplace
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (isReadmeExists) {
|
|
43
|
-
logger.debug(`${readmeFileName} found, skipping creation of README.md file.`)
|
|
44
|
-
return
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const readmeFile = new URL('README.md', import.meta.url)
|
|
48
|
-
const readme = await readFile(readmeFile, 'utf-8')
|
|
49
|
-
await writeFile(readmeFileName, readme)
|
|
50
|
-
logger.debug(`${readmeFileName} successfully created.`)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
33
|
export function parseDBArgs (_args) {
|
|
54
34
|
return parseArgs(_args, {
|
|
55
35
|
default: {
|
|
@@ -79,8 +59,27 @@ const createPlatformaticDB = async (_args, opts) => {
|
|
|
79
59
|
const args = parseDBArgs(_args)
|
|
80
60
|
const version = await getVersion()
|
|
81
61
|
const pkgManager = getPkgManager()
|
|
82
|
-
const projectDir = opts.dir || await askDir(logger, '.')
|
|
62
|
+
const projectDir = opts.dir || await askDir(logger, join('.', 'platformatic-db'))
|
|
63
|
+
|
|
64
|
+
// Create the project directory
|
|
65
|
+
try {
|
|
66
|
+
await stat(projectDir)
|
|
67
|
+
logger.error(`Directory ${projectDir} already exists. Please choose another path.`)
|
|
68
|
+
process.exit(1)
|
|
69
|
+
} catch (err) {}
|
|
70
|
+
|
|
83
71
|
const isRuntimeContext = opts.isRuntimeContext || false
|
|
72
|
+
const toAsk = []
|
|
73
|
+
|
|
74
|
+
// Ask for port if not in runtime context
|
|
75
|
+
const portQuestion = getPort(args.port)
|
|
76
|
+
portQuestion.when = !isRuntimeContext
|
|
77
|
+
toAsk.push(portQuestion)
|
|
78
|
+
|
|
79
|
+
// Ask to install deps
|
|
80
|
+
const installDepsQuestion = getRunPackageManagerInstall(pkgManager)
|
|
81
|
+
installDepsQuestion.when = !opts.skipPackageJson
|
|
82
|
+
toAsk.push(installDepsQuestion)
|
|
84
83
|
|
|
85
84
|
const { database } = await inquirer.prompt({
|
|
86
85
|
type: 'list',
|
|
@@ -123,16 +122,27 @@ const createPlatformaticDB = async (_args, opts) => {
|
|
|
123
122
|
break
|
|
124
123
|
}
|
|
125
124
|
}
|
|
126
|
-
|
|
125
|
+
|
|
126
|
+
toAsk.push({
|
|
127
127
|
type: 'list',
|
|
128
128
|
name: 'defaultMigrations',
|
|
129
129
|
message: 'Do you want to create default migrations?',
|
|
130
130
|
default: true,
|
|
131
131
|
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
132
|
-
}
|
|
132
|
+
})
|
|
133
|
+
toAsk.push({
|
|
134
|
+
type: 'list',
|
|
135
|
+
name: 'applyMigrations',
|
|
136
|
+
message: 'Do you want to apply migrations?',
|
|
137
|
+
default: true,
|
|
138
|
+
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }],
|
|
139
|
+
when: (answers) => {
|
|
140
|
+
return answers.defaultMigrations
|
|
141
|
+
}
|
|
142
|
+
})
|
|
133
143
|
|
|
134
144
|
if (args.plugin === false) {
|
|
135
|
-
|
|
145
|
+
toAsk.push({
|
|
136
146
|
type: 'list',
|
|
137
147
|
name: 'generatePlugin',
|
|
138
148
|
message: 'Do you want to create a plugin?',
|
|
@@ -140,13 +150,29 @@ const createPlatformaticDB = async (_args, opts) => {
|
|
|
140
150
|
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
141
151
|
})
|
|
142
152
|
}
|
|
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
|
-
}
|
|
148
153
|
|
|
149
|
-
|
|
154
|
+
toAsk.push(getUseTypescript(args.typescript))
|
|
155
|
+
|
|
156
|
+
toAsk.push({
|
|
157
|
+
type: 'list',
|
|
158
|
+
name: 'staticWorkspaceGitHubAction',
|
|
159
|
+
message: 'Do you want to create the github action to deploy this application to Platformatic Cloud?',
|
|
160
|
+
default: true,
|
|
161
|
+
when: !opts.skipGitHubActions,
|
|
162
|
+
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
type: 'list',
|
|
166
|
+
name: 'dynamicWorkspaceGitHubAction',
|
|
167
|
+
message: 'Do you want to enable PR Previews in your application?',
|
|
168
|
+
default: true,
|
|
169
|
+
when: !opts.skipGitHubActions,
|
|
170
|
+
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
// Promtp for questions
|
|
174
|
+
const wizardOptions = await inquirer.prompt(toAsk)
|
|
175
|
+
|
|
150
176
|
await mkdir(projectDir, { recursive: true })
|
|
151
177
|
|
|
152
178
|
const generatePlugin = args.plugin || wizardOptions.generatePlugin
|
|
@@ -162,10 +188,13 @@ const createPlatformaticDB = async (_args, opts) => {
|
|
|
162
188
|
migrations: wizardOptions.defaultMigrations ? args.migrations : '',
|
|
163
189
|
plugin: generatePlugin,
|
|
164
190
|
types: useTypes,
|
|
165
|
-
typescript: useTypescript
|
|
191
|
+
typescript: useTypescript,
|
|
192
|
+
staticWorkspaceGitHubAction: wizardOptions.staticWorkspaceGitHubAction,
|
|
193
|
+
dynamicWorkspaceGitHubAction: wizardOptions.dynamicWorkspaceGitHubAction,
|
|
194
|
+
runtimeContext: opts.runtimeContext
|
|
166
195
|
}
|
|
167
196
|
|
|
168
|
-
|
|
197
|
+
await createDB(params, logger, projectDir, version)
|
|
169
198
|
|
|
170
199
|
const fastifyVersion = await getDependencyVersion('fastify')
|
|
171
200
|
|
|
@@ -181,25 +210,19 @@ const createPlatformaticDB = async (_args, opts) => {
|
|
|
181
210
|
// Create the package.json, .gitignore, readme
|
|
182
211
|
await createPackageJson(version, fastifyVersion, logger, projectDir, useTypescript, scripts, dependencies)
|
|
183
212
|
await createGitignore(logger, projectDir)
|
|
184
|
-
await createReadme(logger, projectDir)
|
|
213
|
+
await createReadme(logger, projectDir, 'db')
|
|
185
214
|
|
|
186
215
|
let hasPlatformaticInstalled = false
|
|
187
|
-
if (
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
if (runPackageManagerInstall) {
|
|
193
|
-
const spinner = ora('Installing dependencies...').start()
|
|
194
|
-
await execa(pkgManager, ['install'], { cwd: projectDir })
|
|
195
|
-
spinner.succeed('...done!')
|
|
196
|
-
hasPlatformaticInstalled = true
|
|
197
|
-
}
|
|
216
|
+
if (wizardOptions.runPackageManagerInstall) {
|
|
217
|
+
const spinner = ora('Installing dependencies...').start()
|
|
218
|
+
await execa(pkgManager, ['install'], { cwd: projectDir })
|
|
219
|
+
spinner.succeed()
|
|
220
|
+
hasPlatformaticInstalled = true
|
|
198
221
|
}
|
|
199
222
|
|
|
200
223
|
if (!hasPlatformaticInstalled) {
|
|
201
224
|
try {
|
|
202
|
-
const npmLs = JSON.parse(await execa('npm', ['ls', '--json']))
|
|
225
|
+
const npmLs = JSON.parse(await execa('npm', ['ls', '--json']).toString())
|
|
203
226
|
hasPlatformaticInstalled = !!npmLs.dependencies.platformatic
|
|
204
227
|
} catch {
|
|
205
228
|
// Ignore all errors, this can fail
|
|
@@ -216,43 +239,28 @@ const createPlatformaticDB = async (_args, opts) => {
|
|
|
216
239
|
// - run the migrations
|
|
217
240
|
// - generate types
|
|
218
241
|
// if we don't generate migrations, we don't ask to apply them (the folder might not exist)
|
|
219
|
-
if (wizardOptions.defaultMigrations) {
|
|
220
|
-
const
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
if (applyMigrations) {
|
|
229
|
-
const spinner = ora('Applying migrations...').start()
|
|
230
|
-
// We need to apply migrations using the platformatic installed in the project
|
|
231
|
-
try {
|
|
232
|
-
await execa(pkgManager, ['exec', 'platformatic', 'db', 'migrations', 'apply'], { cwd: projectDir })
|
|
233
|
-
spinner.succeed('...done!')
|
|
234
|
-
} catch (err) {
|
|
235
|
-
logger.trace({ err })
|
|
236
|
-
spinner.fail('...failed! Try again by running "platformatic db migrations apply"')
|
|
237
|
-
}
|
|
242
|
+
if (wizardOptions.defaultMigrations && wizardOptions.applyMigrations) {
|
|
243
|
+
const spinner = ora('Applying migrations...').start()
|
|
244
|
+
// We need to apply migrations using the platformatic installed in the project
|
|
245
|
+
try {
|
|
246
|
+
await execa(pkgManager, ['exec', 'platformatic', 'db', 'migrations', 'apply'], { cwd: projectDir })
|
|
247
|
+
spinner.succeed()
|
|
248
|
+
} catch (err) {
|
|
249
|
+
logger.trace({ err })
|
|
250
|
+
spinner.fail('Failed applying migrations! Try again by running "platformatic db migrations apply"')
|
|
238
251
|
}
|
|
239
252
|
}
|
|
240
253
|
if (generatePlugin) {
|
|
241
254
|
const spinner = ora('Generating types...').start()
|
|
242
255
|
try {
|
|
243
256
|
await execa(pkgManager, ['exec', 'platformatic', 'db', 'types'], { cwd: projectDir })
|
|
244
|
-
spinner.succeed(
|
|
257
|
+
spinner.succeed()
|
|
245
258
|
} catch (err) {
|
|
246
259
|
logger.trace({ err })
|
|
247
|
-
spinner.fail('
|
|
260
|
+
spinner.fail('Failed to generate Types. Try again by running "platformatic service types"')
|
|
248
261
|
}
|
|
249
262
|
}
|
|
250
263
|
}
|
|
251
|
-
|
|
252
|
-
if (!opts.skipGitHubActions) {
|
|
253
|
-
await askStaticWorkspaceGHAction(logger, env, 'db', useTypescript, projectDir)
|
|
254
|
-
await askDynamicWorkspaceCreateGHAction(logger, env, 'db', useTypescript, projectDir)
|
|
255
|
-
}
|
|
256
264
|
}
|
|
257
265
|
|
|
258
266
|
export default createPlatformaticDB
|