create-platformatic 1.49.1 → 1.51.8
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 +2 -5
- package/package.json +15 -12
- package/src/colors.mjs +4 -0
- package/src/create-git-repository.mjs +108 -0
- package/src/index.mjs +306 -0
- package/src/say.mjs +20 -0
- package/src/utils.mjs +130 -2
- package/test/cli/.gitkeep +0 -0
- package/test/cli/README.md +8 -0
- package/test/cli/composer.test.mjs +80 -0
- package/test/cli/db.test.mjs +86 -0
- package/test/cli/helper.mjs +159 -0
- package/test/cli/runtime.test.mjs +104 -0
- package/test/cli/service.test.mjs +200 -0
- package/test/cli/stackable.test.mjs +101 -0
- package/test/cli/timeout.mjs +3 -0
- package/test/unit/create-git-repository.test.mjs +61 -0
- package/test/unit/fetch-stackables.mjs +71 -0
- package/test/unit/utils.test.mjs +291 -0
- package/help/db.txt +0 -11
- package/help/help.txt +0 -8
- package/help/service.txt +0 -7
- package/src/ghaction.mjs +0 -185
package/create-platformatic.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { createPlatformatic } from '
|
|
2
|
+
import { createPlatformatic } from './src/index.mjs'
|
|
3
3
|
import isMain from 'es-main'
|
|
4
4
|
import parseArgs from 'minimist'
|
|
5
5
|
import { readFile } from 'fs/promises'
|
|
@@ -30,7 +30,4 @@ if (isMain(import.meta)) {
|
|
|
30
30
|
await createPlatformatic(_args)
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
export {
|
|
34
|
-
createStaticWorkspaceGHAction,
|
|
35
|
-
createDynamicWorkspaceGHAction
|
|
36
|
-
} from './src/ghaction.mjs'
|
|
33
|
+
export { createPlatformatic }
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-platformatic",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Create platformatic
|
|
3
|
+
"version": "1.51.8",
|
|
4
|
+
"description": "Create platformatic application interactive tool",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/platformatic/platformatic.git"
|
|
@@ -10,10 +10,10 @@
|
|
|
10
10
|
".": "./create-platformatic.mjs"
|
|
11
11
|
},
|
|
12
12
|
"bin": {
|
|
13
|
-
"create-platformatic": "./create-platformatic.mjs"
|
|
13
|
+
"create-platformatic-auto": "./create-platformatic.mjs"
|
|
14
14
|
},
|
|
15
15
|
"license": "Apache-2.0",
|
|
16
|
-
"author": "
|
|
16
|
+
"author": "Matteo Collina <hello@matteocollina.com>",
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@types/node": "^20.11.28",
|
|
19
19
|
"boring-name-generator": "^1.0.3",
|
|
@@ -30,17 +30,18 @@
|
|
|
30
30
|
"ora": "^6.3.1",
|
|
31
31
|
"pino": "^8.19.0",
|
|
32
32
|
"pino-pretty": "^11.0.0",
|
|
33
|
+
"resolve": "^1.22.8",
|
|
33
34
|
"semver": "^7.6.0",
|
|
34
35
|
"strip-ansi": "^7.1.0",
|
|
35
36
|
"undici": "^6.9.0",
|
|
36
37
|
"which": "^3.0.1",
|
|
37
|
-
"@platformatic/config": "1.
|
|
38
|
-
"@platformatic/
|
|
39
|
-
"@platformatic/
|
|
38
|
+
"@platformatic/config": "1.51.8",
|
|
39
|
+
"@platformatic/generators": "1.51.8",
|
|
40
|
+
"@platformatic/utils": "1.51.8"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|
|
42
43
|
"ajv": "^8.12.0",
|
|
43
|
-
"borp": "^0.
|
|
44
|
+
"borp": "^0.16.0",
|
|
44
45
|
"c8": "^10.0.0",
|
|
45
46
|
"cross-env": "^7.0.3",
|
|
46
47
|
"dotenv": "^16.4.5",
|
|
@@ -50,12 +51,14 @@
|
|
|
50
51
|
"standard": "^17.1.0",
|
|
51
52
|
"typescript": "~5.5.0",
|
|
52
53
|
"yaml": "^2.4.1",
|
|
53
|
-
"@platformatic/
|
|
54
|
-
"@platformatic/
|
|
54
|
+
"@platformatic/composer": "1.51.8",
|
|
55
|
+
"@platformatic/db": "1.51.8",
|
|
56
|
+
"@platformatic/runtime": "1.51.8",
|
|
57
|
+
"@platformatic/service": "1.51.8"
|
|
55
58
|
},
|
|
56
59
|
"scripts": {
|
|
57
|
-
"test:cli": "borp --pattern \"test/cli
|
|
58
|
-
"test:unit": "pnpm run lint && cross-env NODE_OPTIONS=\"--loader=esmock --no-warnings\" borp --
|
|
60
|
+
"test:cli": "borp --pattern \"test/cli/*test.mjs\" --timeout 120000 --concurrency=1",
|
|
61
|
+
"test:unit": "pnpm run lint && cross-env NODE_OPTIONS=\"--loader=esmock --no-warnings\" borp --pattern \"test/unit/*test.mjs\" --timeout 120000 --concurrency=1",
|
|
59
62
|
"test": "npm run test:unit && npm run test:cli",
|
|
60
63
|
"lint": "standard | snazzy"
|
|
61
64
|
}
|
package/src/colors.mjs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { execa } from 'execa'
|
|
2
|
+
|
|
3
|
+
export const GIT_FIRST_COMMIT_MESSAGE = 'Platformatic project started! 🚀'
|
|
4
|
+
export const GIT_MAIN_BRANCH = 'main'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Creates a Git repository and performs the initial commit if it doesn't already exist.
|
|
8
|
+
*
|
|
9
|
+
* This function checks if Git is installed, initializes a Git repository in the specified
|
|
10
|
+
* directory if it's not already a Git repository, and performs the initial commit.
|
|
11
|
+
*
|
|
12
|
+
* @param {import('pino.').BaseLogger} logger - The logger interface for logging messages.
|
|
13
|
+
* @param {string} [dir='.'] - The target directory where the Git repository should be created.
|
|
14
|
+
*/
|
|
15
|
+
export async function createGitRepository (logger, dir = '.') {
|
|
16
|
+
if (!await isGitInstalled()) {
|
|
17
|
+
logger.error('Git is not installed')
|
|
18
|
+
return
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!await gitInit(logger, dir)) {
|
|
22
|
+
return
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!await gitCommit(logger, dir)) {
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
logger.info('Git repository initialized.')
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Checks if Git is installed on the system.
|
|
34
|
+
*
|
|
35
|
+
* @async
|
|
36
|
+
* @returns {Promise<boolean>} A Promise that resolves to true if Git is installed, false otherwise.
|
|
37
|
+
*/
|
|
38
|
+
async function isGitInstalled () {
|
|
39
|
+
try {
|
|
40
|
+
await execa('git', ['--version'])
|
|
41
|
+
return true
|
|
42
|
+
} catch (err) {
|
|
43
|
+
return false
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Checks if a Git repository exists in the specified directory.
|
|
49
|
+
*
|
|
50
|
+
* @async
|
|
51
|
+
* @param {string} dir - The directory to check for a Git repository.
|
|
52
|
+
* @returns {Promise<boolean>} A Promise that resolves to true if a Git repository exists in the directory, false otherwise.
|
|
53
|
+
*/
|
|
54
|
+
async function doesGitRepositoryExist (dir) {
|
|
55
|
+
try {
|
|
56
|
+
await execa('git', ['rev-parse', '--is-inside-work-tree'], { cwd: dir })
|
|
57
|
+
return true
|
|
58
|
+
} catch (e) {
|
|
59
|
+
return false
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Initializes a Git repository in the specified directory if it doesn't already exist.
|
|
65
|
+
*
|
|
66
|
+
* @async
|
|
67
|
+
* @param {import('pino.').BaseLogger} - The logger object for logging messages.
|
|
68
|
+
* @param {string} dir - The directory where the Git repository should be initialized.
|
|
69
|
+
* @returns {Promise<boolean>} A Promise that resolves to true if the Git repository is successfully initialized, false otherwise.
|
|
70
|
+
*/
|
|
71
|
+
async function gitInit (logger, dir) {
|
|
72
|
+
try {
|
|
73
|
+
if (await doesGitRepositoryExist(dir)) {
|
|
74
|
+
logger.info('Git repository already exists.')
|
|
75
|
+
return false
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
await execa('git', ['init', '-b', GIT_MAIN_BRANCH], { cwd: dir })
|
|
79
|
+
logger.debug('Git repository initialized.')
|
|
80
|
+
return true
|
|
81
|
+
} catch (err) {
|
|
82
|
+
logger.error('Git repository init failed.')
|
|
83
|
+
logger.debug({ err })
|
|
84
|
+
return false
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Commits changes in a Git repository located in the specified directory.
|
|
90
|
+
*
|
|
91
|
+
* @async
|
|
92
|
+
* @param {import('pino.').BaseLogger} - The logger object for logging messages.
|
|
93
|
+
* @param {string} dir - The directory of the Git repository where changes should be committed.
|
|
94
|
+
* @returns {Promise<boolean>} A Promise that resolves to true if the Git commit is successful, false otherwise.
|
|
95
|
+
*/
|
|
96
|
+
async function gitCommit (logger, dir) {
|
|
97
|
+
try {
|
|
98
|
+
await execa('git', ['add', '-A'], { cwd: dir })
|
|
99
|
+
await execa('git', ['commit', '-n', '-m', GIT_FIRST_COMMIT_MESSAGE], { cwd: dir })
|
|
100
|
+
logger.debug('Git commit done.')
|
|
101
|
+
return true
|
|
102
|
+
} catch (err) {
|
|
103
|
+
console.log(err)
|
|
104
|
+
logger.error('Git commit failed.')
|
|
105
|
+
logger.debug({ err })
|
|
106
|
+
return false
|
|
107
|
+
}
|
|
108
|
+
}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { say } from './say.mjs'
|
|
2
|
+
import path, { basename, join } from 'node:path'
|
|
3
|
+
import inquirer from 'inquirer'
|
|
4
|
+
import generateName from 'boring-name-generator'
|
|
5
|
+
import { getUsername, getVersion, minimumSupportedNodeVersions, isCurrentVersionSupported, safeMkdir } from './utils.mjs'
|
|
6
|
+
import { createGitRepository } from './create-git-repository.mjs'
|
|
7
|
+
import { getPkgManager } from '@platformatic/utils'
|
|
8
|
+
import { StackableGenerator } from '@platformatic/generators'
|
|
9
|
+
import pino from 'pino'
|
|
10
|
+
import pretty from 'pino-pretty'
|
|
11
|
+
import { execa } from 'execa'
|
|
12
|
+
import parseArgs from 'minimist'
|
|
13
|
+
import ora from 'ora'
|
|
14
|
+
import { pathToFileURL } from 'node:url'
|
|
15
|
+
import { writeFile } from 'node:fs/promises'
|
|
16
|
+
import { request } from 'undici'
|
|
17
|
+
import { setTimeout } from 'node:timers/promises'
|
|
18
|
+
import resolve from 'resolve'
|
|
19
|
+
|
|
20
|
+
const MARKETPLACE_HOST = 'https://marketplace.platformatic.dev'
|
|
21
|
+
|
|
22
|
+
export async function fetchStackables (marketplaceHost) {
|
|
23
|
+
marketplaceHost = marketplaceHost || MARKETPLACE_HOST
|
|
24
|
+
|
|
25
|
+
const stackablesRequest = request(marketplaceHost + '/templates')
|
|
26
|
+
const stackablesRequestTimeout = setTimeout(5000, new Error('Request timed out'))
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
const { statusCode, body } = await Promise.race([
|
|
30
|
+
stackablesRequest,
|
|
31
|
+
stackablesRequestTimeout
|
|
32
|
+
])
|
|
33
|
+
if (statusCode === 200) {
|
|
34
|
+
return (await body.json()).map(stackable => stackable.name)
|
|
35
|
+
}
|
|
36
|
+
} catch (err) {}
|
|
37
|
+
|
|
38
|
+
return ['@platformatic/composer', '@platformatic/db', '@platformatic/service']
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function chooseStackable (stackables) {
|
|
42
|
+
const options = await inquirer.prompt({
|
|
43
|
+
type: 'list',
|
|
44
|
+
name: 'type',
|
|
45
|
+
message: 'Which kind of project do you want to create?',
|
|
46
|
+
default: stackables.indexOf('@platformatic/service'),
|
|
47
|
+
choices: stackables
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
return options.type
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function importOrLocal ({ pkgManager, name, projectDir, pkg }) {
|
|
54
|
+
try {
|
|
55
|
+
return await import(pkg)
|
|
56
|
+
} catch (err) {
|
|
57
|
+
try {
|
|
58
|
+
const fileToImport = resolve.sync(pkg, { basedir: projectDir })
|
|
59
|
+
return await import(pathToFileURL(fileToImport))
|
|
60
|
+
} catch { }
|
|
61
|
+
|
|
62
|
+
const spinner = ora(`Installing ${pkg}...`).start()
|
|
63
|
+
await execa(pkgManager, ['install', pkg], { cwd: projectDir })
|
|
64
|
+
spinner.succeed()
|
|
65
|
+
|
|
66
|
+
const fileToImport = resolve.sync(pkg, { basedir: projectDir })
|
|
67
|
+
return await import(pathToFileURL(fileToImport))
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export const createPlatformatic = async (argv) => {
|
|
72
|
+
const args = parseArgs(argv, {
|
|
73
|
+
default: {
|
|
74
|
+
install: true
|
|
75
|
+
},
|
|
76
|
+
boolean: ['install'],
|
|
77
|
+
string: ['global-config', 'marketplace-host']
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
const username = await getUsername()
|
|
81
|
+
const version = await getVersion()
|
|
82
|
+
const greeting = username ? `Hello ${username},` : 'Hello,'
|
|
83
|
+
await say(`${greeting} welcome to ${version ? `Platformatic ${version}!` : 'Platformatic!'}`)
|
|
84
|
+
|
|
85
|
+
const currentVersion = process.versions.node
|
|
86
|
+
const supported = isCurrentVersionSupported(currentVersion)
|
|
87
|
+
if (!supported) {
|
|
88
|
+
const supportedVersions = minimumSupportedNodeVersions.join(' or >= ')
|
|
89
|
+
await say(`Platformatic is not supported on Node.js v${currentVersion}.`)
|
|
90
|
+
await say(`Please use one of the following Node.js versions >= ${supportedVersions}.`)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const logger = pino(pretty({
|
|
94
|
+
translateTime: 'SYS:HH:MM:ss',
|
|
95
|
+
ignore: 'hostname,pid'
|
|
96
|
+
}))
|
|
97
|
+
|
|
98
|
+
const pkgManager = getPkgManager()
|
|
99
|
+
|
|
100
|
+
const { projectType } = await inquirer.prompt({
|
|
101
|
+
type: 'list',
|
|
102
|
+
name: 'projectType',
|
|
103
|
+
message: 'What kind of project do you want to create?',
|
|
104
|
+
default: 'application',
|
|
105
|
+
choices: [
|
|
106
|
+
{ name: 'Application', value: 'application' },
|
|
107
|
+
{ name: 'Stackable', value: 'stackable' }
|
|
108
|
+
]
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
if (projectType === 'application') {
|
|
112
|
+
await createApplication(args, logger, pkgManager)
|
|
113
|
+
} else {
|
|
114
|
+
await createStackable(args, logger, pkgManager)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async function createApplication (args, logger, pkgManager) {
|
|
119
|
+
const optionsDir = await inquirer.prompt({
|
|
120
|
+
type: 'input',
|
|
121
|
+
name: 'dir',
|
|
122
|
+
message: 'Where would you like to create your project?',
|
|
123
|
+
default: 'platformatic'
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
const projectDir = path.resolve(process.cwd(), optionsDir.dir)
|
|
127
|
+
const projectName = basename(projectDir)
|
|
128
|
+
|
|
129
|
+
await safeMkdir(projectDir)
|
|
130
|
+
|
|
131
|
+
const runtime = await importOrLocal({
|
|
132
|
+
pkgManager,
|
|
133
|
+
name: projectName,
|
|
134
|
+
projectDir,
|
|
135
|
+
pkg: '@platformatic/runtime'
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
const generator = new runtime.Generator({
|
|
139
|
+
logger,
|
|
140
|
+
name: projectName,
|
|
141
|
+
inquirer
|
|
142
|
+
})
|
|
143
|
+
generator.setConfig({
|
|
144
|
+
...generator.config,
|
|
145
|
+
targetDirectory: projectDir
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
await generator.populateFromExistingConfig()
|
|
149
|
+
if (generator.existingConfig) {
|
|
150
|
+
await say('Using existing configuration')
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const stackables = await fetchStackables(args['marketplace-host'])
|
|
154
|
+
|
|
155
|
+
const names = []
|
|
156
|
+
|
|
157
|
+
while (true) {
|
|
158
|
+
const stackableName = await chooseStackable(stackables)
|
|
159
|
+
// await say(`Creating a ${stackable} project in ${projectDir}...`)
|
|
160
|
+
|
|
161
|
+
const stackable = await importOrLocal({
|
|
162
|
+
pkgManager,
|
|
163
|
+
name: projectName,
|
|
164
|
+
projectDir,
|
|
165
|
+
pkg: stackableName
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
const { serviceName } = await inquirer.prompt({
|
|
169
|
+
type: 'input',
|
|
170
|
+
name: 'serviceName',
|
|
171
|
+
message: 'What is the name of the service?',
|
|
172
|
+
default: generateName().dashed,
|
|
173
|
+
validate: (value) => {
|
|
174
|
+
if (value.length === 0) {
|
|
175
|
+
return 'Please enter a name'
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (value.includes(' ')) {
|
|
179
|
+
return 'Please enter a name without spaces'
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (names.includes(value)) {
|
|
183
|
+
return 'This name is already used, please choose another one.'
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return true
|
|
187
|
+
}
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
names.push(serviceName)
|
|
191
|
+
|
|
192
|
+
const stackableGenerator = new stackable.Generator({
|
|
193
|
+
logger,
|
|
194
|
+
inquirer
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
stackableGenerator.setConfig({
|
|
198
|
+
...stackableGenerator.config,
|
|
199
|
+
serviceName
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
generator.addService(stackableGenerator, serviceName)
|
|
203
|
+
|
|
204
|
+
await stackableGenerator.ask()
|
|
205
|
+
|
|
206
|
+
const { shouldBreak } = await inquirer.prompt([
|
|
207
|
+
{
|
|
208
|
+
type: 'list',
|
|
209
|
+
name: 'shouldBreak',
|
|
210
|
+
message: 'Do you want to create another service?',
|
|
211
|
+
default: false,
|
|
212
|
+
choices: [{ name: 'yes', value: false }, { name: 'no', value: true }]
|
|
213
|
+
}
|
|
214
|
+
])
|
|
215
|
+
|
|
216
|
+
if (shouldBreak) {
|
|
217
|
+
break
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
let entrypoint = ''
|
|
222
|
+
|
|
223
|
+
if (names.length > 1) {
|
|
224
|
+
const results = await inquirer.prompt([
|
|
225
|
+
{
|
|
226
|
+
type: 'list',
|
|
227
|
+
name: 'entrypoint',
|
|
228
|
+
message: 'Which service should be exposed?',
|
|
229
|
+
choices: names.map(name => ({ name, value: name }))
|
|
230
|
+
}
|
|
231
|
+
])
|
|
232
|
+
entrypoint = results.entrypoint
|
|
233
|
+
} else {
|
|
234
|
+
entrypoint = names[0]
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
generator.setEntryPoint(entrypoint)
|
|
238
|
+
|
|
239
|
+
await generator.ask()
|
|
240
|
+
await generator.prepare()
|
|
241
|
+
await generator.writeFiles()
|
|
242
|
+
|
|
243
|
+
// Create project here
|
|
244
|
+
|
|
245
|
+
const { initGitRepository } = await inquirer.prompt({
|
|
246
|
+
type: 'list',
|
|
247
|
+
name: 'initGitRepository',
|
|
248
|
+
message: 'Do you want to init the git repository?',
|
|
249
|
+
default: false,
|
|
250
|
+
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
if (initGitRepository) {
|
|
254
|
+
await createGitRepository(logger, projectDir)
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (pkgManager === 'pnpm') {
|
|
258
|
+
// add pnpm-workspace.yaml file if needed
|
|
259
|
+
const content = `packages:
|
|
260
|
+
# all packages in direct subdirs of packages/
|
|
261
|
+
- 'services/*'`
|
|
262
|
+
await (writeFile(join(projectDir, 'pnpm-workspace.yaml'), content))
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (args.install) {
|
|
266
|
+
const spinner = ora('Installing dependencies...').start()
|
|
267
|
+
await execa(pkgManager, ['install'], { cwd: projectDir })
|
|
268
|
+
spinner.succeed()
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
logger.info('Project created successfully, executing post-install actions...')
|
|
272
|
+
await generator.postInstallActions()
|
|
273
|
+
logger.info('You are all set! Run `npm start` to start your project.')
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async function createStackable (args, logger, pkgManager) {
|
|
277
|
+
logger.info('Creating a stackable project...')
|
|
278
|
+
|
|
279
|
+
const generator = new StackableGenerator({ logger, inquirer })
|
|
280
|
+
await generator.ask()
|
|
281
|
+
await generator.prepare()
|
|
282
|
+
await generator.writeFiles()
|
|
283
|
+
|
|
284
|
+
const projectDir = path.resolve(process.cwd(), generator.config.targetDirectory)
|
|
285
|
+
|
|
286
|
+
const { initGitRepository } = await inquirer.prompt({
|
|
287
|
+
type: 'list',
|
|
288
|
+
name: 'initGitRepository',
|
|
289
|
+
message: 'Do you want to init the git repository?',
|
|
290
|
+
default: false,
|
|
291
|
+
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
if (initGitRepository) {
|
|
295
|
+
await createGitRepository(logger, projectDir)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (args.install) {
|
|
299
|
+
const spinner = ora('Installing dependencies...').start()
|
|
300
|
+
await execa(pkgManager, ['install'], { cwd: projectDir })
|
|
301
|
+
spinner.succeed()
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
await generator.postInstallActions()
|
|
305
|
+
logger.info('Stackable created successfully! Run `npm run create` to create an application.')
|
|
306
|
+
}
|
package/src/say.mjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import logUpdate from 'log-update'
|
|
2
|
+
import { pltGreen } from './colors.mjs'
|
|
3
|
+
import { sleep, randomBetween } from './utils.mjs'
|
|
4
|
+
|
|
5
|
+
export const say = async (messages) => {
|
|
6
|
+
const _messages = Array.isArray(messages) ? messages : [messages]
|
|
7
|
+
|
|
8
|
+
for (const message of _messages) {
|
|
9
|
+
const _message = Array.isArray(message) ? message : message.split(' ')
|
|
10
|
+
const msg = []
|
|
11
|
+
for (const word of [''].concat(_message)) {
|
|
12
|
+
msg.push(word)
|
|
13
|
+
logUpdate(pltGreen(msg.join(' ')))
|
|
14
|
+
await sleep(randomBetween(75, 100))
|
|
15
|
+
process.stdout.write('\u0007') // Do we want to enable terminal bell?
|
|
16
|
+
}
|
|
17
|
+
await sleep(randomBetween(75, 200))
|
|
18
|
+
}
|
|
19
|
+
logUpdate.done()
|
|
20
|
+
}
|
package/src/utils.mjs
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { execa } from 'execa'
|
|
2
|
+
import { access, constants, mkdir, readFile } from 'fs/promises'
|
|
3
|
+
import { resolve, join, dirname } from 'path'
|
|
4
|
+
import { createRequire } from 'module'
|
|
5
|
+
import semver from 'semver'
|
|
6
|
+
import * as desm from 'desm'
|
|
7
|
+
import * as url from 'url'
|
|
8
|
+
|
|
9
|
+
import ConfigManager from '@platformatic/config'
|
|
10
|
+
|
|
11
|
+
export const sleep = ms => new Promise((resolve) => setTimeout(resolve, ms))
|
|
12
|
+
export const randomBetween = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
|
|
13
|
+
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
|
|
3
14
|
|
|
4
15
|
export async function isFileAccessible (filename, directory) {
|
|
5
16
|
try {
|
|
@@ -10,6 +21,123 @@ export async function isFileAccessible (filename, directory) {
|
|
|
10
21
|
return false
|
|
11
22
|
}
|
|
12
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Gets the username from git config or `whoami` command
|
|
26
|
+
* @returns string | null
|
|
27
|
+
*/
|
|
28
|
+
export const getUsername = async () => {
|
|
29
|
+
try {
|
|
30
|
+
const { stdout } = await execa('git', ['config', 'user.name'])
|
|
31
|
+
if (stdout?.trim()) {
|
|
32
|
+
return stdout.trim()
|
|
33
|
+
}
|
|
34
|
+
} catch (err) {
|
|
35
|
+
// ignore: git failed
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const { stdout } = await execa('whoami')
|
|
39
|
+
if (stdout?.trim()) {
|
|
40
|
+
return stdout.trim()
|
|
41
|
+
}
|
|
42
|
+
} catch (err) {
|
|
43
|
+
// ignore: whoami failed
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return null
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get the platformatic package version from package.json
|
|
50
|
+
* @returns string
|
|
51
|
+
*/
|
|
52
|
+
/* c8 ignore next 4 */
|
|
53
|
+
export const getVersion = async () => {
|
|
54
|
+
const data = await readFile(desm.join(import.meta.url, '..', 'package.json'), 'utf8')
|
|
55
|
+
return JSON.parse(data).version
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function isDirectoryWriteable (directory) {
|
|
59
|
+
try {
|
|
60
|
+
await access(directory, constants.R_OK | constants.W_OK)
|
|
61
|
+
return true
|
|
62
|
+
} catch (err) {
|
|
63
|
+
return false
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const findConfigFile = async (directory) => (ConfigManager.findConfigFile(directory))
|
|
68
|
+
export const findDBConfigFile = async (directory) => (ConfigManager.findConfigFile(directory, 'db'))
|
|
69
|
+
export const findServiceConfigFile = async (directory) => (ConfigManager.findConfigFile(directory, 'service'))
|
|
70
|
+
export const findComposerConfigFile = async (directory) => (ConfigManager.findConfigFile(directory, 'composer'))
|
|
71
|
+
export const findRuntimeConfigFile = async (directory) => (ConfigManager.findConfigFile(directory, 'runtime'))
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Gets the version of the specified dependency package from package.json
|
|
75
|
+
* @param {string} dependencyName
|
|
76
|
+
* @returns string
|
|
77
|
+
*/
|
|
78
|
+
export const getDependencyVersion = async (dependencyName) => {
|
|
79
|
+
const rootPackageJson = join(__dirname, '..', 'package.json')
|
|
80
|
+
const packageJsonContents = JSON.parse(await readFile(rootPackageJson, 'utf8'))
|
|
81
|
+
const dependencies = packageJsonContents.dependencies
|
|
82
|
+
const devDependencies = packageJsonContents.devDependencies
|
|
83
|
+
const regexp = /(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)/
|
|
84
|
+
if (dependencies[dependencyName]) {
|
|
85
|
+
const match = dependencies[dependencyName].match(regexp)
|
|
86
|
+
if (!match) {
|
|
87
|
+
return await resolveWorkspaceDependency(dependencyName)
|
|
88
|
+
}
|
|
89
|
+
return match[0]
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (devDependencies[dependencyName]) {
|
|
93
|
+
const match = devDependencies[dependencyName].match(regexp)
|
|
94
|
+
if (!match) {
|
|
95
|
+
return await resolveWorkspaceDependency(dependencyName)
|
|
96
|
+
}
|
|
97
|
+
return match[0]
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function resolveWorkspaceDependency (dependencyName) {
|
|
101
|
+
const require = createRequire(import.meta.url)
|
|
102
|
+
let dependencyPath = dirname(require.resolve(dependencyName))
|
|
103
|
+
// some deps are resolved not at their root level
|
|
104
|
+
// for instance 'typescript' will be risolved in its own ./lib directory
|
|
105
|
+
// next loop is to find the nearest parent directory that contains a package.json file
|
|
106
|
+
while (!await isFileAccessible(join(dependencyPath, 'package.json'))) {
|
|
107
|
+
dependencyPath = join(dependencyPath, '..')
|
|
108
|
+
if (dependencyPath === '/') {
|
|
109
|
+
throw new Error(`Cannot find package.json for ${dependencyName}`)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const pathToPackageJson = join(dependencyPath, 'package.json')
|
|
113
|
+
const packageJsonFile = await readFile(pathToPackageJson, 'utf-8')
|
|
114
|
+
const packageJson = JSON.parse(packageJsonFile)
|
|
115
|
+
return packageJson.version
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export const minimumSupportedNodeVersions = ['18.8.0', '20.6.0']
|
|
120
|
+
|
|
121
|
+
export const isCurrentVersionSupported = (currentVersion) => {
|
|
122
|
+
// TODO: add try/catch if some unsupported node version is passed
|
|
123
|
+
for (const version of minimumSupportedNodeVersions) {
|
|
124
|
+
if (semver.major(currentVersion) === semver.major(version) && semver.gte(currentVersion, version)) {
|
|
125
|
+
return true
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return false
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function convertServiceNameToPrefix (serviceName) {
|
|
132
|
+
return serviceName.replace(/-/g, '_').toUpperCase()
|
|
133
|
+
}
|
|
134
|
+
export function addPrefixToEnv (env, prefix) {
|
|
135
|
+
const output = {}
|
|
136
|
+
Object.entries(env).forEach(([key, value]) => {
|
|
137
|
+
output[`${prefix}_${key}`] = value
|
|
138
|
+
})
|
|
139
|
+
return output
|
|
140
|
+
}
|
|
13
141
|
|
|
14
142
|
export async function safeMkdir (dir) {
|
|
15
143
|
try {
|
|
File without changes
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
To have this test working, you need to link 'platformatic' globally with:
|
|
2
|
+
|
|
3
|
+
```bash
|
|
4
|
+
# in platformatic root
|
|
5
|
+
cd packages/cli
|
|
6
|
+
pnpm link --global
|
|
7
|
+
```
|
|
8
|
+
The reason is that the type generation is done expecting the 'platformatic' command to be in the cli, see: https://github.com/platformatic/platformatic/blob/main/packages/create-platformatic/src/service/create-service-cli.mjs#L120
|