@platformatic/generators 2.0.0-alpha.2 → 2.0.0-alpha.3

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.
@@ -4,27 +4,27 @@ export namespace FileGenerator {
4
4
  export type FileGeneratorOptions = {
5
5
  logger?: BaseLogger
6
6
  }
7
-
7
+
8
8
  export type FileObject = {
9
9
  path: string,
10
10
  file: string,
11
11
  contents: string
12
12
  }
13
-
13
+
14
14
  export class FileGenerator {
15
15
  files: FileObject[]
16
16
  targetDirectory: string
17
-
18
- constructor(opts?: FileGeneratorOptions)
19
-
20
- setTargetDirectory(dir: string): void
21
- addFile(file: FileObject): void
22
- appendfile(file: FileObject): void
23
- reset(): void
24
- writeFiles(): Promise<void>
25
- listFiles(): FileObject
26
- loadFile(): Promise<FileObject>
27
- getFileObject(file: string, path?: string): FileObject
17
+
18
+ constructor (opts?: FileGeneratorOptions)
19
+
20
+ setTargetDirectory (dir: string): void
21
+ addFile (file: FileObject): void
22
+ appendfile (file: FileObject): void
23
+ reset (): void
24
+ writeFiles (): Promise<void>
25
+ listFiles (): FileObject
26
+ loadFile (): Promise<FileObject>
27
+ getFileObject (file: string, path?: string): FileObject
28
28
  }
29
-
29
+
30
30
  }
@@ -1,5 +1,5 @@
1
1
  'use strict'
2
- const { safeMkdir } = require('./utils')
2
+ const { createDirectory } = require('@platformatic/utils')
3
3
  const { join } = require('node:path')
4
4
  const { writeFile, readFile } = require('node:fs/promises')
5
5
 
@@ -9,7 +9,7 @@ const fakeLogger = {
9
9
  warn: () => {},
10
10
  debug: () => {},
11
11
  trace: () => {},
12
- error: () => {}
12
+ error: () => {},
13
13
  }
14
14
  /* c8 ignore start */
15
15
 
@@ -59,14 +59,14 @@ class FileGenerator {
59
59
  if (!this.targetDirectory) {
60
60
  throw new Error('No target directory set.')
61
61
  }
62
- await safeMkdir(this.targetDirectory)
62
+ await createDirectory(this.targetDirectory)
63
63
  for (const fileToWrite of this.files) {
64
64
  if (fileToWrite.contents.length === 0) {
65
65
  continue
66
66
  }
67
67
  const baseDir = join(this.targetDirectory, fileToWrite.path)
68
68
  if (fileToWrite.path !== '') {
69
- await safeMkdir(baseDir)
69
+ await createDirectory(baseDir)
70
70
  }
71
71
  const fullFilePath = join(baseDir, fileToWrite.file)
72
72
  await writeFile(fullFilePath, fileToWrite.contents, fileToWrite.options)
@@ -18,7 +18,7 @@ const fakeLogger = {
18
18
  warn: () => {},
19
19
  debug: () => {},
20
20
  trace: () => {},
21
- error: () => {}
21
+ error: () => {},
22
22
  }
23
23
  /* c8 ignore start */
24
24
 
@@ -41,7 +41,7 @@ class StackableGenerator extends FileGenerator {
41
41
  typescript: false,
42
42
  initGitRepository: false,
43
43
  dependencies: {},
44
- devDependencies: {}
44
+ devDependencies: {},
45
45
  }
46
46
  }
47
47
 
@@ -59,7 +59,7 @@ class StackableGenerator extends FileGenerator {
59
59
  this.config = {
60
60
  ...this.getDefaultConfig(),
61
61
  ...oldConfig,
62
- ...config
62
+ ...config,
63
63
  }
64
64
 
65
65
  if (this.config.targetDirectory) {
@@ -74,7 +74,7 @@ class StackableGenerator extends FileGenerator {
74
74
  const newConfig = await this.inquirer.prompt(this.questions)
75
75
  this.setConfig({
76
76
  ...this.config,
77
- ...newConfig
77
+ ...newConfig,
78
78
  })
79
79
  }
80
80
  }
@@ -92,7 +92,7 @@ class StackableGenerator extends FileGenerator {
92
92
  this.addFile({
93
93
  path: '',
94
94
  file: 'package.json',
95
- contents: JSON.stringify(template, null, 2)
95
+ contents: JSON.stringify(template, null, 2),
96
96
  })
97
97
 
98
98
  if (this.config.typescript) {
@@ -100,7 +100,7 @@ class StackableGenerator extends FileGenerator {
100
100
  this.addFile({
101
101
  path: '',
102
102
  file: 'tsconfig.json',
103
- contents: JSON.stringify(this.getTsConfig(), null, 2)
103
+ contents: JSON.stringify(this.getTsConfig(), null, 2),
104
104
  })
105
105
  }
106
106
 
@@ -116,7 +116,7 @@ class StackableGenerator extends FileGenerator {
116
116
  await this._afterPrepare()
117
117
 
118
118
  return {
119
- targetDirectory: this.targetDirectory
119
+ targetDirectory: this.targetDirectory,
120
120
  }
121
121
  } catch (err) {
122
122
  if (err.code?.startsWith('PLT_GEN')) {
@@ -140,15 +140,15 @@ class StackableGenerator extends FileGenerator {
140
140
  noEmitOnError: true,
141
141
  incremental: true,
142
142
  strict: true,
143
- outDir: 'dist'
143
+ outDir: 'dist',
144
144
  },
145
145
  watchOptions: {
146
146
  watchFile: 'fixedPollingInterval',
147
147
  watchDirectory: 'fixedPollingInterval',
148
148
  fallbackPolling: 'dynamicPriority',
149
149
  synchronousWatchDirectory: true,
150
- excludeDirectories: ['**/node_modules', 'dist']
151
- }
150
+ excludeDirectories: ['**/node_modules', 'dist'],
151
+ },
152
152
  }
153
153
  }
154
154
 
@@ -159,7 +159,7 @@ class StackableGenerator extends FileGenerator {
159
159
  type: 'input',
160
160
  name: 'targetDirectory',
161
161
  message: 'Where would you like to create your project?',
162
- default: 'platformatic'
162
+ default: 'platformatic',
163
163
  })
164
164
  }
165
165
 
@@ -167,7 +167,7 @@ class StackableGenerator extends FileGenerator {
167
167
  type: 'input',
168
168
  name: 'stackableName',
169
169
  message: 'What is the name of the stackable?',
170
- default: 'my-stackable'
170
+ default: 'my-stackable',
171
171
  })
