-
Notifications
You must be signed in to change notification settings - Fork 1
/
compose.ts
64 lines (51 loc) · 1.68 KB
/
compose.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import fs from 'node:fs'
import { join } from 'node:path'
import { projectConfig } from '../config/project.config'
import cmd from './cmd'
import { isFile } from './file-system'
import type { ComposeExecParams, ComposeFilesParams } from './interfaces'
export function composeExec({ command }: ComposeExecParams) {
let composeCommand = ['docker', 'compose']
composeCommand = composeCommand
.concat(composeExecParams())
.concat(composeFiles({ prefix: 'compose' }))
.concat(command)
return cmd.run(composeCommand.join(' '))
}
export function composeFiles({ prefix = 'compose' }: ComposeFilesParams) {
const { filesPath } = projectConfig.load()
const reposPath = projectConfig.reposPaths()
const files = []
for (const repo of reposPath) {
const paths = fs.readdirSync(join(repo, filesPath))
for (const path of paths) {
if (path.match(`^.*${prefix}.*\.yml$`)) {
files.push(`--file ${join(repo, filesPath, path)}`)
}
}
}
return files
}
export function composeExecParams() {
const { projectName, envFile, projectType, monoRepoServices } =
projectConfig.load()
const reposPath = projectConfig.reposPaths()
const params = []
params.push(`--project-name ${projectName}`)
params.push(`--env-file ${envFile}`)
for (const repo of reposPath) {
if (projectType === 'MonoRepo' && monoRepoServices) {
for (const path of projectConfig.repoServicesPaths()) {
if (isFile(join(path, '.env'))) {
params.push(`--env-file ${join(path, '.env')}`)
}
}
} else {
const envFile = join(repo, '.env')
if (isFile(envFile)) {
params.push(`--env-file ${envFile}`)
}
}
}
return params
}