@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
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { test, describe } = require('node:test')
|
|
4
|
-
const assert = require('node:assert')
|
|
5
|
-
const { FileGenerator } = require('../lib/file-generator')
|
|
6
|
-
const { rm, readFile } = require('node:fs/promises')
|
|
7
|
-
const { join } = require('node:path')
|
|
8
|
-
const { tmpdir } = require('node:os')
|
|
9
|
-
const { safeMkdir } = require('../lib/utils')
|
|
10
|
-
|
|
11
|
-
let dirCount = 0
|
|
12
|
-
describe('FileGenerator', () => {
|
|
13
|
-
test('should return null if file is not found', async () => {
|
|
14
|
-
const fg = new FileGenerator()
|
|
15
|
-
const fileObject = fg.getFileObject('sample', 'file')
|
|
16
|
-
assert.strictEqual(null, fileObject)
|
|
17
|
-
})
|
|
18
|
-
test('should throw if no targeDirectory is set', async (t) => {
|
|
19
|
-
const fg = new FileGenerator()
|
|
20
|
-
assert.rejects(async () => {
|
|
21
|
-
await fg.writeFiles()
|
|
22
|
-
})
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
test('should replace a file with same name and path', async (t) => {
|
|
26
|
-
const fg = new FileGenerator()
|
|
27
|
-
fg.addFile({ path: 'path', file: 'file', contents: 'hello world' })
|
|
28
|
-
fg.addFile({ path: 'path', file: 'file', contents: 'foobar' })
|
|
29
|
-
|
|
30
|
-
const fileObject = fg.getFileObject('file', 'path')
|
|
31
|
-
assert.equal(fileObject.contents, 'foobar')
|
|
32
|
-
assert.equal(fg.files.length, 1)
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
test('should list files', async (t) => {
|
|
36
|
-
const fg = new FileGenerator()
|
|
37
|
-
fg.addFile({ path: 'path', file: 'helloworld.txt', contents: 'hello world' })
|
|
38
|
-
fg.addFile({ path: 'path', file: 'foobar.txt', contents: 'foobar' })
|
|
39
|
-
fg.addFile({ path: '/anotherpath', file: 'foobar.txt', contents: 'foobar' })
|
|
40
|
-
|
|
41
|
-
assert.deepEqual(fg.listFiles(), [
|
|
42
|
-
join('path', 'helloworld.txt'),
|
|
43
|
-
join('path', 'foobar.txt'),
|
|
44
|
-
join('anotherpath', 'foobar.txt')
|
|
45
|
-
])
|
|
46
|
-
})
|
|
47
|
-
test('should append file content', async (t) => {
|
|
48
|
-
const fg = new FileGenerator()
|
|
49
|
-
fg.addFile({ path: 'path', file: 'helloworld.txt', contents: 'hello world' })
|
|
50
|
-
|
|
51
|
-
fg.appendfile({ path: '/path', file: 'helloworld.txt', contents: 'Welcome to plaftormatic' })
|
|
52
|
-
|
|
53
|
-
const fileObject = fg.getFileObject('helloworld.txt', 'path')
|
|
54
|
-
assert.equal(fileObject.contents, 'hello world\nWelcome to plaftormatic')
|
|
55
|
-
|
|
56
|
-
// new file
|
|
57
|
-
fg.appendfile({ path: '/path', file: 'foobar.txt', contents: 'foobar' })
|
|
58
|
-
const newFileObject = fg.getFileObject('foobar.txt', 'path')
|
|
59
|
-
assert.equal(newFileObject.contents, 'foobar')
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
test('should reset all files', async (t) => {
|
|
63
|
-
const fg = new FileGenerator()
|
|
64
|
-
fg.addFile({ path: 'path', file: 'file', contents: 'hello world' })
|
|
65
|
-
fg.addFile({ path: 'path', file: 'file', contents: 'foobar' })
|
|
66
|
-
|
|
67
|
-
fg.reset()
|
|
68
|
-
assert.equal(fg.files.length, 0)
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
test('should write files', async (t) => {
|
|
72
|
-
const tempDir = join(tmpdir(), `plt-file-generator-test-${dirCount++}`)
|
|
73
|
-
t.after(async () => {
|
|
74
|
-
await rm(tempDir, { recursive: true })
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
await safeMkdir(tempDir)
|
|
78
|
-
const fg = new FileGenerator()
|
|
79
|
-
fg.setTargetDirectory(tempDir)
|
|
80
|
-
fg.addFile({ path: 'myDir', file: 'helloworld.txt', contents: 'hello world' })
|
|
81
|
-
|
|
82
|
-
await fg.writeFiles()
|
|
83
|
-
const fileContents = await readFile(join(tempDir, 'myDir', 'helloworld.txt'), 'utf8')
|
|
84
|
-
assert.equal(fileContents, 'hello world')
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
test('should not write empty files', async (t) => {
|
|
88
|
-
const tempDir = join(tmpdir(), `plt-file-generator-test-${dirCount++}`)
|
|
89
|
-
t.after(async () => {
|
|
90
|
-
await rm(tempDir, { recursive: true })
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
await safeMkdir(tempDir)
|
|
94
|
-
const fg = new FileGenerator()
|
|
95
|
-
fg.setTargetDirectory(tempDir)
|
|
96
|
-
fg.addFile({ path: 'myDir', file: 'helloworld.txt', contents: '' })
|
|
97
|
-
|
|
98
|
-
await fg.writeFiles()
|
|
99
|
-
try {
|
|
100
|
-
await readFile(join(tempDir, 'myDir', 'helloworld.txt'), 'utf8')
|
|
101
|
-
assert.fail()
|
|
102
|
-
} catch (err) {
|
|
103
|
-
assert.equal(err.code, 'ENOENT')
|
|
104
|
-
}
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
test('should load file from filesystem', async (t) => {
|
|
108
|
-
const tempDir = join(tmpdir(), `plt-file-generator-test-${dirCount++}`)
|
|
109
|
-
t.after(async () => {
|
|
110
|
-
await rm(tempDir, { recursive: true })
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
await safeMkdir(tempDir)
|
|
114
|
-
const fg = new FileGenerator()
|
|
115
|
-
fg.setTargetDirectory(tempDir)
|
|
116
|
-
fg.addFile({ path: 'myDir', file: 'helloworld.txt', contents: 'hello world' })
|
|
117
|
-
await fg.writeFiles()
|
|
118
|
-
|
|
119
|
-
fg.reset()
|
|
120
|
-
const fileObject = await fg.loadFile({
|
|
121
|
-
path: 'myDir',
|
|
122
|
-
file: 'helloworld.txt'
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
assert.deepEqual(fileObject, {
|
|
126
|
-
path: 'myDir',
|
|
127
|
-
file: 'helloworld.txt',
|
|
128
|
-
contents: 'hello world',
|
|
129
|
-
options: {}
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
assert.equal(fg.files.length, 1)
|
|
133
|
-
assert.deepEqual(fg.files[0], {
|
|
134
|
-
path: 'myDir',
|
|
135
|
-
file: 'helloworld.txt',
|
|
136
|
-
contents: 'hello world',
|
|
137
|
-
options: {}
|
|
138
|
-
})
|
|
139
|
-
})
|
|
140
|
-
})
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
PLT_SERVER_HOSTNAME=0.0.0.0
|
|
2
|
-
PORT=3042
|
|
3
|
-
PLT_SERVER_LOGGER_LEVEL=info
|
|
4
|
-
PLT_RIVAL_TYPESCRIPT=true
|
|
5
|
-
PLT_RIVAL_FST_PLUGIN_OAUTH2_NAME=googleOAuth2
|
|
6
|
-
PLT_RIVAL_FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_ID=sample_client_id
|
|
7
|
-
PLT_RIVAL_FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_SECRET=sample_client_secret
|
|
8
|
-
PLT_RIVAL_FST_PLUGIN_OAUTH2_REDIRECT_PATH=/login/google
|
|
9
|
-
PLT_RIVAL_FST_PLUGIN_OAUTH2_CALLBACK_URI=http://localhost:3000/login/google/callback
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://platformatic.dev/schemas/v1.25.0/runtime",
|
|
3
|
-
"entrypoint": "rival",
|
|
4
|
-
"hotReload": true,
|
|
5
|
-
"autoload": {
|
|
6
|
-
"path": "services",
|
|
7
|
-
"exclude": [
|
|
8
|
-
"docs"
|
|
9
|
-
]
|
|
10
|
-
},
|
|
11
|
-
"server": {
|
|
12
|
-
"hostname": "{PLT_SERVER_HOSTNAME}",
|
|
13
|
-
"port": "{PORT}",
|
|
14
|
-
"logger": {
|
|
15
|
-
"level": "{PLT_SERVER_LOGGER_LEVEL}"
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://platformatic.dev/schemas/v1.25.0/service",
|
|
3
|
-
"service": {
|
|
4
|
-
"openapi": true
|
|
5
|
-
},
|
|
6
|
-
"watch": true,
|
|
7
|
-
"plugins": {
|
|
8
|
-
"paths": [
|
|
9
|
-
{
|
|
10
|
-
"path": "./plugins",
|
|
11
|
-
"encapsulate": false
|
|
12
|
-
},
|
|
13
|
-
"./routes"
|
|
14
|
-
],
|
|
15
|
-
"typescript": "{PLT_RIVAL_TYPESCRIPT}",
|
|
16
|
-
"packages": [
|
|
17
|
-
{
|
|
18
|
-
"name": "@fastify/oauth2",
|
|
19
|
-
"options": {
|
|
20
|
-
"name": "{PLT_RIVAL_FST_PLUGIN_OAUTH2_NAME}",
|
|
21
|
-
"credentials": {
|
|
22
|
-
"client": {
|
|
23
|
-
"id": "{PLT_RIVAL_FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_ID}",
|
|
24
|
-
"secret": "{PLT_RIVAL_FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_SECRET}"
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
"startRedirectPath": "{PLT_RIVAL_FST_PLUGIN_OAUTH2_REDIRECT_PATH}",
|
|
28
|
-
"callbackUri": "{PLT_RIVAL_FST_PLUGIN_OAUTH2_CALLBACK_URI}"
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
]
|
|
32
|
-
}
|
|
33
|
-
}
|
package/test/helpers.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { join } = require('node:path')
|
|
4
|
-
const fs = require('node:fs/promises')
|
|
5
|
-
const { safeMkdir } = require('../lib/utils')
|
|
6
|
-
const { MockAgent, setGlobalDispatcher } = require('undici')
|
|
7
|
-
|
|
8
|
-
const mockAgent = new MockAgent()
|
|
9
|
-
setGlobalDispatcher(mockAgent)
|
|
10
|
-
mockAgent.disableNetConnect()
|
|
11
|
-
|
|
12
|
-
let counter = 0
|
|
13
|
-
|
|
14
|
-
async function getTempDir (baseDir) {
|
|
15
|
-
if (baseDir === undefined) {
|
|
16
|
-
baseDir = __dirname
|
|
17
|
-
}
|
|
18
|
-
const dir = join(baseDir, 'tmp', `platformatic-generators-${process.pid}-${Date.now()}-${counter++}`)
|
|
19
|
-
await safeMkdir(dir)
|
|
20
|
-
return dir
|
|
21
|
-
}
|
|
22
|
-
async function moveToTmpdir (teardown) {
|
|
23
|
-
const cwd = process.cwd()
|
|
24
|
-
// const tmp = join(__dirname, 'tmp')
|
|
25
|
-
// try {
|
|
26
|
-
// await fs.mkdir(tmp)
|
|
27
|
-
// } catch {
|
|
28
|
-
// }
|
|
29
|
-
const dir = await getTempDir()
|
|
30
|
-
process.chdir(dir)
|
|
31
|
-
teardown(() => process.chdir(cwd))
|
|
32
|
-
if (!process.env.SKIP_RM_TMP) {
|
|
33
|
-
teardown(() => fs.rm(dir, { recursive: true }).catch(() => {}))
|
|
34
|
-
}
|
|
35
|
-
return dir
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function mockNpmJsRequestForPkgs (pkgs) {
|
|
39
|
-
for (const pkg of pkgs) {
|
|
40
|
-
mockAgent
|
|
41
|
-
.get('https://registry.npmjs.org')
|
|
42
|
-
.intercept({
|
|
43
|
-
method: 'GET',
|
|
44
|
-
path: `/${pkg}`
|
|
45
|
-
})
|
|
46
|
-
.reply(200, {
|
|
47
|
-
'dist-tags': {
|
|
48
|
-
latest: '1.42.0'
|
|
49
|
-
}
|
|
50
|
-
})
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
module.exports = {
|
|
54
|
-
fakeLogger: {
|
|
55
|
-
info: () => {},
|
|
56
|
-
debug: () => {},
|
|
57
|
-
warn: () => {},
|
|
58
|
-
error: () => {}
|
|
59
|
-
},
|
|
60
|
-
getTempDir,
|
|
61
|
-
moveToTmpdir,
|
|
62
|
-
mockNpmJsRequestForPkgs,
|
|
63
|
-
mockAgent
|
|
64
|
-
}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { readFile, rm } = require('node:fs/promises')
|
|
4
|
-
const { test, afterEach } = require('node:test')
|
|
5
|
-
const assert = require('node:assert')
|
|
6
|
-
const { join } = require('node:path')
|
|
7
|
-
|
|
8
|
-
const { fakeLogger, getTempDir } = require('./helpers')
|
|
9
|
-
const { StackableGenerator } = require('../lib/stackable-generator')
|
|
10
|
-
|
|
11
|
-
afterEach(async () => {
|
|
12
|
-
try {
|
|
13
|
-
await rm(join(__dirname, 'tmp'), { recursive: true })
|
|
14
|
-
} catch (err) {
|
|
15
|
-
// do nothing
|
|
16
|
-
}
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
test('should create a stackable project without typescript', async (t) => {
|
|
20
|
-
const dir = await getTempDir()
|
|
21
|
-
const gen = new StackableGenerator({
|
|
22
|
-
logger: fakeLogger
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
gen.setConfig({
|
|
26
|
-
targetDirectory: dir
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
await gen.run()
|
|
30
|
-
// check files are created
|
|
31
|
-
const packageJson = JSON.parse(await readFile(join(dir, 'package.json'), 'utf8'))
|
|
32
|
-
assert.ok(packageJson.scripts)
|
|
33
|
-
assert.ok(packageJson.dependencies)
|
|
34
|
-
assert.ok(packageJson.engines)
|
|
35
|
-
|
|
36
|
-
const indexFile = await readFile(join(dir, 'index.js'), 'utf8')
|
|
37
|
-
assert.ok(indexFile.length > 0)
|
|
38
|
-
|
|
39
|
-
const indexTypesFile = await readFile(join(dir, 'index.d.ts'), 'utf8')
|
|
40
|
-
assert.ok(indexTypesFile.length > 0)
|
|
41
|
-
|
|
42
|
-
const schemaFile = await readFile(join(dir, 'lib', 'schema.js'), 'utf8')
|
|
43
|
-
assert.ok(schemaFile.length > 0)
|
|
44
|
-
|
|
45
|
-
const generatorFile = await readFile(join(dir, 'lib', 'generator.js'), 'utf8')
|
|
46
|
-
assert.ok(generatorFile.length > 0)
|
|
47
|
-
|
|
48
|
-
const startCommandFile = await readFile(join(dir, 'cli', 'start.js'), 'utf8')
|
|
49
|
-
assert.ok(startCommandFile.length > 0)
|
|
50
|
-
|
|
51
|
-
const createCommandFile = await readFile(join(dir, 'cli', 'create.js'), 'utf8')
|
|
52
|
-
assert.ok(createCommandFile.length > 0)
|
|
53
|
-
|
|
54
|
-
const gitignore = await readFile(join(dir, '.gitignore'), 'utf8')
|
|
55
|
-
assert.ok(gitignore.length > 0)
|
|
56
|
-
|
|
57
|
-
const testIndexFile = await readFile(join(dir, 'test', 'index.test.js'), 'utf8')
|
|
58
|
-
assert.ok(testIndexFile.length > 0)
|
|
59
|
-
|
|
60
|
-
const testSchemaFile = await readFile(join(dir, 'test', 'schema.test.js'), 'utf8')
|
|
61
|
-
assert.ok(testSchemaFile.length > 0)
|
|
62
|
-
|
|
63
|
-
const testGeneratorFile = await readFile(join(dir, 'test', 'generator.test.js'), 'utf8')
|
|
64
|
-
assert.ok(testGeneratorFile.length > 0)
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
test('should create a stackable project with typescript', async (t) => {
|
|
68
|
-
const dir = await getTempDir()
|
|
69
|
-
const gen = new StackableGenerator({
|
|
70
|
-
logger: fakeLogger
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
gen.setConfig({
|
|
74
|
-
targetDirectory: dir,
|
|
75
|
-
typescript: true
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
await gen.run()
|
|
79
|
-
// check files are created
|
|
80
|
-
const packageJson = JSON.parse(await readFile(join(dir, 'package.json'), 'utf8'))
|
|
81
|
-
assert.ok(packageJson.scripts)
|
|
82
|
-
assert.ok(packageJson.dependencies)
|
|
83
|
-
assert.ok(packageJson.engines)
|
|
84
|
-
|
|
85
|
-
const indexFile = await readFile(join(dir, 'index.ts'), 'utf8')
|
|
86
|
-
assert.ok(indexFile.length > 0)
|
|
87
|
-
|
|
88
|
-
const indexTypesFile = await readFile(join(dir, 'index.d.ts'), 'utf8')
|
|
89
|
-
assert.ok(indexTypesFile.length > 0)
|
|
90
|
-
|
|
91
|
-
const schemaFile = await readFile(join(dir, 'lib', 'schema.ts'), 'utf8')
|
|
92
|
-
assert.ok(schemaFile.length > 0)
|
|
93
|
-
|
|
94
|
-
const generatorFile = await readFile(join(dir, 'lib', 'generator.ts'), 'utf8')
|
|
95
|
-
assert.ok(generatorFile.length > 0)
|
|
96
|
-
|
|
97
|
-
const startCommandFile = await readFile(join(dir, 'cli', 'start.ts'), 'utf8')
|
|
98
|
-
assert.ok(startCommandFile.length > 0)
|
|
99
|
-
|
|
100
|
-
const createCommandFile = await readFile(join(dir, 'cli', 'create.ts'), 'utf8')
|
|
101
|
-
assert.ok(createCommandFile.length > 0)
|
|
102
|
-
|
|
103
|
-
const gitignore = await readFile(join(dir, '.gitignore'), 'utf8')
|
|
104
|
-
assert.ok(gitignore.length > 0)
|
|
105
|
-
|
|
106
|
-
const testIndexFile = await readFile(join(dir, 'test', 'index.test.ts'), 'utf8')
|
|
107
|
-
assert.ok(testIndexFile.length > 0)
|
|
108
|
-
|
|
109
|
-
const testSchemaFile = await readFile(join(dir, 'test', 'schema.test.ts'), 'utf8')
|
|
110
|
-
assert.ok(testSchemaFile.length > 0)
|
|
111
|
-
|
|
112
|
-
const testGeneratorFile = await readFile(join(dir, 'test', 'generator.test.ts'), 'utf8')
|
|
113
|
-
assert.ok(testGeneratorFile.length > 0)
|
|
114
|
-
})
|
package/test/utils.test.js
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { test, describe } = require('node:test')
|
|
4
|
-
const { EOL } = require('node:os')
|
|
5
|
-
const assert = require('node:assert')
|
|
6
|
-
const {
|
|
7
|
-
stripVersion,
|
|
8
|
-
convertServiceNameToPrefix,
|
|
9
|
-
envObjectToString,
|
|
10
|
-
extractEnvVariablesFromText,
|
|
11
|
-
getPackageConfigurationObject,
|
|
12
|
-
addPrefixToString
|
|
13
|
-
} = require('../lib/utils')
|
|
14
|
-
const { flattenObject } = require('../lib/utils')
|
|
15
|
-
const { getServiceTemplateFromSchemaUrl } = require('../lib/utils')
|
|
16
|
-
const { envStringToObject } = require('../lib/utils')
|
|
17
|
-
|
|
18
|
-
describe('utils', () => {
|
|
19
|
-
describe('stripVersion', async () => {
|
|
20
|
-
test('should return the same string if not semver', async (t) => {
|
|
21
|
-
assert.equal('no-version', stripVersion('no-version'))
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
test('should match semver', async (t) => {
|
|
25
|
-
assert.equal('1.2.3', stripVersion('v1.2.3'))
|
|
26
|
-
assert.equal('1.2.3', stripVersion('~1.2.3'))
|
|
27
|
-
assert.equal('1.2.3', stripVersion('^1.2.3'))
|
|
28
|
-
})
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
describe('convertServiceNameToPrefix', () => {
|
|
32
|
-
test('should convert service name to env prefix', async (t) => {
|
|
33
|
-
const expectations = {
|
|
34
|
-
'my-service': 'MY_SERVICE',
|
|
35
|
-
a: 'A',
|
|
36
|
-
MY_SERVICE: 'MY_SERVICE',
|
|
37
|
-
asderas123: 'ASDERAS123'
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
Object.entries(expectations).forEach((exp) => {
|
|
41
|
-
const converted = convertServiceNameToPrefix(exp[0])
|
|
42
|
-
assert.equal(exp[1], converted)
|
|
43
|
-
})
|
|
44
|
-
})
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
describe('envObjectToString', () => {
|
|
48
|
-
test('should convert env object to string', async () => {
|
|
49
|
-
const env = {
|
|
50
|
-
FOO: 'bar',
|
|
51
|
-
DATABASE_URL: 'sqlite://./db.sqlite'
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
assert.equal(envObjectToString(env), `FOO=bar${EOL}DATABASE_URL=sqlite://./db.sqlite`)
|
|
55
|
-
})
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
describe('extractEnvVariablesFromText', () => {
|
|
59
|
-
test('should extract env vars from text', async () => {
|
|
60
|
-
const text = `
|
|
61
|
-
This is a sample text where an {ENV_VAR} should be detected
|
|
62
|
-
|
|
63
|
-
DATABASE_URL={DATABASE_URL}
|
|
64
|
-
`
|
|
65
|
-
const env = extractEnvVariablesFromText(text)
|
|
66
|
-
assert.deepEqual(env, ['ENV_VAR', 'DATABASE_URL'])
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test('should not extract {} as empty env var', async () => {
|
|
70
|
-
const text = `This is a sample text where an {ENV_VAR} should be detected
|
|
71
|
-
but this {} should not be parsed
|
|
72
|
-
`
|
|
73
|
-
const env = extractEnvVariablesFromText(text)
|
|
74
|
-
assert.deepEqual(env, ['ENV_VAR'])
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
test('should return empty array if no env vars are detected', async () => {
|
|
78
|
-
const text = 'This is a sample text without any env var. This {} should not be parsed'
|
|
79
|
-
const env = extractEnvVariablesFromText(text)
|
|
80
|
-
assert.deepEqual(env, [])
|
|
81
|
-
})
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
describe('getPackageConfigurationObject', async () => {
|
|
85
|
-
const input = [
|
|
86
|
-
{
|
|
87
|
-
path: 'prefix',
|
|
88
|
-
value: '/foo',
|
|
89
|
-
type: 'string'
|
|
90
|
-
},
|
|
91
|
-
{
|
|
92
|
-
path: 'foo.fooOption1',
|
|
93
|
-
value: 'value1',
|
|
94
|
-
type: 'string'
|
|
95
|
-
},
|
|
96
|
-
{
|
|
97
|
-
path: 'foo.fooOption2',
|
|
98
|
-
value: 'value2',
|
|
99
|
-
type: 'string'
|
|
100
|
-
},
|
|
101
|
-
{
|
|
102
|
-
path: 'foo.fooOption3',
|
|
103
|
-
value: 'value3',
|
|
104
|
-
type: 'string',
|
|
105
|
-
name: 'THE_FOO_OPTION_3'
|
|
106
|
-
},
|
|
107
|
-
{
|
|
108
|
-
path: 'foobar',
|
|
109
|
-
value: '123',
|
|
110
|
-
type: 'number'
|
|
111
|
-
},
|
|
112
|
-
{
|
|
113
|
-
path: 'boolean.truthy',
|
|
114
|
-
value: 'true',
|
|
115
|
-
type: 'boolean'
|
|
116
|
-
},
|
|
117
|
-
{
|
|
118
|
-
path: 'boolean.falsey',
|
|
119
|
-
value: 'false',
|
|
120
|
-
type: 'boolean'
|
|
121
|
-
}
|
|
122
|
-
]
|
|
123
|
-
const output = getPackageConfigurationObject(input)
|
|
124
|
-
assert.deepEqual(output.config, {
|
|
125
|
-
prefix: '/foo',
|
|
126
|
-
foo: {
|
|
127
|
-
fooOption1: 'value1',
|
|
128
|
-
fooOption2: 'value2',
|
|
129
|
-
fooOption3: '{THE_FOO_OPTION_3}'
|
|
130
|
-
},
|
|
131
|
-
foobar: 123,
|
|
132
|
-
boolean: {
|
|
133
|
-
truthy: true,
|
|
134
|
-
falsey: false
|
|
135
|
-
}
|
|
136
|
-
})
|
|
137
|
-
|
|
138
|
-
assert.deepEqual(output.env, {
|
|
139
|
-
THE_FOO_OPTION_3: 'value3'
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
// should throw
|
|
143
|
-
try {
|
|
144
|
-
getPackageConfigurationObject([
|
|
145
|
-
{
|
|
146
|
-
path: 'wrong',
|
|
147
|
-
type: 'object',
|
|
148
|
-
value: {}
|
|
149
|
-
}
|
|
150
|
-
])
|
|
151
|
-
assert.fail()
|
|
152
|
-
} catch (err) {
|
|
153
|
-
assert.equal(err.code, 'PLT_GEN_WRONG_TYPE')
|
|
154
|
-
assert.equal(err.message, "Invalid value type. Accepted values are 'string', 'number' and 'boolean', found 'object'.")
|
|
155
|
-
}
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
describe('addPrefixToString', () => {
|
|
159
|
-
test('should add prefix to string', async () => {
|
|
160
|
-
assert.equal(addPrefixToString('PLT_SERVICE_FOO', 'SERVICE'), 'PLT_SERVICE_FOO')
|
|
161
|
-
assert.equal(addPrefixToString('FOO', 'SERVICE'), 'PLT_SERVICE_FOO')
|
|
162
|
-
assert.equal(addPrefixToString('FOO', ''), 'FOO')
|
|
163
|
-
})
|
|
164
|
-
})
|
|
165
|
-
|
|
166
|
-
describe('flattenObject', () => {
|
|
167
|
-
test('should return a single depth object', () => {
|
|
168
|
-
const packageObject = {
|
|
169
|
-
name: '@fastify/oauth2',
|
|
170
|
-
options: {
|
|
171
|
-
name: '{PLT_RIVAL_FST_PLUGIN_OAUTH2_NAME}',
|
|
172
|
-
credentials: {
|
|
173
|
-
client: {
|
|
174
|
-
id: '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_ID}',
|
|
175
|
-
secret: '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_SECRET}'
|
|
176
|
-
}
|
|
177
|
-
},
|
|
178
|
-
startRedirectPath: '{PLT_RIVAL_FST_PLUGIN_OAUTH2_REDIRECT_PATH}',
|
|
179
|
-
callbackUri: '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CALLBACK_URI}'
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
const expected = {
|
|
183
|
-
name: '@fastify/oauth2',
|
|
184
|
-
'options.name': '{PLT_RIVAL_FST_PLUGIN_OAUTH2_NAME}',
|
|
185
|
-
'options.credentials.client.id': '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_ID}',
|
|
186
|
-
'options.credentials.client.secret': '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_SECRET}',
|
|
187
|
-
'options.startRedirectPath': '{PLT_RIVAL_FST_PLUGIN_OAUTH2_REDIRECT_PATH}',
|
|
188
|
-
'options.callbackUri': '{PLT_RIVAL_FST_PLUGIN_OAUTH2_CALLBACK_URI}'
|
|
189
|
-
}
|
|
190
|
-
assert.deepEqual(flattenObject(packageObject), expected)
|
|
191
|
-
})
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
describe('getServiceTemplateFromSchemaUrl', () => {
|
|
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'
|
|
199
|
-
|
|
200
|
-
assert.equal(getServiceTemplateFromSchemaUrl(composerSchema), '@platformatic/composer')
|
|
201
|
-
assert.equal(getServiceTemplateFromSchemaUrl(serviceSchema), '@platformatic/service')
|
|
202
|
-
assert.equal(getServiceTemplateFromSchemaUrl(dbSchema), '@platformatic/db')
|
|
203
|
-
})
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
describe('envStringToObject', () => {
|
|
207
|
-
test('should convert .env-like string to object', () => {
|
|
208
|
-
const template = [
|
|
209
|
-
'',
|
|
210
|
-
'# this is a comment that will be not parsed',
|
|
211
|
-
'MY_VAR=value',
|
|
212
|
-
'PLT_SERVICE_NAME_FOOBAR=foobar'
|
|
213
|
-
]
|
|
214
|
-
|
|
215
|
-
const expected = {
|
|
216
|
-
MY_VAR: 'value',
|
|
217
|
-
PLT_SERVICE_NAME_FOOBAR: 'foobar'
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
assert.deepEqual(envStringToObject(template.join(EOL)), expected)
|
|
221
|
-
})
|
|
222
|
-
})
|
|
223
|
-
})
|