@platformatic/generators 2.0.0-alpha.2 → 2.0.0-alpha.21
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/eslint.config.js +3 -0
- package/index.d.ts +1 -1
- package/index.js +1 -3
- package/index.test-d.ts +4 -4
- package/lib/base-generator.d.ts +25 -25
- package/lib/base-generator.js +31 -30
- package/lib/create-gitignore.js +2 -2
- package/lib/create-plugin.d.ts +4 -4
- package/lib/create-plugin.js +9 -9
- package/lib/errors.js +1 -1
- package/lib/file-generator.d.ts +14 -14
- package/lib/file-generator.js +4 -4
- package/lib/utils.d.ts +8 -9
- package/lib/utils.js +18 -23
- package/package.json +12 -11
- package/lib/create-stackable-cli.js +0 -161
- package/lib/create-stackable-files.js +0 -518
- package/lib/create-stackable-plugin.js +0 -49
- package/lib/create-stackable-tests.js +0 -395
- package/lib/stackable-generator.js +0 -317
- package/test/base-generator.test.js +0 -973
- package/test/file-generator.test.js +0 -140
- package/test/fixtures/sample-runtime/.env +0 -9
- package/test/fixtures/sample-runtime/platformatic.json +0 -18
- package/test/fixtures/sample-runtime/services/no-plugin/platformatic.json +0 -7
- package/test/fixtures/sample-runtime/services/rival/platformatic.json +0 -33
- package/test/helpers.js +0 -64
- package/test/stackable-generator.test.js +0 -114
- package/test/utils.test.js +0 -223
package/package.json
CHANGED
|
@@ -1,30 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/generators",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.21",
|
|
4
4
|
"description": "Main classes and utils for generators.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"keywords": [],
|
|
7
7
|
"author": "",
|
|
8
8
|
"license": "Apache-2.0",
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@fastify/error": "^
|
|
10
|
+
"@fastify/error": "^4.0.0",
|
|
11
11
|
"boring-name-generator": "^1.0.3",
|
|
12
12
|
"change-case-all": "^2.1.0",
|
|
13
|
-
"fastify": "^
|
|
14
|
-
"pino": "^
|
|
15
|
-
"undici": "^6.9.0"
|
|
13
|
+
"fastify": "^5.0.0",
|
|
14
|
+
"pino": "^9.0.0",
|
|
15
|
+
"undici": "^6.9.0",
|
|
16
|
+
"@platformatic/utils": "2.0.0-alpha.21"
|
|
16
17
|
},
|
|
17
18
|
"devDependencies": {
|
|
18
19
|
"@types/inquirer": "^9.0.7",
|
|
19
|
-
"borp": "^0.
|
|
20
|
+
"borp": "^0.17.0",
|
|
20
21
|
"c8": "^10.0.0",
|
|
21
|
-
"
|
|
22
|
-
"
|
|
22
|
+
"eslint": "9",
|
|
23
|
+
"neostandard": "^0.11.1",
|
|
23
24
|
"tsd": "^0.31.0",
|
|
24
|
-
"typescript": "^5.4
|
|
25
|
+
"typescript": "^5.5.4"
|
|
25
26
|
},
|
|
26
27
|
"scripts": {
|
|
27
|
-
"lint": "
|
|
28
|
-
"test": "pnpm run lint && borp -C -X fixtures -X test --concurrency=1 && tsd"
|
|
28
|
+
"lint": "eslint",
|
|
29
|
+
"test": "pnpm run lint && borp --timeout=180000 -C -X fixtures -X test --concurrency=1 && tsd"
|
|
29
30
|
}
|
|
30
31
|
}
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { kebabCase } = require('change-case-all')
|
|
4
|
-
|
|
5
|
-
function getJsStackableStartCli () {
|
|
6
|
-
return `\
|
|
7
|
-
#!/usr/bin/env node
|
|
8
|
-
'use strict'
|
|
9
|
-
|
|
10
|
-
const stackable = require('../index')
|
|
11
|
-
const { start } = require('@platformatic/service')
|
|
12
|
-
const { printAndExitLoadConfigError } = require('@platformatic/config')
|
|
13
|
-
|
|
14
|
-
start(stackable, process.argv.splice(2)).catch(printAndExitLoadConfigError)
|
|
15
|
-
`
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function getTsStackableStartCli () {
|
|
19
|
-
return `\
|
|
20
|
-
#!/usr/bin/env node
|
|
21
|
-
import stackable from '../index'
|
|
22
|
-
import { start } from '@platformatic/service'
|
|
23
|
-
import { printAndExitLoadConfigError } from '@platformatic/config'
|
|
24
|
-
|
|
25
|
-
start(stackable, process.argv.splice(2)).catch(printAndExitLoadConfigError)
|
|
26
|
-
`
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function getJsStackableCreateCli (stackableName) {
|
|
30
|
-
return `\
|
|
31
|
-
#!/usr/bin/env node
|
|
32
|
-
'use strict'
|
|
33
|
-
|
|
34
|
-
const { join } = require('node:path')
|
|
35
|
-
const { parseArgs } = require('node:util')
|
|
36
|
-
const { Generator } = require('../lib/generator')
|
|
37
|
-
|
|
38
|
-
async function execute () {
|
|
39
|
-
const args = parseArgs({
|
|
40
|
-
args: process.argv.slice(2),
|
|
41
|
-
options: {
|
|
42
|
-
dir: {
|
|
43
|
-
type: 'string',
|
|
44
|
-
default: join(process.cwd(), '${kebabCase(stackableName + '-app')}')
|
|
45
|
-
},
|
|
46
|
-
port: { type: 'string', default: '3042' },
|
|
47
|
-
hostname: { type: 'string', default: '0.0.0.0' },
|
|
48
|
-
plugin: { type: 'boolean', default: true },
|
|
49
|
-
tests: { type: 'boolean', default: true },
|
|
50
|
-
typescript: { type: 'boolean', default: false },
|
|
51
|
-
git: { type: 'boolean', default: false },
|
|
52
|
-
install: { type: 'boolean', default: true }
|
|
53
|
-
}
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
const generator = new Generator()
|
|
57
|
-
|
|
58
|
-
generator.setConfig({
|
|
59
|
-
port: parseInt(args.values.port),
|
|
60
|
-
hostname: args.values.hostname,
|
|
61
|
-
plugin: args.values.plugin,
|
|
62
|
-
tests: args.values.tests,
|
|
63
|
-
typescript: args.values.typescript,
|
|
64
|
-
initGitRepository: args.values.git,
|
|
65
|
-
targetDirectory: args.values.dir
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
await generator.run()
|
|
69
|
-
|
|
70
|
-
console.log('Application created successfully! Run \`npm run start\` to start an application.')
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
execute()
|
|
74
|
-
`
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function getTsStackableCreateCli (stackableName) {
|
|
78
|
-
return `\
|
|
79
|
-
#!/usr/bin/env node
|
|
80
|
-
import { join } from 'node:path'
|
|
81
|
-
import { parseArgs } from 'node:util'
|
|
82
|
-
import { Generator } from '../lib/generator'
|
|
83
|
-
|
|
84
|
-
async function execute (): Promise<void> {
|
|
85
|
-
const args = parseArgs({
|
|
86
|
-
args: process.argv.slice(2),
|
|
87
|
-
options: {
|
|
88
|
-
dir: {
|
|
89
|
-
type: 'string',
|
|
90
|
-
default: join(process.cwd(), '${kebabCase(stackableName + '-app')}')
|
|
91
|
-
},
|
|
92
|
-
port: { type: 'string' },
|
|
93
|
-
hostname: { type: 'string', default: '0.0.0.0' },
|
|
94
|
-
plugin: { type: 'boolean', default: true },
|
|
95
|
-
tests: { type: 'boolean', default: true },
|
|
96
|
-
typescript: { type: 'boolean', default: false },
|
|
97
|
-
git: { type: 'boolean', default: false },
|
|
98
|
-
install: { type: 'boolean', default: true }
|
|
99
|
-
}
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
const generator = new Generator()
|
|
103
|
-
|
|
104
|
-
generator.setConfig({
|
|
105
|
-
port: parseInt(args.values.port || '3042'),
|
|
106
|
-
hostname: args.values.hostname,
|
|
107
|
-
plugin: args.values.plugin,
|
|
108
|
-
tests: args.values.tests,
|
|
109
|
-
typescript: args.values.typescript,
|
|
110
|
-
initGitRepository: args.values.git,
|
|
111
|
-
targetDirectory: args.values.dir
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
await generator.run()
|
|
115
|
-
|
|
116
|
-
console.log('Application created successfully! Run \`npm run start\` to start an application.')
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
execute().catch(err => {
|
|
120
|
-
throw err
|
|
121
|
-
})
|
|
122
|
-
`
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function generateStackableCli (typescript, stackableName) {
|
|
126
|
-
if (typescript) {
|
|
127
|
-
return [
|
|
128
|
-
{
|
|
129
|
-
path: 'cli',
|
|
130
|
-
file: 'start.ts',
|
|
131
|
-
contents: getTsStackableStartCli(),
|
|
132
|
-
options: { mode: 0o755 }
|
|
133
|
-
},
|
|
134
|
-
{
|
|
135
|
-
path: 'cli',
|
|
136
|
-
file: 'create.ts',
|
|
137
|
-
contents: getTsStackableCreateCli(stackableName),
|
|
138
|
-
options: { mode: 0o755 }
|
|
139
|
-
}
|
|
140
|
-
]
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return [
|
|
144
|
-
{
|
|
145
|
-
path: 'cli',
|
|
146
|
-
file: 'start.js',
|
|
147
|
-
contents: getJsStackableStartCli(),
|
|
148
|
-
options: { mode: 0o755 }
|
|
149
|
-
},
|
|
150
|
-
{
|
|
151
|
-
path: 'cli',
|
|
152
|
-
file: 'create.js',
|
|
153
|
-
contents: getJsStackableCreateCli(stackableName),
|
|
154
|
-
options: { mode: 0o755 }
|
|
155
|
-
}
|
|
156
|
-
]
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
module.exports = {
|
|
160
|
-
generateStackableCli
|
|
161
|
-
}
|
|
@@ -1,518 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { pascalCase, camelCase, capitalCase, kebabCase } = require('change-case-all')
|
|
4
|
-
|
|
5
|
-
function getJsStackableIndexFile (stackableName) {
|
|
6
|
-
return `\
|
|
7
|
-
'use strict'
|
|
8
|
-
|
|
9
|
-
const { platformaticService } = require('@platformatic/service')
|
|
10
|
-
const { schema } = require('./lib/schema')
|
|
11
|
-
const { Generator } = require('./lib/generator')
|
|
12
|
-
const { version } = require('./package.json')
|
|
13
|
-
|
|
14
|
-
async function stackable (fastify, opts) {
|
|
15
|
-
await fastify.register(platformaticService, opts)
|
|
16
|
-
await fastify.register(require('./plugins/example'), opts)
|
|
17
|
-
}
|
|
18
|
-
stackable.configType = '${kebabCase(stackableName + '-app')}'
|
|
19
|
-
stackable.schema = schema
|
|
20
|
-
stackable.Generator = Generator
|
|
21
|
-
stackable.configManagerConfig = {
|
|
22
|
-
version,
|
|
23
|
-
schema,
|
|
24
|
-
allowToWatch: ['.env'],
|
|
25
|
-
schemaOptions: {
|
|
26
|
-
useDefaults: true,
|
|
27
|
-
coerceTypes: true,
|
|
28
|
-
allErrors: true,
|
|
29
|
-
strict: false
|
|
30
|
-
},
|
|
31
|
-
async transformConfig () {}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// break Fastify encapsulation
|
|
35
|
-
stackable[Symbol.for('skip-override')] = true
|
|
36
|
-
|
|
37
|
-
module.exports = stackable
|
|
38
|
-
`
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function getTsStackableIndexFile (stackableName) {
|
|
42
|
-
const stackableConfigType = pascalCase(stackableName + 'Config')
|
|
43
|
-
|
|
44
|
-
return `\
|
|
45
|
-
import { platformaticService, Stackable } from '@platformatic/service'
|
|
46
|
-
import { schema } from './lib/schema'
|
|
47
|
-
import { Generator } from './lib/generator'
|
|
48
|
-
import { ${stackableConfigType} } from './config'
|
|
49
|
-
import { readFileSync } from 'node:fs'
|
|
50
|
-
|
|
51
|
-
const { version } = JSON.parse(readFileSync('package.json', 'utf8'))
|
|
52
|
-
|
|
53
|
-
const stackable: Stackable<${stackableConfigType}> = async function (fastify, opts) {
|
|
54
|
-
await fastify.register(platformaticService, opts)
|
|
55
|
-
await fastify.register(require('./plugins/example'), opts)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
stackable.configType = '${kebabCase(stackableName + '-app')}'
|
|
59
|
-
stackable.schema = schema
|
|
60
|
-
stackable.Generator = Generator
|
|
61
|
-
stackable.configManagerConfig = {
|
|
62
|
-
version,
|
|
63
|
-
schema,
|
|
64
|
-
allowToWatch: ['.env'],
|
|
65
|
-
schemaOptions: {
|
|
66
|
-
useDefaults: true,
|
|
67
|
-
coerceTypes: true,
|
|
68
|
-
allErrors: true,
|
|
69
|
-
strict: false
|
|
70
|
-
},
|
|
71
|
-
async transformConfig () {}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// break Fastify encapsulation
|
|
75
|
-
// @ts-ignore
|
|
76
|
-
stackable[Symbol.for('skip-override')] = true
|
|
77
|
-
|
|
78
|
-
export default stackable
|
|
79
|
-
export { Generator, schema }
|
|
80
|
-
`
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function getStackableIndexTypesFile (stackableName) {
|
|
84
|
-
const stackableConfigType = pascalCase(stackableName + 'Config')
|
|
85
|
-
|
|
86
|
-
return `\
|
|
87
|
-
import { FastifyInstance } from 'fastify'
|
|
88
|
-
import { PlatformaticApp } from '@platformatic/service'
|
|
89
|
-
import { ${stackableConfigType} } from './config'
|
|
90
|
-
|
|
91
|
-
declare module 'fastify' {
|
|
92
|
-
interface FastifyInstance {
|
|
93
|
-
platformatic: PlatformaticApp<${stackableConfigType}>
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export { PlatformaticApp, ${stackableConfigType} }
|
|
98
|
-
`
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function getJsGlobalTypesTemplateFile (stackableName) {
|
|
102
|
-
const stackableConfigType = pascalCase(stackableName + 'Config')
|
|
103
|
-
|
|
104
|
-
return `\
|
|
105
|
-
'use strict'
|
|
106
|
-
|
|
107
|
-
function generateGlobalTypesFile (npmPackageName) {
|
|
108
|
-
return \`\
|
|
109
|
-
import { FastifyInstance } from 'fastify'
|
|
110
|
-
import { ${stackableConfigType}, PlatformaticApp } from '\${npmPackageName}'
|
|
111
|
-
|
|
112
|
-
declare module 'fastify' {
|
|
113
|
-
interface FastifyInstance {
|
|
114
|
-
platformatic: PlatformaticApp<${stackableConfigType}>
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
\`
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
module.exports = {
|
|
121
|
-
generateGlobalTypesFile
|
|
122
|
-
}
|
|
123
|
-
`
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function getTsGlobalTypesTemplateFile (stackableName) {
|
|
127
|
-
const stackableConfigType = pascalCase(stackableName + 'Config')
|
|
128
|
-
|
|
129
|
-
return `\
|
|
130
|
-
export function generateGlobalTypesFile (npmPackageName: string): string {
|
|
131
|
-
return \`import { FastifyInstance } from 'fastify'
|
|
132
|
-
import { ${stackableConfigType}, PlatformaticApp } from '\${npmPackageName}'
|
|
133
|
-
|
|
134
|
-
declare module 'fastify' {
|
|
135
|
-
interface FastifyInstance {
|
|
136
|
-
platformatic: PlatformaticApp<${stackableConfigType}>
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
\`
|
|
140
|
-
}
|
|
141
|
-
`
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function getJsStackableGeneratorFile (stackableName) {
|
|
145
|
-
const stackableGeneratorType = pascalCase(stackableName + 'Generator')
|
|
146
|
-
|
|
147
|
-
return `\
|
|
148
|
-
'use strict'
|
|
149
|
-
|
|
150
|
-
const { join } = require('node:path')
|
|
151
|
-
const { readFile } = require('node:fs/promises')
|
|
152
|
-
const { Generator: ServiceGenerator } = require('@platformatic/service')
|
|
153
|
-
const { schema } = require('./schema')
|
|
154
|
-
const { generateGlobalTypesFile } = require('./templates/types')
|
|
155
|
-
|
|
156
|
-
class ${stackableGeneratorType} extends ServiceGenerator {
|
|
157
|
-
getDefaultConfig () {
|
|
158
|
-
const defaultBaseConfig = super.getDefaultConfig()
|
|
159
|
-
const defaultConfig = {
|
|
160
|
-
greeting: 'Hello world!'
|
|
161
|
-
}
|
|
162
|
-
return Object.assign({}, defaultBaseConfig, defaultConfig)
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
getConfigFieldsDefinitions () {
|
|
166
|
-
const serviceConfigFieldsDefs = super.getConfigFieldsDefinitions()
|
|
167
|
-
return [
|
|
168
|
-
...serviceConfigFieldsDefs,
|
|
169
|
-
{
|
|
170
|
-
var: 'PLT_GREETING_TEXT',
|
|
171
|
-
label: 'What should the stackable greeting say?',
|
|
172
|
-
default: 'Hello world!',
|
|
173
|
-
type: 'string'
|
|
174
|
-
}
|
|
175
|
-
]
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
async _getConfigFileContents () {
|
|
179
|
-
const baseConfig = await super._getConfigFileContents()
|
|
180
|
-
const packageJson = await this.getStackablePackageJson()
|
|
181
|
-
const config = {
|
|
182
|
-
$schema: './stackable.schema.json',
|
|
183
|
-
module: \`\${packageJson.name}@\${packageJson.version}\`,
|
|
184
|
-
greeting: {
|
|
185
|
-
text: \`{\${this.getEnvVarName('PLT_GREETING_TEXT')}}\`
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return Object.assign({}, baseConfig, config)
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
async _beforePrepare () {
|
|
192
|
-
super._beforePrepare()
|
|
193
|
-
|
|
194
|
-
this.addEnvVars({
|
|
195
|
-
PLT_GREETING_TEXT: this.config.greeting ?? 'Hello world!'
|
|
196
|
-
}, { overwrite: false, default: true })
|
|
197
|
-
|
|
198
|
-
const packageJson = await this.getStackablePackageJson()
|
|
199
|
-
|
|
200
|
-
this.config.dependencies = {
|
|
201
|
-
[packageJson.name]: \`^\${packageJson.version}\`
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
async _afterPrepare () {
|
|
206
|
-
const packageJson = await this.getStackablePackageJson()
|
|
207
|
-
this.addFile({
|
|
208
|
-
path: '',
|
|
209
|
-
file: 'global.d.ts',
|
|
210
|
-
contents: generateGlobalTypesFile(packageJson.name)
|
|
211
|
-
})
|
|
212
|
-
|
|
213
|
-
this.addFile({
|
|
214
|
-
path: '',
|
|
215
|
-
file: 'stackable.schema.json',
|
|
216
|
-
contents: JSON.stringify(schema, null, 2)
|
|
217
|
-
})
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
async getStackablePackageJson () {
|
|
221
|
-
if (!this._packageJson) {
|
|
222
|
-
const packageJsonPath = join(__dirname, '..', 'package.json')
|
|
223
|
-
const packageJsonFile = await readFile(packageJsonPath, 'utf8')
|
|
224
|
-
const packageJson = JSON.parse(packageJsonFile)
|
|
225
|
-
|
|
226
|
-
if (!packageJson.name) {
|
|
227
|
-
throw new Error('Missing package name in package.json')
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (!packageJson.version) {
|
|
231
|
-
throw new Error('Missing package version in package.json')
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
this._packageJson = packageJson
|
|
235
|
-
return packageJson
|
|
236
|
-
}
|
|
237
|
-
return this._packageJson
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
module.exports = ${stackableGeneratorType}
|
|
242
|
-
module.exports.Generator = ${stackableGeneratorType}
|
|
243
|
-
`
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
function getTsStackableGeneratorFile (stackableName) {
|
|
247
|
-
const stackableGeneratorType = pascalCase(stackableName + 'Generator')
|
|
248
|
-
|
|
249
|
-
return `\
|
|
250
|
-
import { join } from 'node:path'
|
|
251
|
-
import { readFile } from 'node:fs/promises'
|
|
252
|
-
import { Generator as ServiceGenerator } from '@platformatic/service'
|
|
253
|
-
import { BaseGenerator } from '@platformatic/generators'
|
|
254
|
-
import { schema } from './schema'
|
|
255
|
-
import { generateGlobalTypesFile } from './templates/types'
|
|
256
|
-
|
|
257
|
-
type PackageJson = {
|
|
258
|
-
name: string
|
|
259
|
-
version: string
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
class ${stackableGeneratorType} extends ServiceGenerator {
|
|
263
|
-
private _packageJson: PackageJson | null = null
|
|
264
|
-
|
|
265
|
-
getDefaultConfig (): { [x: string]: BaseGenerator.JSONValue } {
|
|
266
|
-
const defaultBaseConfig = super.getDefaultConfig()
|
|
267
|
-
const defaultConfig = {
|
|
268
|
-
greeting: 'Hello world!'
|
|
269
|
-
}
|
|
270
|
-
return Object.assign({}, defaultBaseConfig, defaultConfig)
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
getConfigFieldsDefinitions (): BaseGenerator.ConfigFieldDefinition[] {
|
|
274
|
-
const serviceConfigFieldsDefs = super.getConfigFieldsDefinitions()
|
|
275
|
-
return [
|
|
276
|
-
...serviceConfigFieldsDefs,
|
|
277
|
-
{
|
|
278
|
-
var: 'PLT_GREETING_TEXT',
|
|
279
|
-
label: 'What should the stackable greeting say?',
|
|
280
|
-
default: 'Hello world!',
|
|
281
|
-
type: 'string'
|
|
282
|
-
}
|
|
283
|
-
]
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
async _getConfigFileContents (): Promise<{ [x: string]: BaseGenerator.JSONValue }> {
|
|
287
|
-
const baseConfig = await super._getConfigFileContents()
|
|
288
|
-
const packageJson = await this.getStackablePackageJson()
|
|
289
|
-
const config = {
|
|
290
|
-
$schema: './stackable.schema.json',
|
|
291
|
-
module: \`\${packageJson.name}@\${packageJson.version}\`,
|
|
292
|
-
greeting: {
|
|
293
|
-
text: \`{\${this.getEnvVarName('PLT_GREETING_TEXT')}}\`
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
return Object.assign({}, baseConfig, config)
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
async _beforePrepare (): Promise<void> {
|
|
300
|
-
await super._beforePrepare()
|
|
301
|
-
|
|
302
|
-
this.addEnvVars({
|
|
303
|
-
PLT_GREETING_TEXT: this.config.greeting ?? 'Hello world!'
|
|
304
|
-
}, { overwrite: false, default: true })
|
|
305
|
-
|
|
306
|
-
const packageJson = await this.getStackablePackageJson()
|
|
307
|
-
|
|
308
|
-
this.config.dependencies = {
|
|
309
|
-
[packageJson.name]: \`^\${packageJson.version}\`
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
async _afterPrepare (): Promise<void> {
|
|
314
|
-
const packageJson = await this.getStackablePackageJson()
|
|
315
|
-
this.addFile({
|
|
316
|
-
path: '',
|
|
317
|
-
file: 'global.d.ts',
|
|
318
|
-
contents: generateGlobalTypesFile(packageJson.name)
|
|
319
|
-
})
|
|
320
|
-
|
|
321
|
-
this.addFile({
|
|
322
|
-
path: '',
|
|
323
|
-
file: 'stackable.schema.json',
|
|
324
|
-
contents: JSON.stringify(schema, null, 2)
|
|
325
|
-
})
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
async getStackablePackageJson (): Promise<PackageJson> {
|
|
329
|
-
if (!this._packageJson) {
|
|
330
|
-
const packageJsonPath = join(__dirname, '..', '..', 'package.json')
|
|
331
|
-
const packageJsonFile = await readFile(packageJsonPath, 'utf8')
|
|
332
|
-
const packageJson: Partial<PackageJson> = JSON.parse(packageJsonFile)
|
|
333
|
-
|
|
334
|
-
if (packageJson.name === undefined || packageJson.name === null) {
|
|
335
|
-
throw new Error('Missing package name in package.json')
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
if (packageJson.version === undefined || packageJson.version === null) {
|
|
339
|
-
throw new Error('Missing package version in package.json')
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
this._packageJson = packageJson as PackageJson
|
|
343
|
-
return packageJson as PackageJson
|
|
344
|
-
}
|
|
345
|
-
return this._packageJson
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
export default ${stackableGeneratorType}
|
|
350
|
-
export { ${stackableGeneratorType} as Generator }
|
|
351
|
-
`
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
function getJsStackableSchemaFile (stackableName) {
|
|
355
|
-
const schemaId = kebabCase(stackableName)
|
|
356
|
-
const schemaTitle = capitalCase(stackableName + 'Config')
|
|
357
|
-
const schemaVarName = camelCase(stackableName + 'Schema')
|
|
358
|
-
|
|
359
|
-
return `\
|
|
360
|
-
'use strict'
|
|
361
|
-
|
|
362
|
-
const { schema } = require('@platformatic/service')
|
|
363
|
-
const { version } = require('../package.json')
|
|
364
|
-
|
|
365
|
-
const ${schemaVarName} = {
|
|
366
|
-
...schema,
|
|
367
|
-
$id: '${schemaId}',
|
|
368
|
-
title: '${schemaTitle}',
|
|
369
|
-
version,
|
|
370
|
-
properties: {
|
|
371
|
-
...schema.properties,
|
|
372
|
-
module: { type: 'string' },
|
|
373
|
-
greeting: {
|
|
374
|
-
type: 'object',
|
|
375
|
-
properties: {
|
|
376
|
-
text: {
|
|
377
|
-
type: 'string'
|
|
378
|
-
}
|
|
379
|
-
},
|
|
380
|
-
required: ['text'],
|
|
381
|
-
additionalProperties: false
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
module.exports.schema = ${schemaVarName}
|
|
387
|
-
|
|
388
|
-
if (require.main === module) {
|
|
389
|
-
console.log(JSON.stringify(${schemaVarName}, null, 2))
|
|
390
|
-
}
|
|
391
|
-
`
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
function getTsStackableSchemaFile (stackableName) {
|
|
395
|
-
const schemaId = kebabCase(stackableName)
|
|
396
|
-
const schemaTitle = capitalCase(stackableName + 'Config')
|
|
397
|
-
const schemaVarName = camelCase(stackableName + 'Schema')
|
|
398
|
-
|
|
399
|
-
return `\
|
|
400
|
-
import { schema } from '@platformatic/service'
|
|
401
|
-
import { readFileSync } from 'node:fs'
|
|
402
|
-
|
|
403
|
-
const { version } = JSON.parse(readFileSync('package.json', 'utf8'))
|
|
404
|
-
|
|
405
|
-
const ${schemaVarName} = {
|
|
406
|
-
...schema,
|
|
407
|
-
$id: '${schemaId}',
|
|
408
|
-
title: '${schemaTitle}',
|
|
409
|
-
version,
|
|
410
|
-
properties: {
|
|
411
|
-
...schema.properties,
|
|
412
|
-
module: { type: 'string' },
|
|
413
|
-
greeting: {
|
|
414
|
-
type: 'object',
|
|
415
|
-
properties: {
|
|
416
|
-
text: {
|
|
417
|
-
type: 'string'
|
|
418
|
-
}
|
|
419
|
-
},
|
|
420
|
-
required: ['text'],
|
|
421
|
-
additionalProperties: false
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
export { ${schemaVarName} as schema }
|
|
427
|
-
|
|
428
|
-
if (require.main === module) {
|
|
429
|
-
console.log(JSON.stringify(${schemaVarName}, null, 2))
|
|
430
|
-
}
|
|
431
|
-
`
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
function getStackableConfigTypesFile (stackableName) {
|
|
435
|
-
const stackableConfigType = pascalCase(stackableName + 'Config')
|
|
436
|
-
|
|
437
|
-
return `\
|
|
438
|
-
// Use npm run build:config to generate this file from the Stackable schema
|
|
439
|
-
export interface ${stackableConfigType} {
|
|
440
|
-
greeting?: {
|
|
441
|
-
text: string;
|
|
442
|
-
};
|
|
443
|
-
}
|
|
444
|
-
`
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
function generateStackableFiles (typescript, stackableName) {
|
|
448
|
-
if (typescript) {
|
|
449
|
-
return [
|
|
450
|
-
{
|
|
451
|
-
path: '',
|
|
452
|
-
file: 'index.ts',
|
|
453
|
-
contents: getTsStackableIndexFile(stackableName)
|
|
454
|
-
},
|
|
455
|
-
{
|
|
456
|
-
path: '',
|
|
457
|
-
file: 'index.d.ts',
|
|
458
|
-
contents: getStackableIndexTypesFile(stackableName)
|
|
459
|
-
},
|
|
460
|
-
{
|
|
461
|
-
path: '',
|
|
462
|
-
file: 'config.d.ts',
|
|
463
|
-
contents: getStackableConfigTypesFile(stackableName)
|
|
464
|
-
},
|
|
465
|
-
{
|
|
466
|
-
path: 'lib',
|
|
467
|
-
file: 'generator.ts',
|
|
468
|
-
contents: getTsStackableGeneratorFile(stackableName)
|
|
469
|
-
},
|
|
470
|
-
{
|
|
471
|
-
path: 'lib/templates',
|
|
472
|
-
file: 'types.ts',
|
|
473
|
-
contents: getTsGlobalTypesTemplateFile(stackableName)
|
|
474
|
-
},
|
|
475
|
-
{
|
|
476
|
-
path: 'lib',
|
|
477
|
-
file: 'schema.ts',
|
|
478
|
-
contents: getTsStackableSchemaFile(stackableName)
|
|
479
|
-
}
|
|
480
|
-
]
|
|
481
|
-
}
|
|
482
|
-
return [
|
|
483
|
-
{
|
|
484
|
-
path: '',
|
|
485
|
-
file: 'index.js',
|
|
486
|
-
contents: getJsStackableIndexFile(stackableName)
|
|
487
|
-
},
|
|
488
|
-
{
|
|
489
|
-
path: '',
|
|
490
|
-
file: 'index.d.ts',
|
|
491
|
-
contents: getStackableIndexTypesFile(stackableName)
|
|
492
|
-
},
|
|
493
|
-
{
|
|
494
|
-
path: '',
|
|
495
|
-
file: 'config.d.ts',
|
|
496
|
-
contents: getStackableConfigTypesFile(stackableName)
|
|
497
|
-
},
|
|
498
|
-
{
|
|
499
|
-
path: 'lib',
|
|
500
|
-
file: 'generator.js',
|
|
501
|
-
contents: getJsStackableGeneratorFile(stackableName)
|
|
502
|
-
},
|
|
503
|
-
{
|
|
504
|
-
path: 'lib/templates',
|
|
505
|
-
file: 'types.js',
|
|
506
|
-
contents: getJsGlobalTypesTemplateFile(stackableName)
|
|
507
|
-
},
|
|
508
|
-
{
|
|
509
|
-
path: 'lib',
|
|
510
|
-
file: 'schema.js',
|
|
511
|
-
contents: getJsStackableSchemaFile(stackableName)
|
|
512
|
-
}
|
|
513
|
-
]
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
module.exports = {
|
|
517
|
-
generateStackableFiles
|
|
518
|
-
}
|