create-platformatic 3.4.1 → 3.5.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/README.md +1 -1
- package/bin/cli.js +23 -0
- package/eslint.config.js +2 -2
- package/package.json +15 -47
- package/test/index.test.js +15 -0
- package/test/loader.js +13 -0
- package/create-platformatic.mjs +0 -26
- package/src/colors.mjs +0 -4
- package/src/create-git-repository.mjs +0 -108
- package/src/index.mjs +0 -282
- package/src/say.mjs +0 -20
- package/src/utils.mjs +0 -140
package/README.md
CHANGED
package/bin/cli.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { checkNodeVersionForApplications } from '@platformatic/foundation'
|
|
4
|
+
import { createPlatformatic } from 'create-wattpm'
|
|
5
|
+
import parseArgs from 'minimist'
|
|
6
|
+
import { readFile } from 'node:fs/promises'
|
|
7
|
+
import { join } from 'node:path'
|
|
8
|
+
|
|
9
|
+
checkNodeVersionForApplications()
|
|
10
|
+
|
|
11
|
+
const _args = process.argv.slice(2)
|
|
12
|
+
const args = parseArgs(_args, {
|
|
13
|
+
alias: {
|
|
14
|
+
v: 'version'
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
if (args.version) {
|
|
19
|
+
console.log('v' + JSON.parse(await readFile(join(import.meta.dirname, 'package.json'), 'utf8')).version)
|
|
20
|
+
process.exit(0)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
await createPlatformatic(_args)
|
package/eslint.config.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
import neostandard from 'neostandard'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
export default neostandard()
|
package/package.json
CHANGED
|
@@ -1,65 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-platformatic",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.0",
|
|
4
4
|
"description": "Create platformatic application interactive tool",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/platformatic/platformatic.git"
|
|
8
8
|
},
|
|
9
|
-
"exports": {
|
|
10
|
-
".": "./create-platformatic.mjs"
|
|
11
|
-
},
|
|
12
9
|
"bin": {
|
|
13
|
-
"create-platformatic": "./
|
|
10
|
+
"create-platformatic": "./bin/cli.js"
|
|
14
11
|
},
|
|
12
|
+
"type": "module",
|
|
15
13
|
"license": "Apache-2.0",
|
|
16
|
-
"author": "
|
|
14
|
+
"author": "Platformatic Inc. <oss@platformatic.dev> (https://platformatic.dev)",
|
|
17
15
|
"dependencies": {
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"columnify": "^1.6.0",
|
|
21
|
-
"commist": "^3.2.0",
|
|
22
|
-
"desm": "^1.3.1",
|
|
23
|
-
"es-main": "^1.3.0",
|
|
24
|
-
"execa": "^8.0.1",
|
|
25
|
-
"help-me": "^5.0.0",
|
|
26
|
-
"inquirer": "^9.2.16",
|
|
27
|
-
"log-update": "^6.0.0",
|
|
28
|
-
"minimist": "^1.2.8",
|
|
29
|
-
"ora": "^6.3.1",
|
|
30
|
-
"pino": "^9.0.0",
|
|
31
|
-
"pino-pretty": "^11.0.0",
|
|
32
|
-
"resolve": "^1.22.8",
|
|
33
|
-
"semver": "^7.6.0",
|
|
34
|
-
"strip-ansi": "^7.1.0",
|
|
35
|
-
"undici": "^6.9.0",
|
|
36
|
-
"which": "^3.0.1",
|
|
37
|
-
"@platformatic/config": "3.4.1",
|
|
38
|
-
"@platformatic/generators": "3.4.1",
|
|
39
|
-
"@platformatic/utils": "3.4.1"
|
|
16
|
+
"@platformatic/foundation": "3.5.0",
|
|
17
|
+
"create-wattpm": "3.5.0"
|
|
40
18
|
},
|
|
41
19
|
"devDependencies": {
|
|
42
|
-
"
|
|
43
|
-
"ajv": "^8.12.0",
|
|
44
|
-
"borp": "^0.17.0",
|
|
45
|
-
"c8": "^10.0.0",
|
|
46
|
-
"cross-env": "^7.0.3",
|
|
47
|
-
"dotenv": "^16.4.5",
|
|
20
|
+
"cleaner-spec-reporter": "^0.5.0",
|
|
48
21
|
"eslint": "9",
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
"
|
|
54
|
-
"@platformatic/composer": "3.4.1",
|
|
55
|
-
"@platformatic/runtime": "3.4.1",
|
|
56
|
-
"@platformatic/db": "3.4.1",
|
|
57
|
-
"@platformatic/service": "3.4.1"
|
|
22
|
+
"import-in-the-middle": "^1.14.2",
|
|
23
|
+
"neostandard": "^0.12.0"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=22.19.0"
|
|
58
27
|
},
|
|
59
28
|
"scripts": {
|
|
60
|
-
"test
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"lint": "eslint"
|
|
29
|
+
"test": "node --test --test-reporter=cleaner-spec-reporter --test-concurrency=1 --test-timeout=2000000 test/*.test.js test/**/*.test.js",
|
|
30
|
+
"lint": "eslint",
|
|
31
|
+
"license": "license-checker --production --onlyAllow 'Apache-2.0;MIT;ISC;BSD-2-Clause;BSD-3-Clause;CC-BY-4.0;BlueOak-1.0.0'"
|
|
64
32
|
}
|
|
65
33
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { execa } from 'execa'
|
|
2
|
+
import { deepStrictEqual } from 'node:assert'
|
|
3
|
+
import { resolve } from 'node:path'
|
|
4
|
+
import { test } from 'node:test'
|
|
5
|
+
import { pathToFileURL } from 'node:url'
|
|
6
|
+
|
|
7
|
+
test('should be an alias for create-wattpm', async t => {
|
|
8
|
+
const { stdout } = await execa(process.argv[0], [
|
|
9
|
+
'--import',
|
|
10
|
+
pathToFileURL(resolve(import.meta.dirname, './loader.js')).toString(),
|
|
11
|
+
resolve(import.meta.dirname, '../bin/cli.js')
|
|
12
|
+
])
|
|
13
|
+
|
|
14
|
+
deepStrictEqual(stdout.trim(), 'OK')
|
|
15
|
+
})
|
package/test/loader.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Hook } from 'import-in-the-middle'
|
|
2
|
+
import { register } from 'node:module'
|
|
3
|
+
import { fileURLToPath } from 'node:url'
|
|
4
|
+
|
|
5
|
+
register('import-in-the-middle/hook.mjs', import.meta.url)
|
|
6
|
+
|
|
7
|
+
// The package name will not work as we are in a pnpm workspace
|
|
8
|
+
// eslint-disable-next-line no-new
|
|
9
|
+
new Hook([fileURLToPath(new URL('../../create-wattpm/lib/index.js', import.meta.url))], function (exported) {
|
|
10
|
+
exported.createPlatformatic = async function () {
|
|
11
|
+
console.log('OK')
|
|
12
|
+
}
|
|
13
|
+
})
|
package/create-platformatic.mjs
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { checkNodeVersionForServices } from '@platformatic/utils'
|
|
3
|
-
import { join } from 'desm'
|
|
4
|
-
import isMain from 'es-main'
|
|
5
|
-
import { readFile } from 'fs/promises'
|
|
6
|
-
import parseArgs from 'minimist'
|
|
7
|
-
import { createPlatformatic } from './src/index.mjs'
|
|
8
|
-
|
|
9
|
-
if (isMain(import.meta)) {
|
|
10
|
-
checkNodeVersionForServices()
|
|
11
|
-
|
|
12
|
-
const _args = process.argv.slice(2)
|
|
13
|
-
const args = parseArgs(_args, {
|
|
14
|
-
alias: {
|
|
15
|
-
v: 'version'
|
|
16
|
-
}
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
if (args.version) {
|
|
20
|
-
console.log('v' + JSON.parse(await readFile(join(import.meta.url, 'package.json'), 'utf8')).version)
|
|
21
|
-
process.exit(0)
|
|
22
|
-
}
|
|
23
|
-
await createPlatformatic(_args)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export { createPlatformatic }
|
package/src/colors.mjs
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,282 +0,0 @@
|
|
|
1
|
-
import { createDirectory, getPkgManager } from '@platformatic/utils'
|
|
2
|
-
import generateName from 'boring-name-generator'
|
|
3
|
-
import { execa } from 'execa'
|
|
4
|
-
import inquirer from 'inquirer'
|
|
5
|
-
import parseArgs from 'minimist'
|
|
6
|
-
import { writeFile, readFile } from 'node:fs/promises'
|
|
7
|
-
import path, { basename, join } from 'node:path'
|
|
8
|
-
import { setTimeout } from 'node:timers/promises'
|
|
9
|
-
import { pathToFileURL } from 'node:url'
|
|
10
|
-
import ora from 'ora'
|
|
11
|
-
import pino from 'pino'
|
|
12
|
-
import pretty from 'pino-pretty'
|
|
13
|
-
import resolve from 'resolve'
|
|
14
|
-
import { request } from 'undici'
|
|
15
|
-
import { createGitRepository } from './create-git-repository.mjs'
|
|
16
|
-
import { say } from './say.mjs'
|
|
17
|
-
import { getUsername, getVersion, isCurrentVersionSupported, minimumSupportedNodeVersions } from './utils.mjs'
|
|
18
|
-
|
|
19
|
-
const MARKETPLACE_HOST = 'https://marketplace.platformatic.dev'
|
|
20
|
-
const defaultStackables = ['@platformatic/composer', '@platformatic/db', '@platformatic/service']
|
|
21
|
-
|
|
22
|
-
export async function fetchStackables (marketplaceHost) {
|
|
23
|
-
// Skip the remote network request if we are running tests
|
|
24
|
-
if (process.env.MARKETPLACE_TEST) {
|
|
25
|
-
return [...defaultStackables]
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
marketplaceHost = marketplaceHost || MARKETPLACE_HOST
|
|
29
|
-
|
|
30
|
-
const stackablesRequest = request(marketplaceHost + '/templates')
|
|
31
|
-
const stackablesRequestTimeout = setTimeout(5000, new Error('Request timed out'))
|
|
32
|
-
|
|
33
|
-
try {
|
|
34
|
-
const { statusCode, body } = await Promise.race([stackablesRequest, stackablesRequestTimeout])
|
|
35
|
-
if (statusCode === 200) {
|
|
36
|
-
return (await body.json()).map(stackable => stackable.name)
|
|
37
|
-
}
|
|
38
|
-
} catch (err) {}
|
|
39
|
-
|
|
40
|
-
return [...defaultStackables]
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export async function chooseStackable (stackables) {
|
|
44
|
-
const options = await inquirer.prompt({
|
|
45
|
-
type: 'list',
|
|
46
|
-
name: 'type',
|
|
47
|
-
message: 'Which kind of project do you want to create?',
|
|
48
|
-
default: stackables.indexOf('@platformatic/service'),
|
|
49
|
-
choices: stackables,
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
return options.type
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
async function importOrLocal ({ pkgManager, name, projectDir, pkg }) {
|
|
56
|
-
try {
|
|
57
|
-
return await import(pkg)
|
|
58
|
-
} catch (err) {
|
|
59
|
-
try {
|
|
60
|
-
const fileToImport = resolve.sync(pkg, { basedir: projectDir })
|
|
61
|
-
return await import(pathToFileURL(fileToImport))
|
|
62
|
-
} catch {}
|
|
63
|
-
|
|
64
|
-
let version = ''
|
|
65
|
-
|
|
66
|
-
if (defaultStackables.includes(pkg) || pkg === '@platformatic/runtime') {
|
|
67
|
-
// Let's find if we are using one of the default stackables
|
|
68
|
-
// If we are, we have to use the "local" version of the package
|
|
69
|
-
|
|
70
|
-
const meta = await JSON.parse(await readFile(join(import.meta.dirname, '..', 'package.json'), 'utf-8'))
|
|
71
|
-
if (meta.version.includes('-')) {
|
|
72
|
-
version = `@${meta.version}`
|
|
73
|
-
} else {
|
|
74
|
-
version = `@^${meta.version}`
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const spinner = ora(`Installing ${pkg + version}...`).start()
|
|
79
|
-
await execa(pkgManager, ['install', pkg + version], { cwd: projectDir })
|
|
80
|
-
spinner.succeed()
|
|
81
|
-
|
|
82
|
-
const fileToImport = resolve.sync(pkg, { basedir: projectDir })
|
|
83
|
-
return await import(pathToFileURL(fileToImport))
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export const createPlatformatic = async argv => {
|
|
88
|
-
const args = parseArgs(argv, {
|
|
89
|
-
default: {
|
|
90
|
-
install: true,
|
|
91
|
-
},
|
|
92
|
-
boolean: ['install'],
|
|
93
|
-
string: ['global-config', 'marketplace-host'],
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
const username = await getUsername()
|
|
97
|
-
const version = await getVersion()
|
|
98
|
-
const greeting = username ? `Hello ${username},` : 'Hello,'
|
|
99
|
-
await say(`${greeting} welcome to ${version ? `Platformatic ${version}!` : 'Platformatic!'}`)
|
|
100
|
-
|
|
101
|
-
const currentVersion = process.versions.node
|
|
102
|
-
const supported = isCurrentVersionSupported(currentVersion)
|
|
103
|
-
if (!supported) {
|
|
104
|
-
const supportedVersions = minimumSupportedNodeVersions.join(' or >= ')
|
|
105
|
-
await say(`Platformatic is not supported on Node.js v${currentVersion}.`)
|
|
106
|
-
await say(`Please use one of the following Node.js versions >= ${supportedVersions}.`)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const logger = pino(
|
|
110
|
-
pretty({
|
|
111
|
-
translateTime: 'SYS:HH:MM:ss',
|
|
112
|
-
ignore: 'hostname,pid',
|
|
113
|
-
})
|
|
114
|
-
)
|
|
115
|
-
|
|
116
|
-
const pkgManager = getPkgManager()
|
|
117
|
-
await createApplication(args, logger, pkgManager)
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
async function createApplication (args, logger, pkgManager) {
|
|
121
|
-
const optionsDir = await inquirer.prompt({
|
|
122
|
-
type: 'input',
|
|
123
|
-
name: 'dir',
|
|
124
|
-
message: 'Where would you like to create your project?',
|
|
125
|
-
default: 'platformatic',
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
const projectDir = path.resolve(process.cwd(), optionsDir.dir)
|
|
129
|
-
const projectName = basename(projectDir)
|
|
130
|
-
|
|
131
|
-
await createDirectory(projectDir)
|
|
132
|
-
|
|
133
|
-
const runtime = await importOrLocal({
|
|
134
|
-
pkgManager,
|
|
135
|
-
name: projectName,
|
|
136
|
-
projectDir,
|
|
137
|
-
pkg: '@platformatic/runtime',
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
const generator = new runtime.Generator({
|
|
141
|
-
logger,
|
|
142
|
-
name: projectName,
|
|
143
|
-
inquirer,
|
|
144
|
-
})
|
|
145
|
-
generator.setConfig({
|
|
146
|
-
...generator.config,
|
|
147
|
-
targetDirectory: projectDir,
|
|
148
|
-
})
|
|
149
|
-
|
|
150
|
-
await generator.populateFromExistingConfig()
|
|
151
|
-
if (generator.existingConfig) {
|
|
152
|
-
await say('Using existing configuration')
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const stackables = await fetchStackables(args['marketplace-host'])
|
|
156
|
-
|
|
157
|
-
const names = []
|
|
158
|
-
|
|
159
|
-
while (true) {
|
|
160
|
-
const stackableName = await chooseStackable(stackables)
|
|
161
|
-
// await say(`Creating a ${stackable} project in ${projectDir}...`)
|
|
162
|
-
|
|
163
|
-
const stackable = await importOrLocal({
|
|
164
|
-
pkgManager,
|
|
165
|
-
name: projectName,
|
|
166
|
-
projectDir,
|
|
167
|
-
pkg: stackableName,
|
|
168
|
-
})
|
|
169
|
-
|
|
170
|
-
const { serviceName } = await inquirer.prompt({
|
|
171
|
-
type: 'input',
|
|
172
|
-
name: 'serviceName',
|
|
173
|
-
message: 'What is the name of the service?',
|
|
174
|
-
default: generateName().dashed,
|
|
175
|
-
validate: value => {
|
|
176
|
-
if (value.length === 0) {
|
|
177
|
-
return 'Please enter a name'
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (value.includes(' ')) {
|
|
181
|
-
return 'Please enter a name without spaces'
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (names.includes(value)) {
|
|
185
|
-
return 'This name is already used, please choose another one.'
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return true
|
|
189
|
-
},
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
names.push(serviceName)
|
|
193
|
-
|
|
194
|
-
const stackableGenerator = new stackable.Generator({
|
|
195
|
-
logger,
|
|
196
|
-
inquirer,
|
|
197
|
-
})
|
|
198
|
-
|
|
199
|
-
stackableGenerator.setConfig({
|
|
200
|
-
...stackableGenerator.config,
|
|
201
|
-
serviceName,
|
|
202
|
-
})
|
|
203
|
-
|
|
204
|
-
generator.addService(stackableGenerator, serviceName)
|
|
205
|
-
|
|
206
|
-
await stackableGenerator.ask()
|
|
207
|
-
|
|
208
|
-
const { shouldBreak } = await inquirer.prompt([
|
|
209
|
-
{
|
|
210
|
-
type: 'list',
|
|
211
|
-
name: 'shouldBreak',
|
|
212
|
-
message: 'Do you want to create another service?',
|
|
213
|
-
default: false,
|
|
214
|
-
choices: [
|
|
215
|
-
{ name: 'yes', value: false },
|
|
216
|
-
{ name: 'no', value: true },
|
|
217
|
-
],
|
|
218
|
-
},
|
|
219
|
-
])
|
|
220
|
-
|
|
221
|
-
if (shouldBreak) {
|
|
222
|
-
break
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
let entrypoint = ''
|
|
227
|
-
|
|
228
|
-
if (names.length > 1) {
|
|
229
|
-
const results = await inquirer.prompt([
|
|
230
|
-
{
|
|
231
|
-
type: 'list',
|
|
232
|
-
name: 'entrypoint',
|
|
233
|
-
message: 'Which service should be exposed?',
|
|
234
|
-
choices: names.map(name => ({ name, value: name })),
|
|
235
|
-
},
|
|
236
|
-
])
|
|
237
|
-
entrypoint = results.entrypoint
|
|
238
|
-
} else {
|
|
239
|
-
entrypoint = names[0]
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
generator.setEntryPoint(entrypoint)
|
|
243
|
-
|
|
244
|
-
await generator.ask()
|
|
245
|
-
await generator.prepare()
|
|
246
|
-
await generator.writeFiles()
|
|
247
|
-
|
|
248
|
-
// Create project here
|
|
249
|
-
|
|
250
|
-
const { initGitRepository } = await inquirer.prompt({
|
|
251
|
-
type: 'list',
|
|
252
|
-
name: 'initGitRepository',
|
|
253
|
-
message: 'Do you want to init the git repository?',
|
|
254
|
-
default: false,
|
|
255
|
-
choices: [
|
|
256
|
-
{ name: 'yes', value: true },
|
|
257
|
-
{ name: 'no', value: false },
|
|
258
|
-
],
|
|
259
|
-
})
|
|
260
|
-
|
|
261
|
-
if (initGitRepository) {
|
|
262
|
-
await createGitRepository(logger, projectDir)
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
if (pkgManager === 'pnpm') {
|
|
266
|
-
// add pnpm-workspace.yaml file if needed
|
|
267
|
-
const content = `packages:
|
|
268
|
-
# all packages in direct subdirs of packages/
|
|
269
|
-
- 'services/*'`
|
|
270
|
-
await writeFile(join(projectDir, 'pnpm-workspace.yaml'), content)
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
if (args.install) {
|
|
274
|
-
const spinner = ora('Installing dependencies...').start()
|
|
275
|
-
await execa(pkgManager, ['install'], { cwd: projectDir })
|
|
276
|
-
spinner.succeed()
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
logger.info('Project created successfully, executing post-install actions...')
|
|
280
|
-
await generator.postInstallActions()
|
|
281
|
-
logger.info('You are all set! Run `npm start` to start your project.')
|
|
282
|
-
}
|
package/src/say.mjs
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import * as desm from 'desm'
|
|
2
|
-
import { execa } from 'execa'
|
|
3
|
-
import { access, constants, readFile } from 'fs/promises'
|
|
4
|
-
import { createRequire } from 'module'
|
|
5
|
-
import { dirname, join, resolve } from 'path'
|
|
6
|
-
import semver from 'semver'
|
|
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))
|
|
14
|
-
|
|
15
|
-
export async function isFileAccessible (filename, directory) {
|
|
16
|
-
try {
|
|
17
|
-
const filePath = directory ? resolve(directory, filename) : filename
|
|
18
|
-
await access(filePath)
|
|
19
|
-
return true
|
|
20
|
-
} catch (err) {
|
|
21
|
-
return false
|
|
22
|
-
}
|
|
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
|
-
}
|