172
172
 
173
173
  // typescript
@@ -176,7 +176,7 @@ class StackableGenerator extends FileGenerator {
176
176
  name: 'typescript',
177
177
  message: 'Do you want to use TypeScript?',
178
178
  default: false,
179
- choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
179
+ choices: [{ name: 'yes', value: true }, { name: 'no', value: false }],
180
180
  })
181
181
  }
182
182
 
@@ -207,12 +207,12 @@ class StackableGenerator extends FileGenerator {
207
207
  const dependencies = {
208
208
  '@platformatic/config': `^${this.platformaticVersion}`,
209
209
  '@platformatic/service': `^${this.platformaticVersion}`,
210
- 'json-schema-to-typescript': '^13.0.0'
210
+ 'json-schema-to-typescript': '^13.0.0',
211
211
  }
212
212
 
213
213
  const devDependencies = {
214
214
  borp: '^0.10.0',
215
- fastify: `^${this.fastifyVersion}`
215
+ fastify: `^${this.fastifyVersion}`,
216
216
  }
217
217
 
218
218
  const npmPackageName = kebabCase(this.config.stackableName)
@@ -229,7 +229,7 @@ class StackableGenerator extends FileGenerator {
229
229
  main: 'dist/index.js',
230
230
  bin: {
231
231
  [createStackableCommand]: './dist/cli/create.js',
232
- [startStackableCommand]: './dist/cli/start.js'
232
+ [startStackableCommand]: './dist/cli/start.js',
233
233
  },
234
234
  scripts: {
235
235
  build: 'tsc --build',
@@ -237,21 +237,21 @@ class StackableGenerator extends FileGenerator {
237
237
  'gen-types': 'json2ts > config.d.ts < schema.json',
238
238
  'build:config': 'pnpm run gen-schema && pnpm run gen-types',
239
239
  clean: 'rm -fr ./dist',
240
- test: 'borp'
240
+ test: 'borp',
241
241
  },
242
242
  engines: {
243
- node: '^18.8.0 || >=20.6.0'
243
+ node: '^18.8.0 || >=20.6.0',
244
244
  },
245
245
  devDependencies: {
246
246
  ...devDependencies,
247
247
  typescript: typescriptVersion,
248
- ...this.config.devDependencies
248
+ ...this.config.devDependencies,
249
249
  },
250
250
  dependencies: {
251
251
  ...dependencies,
252
252
  '@platformatic/generators': `^${this.platformaticVersion}`,
253
- ...this.config.dependencies
254
- }
253
+ ...this.config.dependencies,
254
+ },
255
255
  }
256
256
  }
