@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.
@@ -3,10 +3,10 @@
3
3
  const { test, describe } = require('node:test')
4
4
  const assert = require('node:assert')
5
5
  const { FileGenerator } = require('../lib/file-generator')
6
- const { rm, readFile } = require('node:fs/promises')
6
+ const { readFile } = require('node:fs/promises')
7
7
  const { join } = require('node:path')
8
8
  const { tmpdir } = require('node:os')
9
- const { safeMkdir } = require('../lib/utils')
9
+ const { createDirectory, safeRemove } = require('@platformatic/utils')
10
10
 
11
11
  let dirCount = 0
12
12
  describe('FileGenerator', () => {
@@ -15,14 +15,14 @@ describe('FileGenerator', () => {
15
15
  const fileObject = fg.getFileObject('sample', 'file')
16
16
  assert.strictEqual(null, fileObject)
17
17
  })
18
- test('should throw if no targeDirectory is set', async (t) => {
18
+ test('should throw if no targeDirectory is set', async t => {
19
19
  const fg = new FileGenerator()
20
20
  assert.rejects(async () => {
21
21
  await fg.writeFiles()
22
22
  })
23
23
  })
24
24
 
25
- test('should replace a file with same name and path', async (t) => {
25
+ test('should replace a file with same name and path', async t => {
26
26
  const fg = new FileGenerator()
27
27
  fg.addFile({ path: 'path', file: 'file', contents: 'hello world' })
28
28
  fg.addFile({ path: 'path', file: 'file', contents: 'foobar' })
@@ -32,7 +32,7 @@ describe('FileGenerator', () => {
32
32
  assert.equal(fg.files.length, 1)
33
33
  })
34
34
 
35
- test('should list files', async (t) => {
35
+ test('should list files', async t => {
36
36
  const fg = new FileGenerator()
37
37
  fg.addFile({ path: 'path', file: 'helloworld.txt', contents: 'hello world' })
38
38
  fg.addFile({ path: 'path', file: 'foobar.txt', contents: 'foobar' })
@@ -41,10 +41,10 @@ describe('FileGenerator', () => {
41
41
  assert.deepEqual(fg.listFiles(), [
42
42
  join('path', 'helloworld.txt'),
43
43
  join('path', 'foobar.txt'),
44
- join('anotherpath', 'foobar.txt')
44
+ join('anotherpath', 'foobar.txt'),
45
45
  ])
46
46
  })
47
- test('should append file content', async (t) => {
47
+ test('should append file content', async t => {
48
48
  const fg = new FileGenerator()
49
49
  fg.addFile({ path: 'path', file: 'helloworld.txt', contents: 'hello world' })
50
50
 
@@ -59,7 +59,7 @@ describe('FileGenerator', () => {
59
59
  assert.equal(newFileObject.contents, 'foobar')
60
60
  })
61
61
 
62
- test('should reset all files', async (t) => {
62
+ test('should reset all files', async t => {
63
63
  const fg = new FileGenerator()
64
64
  fg.addFile({ path: 'path', file: 'file', contents: 'hello world' })
65
65
  fg.addFile({ path: 'path', file: 'file', contents: 'foobar' })
@@ -68,13 +68,13 @@ describe('FileGenerator', () => {
68
68
  assert.equal(fg.files.length, 0)
69
69
  })
70
70
 
71
- test('should write files', async (t) => {
71
+ test('should write files', async t => {
72
72
  const tempDir = join(tmpdir(), `plt-file-generator-test-${dirCount++}`)
73
73
  t.after(async () => {
74
- await rm(tempDir, { recursive: true })
74
+ await safeRemove(tempDir)
75
75
  })
76
76
 
77
- await safeMkdir(tempDir)
77
+ await createDirectory(tempDir)
78
78
  const fg = new FileGenerator()
79
79
  fg.setTargetDirectory(tempDir)
80
80
  fg.addFile({ path: 'myDir', file: 'helloworld.txt', contents: 'hello world' })
@@ -84,13 +84,13 @@ describe('FileGenerator', () => {
84
84
  assert.equal(fileContents, 'hello world')
85
85
  })
86
86
 
87
- test('should not write empty files', async (t) => {
87
+ test('should not write empty files', async t => {
88
88
  const tempDir = join(tmpdir(), `plt-file-generator-test-${dirCount++}`)
89
89
  t.after(async () => {
90
- await rm(tempDir, { recursive: true })
90
+ await safeRemove(tempDir)
91
91
  })
92
92
 
93
- await safeMkdir(tempDir)
93
+ await createDirectory(tempDir)
94
94
  const fg = new FileGenerator()
95
95
  fg.setTargetDirectory(tempDir)
96
96
  fg.addFile({ path: 'myDir', file: 'helloworld.txt', contents: '' })
@@ -104,13 +104,13 @@ describe('FileGenerator', () => {
104
104
  }
105
105
  })
106
106
 
107
- test('should load file from filesystem', async (t) => {
107
+ test('should load file from filesystem', async t => {
108
108
  const tempDir = join(tmpdir(), `plt-file-generator-test-${dirCount++}`)
109
109
  t.after(async () => {
110
- await rm(tempDir, { recursive: true })
110
+ await safeRemove(tempDir)
111
111
  })
112
112
 
113
- await safeMkdir(tempDir)
113
+ await createDirectory(tempDir)
114
114
  const fg = new FileGenerator()
115
115
  fg.setTargetDirectory(tempDir)
116
116
  fg.addFile({ path: 'myDir', file: 'helloworld.txt', contents: 'hello world' })
@@ -119,14 +119,14 @@ describe('FileGenerator', () => {
119
119
  fg.reset()
120
120
  const fileObject = await fg.loadFile({
121
121
  path: 'myDir',
122
- file: 'helloworld.txt'
122
+ file: 'helloworld.txt',
123
123
  })
124
124
 
125
125
  assert.deepEqual(fileObject, {
126
126
  path: 'myDir',
127
127
  file: 'helloworld.txt',
128
128
  contents: 'hello world',
129
- options: {}
129
+ options: {},
130
130
  })
131
131
 
132
132
  assert.equal(fg.files.length, 1)
@@ -134,7 +134,7 @@ describe('FileGenerator', () => {
134
134
  path: 'myDir',
135
135
  file: 'helloworld.txt',
136
136
  contents: 'hello world',
137
- options: {}
137
+ options: {},
138
138
  })
139
139
  })
140
140
  })
@@ -1,7 +1,7 @@
1
1
  {
2
- "$schema": "https://platformatic.dev/schemas/v1.25.0/runtime",
2
+ "$schema": "https://schemas.platformatic.dev/@platformatic/runtime/1.52.0.json",
3
3
  "entrypoint": "rival",
4
- "hotReload": true,
4
+ "watch": true,
5
5
  "autoload": {
6
6
  "path": "services",
7
7
  "exclude": [
@@ -1,5 +1,5 @@
1
1
  {
2
- "$schema": "https://platformatic.dev/schemas/v1.25.0/service",
2
+ "$schema": "https://schemas.platformatic.dev/@platformatic/service/1.52.0.json",
3
3
  "service": {
4
4
  "openapi": true
5
5
  },
@@ -1,5 +1,5 @@
1
1
  {
2
- "$schema": "https://platformatic.dev/schemas/v1.25.0/service",
2
+ "$schema": "https://schemas.platformatic.dev/@platformatic/service/1.52.0.json",
3
3
  "service": {
4
4
  "openapi": true
5
5
  },
package/test/helpers.js CHANGED
@@ -1,9 +1,8 @@
1
1
  'use strict'
2
2
 
3
3
  const { join } = require('node:path')
4
- const fs = require('node:fs/promises')
5
- const { safeMkdir } = require('../lib/utils')
6
4
  const { MockAgent, setGlobalDispatcher } = require('undici')
5
+ const { safeRemove, createDirectory } = require('@platformatic/utils')
7
6
 
8
7
  const mockAgent = new MockAgent()
9
8
  setGlobalDispatcher(mockAgent)
@@ -16,21 +15,17 @@ async function getTempDir (baseDir) {
16
15
  baseDir = __dirname
17
16
  }
18
17
  const dir = join(baseDir, 'tmp', `platformatic-generators-${process.pid}-${Date.now()}-${counter++}`)
19
- await safeMkdir(dir)
18
+ await createDirectory(dir)
20
19
  return dir
21
20
  }
22
21
  async function moveToTmpdir (teardown) {
23
22
  const cwd = process.cwd()
24
- // const tmp = join(__dirname, 'tmp')
25
- // try {
26
- // await fs.mkdir(tmp)
27
- // } catch {
28
- // }
23
+
29
24
  const dir = await getTempDir()
30
25
  process.chdir(dir)
31
26
  teardown(() => process.chdir(cwd))
32
27
  if (!process.env.SKIP_RM_TMP) {
33
- teardown(() => fs.rm(dir, { recursive: true }).catch(() => {}))
28
+ teardown(() => safeRemove(dir))
34
29
  }
35
30
  return dir
36
31
  }
@@ -41,12 +36,12 @@ function mockNpmJsRequestForPkgs (pkgs) {
41
36
  .get('https://registry.npmjs.org')
42
37
  .intercept({
43
38
  method: 'GET',
44
- path: `/${pkg}`
39
+ path: `/${pkg}`,
45
40
  })
46
41
  .reply(200, {
47
42
  'dist-tags': {
48
- latest: '1.42.0'
49
- }
43
+ latest: '1.42.0',
44
+ },
50
45
  })
51
46
  }
52
47
  }
@@ -55,10 +50,10 @@ module.exports = {
55
50
  info: () => {},
56
51
  debug: () => {},
57
52
  warn: () => {},
58
- error: () => {}
53
+ error: () => {},
59
54
  },
60
55
  getTempDir,
61
56
  moveToTmpdir,
62
57
  mockNpmJsRequestForPkgs,
63
- mockAgent
58
+ mockAgent,
64
59
  }
@@ -1,29 +1,30 @@
1
1
  'use strict'
2
2
 
3
- const { readFile, rm } = require('node:fs/promises')
3
+ const { readFile } = require('node:fs/promises')
4
4
  const { test, afterEach } = require('node:test')
5
5
  const assert = require('node:assert')
6
6
  const { join } = require('node:path')
7
7
 
8
+ const { safeRemove } = require('@platformatic/utils')
8
9
  const { fakeLogger, getTempDir } = require('./helpers')
9
10
  const { StackableGenerator } = require('../lib/stackable-generator')
10
11
 
11
12
  afterEach(async () => {
12
13
  try {
13
- await rm(join(__dirname, 'tmp'), { recursive: true })
14
+ await safeRemove(join(__dirname, 'tmp'))
14
15
  } catch (err) {
15
16
  // do nothing
16
17
  }
17
18
  })
18
19
 
19
- test('should create a stackable project without typescript', async (t) => {
20
+ test('should create a stackable project without typescript', async t => {
20
21
  const dir = await getTempDir()
21
22
  const gen = new StackableGenerator({
22
- logger: fakeLogger
23
+ logger: fakeLogger,
23
24
  })
24
25
 
25
26
  gen.setConfig({
26
- targetDirectory: dir
27
+ targetDirectory: dir,
27
28
  })
28
29
 
29
30
  await gen.run()
@@ -64,15 +65,15 @@ test('should create a stackable project without typescript', async (t) => {
64
65
  assert.ok(testGeneratorFile.length > 0)
65
66
  })
66
67
 
67
- test('should create a stackable project with typescript', async (t) => {
68
+ test('should create a stackable project with typescript', async t => {
68
69
  const dir = await getTempDir()
69
70
  const gen = new StackableGenerator({
70
- logger: fakeLogger
71
+ logger: fakeLogger,
71
72
  })
72
73
 
73
74
  gen.setConfig({
74
75
  targetDirectory: dir,
75
- typescript: true
76
+ typescript: true,
76
77
  })
77
78
 
78
79
  await gen.run()
@@ -9,7 +9,7 @@ const {
9
9
  envObjectToString,
10
10
  extractEnvVariablesFromText,
11
11
  getPackageConfigurationObject,
12
- addPrefixToString
12
+ addPrefixToString,
13
13
  } = require('../lib/utils')
14
14
  const { flattenObject } = require('../lib/utils')
15
15
  const { getServiceTemplateFromSchemaUrl } = require('../lib/utils')
@@ -34,7 +34,7 @@ describe('utils', () => {
34
34
  'my-service': 'MY_SERVICE',
35
35
  a: 'A',
36
36
  MY_SERVICE: 'MY_SERVICE',
37
- asderas123: 'ASDERAS123'
37
+ asderas123: 'ASDERAS123',
38
38
  }
39
39
 
40
40
  Object.entries(expectations).forEach((exp) => {
@@ -48,7 +48,7 @@ describe('utils', () => {
48
48
  test('should convert env object to string', async () => {
49
49
  const env = {
50
50
  FOO: 'bar',
51
- DATABASE_URL: 'sqlite://./db.sqlite'
51
+ DATABASE_URL: 'sqlite://./db.sqlite',
52
52
  }
53
53
 
54
54
  assert.equal(envObjectToString(env), `FOO=bar${EOL}DATABASE_URL=sqlite://./db.sqlite`)
@@ -86,39 +86,39 @@ describe('utils', () => {
86
86
  {
87
87
  path: 'prefix',
88
88
  value: '/foo',
89
- type: 'string'
89
+ type: 'string',
90
90
  },
91
91
  {
92
92
  path: 'foo.fooOption1',
93
93
  value: 'value1',
94
- type: 'string'
94
+ type: 'string',
95
95
  },
96
96
  {
97
97
  path: 'foo.fooOption2',
98
98
  value: 'value2',
99
- type: 'string'
99
+ type: 'string',
100
100
  },
101
101
  {
102
102
  path: 'foo.fooOption3',
103
103
  value: 'value3',
104
104
  type: 'string',
105
- name: 'THE_FOO_OPTION_3'
105
+ name: 'THE_FOO_OPTION_3',
106
106
  },
107
107
  {
108
108
  path: 'foobar',
109
109
  value: '123',
110
- type: 'number'
110
+ type: 'number',
111
111
  },
112
112
  {
113
113
  path: 'boolean.truthy',
114
114
  value: 'true',
115
- type: 'boolean'
115
+ type: 'boolean',
116
116
  },
117
117
  {
118
118
  path: 'boolean.falsey',
119
119
  value: 'false',
120
- type: 'boolean'
121
- }
120
+ type: 'boolean',
121
+ },
122
122
  ]
123
123
  const output = getPackageConfigurationObject(input)
124
124
  assert.deepEqual(output.config, {
@@ -126,17 +126,17 @@ describe('utils', () => {
126
126
  foo: {
127
127
  fooOption1: 'value1',
128
128
  fooOption2: 'value2',
129
- fooOption3: '{THE_FOO_OPTION_3}'
129
+ fooOption3: '{THE_FOO_OPTION_3}',
130
130
  },
131
131
  foobar: 123,
132
132
  boolean: {
133
133
  truthy: true,
134
- falsey: false
135
- }
134
+ falsey: false,
135
+ },
136
136
  })
137
137
 
138
138
  assert.deepEqual(output.env, {
139
- THE_FOO_OPTION_3: 'value3'
139
+ THE_FOO_OPTION_3: 'value3',
140
140
  })
141
141
 
142
142
  // should throw
@@ -145,8 +145,8 @@ describe('utils', () => {
145
145
  {
146
146
  path: 'wrong',
147
147
  type: 'object',
148
- value: {}
149
- }
148
+ value: {},
149
+ },
150
150
  ])
151
151
  assert.fail()
152
152
  } catch (err) {
@@ -172,12 +172,12 @@ describe('utils', () => {
172
172
  credentials: {
173
173
  client: {
174
174
  id: '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_ID}',
175
- secret: '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_SECRET}'
176
- }
175
+ secret: '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_SECRET}',
176
+ },
177
177
  },
178
178
  startRedirectPath: '{PLT_RIVAL_FST_PLUGIN_OAUTH2_REDIRECT_PATH}',
179
- callbackUri: '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CALLBACK_URI}'
180
- }
179
+ callbackUri: '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CALLBACK_URI}',
180
+ },
181
181
  }
182
182
  const expected = {
183
183
  name: '@fastify/oauth2',
@@ -185,7 +185,7 @@ describe('utils', () => {
185
185
  'options.credentials.client.id': '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_ID}',
186
186
  'options.credentials.client.secret': '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_SECRET}',
187
187
  'options.startRedirectPath': '{PLT_RIVAL_FST_PLUGIN_OAUTH2_REDIRECT_PATH}',
188
- 'options.callbackUri': '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CALLBACK_URI}'
188
+ 'options.callbackUri': '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CALLBACK_URI}',
189
189
  }
190
190
  assert.deepEqual(flattenObject(packageObject), expected)
191
191
  })
@@ -193,9 +193,9 @@ describe('utils', () => {
193
193
 
194
194
  describe('getServiceTemplateFromSchemaUrl', () => {
195
195
  test('should get the right template name from schema url', () => {
196
- const composerSchema = 'https://platformatic.dev/schemas/v1.25.0/composer'
197
- const serviceSchema = 'https://platformatic.dev/schemas/v1.25.0/service'
198
- const dbSchema = 'https://platformatic.dev/schemas/v1.25.0/db'
196
+ const composerSchema = 'https://schemas.platformatic.dev/@platformatic/composer/1.52.0.json'
197
+ const serviceSchema = 'https://schemas.platformatic.dev/@platformatic/service/1.52.0.json'
198
+ const dbSchema = 'https://schemas.platformatic.dev/@platformatic/db/1.52.0.json'
199
199
 
200
200
  assert.equal(getServiceTemplateFromSchemaUrl(composerSchema), '@platformatic/composer')
201
201
  assert.equal(getServiceTemplateFromSchemaUrl(serviceSchema), '@platformatic/service')
@@ -209,12 +209,12 @@ describe('utils', () => {
209
209
  '',
210
210
  '# this is a comment that will be not parsed',
211
211
  'MY_VAR=value',
212
- 'PLT_SERVICE_NAME_FOOBAR=foobar'
212
+ 'PLT_SERVICE_NAME_FOOBAR=foobar',
213
213
  ]
214
214
 
215
215
  const expected = {
216
216
  MY_VAR: 'value',
217
- PLT_SERVICE_NAME_FOOBAR: 'foobar'
217
+ PLT_SERVICE_NAME_FOOBAR: 'foobar',
218
218
  }
219
219
 
220
220
  assert.deepEqual(envStringToObject(template.join(EOL)), expected)