-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject.config.ts
208 lines (176 loc) · 6.44 KB
/
project.config.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import fs from 'node:fs'
import { join } from 'node:path'
import {
createFile,
createPath,
deletePath,
isDirectory,
isFile,
} from '../utils/file-system'
import logger from '../utils/log'
import { CONFIG_PATH, DEMS_REPOS_PATH, cliConfig } from './cli.config'
const CURRENT_PROJECT = cliConfig.activeProject()
const PROJECT_CONFIG_ROOT = join(CONFIG_PATH, CURRENT_PROJECT)
const PROJECT_CONFIG_FILE = join(PROJECT_CONFIG_ROOT, 'config.json')
const PROJECT_ENV_FILE = join(PROJECT_CONFIG_ROOT, '.env')
const DEMS_FILES_PATH = '.ops'
// Single: A single repository containing a single application or service.
// MonoRepo: A single repository containing N applications.
// MultiRepo: Multiple repositories containing a single application or service.
export type ProjectTypes = 'Single' | 'MonoRepo' | 'MultiRepo'
export interface ProjectConfigSpec {
// The project name determines how docker compose services will be prefixed,
// thus maintaining compslete isolation with other services deployed locally.
// It also servers as a unique reference across DEMS tasks.
projectName: string
// The project root path is where DEMS will store project-scoped confguration
// files. The project root path is by default relative to the DEMS CLI root
// config path (~/.dems).
projectRootPath: string
// The project config file (config.json). Relative to project root path.
configFile: string
// The application configuration needed by DEMS is expected to be inside each
// application repository. The DEMS file path is the relative path of each
// application repository where DEMS-files will be stored.
filesPath: string
// All docker compose commands run by DEMS will have the --env-file [path].
// The env file will be appended to docker compose commands.
envFile: string
// The Dockerfile name used by DEMS for the project. The location of the
// Dockerfile is expected by DEMS in the application's repository DEMS
// files directory.
dockerfile: string
// The repositories object is a refernce to the application repositories,
// their names (key) and path (value). This is required for DEMS to be aware
// of the desired state of applications to be managed.
repositories: { [key: string]: string }
// Defines the project type that identifies the project. This will change the
// behavior of DEMS commands and helper, so it's important that it's set to the
// right type.
projectType: ProjectTypes
// Individual projects inside a MonoRepo project type. For example,
// if a mono repo has two projects, backend & frontend, then, the
// monoRepoServices will be like (relative to the path of the main repo):
// monoRepoServices: {
// backend: "path/to/backend",
// frontend: "path/to/frontend"
// }
// 'projectType' must be set to 'MonoRepo' for this directive to work.
monoRepoServices?: Array<string>
// The `git` object contains all configuration values for Git resources.
git: {
// The git organization where all repos referenced by DEMS are found.
org: string
// The default git reference (branch, tag, commit) used to clone the repos.
defaultRef: string
}
}
export const projectConfig = {
default(): ProjectConfigSpec {
return {
projectName: 'demo',
projectRootPath: PROJECT_CONFIG_ROOT,
configFile: PROJECT_CONFIG_FILE,
filesPath: DEMS_FILES_PATH,
projectType: 'Single',
monoRepoServices: [''],
dockerfile: 'dems.Dockerfile',
envFile: PROJECT_ENV_FILE,
repositories: {
'demo-api': join(DEMS_REPOS_PATH, 'demo', 'demo-api'),
'demo-webapp': join(DEMS_REPOS_PATH, 'demo', 'demo-web'),
},
git: {
org: 'gbh-tech',
defaultRef: 'main',
},
}
},
load(): ProjectConfigSpec {
if (isFile(PROJECT_CONFIG_FILE)) {
const data = fs.readFileSync(PROJECT_CONFIG_FILE, 'utf-8')
return JSON.parse(data) as ProjectConfigSpec
}
logger.error(
`Project config file does not exist or could not be found at ${PROJECT_CONFIG_FILE}.`,
)
logger.error('Did you run `dems setup`?')
process.exit(1)
},
get(configFile = PROJECT_CONFIG_FILE): ProjectConfigSpec {
this.check(PROJECT_CONFIG_FILE)
return JSON.parse(fs.readFileSync(configFile).toString())
},
save(config: ProjectConfigSpec) {
createPath({ path: config.projectRootPath })
createFile({
file: `${config.projectRootPath}/config.json`,
content: JSON.stringify(config, null, 2),
overwrite: true,
})
},
remove(project: string) {
const projectPath = join(CONFIG_PATH, project)
if (isDirectory(projectPath)) {
deletePath({ path: projectPath, force: true })
logger.info(`Project '${project}' was successfully deleted.`)
return
}
logger.warn(`Config path for '${project}' is not a valid directory.`)
logger.warn(`Project '${project}' is most likely not initialized.`)
},
check(configFile = PROJECT_CONFIG_FILE) {
if (isFile(configFile)) return
logger.warn('Project config file does not exist or is not a valid file.')
logger.warn("Run 'dems setup' to create it.")
process.exit(1)
},
confirmExists(configFile = PROJECT_CONFIG_FILE) {
if (isFile(configFile)) {
logger.warn('Project has been already initialized.')
return true
}
return false
},
repoList() {
return Object.keys(this.load().repositories)
},
repoURLs() {
return Object.values(this.load().repositories)
},
reposPaths() {
const paths = []
const { repositories } = this.load()
const { reposPath } = cliConfig.load()
for (const repo in repositories) {
paths.push(join(reposPath, repo))
}
return paths
},
repoServicesList() {
const { projectType, monoRepoServices } = this.load()
if (projectType === 'MonoRepo') {
return monoRepoServices
}
logger.error('Curent project is not a mono repository.')
process.exit(1)
},
repoServicesPaths() {
const paths = []
const { repositories, projectType } = this.load()
const { reposPath } = cliConfig.load()
if (projectType === 'MonoRepo') {
const { monoRepoServices } = this.load()
if (monoRepoServices) {
for (const service of monoRepoServices) {
paths.push(join(reposPath, Object.keys(repositories)[0], service))
}
}
} else {
logger.error('Cannot determine services paths.')
logger.error('Current project is not of type MonoRepo.')
process.exit(1)
}
return paths
},
}