create-platformatic 0.11.0
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/.taprc +1 -0
- package/LICENSE +201 -0
- package/NOTICE +13 -0
- package/README.md +13 -0
- package/create-platformatic.mjs +31 -0
- package/help/db.txt +11 -0
- package/help/help.txt +8 -0
- package/help/service.txt +7 -0
- package/package.json +47 -0
- package/src/ask-project-dir.mjs +18 -0
- package/src/colors.mjs +4 -0
- package/src/create-gitignore.mjs +34 -0
- package/src/create-package-json.mjs +32 -0
- package/src/db/README.md +38 -0
- package/src/db/create-db-cli.mjs +174 -0
- package/src/db/create-db.mjs +200 -0
- package/src/get-pkg-manager.mjs +11 -0
- package/src/ghaction.mjs +68 -0
- package/src/index.mjs +51 -0
- package/src/say.mjs +20 -0
- package/src/service/README.md +31 -0
- package/src/service/create-service-cli.mjs +88 -0
- package/src/service/create-service.mjs +94 -0
- package/src/utils.mjs +95 -0
- package/test/create-gitignore.test.mjs +35 -0
- package/test/create-package-json.test.mjs +56 -0
- package/test/db/create-db.test.mjs +241 -0
- package/test/get-pkg-manager.test.mjs +36 -0
- package/test/ghaction.test.mjs +62 -0
- package/test/service/create-service.test.mjs +88 -0
- package/test/utils.test.mjs +182 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { test, beforeEach, afterEach } from 'tap'
|
|
2
|
+
import { tmpdir } from 'os'
|
|
3
|
+
import { isFileAccessible } from '../src/utils.mjs'
|
|
4
|
+
import { createGitignore } from '../src/create-gitignore.mjs'
|
|
5
|
+
import { mkdtempSync, rmSync, writeFileSync } from 'fs'
|
|
6
|
+
import { join } from 'path'
|
|
7
|
+
|
|
8
|
+
let log = ''
|
|
9
|
+
const fakeLogger = {
|
|
10
|
+
debug: msg => { log = msg }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
let tmpDir
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
log = ''
|
|
16
|
+
tmpDir = mkdtempSync(join(tmpdir(), 'test-create-platformatic-'))
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
rmSync(tmpDir, { recursive: true, force: true })
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test('creates gitignore file', async ({ end, equal }) => {
|
|
24
|
+
await createGitignore(fakeLogger, tmpDir)
|
|
25
|
+
equal(log, `Gitignore file ${tmpDir}/.gitignore successfully created.`)
|
|
26
|
+
const accessible = await isFileAccessible(join(tmpDir, '.gitignore'))
|
|
27
|
+
equal(accessible, true)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test('do not create gitignore file because already present', async ({ end, equal }) => {
|
|
31
|
+
const gitignore = join(tmpDir, '.gitignore')
|
|
32
|
+
writeFileSync(gitignore, 'TEST')
|
|
33
|
+
await createGitignore(fakeLogger, tmpDir)
|
|
34
|
+
equal(log, `Gitignore file ${tmpDir}/.gitignore found, skipping creation of gitignore file.`)
|
|
35
|
+
})
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { test, beforeEach, afterEach } from 'tap'
|
|
2
|
+
import { tmpdir } from 'os'
|
|
3
|
+
import { isFileAccessible } from '../src/utils.mjs'
|
|
4
|
+
import { createPackageJson } from '../src/create-package-json.mjs'
|
|
5
|
+
import { mkdtempSync, rmSync, writeFileSync, readFileSync } from 'fs'
|
|
6
|
+
import { join } from 'path'
|
|
7
|
+
|
|
8
|
+
let log = ''
|
|
9
|
+
const fakeLogger = {
|
|
10
|
+
debug: msg => { log = msg }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
let tmpDir
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
log = ''
|
|
16
|
+
tmpDir = mkdtempSync(join(tmpdir(), 'test-create-platformatic-'))
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
rmSync(tmpDir, { recursive: true, force: true })
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test('creates package.json file for db project', async ({ end, equal }) => {
|
|
24
|
+
const version = '1.2.3'
|
|
25
|
+
const fastifyVersion = '4.5.6'
|
|
26
|
+
await createPackageJson('db', version, fastifyVersion, fakeLogger, tmpDir)
|
|
27
|
+
equal(log, `${tmpDir}/package.json successfully created.`)
|
|
28
|
+
const accessible = await isFileAccessible(join(tmpDir, 'package.json'))
|
|
29
|
+
equal(accessible, true)
|
|
30
|
+
const packageJson = JSON.parse(readFileSync(join(tmpDir, 'package.json')))
|
|
31
|
+
equal(packageJson.scripts.start, 'platformatic db start')
|
|
32
|
+
equal(packageJson.dependencies.platformatic, `^${version}`)
|
|
33
|
+
equal(packageJson.devDependencies.fastify, `^${fastifyVersion}`)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('creates package.json file for service project', async ({ end, equal }) => {
|
|
37
|
+
const version = '1.2.3'
|
|
38
|
+
const fastifyVersion = '4.5.6'
|
|
39
|
+
await createPackageJson('service', version, fastifyVersion, fakeLogger, tmpDir)
|
|
40
|
+
equal(log, `${tmpDir}/package.json successfully created.`)
|
|
41
|
+
const accessible = await isFileAccessible(join(tmpDir, 'package.json'))
|
|
42
|
+
equal(accessible, true)
|
|
43
|
+
const packageJson = JSON.parse(readFileSync(join(tmpDir, 'package.json')))
|
|
44
|
+
equal(packageJson.scripts.start, 'platformatic service start')
|
|
45
|
+
equal(packageJson.dependencies.platformatic, `^${version}`)
|
|
46
|
+
equal(packageJson.devDependencies.fastify, `^${fastifyVersion}`)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('do not create package.json file because already present', async ({ end, equal }) => {
|
|
50
|
+
const version = '1.2.3'
|
|
51
|
+
const fastifyVersion = '4.5.6'
|
|
52
|
+
const packagejson = join(tmpDir, 'package.json')
|
|
53
|
+
writeFileSync(packagejson, 'TEST')
|
|
54
|
+
await createPackageJson('db', version, fastifyVersion, fakeLogger, tmpDir)
|
|
55
|
+
equal(log, `${tmpDir}/package.json found, skipping creation of package.json file.`)
|
|
56
|
+
})
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import createDB from '../../src/db/create-db.mjs'
|
|
2
|
+
import { test, beforeEach, afterEach } from 'tap'
|
|
3
|
+
import { isFileAccessible } from '../../src/utils.mjs'
|
|
4
|
+
import { tmpdir } from 'os'
|
|
5
|
+
import { mkdtempSync, rmSync, readFileSync, writeFileSync, mkdirSync } from 'fs'
|
|
6
|
+
import { join } from 'path'
|
|
7
|
+
import dotenv from 'dotenv'
|
|
8
|
+
|
|
9
|
+
const moviesMigrationDo = `
|
|
10
|
+
-- Add SQL in this file to create the database tables for your API
|
|
11
|
+
CREATE TABLE IF NOT EXISTS movies (
|
|
12
|
+
id INTEGER PRIMARY KEY,
|
|
13
|
+
title TEXT NOT NULL
|
|
14
|
+
);
|
|
15
|
+
`
|
|
16
|
+
|
|
17
|
+
const moviesMigrationUndo = `
|
|
18
|
+
-- Add SQL in this file to drop the database tables
|
|
19
|
+
DROP TABLE movies;
|
|
20
|
+
`
|
|
21
|
+
|
|
22
|
+
let tmpDir
|
|
23
|
+
let log = []
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
tmpDir = mkdtempSync(join(tmpdir(), 'test-create-platformatic-'))
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
log = []
|
|
30
|
+
rmSync(tmpDir, { recursive: true, force: true })
|
|
31
|
+
process.env = {}
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
const fakeLogger = {
|
|
35
|
+
debug: msg => log.push(msg),
|
|
36
|
+
info: msg => log.push(msg)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
test('creates project with no typescript', async ({ end, equal }) => {
|
|
40
|
+
const params = {
|
|
41
|
+
hostname: 'myhost',
|
|
42
|
+
port: 6666,
|
|
43
|
+
plugin: true
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
await createDB(params, fakeLogger, tmpDir)
|
|
47
|
+
|
|
48
|
+
const pathToDbConfigFile = join(tmpDir, 'platformatic.db.json')
|
|
49
|
+
const pathToMigrationFolder = join(tmpDir, 'migrations')
|
|
50
|
+
const pathToMigrationFileDo = join(pathToMigrationFolder, '001.do.sql')
|
|
51
|
+
const pathToMigrationFileUndo = join(pathToMigrationFolder, '001.undo.sql')
|
|
52
|
+
|
|
53
|
+
const dbConfigFile = readFileSync(pathToDbConfigFile, 'utf8')
|
|
54
|
+
const dbConfig = JSON.parse(dbConfigFile)
|
|
55
|
+
const { server, core, migrations } = dbConfig
|
|
56
|
+
|
|
57
|
+
equal(server.hostname, '{PLT_SERVER_HOSTNAME}')
|
|
58
|
+
equal(server.port, '{PORT}')
|
|
59
|
+
equal(core.connectionString, '{DATABASE_URL}')
|
|
60
|
+
|
|
61
|
+
const pathToDbEnvFile = join(tmpDir, '.env')
|
|
62
|
+
dotenv.config({ path: pathToDbEnvFile })
|
|
63
|
+
equal(process.env.PLT_SERVER_HOSTNAME, 'myhost')
|
|
64
|
+
equal(process.env.PORT, '6666')
|
|
65
|
+
equal(process.env.DATABASE_URL, 'sqlite://./db.sqlite')
|
|
66
|
+
process.env = {}
|
|
67
|
+
|
|
68
|
+
const pathToDbEnvSampleFile = join(tmpDir, '.env.sample')
|
|
69
|
+
dotenv.config({ path: pathToDbEnvSampleFile })
|
|
70
|
+
equal(process.env.PLT_SERVER_HOSTNAME, 'myhost')
|
|
71
|
+
equal(process.env.PORT, '6666')
|
|
72
|
+
equal(process.env.DATABASE_URL, 'sqlite://./db.sqlite')
|
|
73
|
+
|
|
74
|
+
equal(core.graphql, true)
|
|
75
|
+
equal(core.openapi, true)
|
|
76
|
+
equal(migrations.dir, 'migrations')
|
|
77
|
+
|
|
78
|
+
const migrationFileDo = await readFileSync(pathToMigrationFileDo, 'utf8')
|
|
79
|
+
equal(migrationFileDo, moviesMigrationDo)
|
|
80
|
+
const migrationFileUndo = await readFileSync(pathToMigrationFileUndo, 'utf8')
|
|
81
|
+
equal(migrationFileUndo, moviesMigrationUndo)
|
|
82
|
+
|
|
83
|
+
equal(await isFileAccessible(join(tmpDir, 'plugin.js')), true)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('creates project with no typescript and no plugin', async ({ end, equal }) => {
|
|
87
|
+
const params = {
|
|
88
|
+
hostname: 'myhost',
|
|
89
|
+
port: 6666,
|
|
90
|
+
plugin: false
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
await createDB(params, fakeLogger, tmpDir)
|
|
94
|
+
|
|
95
|
+
const pathToDbConfigFile = join(tmpDir, 'platformatic.db.json')
|
|
96
|
+
const pathToMigrationFolder = join(tmpDir, 'migrations')
|
|
97
|
+
const pathToMigrationFileDo = join(pathToMigrationFolder, '001.do.sql')
|
|
98
|
+
const pathToMigrationFileUndo = join(pathToMigrationFolder, '001.undo.sql')
|
|
99
|
+
|
|
100
|
+
const dbConfigFile = readFileSync(pathToDbConfigFile, 'utf8')
|
|
101
|
+
const dbConfig = JSON.parse(dbConfigFile)
|
|
102
|
+
const { server, core, migrations } = dbConfig
|
|
103
|
+
|
|
104
|
+
equal(server.hostname, '{PLT_SERVER_HOSTNAME}')
|
|
105
|
+
equal(server.port, '{PORT}')
|
|
106
|
+
equal(core.connectionString, '{DATABASE_URL}')
|
|
107
|
+
|
|
108
|
+
const pathToDbEnvFile = join(tmpDir, '.env')
|
|
109
|
+
dotenv.config({ path: pathToDbEnvFile })
|
|
110
|
+
equal(process.env.PLT_SERVER_HOSTNAME, 'myhost')
|
|
111
|
+
equal(process.env.PORT, '6666')
|
|
112
|
+
equal(process.env.DATABASE_URL, 'sqlite://./db.sqlite')
|
|
113
|
+
process.env = {}
|
|
114
|
+
|
|
115
|
+
const pathToDbEnvSampleFile = join(tmpDir, '.env.sample')
|
|
116
|
+
dotenv.config({ path: pathToDbEnvSampleFile })
|
|
117
|
+
equal(process.env.PLT_SERVER_HOSTNAME, 'myhost')
|
|
118
|
+
equal(process.env.PORT, '6666')
|
|
119
|
+
equal(process.env.DATABASE_URL, 'sqlite://./db.sqlite')
|
|
120
|
+
|
|
121
|
+
equal(core.graphql, true)
|
|
122
|
+
equal(core.openapi, true)
|
|
123
|
+
equal(migrations.dir, 'migrations')
|
|
124
|
+
|
|
125
|
+
const migrationFileDo = await readFileSync(pathToMigrationFileDo, 'utf8')
|
|
126
|
+
equal(migrationFileDo, moviesMigrationDo)
|
|
127
|
+
const migrationFileUndo = await readFileSync(pathToMigrationFileUndo, 'utf8')
|
|
128
|
+
equal(migrationFileUndo, moviesMigrationUndo)
|
|
129
|
+
|
|
130
|
+
equal(await isFileAccessible(join(tmpDir, 'plugin.js')), false)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('creates project with typescript', async ({ end, equal }) => {
|
|
134
|
+
const params = {
|
|
135
|
+
hostname: 'myhost',
|
|
136
|
+
port: 6666,
|
|
137
|
+
typescript: true
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
await createDB(params, fakeLogger, tmpDir)
|
|
141
|
+
|
|
142
|
+
const pathToDbConfigFile = join(tmpDir, 'platformatic.db.json')
|
|
143
|
+
const pathToMigrationFolder = join(tmpDir, 'migrations')
|
|
144
|
+
const pathToMigrationFileDo = join(pathToMigrationFolder, '001.do.sql')
|
|
145
|
+
const pathToMigrationFileUndo = join(pathToMigrationFolder, '001.undo.sql')
|
|
146
|
+
|
|
147
|
+
const dbConfigFile = readFileSync(pathToDbConfigFile, 'utf8')
|
|
148
|
+
const dbConfig = JSON.parse(dbConfigFile)
|
|
149
|
+
const { server, core, migrations, plugin } = dbConfig
|
|
150
|
+
|
|
151
|
+
equal(server.hostname, '{PLT_SERVER_HOSTNAME}')
|
|
152
|
+
equal(server.port, '{PORT}')
|
|
153
|
+
equal(core.connectionString, '{DATABASE_URL}')
|
|
154
|
+
|
|
155
|
+
const pathToDbEnvFile = join(tmpDir, '.env')
|
|
156
|
+
dotenv.config({ path: pathToDbEnvFile })
|
|
157
|
+
equal(process.env.PLT_SERVER_HOSTNAME, 'myhost')
|
|
158
|
+
equal(process.env.PORT, '6666')
|
|
159
|
+
equal(process.env.DATABASE_URL, 'sqlite://./db.sqlite')
|
|
160
|
+
process.env = {}
|
|
161
|
+
|
|
162
|
+
const pathToDbEnvSampleFile = join(tmpDir, '.env.sample')
|
|
163
|
+
dotenv.config({ path: pathToDbEnvSampleFile })
|
|
164
|
+
equal(process.env.PLT_SERVER_HOSTNAME, 'myhost')
|
|
165
|
+
equal(process.env.PORT, '6666')
|
|
166
|
+
equal(process.env.DATABASE_URL, 'sqlite://./db.sqlite')
|
|
167
|
+
|
|
168
|
+
equal(core.graphql, true)
|
|
169
|
+
equal(core.openapi, true)
|
|
170
|
+
equal(migrations.dir, 'migrations')
|
|
171
|
+
|
|
172
|
+
const migrationFileDo = await readFileSync(pathToMigrationFileDo, 'utf8')
|
|
173
|
+
equal(migrationFileDo, moviesMigrationDo)
|
|
174
|
+
const migrationFileUndo = await readFileSync(pathToMigrationFileUndo, 'utf8')
|
|
175
|
+
equal(migrationFileUndo, moviesMigrationUndo)
|
|
176
|
+
|
|
177
|
+
equal(plugin.path, 'plugin.ts')
|
|
178
|
+
equal(plugin.typescript.outDir, 'dist')
|
|
179
|
+
equal(await isFileAccessible(join(tmpDir, 'plugin.ts')), true)
|
|
180
|
+
equal(await isFileAccessible(join(tmpDir, 'tsconfig.json')), true)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
test('creates project with configuration already present', async ({ end, equal, ok }) => {
|
|
184
|
+
const pathToDbConfigFileOld = join(tmpDir, 'platformatic.db.json')
|
|
185
|
+
writeFileSync(pathToDbConfigFileOld, JSON.stringify({ test: 'test' }))
|
|
186
|
+
const params = {
|
|
187
|
+
hostname: 'myhost',
|
|
188
|
+
port: 6666
|
|
189
|
+
}
|
|
190
|
+
await createDB(params, fakeLogger, tmpDir)
|
|
191
|
+
ok(log.includes('Configuration file platformatic.db.json found, skipping creation of configuration file.'))
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test('creates project with migration folder already present', async ({ end, equal, ok }) => {
|
|
195
|
+
const pathToMigrationsOld = join(tmpDir, 'migrations')
|
|
196
|
+
mkdirSync(pathToMigrationsOld)
|
|
197
|
+
const params = {
|
|
198
|
+
hostname: 'myhost',
|
|
199
|
+
port: 6666
|
|
200
|
+
}
|
|
201
|
+
await createDB(params, fakeLogger, tmpDir)
|
|
202
|
+
equal(log.includes('Migrations folder migrations found, skipping creation of migrations folder.'), true)
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
test('creates project with "do" migration already present', async ({ end, equal, ok }) => {
|
|
206
|
+
const pathToMigrationsOld = join(tmpDir, 'migrations')
|
|
207
|
+
mkdirSync(pathToMigrationsOld)
|
|
208
|
+
const pathToMigrationFileDo = join(pathToMigrationsOld, '001.do.sql')
|
|
209
|
+
writeFileSync(pathToMigrationFileDo, 'test')
|
|
210
|
+
const params = {
|
|
211
|
+
hostname: 'myhost',
|
|
212
|
+
port: 6666
|
|
213
|
+
}
|
|
214
|
+
await createDB(params, fakeLogger, tmpDir)
|
|
215
|
+
ok(log.includes('Migration file 001.do.sql found, skipping creation of migration file.'))
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
test('creates project with tsconfig already present', async ({ end, equal, ok }) => {
|
|
219
|
+
const pathToTsConfig = join(tmpDir, 'tsconfig.json')
|
|
220
|
+
writeFileSync(pathToTsConfig, 'test')
|
|
221
|
+
const params = {
|
|
222
|
+
hostname: 'myhost',
|
|
223
|
+
port: 6666,
|
|
224
|
+
typescript: true
|
|
225
|
+
}
|
|
226
|
+
await createDB(params, fakeLogger, tmpDir)
|
|
227
|
+
ok(log.includes(`Typescript configuration file ${pathToTsConfig} found, skipping creation of typescript configuration file.`))
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
test('creates project with plugin already present', async ({ end, equal, ok }) => {
|
|
231
|
+
const pathToPlugin = join(tmpDir, 'plugin.js')
|
|
232
|
+
writeFileSync(pathToPlugin, 'test')
|
|
233
|
+
const params = {
|
|
234
|
+
hostname: 'myhost',
|
|
235
|
+
port: 6666,
|
|
236
|
+
plugin: true,
|
|
237
|
+
types: true
|
|
238
|
+
}
|
|
239
|
+
await createDB(params, fakeLogger, tmpDir)
|
|
240
|
+
ok(log.includes(`Plugin file ${pathToPlugin} found, skipping creation of plugin file.`))
|
|
241
|
+
})
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { test, beforeEach } from 'tap'
|
|
2
|
+
import { getPkgManager } from '../src/get-pkg-manager.mjs'
|
|
3
|
+
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
delete process.env.npm_config_user_agent
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
test('detects npm', async ({ end, equal }) => {
|
|
9
|
+
process.env.npm_config_user_agent = 'npm/7.18.1 node/v16.4.2 darwin x64'
|
|
10
|
+
equal(getPkgManager(), 'npm')
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
test('detects yarn', async ({ end, equal }) => {
|
|
14
|
+
process.env.npm_config_user_agent = 'yarn/1.22.10 npm/? node/v16.4.2 darwin x64'
|
|
15
|
+
equal(getPkgManager(), 'yarn')
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
test('detects pnpm', async ({ end, equal }) => {
|
|
19
|
+
process.env.npm_config_user_agent = 'pnpm/6.14.1 npm/? node/v16.4.2 darwin x64'
|
|
20
|
+
equal(getPkgManager(), 'pnpm')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test('detects cnpm', async ({ end, equal }) => {
|
|
24
|
+
process.env.npm_config_user_agent = 'cnpm/7.0.0 npminsall/1.0.0 node/v16.4.2 darwin x64'
|
|
25
|
+
equal(getPkgManager(), 'cnpm')
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('defaults to npm if the user agent is unknown', async ({ end, equal }) => {
|
|
29
|
+
process.env.npm_config_user_agent = 'xxxxxxxxxxxxxxxxxx'
|
|
30
|
+
equal(getPkgManager(), 'npm')
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('defaults to npm if the user agent is not set', async ({ end, equal }) => {
|
|
34
|
+
delete process.env.npm_config_user_agent
|
|
35
|
+
equal(getPkgManager(), 'npm')
|
|
36
|
+
})
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { test, beforeEach, afterEach } from 'tap'
|
|
4
|
+
import { mkdtemp, rmdir, writeFile } from 'fs/promises'
|
|
5
|
+
import mkdirp from 'mkdirp'
|
|
6
|
+
import { join } from 'path'
|
|
7
|
+
import { tmpdir } from 'os'
|
|
8
|
+
import { isFileAccessible } from '../src/utils.mjs'
|
|
9
|
+
import { createGHAction } from '../src/ghaction.mjs'
|
|
10
|
+
|
|
11
|
+
let log = []
|
|
12
|
+
let tmpDir
|
|
13
|
+
const fakeLogger = {
|
|
14
|
+
info: msg => { log.push(msg) },
|
|
15
|
+
warn: msg => { log.push(msg) }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
beforeEach(async () => {
|
|
19
|
+
log = []
|
|
20
|
+
tmpDir = await mkdtemp(join(tmpdir(), 'test-create-platformatic-'))
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
afterEach(async () => {
|
|
24
|
+
await rmdir(tmpDir, { recursive: true, force: true })
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const env = {
|
|
28
|
+
DATABASE_URL: 'mydbconnetionstring',
|
|
29
|
+
PLT_SERVER_LOGGER_LEVEL: 'info'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
test('creates gh action', async ({ end, equal }) => {
|
|
33
|
+
await createGHAction(fakeLogger, env, 'db', tmpDir)
|
|
34
|
+
equal(log[0], 'Github action successfully created, please add PLATFORMATIC_API_KEY as repository secret.')
|
|
35
|
+
const accessible = await isFileAccessible(join(tmpDir, '.github/workflows/platformatic-deploy.yml'))
|
|
36
|
+
equal(accessible, true)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('do not create gitignore file because already present', async ({ end, equal }) => {
|
|
40
|
+
await mkdirp(join(tmpDir, '.github', 'workflows'))
|
|
41
|
+
const ghaction = join(tmpDir, '.github', 'workflows', 'platformatic-deploy.yml')
|
|
42
|
+
await writeFile(ghaction, 'TEST')
|
|
43
|
+
await createGHAction(fakeLogger, env, 'db', tmpDir)
|
|
44
|
+
equal(log[0], `Github action file ${tmpDir}/.github/workflows/platformatic-deploy.yml found, skipping creation of github action file.`)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('creates gh action with a warn if a .git folder is not present', async ({ end, equal }) => {
|
|
48
|
+
await createGHAction(fakeLogger, env, 'db', tmpDir)
|
|
49
|
+
equal(log[0], 'Github action successfully created, please add PLATFORMATIC_API_KEY as repository secret.')
|
|
50
|
+
const accessible = await isFileAccessible(join(tmpDir, '.github/workflows/platformatic-deploy.yml'))
|
|
51
|
+
equal(accessible, true)
|
|
52
|
+
equal(log[1], 'No git repository found. The Github action won\'t be triggered.')
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('creates gh action without a warn if a .git folder is present', async ({ end, equal }) => {
|
|
56
|
+
await mkdirp(join(tmpDir, '.git'))
|
|
57
|
+
await createGHAction(fakeLogger, env, 'db', tmpDir)
|
|
58
|
+
equal(log[0], 'Github action successfully created, please add PLATFORMATIC_API_KEY as repository secret.')
|
|
59
|
+
const accessible = await isFileAccessible(join(tmpDir, '.github/workflows/platformatic-deploy.yml'))
|
|
60
|
+
equal(accessible, true)
|
|
61
|
+
equal(log.length, 1)
|
|
62
|
+
})
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import createService from '../../src/service/create-service.mjs'
|
|
2
|
+
import { isFileAccessible } from '../../src/utils.mjs'
|
|
3
|
+
import { test, beforeEach, afterEach } from 'tap'
|
|
4
|
+
import { tmpdir } from 'os'
|
|
5
|
+
import { mkdtempSync, rmSync, readFileSync, writeFileSync, mkdirSync } from 'fs'
|
|
6
|
+
import { join } from 'path'
|
|
7
|
+
import dotenv from 'dotenv'
|
|
8
|
+
|
|
9
|
+
let tmpDir
|
|
10
|
+
let log = []
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
tmpDir = mkdtempSync(join(tmpdir(), 'test-create-platformatic-'))
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
log = []
|
|
17
|
+
rmSync(tmpDir, { recursive: true, force: true })
|
|
18
|
+
process.env = {}
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
const fakeLogger = {
|
|
22
|
+
debug: msg => log.push(msg),
|
|
23
|
+
info: msg => log.push(msg)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
test('creates service with no typescript', async ({ end, equal, same, ok }) => {
|
|
27
|
+
const params = {
|
|
28
|
+
hostname: 'myhost',
|
|
29
|
+
port: 6666,
|
|
30
|
+
typescript: false
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
await createService(params, fakeLogger, tmpDir)
|
|
34
|
+
|
|
35
|
+
const pathToServiceConfigFile = join(tmpDir, 'platformatic.service.json')
|
|
36
|
+
const serviceConfigFile = readFileSync(pathToServiceConfigFile, 'utf8')
|
|
37
|
+
const serviceConfig = JSON.parse(serviceConfigFile)
|
|
38
|
+
const { server, plugin } = serviceConfig
|
|
39
|
+
|
|
40
|
+
equal(server.hostname, '{PLT_SERVER_HOSTNAME}')
|
|
41
|
+
equal(server.port, '{PORT}')
|
|
42
|
+
|
|
43
|
+
const pathToDbEnvFile = join(tmpDir, '.env')
|
|
44
|
+
dotenv.config({ path: pathToDbEnvFile })
|
|
45
|
+
equal(process.env.PLT_SERVER_HOSTNAME, 'myhost')
|
|
46
|
+
equal(process.env.PORT, '6666')
|
|
47
|
+
process.env = {}
|
|
48
|
+
|
|
49
|
+
const pathToDbEnvSampleFile = join(tmpDir, '.env.sample')
|
|
50
|
+
dotenv.config({ path: pathToDbEnvSampleFile })
|
|
51
|
+
equal(process.env.PLT_SERVER_HOSTNAME, 'myhost')
|
|
52
|
+
equal(process.env.PORT, '6666')
|
|
53
|
+
|
|
54
|
+
same(plugin, ['./plugins', './routes'])
|
|
55
|
+
ok(isFileAccessible(join(tmpDir, 'plugins', 'examples.js')))
|
|
56
|
+
ok(isFileAccessible(join(tmpDir, 'routes', 'root.js')))
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('creates project with configuration already present', async ({ end, equal, ok }) => {
|
|
60
|
+
const pathToServiceConfigFileOld = join(tmpDir, 'platformatic.service.json')
|
|
61
|
+
writeFileSync(pathToServiceConfigFileOld, JSON.stringify({ test: 'test' }))
|
|
62
|
+
const params = {
|
|
63
|
+
hostname: 'myhost',
|
|
64
|
+
port: 6666
|
|
65
|
+
}
|
|
66
|
+
await createService(params, fakeLogger, tmpDir)
|
|
67
|
+
ok(log.includes('Configuration file platformatic.service.json found, skipping creation of configuration file.'))
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
test('creates project with plugins already present', async ({ ok }) => {
|
|
71
|
+
const pathToPlugins = join(tmpDir, 'plugins')
|
|
72
|
+
mkdirSync(pathToPlugins)
|
|
73
|
+
const params = {
|
|
74
|
+
hostname: 'myhost'
|
|
75
|
+
}
|
|
76
|
+
await createService(params, fakeLogger, tmpDir)
|
|
77
|
+
ok(log.includes('Plugins folder "plugins" found, skipping creation of plugins folder.'))
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('creates project with routes already present', async ({ ok }) => {
|
|
81
|
+
const pathToPlugins = join(tmpDir, 'routes')
|
|
82
|
+
mkdirSync(pathToPlugins)
|
|
83
|
+
const params = {
|
|
84
|
+
hostname: 'myhost'
|
|
85
|
+
}
|
|
86
|
+
await createService(params, fakeLogger, tmpDir)
|
|
87
|
+
ok(log.includes('Routes folder "routes" found, skipping creation of routes folder.'))
|
|
88
|
+
})
|