@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.
@@ -1,317 +0,0 @@
1
- 'use strict'
2
-
3
- const { join } = require('node:path')
4
- const { readFile } = require('node:fs/promises')
5
- const { kebabCase } = require('change-case-all')
6
- const { stripVersion, getLatestNpmVersion } = require('./utils')
7
- const { FileGenerator } = require('./file-generator')
8
- const { PrepareError } = require('./errors')
9
- const { generateGitignore } = require('./create-gitignore')
10
- const { generateStackableCli } = require('./create-stackable-cli')
11
- const { generateStackableFiles } = require('./create-stackable-files')
12
- const { generateStackablePlugins } = require('./create-stackable-plugin')
13
- const { generateStackableTests } = require('./create-stackable-tests')
14
-
15
- /* c8 ignore start */
16
- const fakeLogger = {
17
- info: () => {},
18
- warn: () => {},
19
- debug: () => {},
20
- trace: () => {},
21
- error: () => {}
22
- }
23
- /* c8 ignore start */
24
-
25
- class StackableGenerator extends FileGenerator {
26
- constructor (opts = {}) {
27
- super(opts)
28
- this.files = []
29
- this.logger = opts.logger || fakeLogger
30
- this.questions = []
31
- this.pkgData = null
32
- this.inquirer = opts.inquirer || null
33
- this.targetDirectory = opts.targetDirectory || null
34
- this.config = this.getDefaultConfig()
35
- this.packages = []
36
- }
37
-
38
- getDefaultConfig () {
39
- return {
40
- stackableName: 'my-stackable',
41
- typescript: false,
42
- initGitRepository: false,
43
- dependencies: {},
44
- devDependencies: {}
45
- }
46
- }
47
-
48
- setConfigFields (fields) {
49
- for (const field of fields) {
50
- this.config[field.configValue] = field.value
51
- }
52
- }
53
-
54
- setConfig (config) {
55
- if (!config) {
56
- this.config = this.getDefaultConfig()
57
- }
58
- const oldConfig = this.config
59
- this.config = {
60
- ...this.getDefaultConfig(),
61
- ...oldConfig,
62
- ...config
63
- }
64
-
65
- if (this.config.targetDirectory) {
66
- this.targetDirectory = this.config.targetDirectory
67
- }
68
- }
69
-
70
- /* c8 ignore start */
71
- async ask () {
72
- if (this.inquirer) {
73
- await this.prepareQuestions()
74
- const newConfig = await this.inquirer.prompt(this.questions)
75
- this.setConfig({
76
- ...this.config,
77
- ...newConfig
78
- })
79
- }
80
- }
81
-
82
- async prepare () {
83
- try {
84
- this.reset()
85
- await this.getFastifyVersion()
86
- await this.getPlatformaticVersion()
87
-
88
- await this._beforePrepare()
89
-
90
- // generate package.json
91
- const template = await this.generatePackageJson()
92
- this.addFile({
93
- path: '',
94
- file: 'package.json',
95
- contents: JSON.stringify(template, null, 2)
96
- })
97
-
98
- if (this.config.typescript) {
99
- // create tsconfig.json
100
- this.addFile({
101
- path: '',
102
- file: 'tsconfig.json',
103
- contents: JSON.stringify(this.getTsConfig(), null, 2)
104
- })
105
- }
106
-
107
- const typescript = this.config.typescript
108
- const stackableName = this.config.stackableName
109
-
110
- this.files.push(...generateStackableFiles(typescript, stackableName))
111
- this.files.push(...generateStackableCli(typescript, stackableName))
112
- this.files.push(...generateStackablePlugins(typescript))
113
- this.files.push(...generateStackableTests(typescript, stackableName))
114
- this.files.push(generateGitignore())
115
-
116
- await this._afterPrepare()
117
-
118
- return {
119
- targetDirectory: this.targetDirectory
120
- }
121
- } catch (err) {
122
- if (err.code?.startsWith('PLT_GEN')) {
123
- // throw the same error
124
- throw err
125
- }
126
- const _err = new PrepareError(err.message)
127
- _err.cause = err
128
- throw _err
129
- }
130
- }
131
-
132
- getTsConfig () {
133
- return {
134
- compilerOptions: {
135
- module: 'commonjs',
136
- esModuleInterop: true,
137
- target: 'es2020',
138
- sourceMap: true,
139
- pretty: true,
140
- noEmitOnError: true,
141
- incremental: true,
142
- strict: true,
143
- outDir: 'dist'
144
- },
145
- watchOptions: {
146
- watchFile: 'fixedPollingInterval',
147
- watchDirectory: 'fixedPollingInterval',
148
- fallbackPolling: 'dynamicPriority',
149
- synchronousWatchDirectory: true,
150
- excludeDirectories: ['**/node_modules', 'dist']
151
- }
152
- }
153
- }
154
-
155
- async prepareQuestions () {
156
- if (!this.config.targetDirectory) {
157
- // directory
158
- this.questions.push({
159
- type: 'input',
160
- name: 'targetDirectory',
161
- message: 'Where would you like to create your project?',
162
- default: 'platformatic'
163
- })
164
- }
165
-
166
- this.questions.push({
167
- type: 'input',
168
- name: 'stackableName',
169
- message: 'What is the name of the stackable?',
170
- default: 'my-stackable'
171
- })
172
-
173
- // typescript
174
- this.questions.push({
175
- type: 'list',
176
- name: 'typescript',
177
- message: 'Do you want to use TypeScript?',
178
- default: false,
179
- choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
180
- })
181
- }
182
-
183
- /**
184
- * Reads the content of package.json and returns it as an object
185
- * @returns Object
186
- */
187
- async readPackageJsonFile () {
188
- if (this.pkgData) {
189
- return this.pkgData
190
- }
191
- const currentPackageJsonPath = join(__dirname, '..', 'package.json')
192
- this.pkgData = JSON.parse(await readFile(currentPackageJsonPath, 'utf8'))
193
- return this.pkgData
194
- }
195
-
196
- async getFastifyVersion () {
197
- const pkgData = await this.readPackageJsonFile()
198
- this.fastifyVersion = stripVersion(pkgData.dependencies.fastify)
199
- }
200
-
201
- async getPlatformaticVersion () {
202
- const pkgData = await this.readPackageJsonFile()
203
- this.platformaticVersion = stripVersion(pkgData.version)
204
- }
205
-
206
- async generatePackageJson () {
207
- const dependencies = {
208
- '@platformatic/config': `^${this.platformaticVersion}`,
209
- '@platformatic/service': `^${this.platformaticVersion}`,
210
- 'json-schema-to-typescript': '^13.0.0'
211
- }
212
-
213
- const devDependencies = {
214
- borp: '^0.10.0',
215
- fastify: `^${this.fastifyVersion}`
216
- }
217
-
218
- const npmPackageName = kebabCase(this.config.stackableName)
219
- const createStackableCommand = kebabCase('create-' + this.config.stackableName)
220
- const startStackableCommand = kebabCase('start-' + this.config.stackableName)
221
-
222
- if (this.config.typescript) {
223
- const packageJsonFile = await readFile(join(__dirname, '..', 'package.json'), 'utf-8')
224
- const typescriptVersion = JSON.parse(packageJsonFile).devDependencies.typescript
225
-
226
- return {
227
- name: npmPackageName,
228
- version: '0.0.1',
229
- main: 'dist/index.js',
230
- bin: {
231
- [createStackableCommand]: './dist/cli/create.js',
232
- [startStackableCommand]: './dist/cli/start.js'
233
- },
234
- scripts: {
235
- build: 'tsc --build',
236
- 'gen-schema': 'node lib/schema.js > schema.json',
237
- 'gen-types': 'json2ts > config.d.ts < schema.json',
238
- 'build:config': 'pnpm run gen-schema && pnpm run gen-types',
239
- clean: 'rm -fr ./dist',
240
- test: 'borp'
241
- },
242
- engines: {
243
- node: '^18.8.0 || >=20.6.0'
244
- },
245
- devDependencies: {
246
- ...devDependencies,
247
- typescript: typescriptVersion,
248
- ...this.config.devDependencies
249
- },
250
- dependencies: {
251
- ...dependencies,
252
- '@platformatic/generators': `^${this.platformaticVersion}`,
253
- ...this.config.dependencies
254
- }
255
- }
256
- }
257
-
258
- return {
259
- name: npmPackageName,
260
- version: '0.0.1',
261
- main: 'index.js',
262
- bin: {
263
- [createStackableCommand]: './cli/create.js',
264
- [startStackableCommand]: './cli/start.js'
265
- },
266
- scripts: {
267
- 'gen-schema': 'node lib/schema.js > schema.json',
268
- 'gen-types': 'json2ts > config.d.ts < schema.json',
269
- 'build:config': 'pnpm run gen-schema && pnpm run gen-types',
270
- prepublishOnly: 'pnpm run build:config',
271
- lint: 'standard',
272
- test: 'borp'
273
- },
274
- engines: {
275
- node: '^18.8.0 || >=20.6.0'
276
- },
277
- devDependencies: {
278
- ...devDependencies,
279
- standard: '^17.0.0',
280
- ...this.config.devDependencies
281
- },
282
- dependencies: {
283
- ...dependencies,
284
- ...this.config.dependencies
285
- }
286
- }
287
- }
288
-
289
- async run () {
290
- const metadata = await this.prepare()
291
- await this.writeFiles()
292
- return metadata
293
- }
294
-
295
- async addPackage (pkg) {
296
- this.config.dependencies[pkg.name] = 'latest'
297
- try {
298
- const version = await getLatestNpmVersion(pkg.name)
299
- if (version) {
300
- this.config.dependencies[pkg.name] = version
301
- }
302
- } catch (err) {
303
- this.logger.warn(`Could not get latest version for ${pkg.name}, setting it to latest`)
304
- }
305
- this.packages.push(pkg)
306
- }
307
-
308
- // implement in the subclass
309
- /* c8 ignore next 1 */
310
- async postInstallActions () {}
311
- async _beforePrepare () {}
312
- async _afterPrepare () {}
313
- async _getConfigFileContents () { return {} }
314
- }
315
-
316
- module.exports = StackableGenerator
317
- module.exports.StackableGenerator = StackableGenerator