257
257
 
@@ -261,7 +261,7 @@ class StackableGenerator extends FileGenerator {
261
261
  main: 'index.js',
262
262
  bin: {
263
263
  [createStackableCommand]: './cli/create.js',
264
- [startStackableCommand]: './cli/start.js'
264
+ [startStackableCommand]: './cli/start.js',
265
265
  },
266
266
  scripts: {
267
267
  'gen-schema': 'node lib/schema.js > schema.json',
@@ -269,20 +269,20 @@ class StackableGenerator extends FileGenerator {
269
269
  'build:config': 'pnpm run gen-schema && pnpm run gen-types',
270
270
  prepublishOnly: 'pnpm run build:config',
271
271
  lint: 'standard',
272
- test: 'borp'
272
+ test: 'borp',
273
273
  },
274
274
  engines: {
275
- node: '^18.8.0 || >=20.6.0'
275
+ node: '^18.8.0 || >=20.6.0',
276
276
  },
277
277
  devDependencies: {
278
278
  ...devDependencies,
279
279
  standard: '^17.0.0',
280
- ...this.config.devDependencies
280
+ ...this.config.devDependencies,
281
281
  },
282
282
  dependencies: {
283
283
  ...dependencies,
284
- ...this.config.dependencies
285
- }
284
+ ...this.config.dependencies,
285
+ },
286
286
  }
287
287
  }
288
288
 
package/lib/utils.d.ts CHANGED
@@ -9,14 +9,13 @@ export type PackageConfiguration = {
9
9
  }
10
10
 
11
11
  export namespace GeneratorUtils {
12
- export function safeMkdir(dir: string): Promise<void>
13
- export function stripVersion(version: string): string
14
- export function convertServiceNameToPrefix(serviceName: string): string
15
- export function envObjectToString(env: Env): string
16
- export function envStringToObject(env: string): Env
17
- export function extractEnvVariablesFromText(text: string): string[]
18
- export function getPackageConfigurationObject(config: PackageConfiguration[]): object
19
- export function flattenObject(obj: object): object
20
- export function getServiceTemplateFromSchemaUrl(schemaUrl: string): string
12
+ export function stripVersion (version: string): string
13
+ export function convertServiceNameToPrefix (serviceName: string): string
14
+ export function envObjectToString (env: Env): string
15
+ export function envStringToObject (env: string): Env
16
+ export function extractEnvVariablesFromText (text: string): string[]
17
+ export function getPackageConfigurationObject (config: PackageConfiguration[]): object
18
+ export function flattenObject (obj: object): object
19
+ export function getServiceTemplateFromSchemaUrl (schemaUrl: string): string
21
20
  export const PLT_ROOT: string
22
21
  }
package/lib/utils.js CHANGED
@@ -1,20 +1,12 @@
1
1
  'use strict'
2
2
 
3
- const { mkdir } = require('node:fs/promises')
4
3
  const { WrongTypeError } = require('./errors')
5
4
  const { join } = require('node:path')
6
5
  const { request } = require('undici')
7
6
  const { setTimeout } = require('timers/promises')
8
7
  const PLT_ROOT = 'PLT_ROOT'
9
8
  const { EOL } = require('node:os')
10
- async function safeMkdir (dir) {
11
- try {
12
- await mkdir(dir, { recursive: true })
13
- /* c8 ignore next 3 */
14
- } catch (err) {
15
- // do nothing
16
- }
17
- }
9
+ const { createDirectory } = require('@platformatic/utils')
18
10
 
19
11
  /**
20
12
  * Strip all extra characters from a simple semver version string
@@ -49,7 +41,7 @@ function addPrefixToString (input, prefix) {
49
41
 
50
42
  function envObjectToString (env) {
51
43
  const output = []
52
- Object.entries(env).forEach((kv) => {
44
+ Object.entries(env).forEach(kv => {
53
45
  output.push(`${kv[0]}=${kv[1]}`)
54
46
  })
55
47
  return output.join(EOL)
@@ -57,12 +49,12 @@ function envObjectToString (env) {
57
49
 
58
50
  function envStringToObject (envString) {
59
51
  const output = {}
60
- const split = envString.split(EOL)
52
+ const split = envString.split(/\r?\n/)
61
53
  split
62
- .filter((line) => {
54
+ .filter(line => {
63
55
  return line.trim() !== '' && line.indexOf('#') !== 0
64
56
  })
65
- .forEach((line) => {
57
+ .forEach(line => {
66
58
  const kv = line.split('=')
67
59
  output[kv[0]] = kv[1]
68
60
  })
@@ -71,16 +63,14 @@ function envStringToObject (envString) {
71
63
  function extractEnvVariablesFromText (text) {
72
64
  const match = text.match(/\{[a-zA-Z0-9-_]*\}/g)
73
65
  if (match) {
74
- return match
75
- .map((found) => found.replace('{', '').replace('}', ''))
76
- .filter((found) => found !== '')
66
+ return match.map(found => found.replace('{', '').replace('}', '')).filter(found => found !== '')
77
67
  }
78
68
  return []
79
69
  }
80
70
  function getPackageConfigurationObject (config, serviceName = '') {
81
71
  const output = {
82
72
  config: {},
83
- env: {}
73
+ env: {},
84
74
  }
85
75
  let current = output.config
86
76
  for (const param of config) {
@@ -90,14 +80,14 @@ function getPackageConfigurationObject (config, serviceName = '') {
90
80
  let value
91
81
  let isPath = false
92
82
  switch (param.type) {
93
- case 'string' :
83
+ case 'string':
94
84
  value = param.value.toString()
95
85
  break
96
86
  case 'number':
97
87
  value = parseInt(param.value)
98
88
  break
99
89
  case 'boolean':
100
- value = (param.value === 'true')
90
+ value = param.value === 'true'
101
91
  break
102
92
  case 'path':
103
93
  value = `${join(`{${PLT_ROOT}}`, param.value)}`
@@ -167,7 +157,7 @@ async function getLatestNpmVersion (pkg) {
167
157
  function flattenObject (ob) {
168
158
  const result = {}
169
159
  for (const i in ob) {
170
- if ((typeof ob[i]) === 'object' && !Array.isArray(ob[i])) {
160
+ if (typeof ob[i] === 'object' && !Array.isArray(ob[i])) {
171
161
  const temp = flattenObject(ob[i])
172
162
  for (const j in temp) {
173
163
  result[i + '.' + j] = temp[j]
@@ -181,8 +171,13 @@ function flattenObject (ob) {
181
171
 
182
172
  function getServiceTemplateFromSchemaUrl (schemaUrl) {
183
173
  const splitted = schemaUrl.split('/')
184
- return `@platformatic/${splitted[splitted.length - 1]}`
174
+
175
+ if (schemaUrl.startsWith('https://platformatic.dev/schemas')) {
176
+ return `@platformatic/${splitted[splitted.length - 1]}`
177
+ }
178
+ return `@platformatic/${splitted[splitted.length - 2]}`
185
179
  }
180
+
186
181
  module.exports = {
187
182
  addPrefixToString,
188
183
  convertServiceNameToPrefix,
@@ -192,8 +187,8 @@ module.exports = {
192
187
  extractEnvVariablesFromText,
193
188
  flattenObject,
194
189
  getServiceTemplateFromSchemaUrl,
195
- safeMkdir,
190
+ createDirectory,
196
191
  stripVersion,
197
192
  PLT_ROOT,
198
- getLatestNpmVersion
193
+ getLatestNpmVersion,
199
194
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/generators",
3
- "version": "2.0.0-alpha.2",
3
+ "version": "2.0.0-alpha.3",
4
4
  "description": "Main classes and utils for generators.",
5
5
  "main": "index.js",
6
6
  "keywords": [],
@@ -12,19 +12,20 @@
12
12
  "change-case-all": "^2.1.0",
13
13
  "fastify": "^4.26.2",
14
14
  "pino": "^8.19.0",
15
- "undici": "^6.9.0"
15
+ "undici": "^6.9.0",
16
+ "@platformatic/utils": "2.0.0-alpha.3"
16
17
  },
17
18
  "devDependencies": {
18
19
  "@types/inquirer": "^9.0.7",
19
- "borp": "^0.16.0",
20
+ "borp": "^0.17.0",
20
21
  "c8": "^10.0.0",
21
- "snazzy": "^9.0.0",
22
- "standard": "^17.1.0",
22
+ "eslint": "9",
23
+ "neostandard": "^0.11.1",
23
24
  "tsd": "^0.31.0",
24
- "typescript": "^5.4.2"
25
+ "typescript": "^5.5.4"
25
26
  },
26
27
  "scripts": {
27
- "lint": "standard | snazzy",
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
  }