1 | Object.defineProperty(exports, "__esModule", { value: true });
|
2 | exports.getStatusProviderFromConfig = exports.getArtifactProviderFromConfig = exports.getGitTagPrefix = exports.getGlobalGithubConfig = exports.checkMinimalConfigVersion = exports.getConfiguration = exports.validateConfiguration = exports.getProjectConfigSchema = exports.getConfigFileDir = exports.getConfigFilePath = exports.findConfigFile = exports.DEFAULT_RELEASE_BRANCH_NAME = exports.CONFIG_FILE_NAME = void 0;
|
3 | const fs_1 = require("fs");
|
4 | const path_1 = require("path");
|
5 | const Ajv = require("ajv");
|
6 | const js_yaml_1 = require("js-yaml");
|
7 | const logger_1 = require("./logger");
|
8 | const errors_1 = require("./utils/errors");
|
9 | const version_1 = require("./utils/version");
|
10 | const github_1 = require("./artifact_providers/github");
|
11 | const zeus_1 = require("./artifact_providers/zeus");
|
12 | const none_1 = require("./artifact_providers/none");
|
13 | const gcs_1 = require("./artifact_providers/gcs");
|
14 | const zeus_2 = require("./status_providers/zeus");
|
15 | const github_2 = require("./status_providers/github");
|
16 |
|
17 | exports.CONFIG_FILE_NAME = '.craft.yml';
|
18 |
|
19 |
|
20 |
|
21 | exports.DEFAULT_RELEASE_BRANCH_NAME = 'release';
|
22 |
|
23 |
|
24 |
|
25 | let _configPathCache;
|
26 |
|
27 |
|
28 |
|
29 | let _configCache;
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function findConfigFile() {
|
36 | if (_configPathCache) {
|
37 | return _configPathCache;
|
38 | }
|
39 | const cwd = process.cwd();
|
40 | const MAX_DEPTH = 1024;
|
41 | let depth = 0;
|
42 | let currentDir = cwd;
|
43 | while (depth <= MAX_DEPTH) {
|
44 | const probePath = path_1.join(currentDir, exports.CONFIG_FILE_NAME);
|
45 | if (fs_1.existsSync(probePath) && fs_1.lstatSync(probePath).isFile()) {
|
46 | _configPathCache = probePath;
|
47 | return _configPathCache;
|
48 | }
|
49 | const parentDir = path_1.dirname(currentDir);
|
50 | if (currentDir === parentDir) {
|
51 |
|
52 | return undefined;
|
53 | }
|
54 | currentDir = parentDir;
|
55 | depth += 1;
|
56 | }
|
57 | logger_1.logger.warn('findConfigFile: Reached maximum allowed directory depth');
|
58 | return undefined;
|
59 | }
|
60 | exports.findConfigFile = findConfigFile;
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function getConfigFilePath() {
|
67 | const configFilePath = findConfigFile();
|
68 | if (!configFilePath) {
|
69 | throw new errors_1.ConfigurationError(`Cannot find Craft configuration file. Have you added "${exports.CONFIG_FILE_NAME}" to your project?`);
|
70 | }
|
71 | return configFilePath;
|
72 | }
|
73 | exports.getConfigFilePath = getConfigFilePath;
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function getConfigFileDir() {
|
80 | const configFilePath = findConfigFile();
|
81 | if (!configFilePath) {
|
82 | return undefined;
|
83 | }
|
84 | return path_1.dirname(configFilePath);
|
85 | }
|
86 | exports.getConfigFileDir = getConfigFileDir;
|
87 |
|
88 |
|
89 |
|
90 | function getProjectConfigSchema() {
|
91 | return require('./schemas/projectConfig.schema');
|
92 | }
|
93 | exports.getProjectConfigSchema = getProjectConfigSchema;
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function validateConfiguration(rawConfig) {
|
102 | logger_1.logger.debug('Parsing and validating the configuration file...');
|
103 | const schemaName = 'projectConfig';
|
104 | const projectConfigSchema = getProjectConfigSchema();
|
105 | const ajvValidator = new Ajv().addSchema(projectConfigSchema, schemaName);
|
106 | const valid = ajvValidator.validate(schemaName, rawConfig);
|
107 | if (valid) {
|
108 | return rawConfig;
|
109 | }
|
110 | else {
|
111 | throw new errors_1.ConfigurationError(`Cannot parse configuration file:\n${ajvValidator.errorsText()}`);
|
112 | }
|
113 | }
|
114 | exports.validateConfiguration = validateConfiguration;
|
115 |
|
116 |
|
117 |
|
118 | function getConfiguration() {
|
119 | if (_configCache) {
|
120 | return _configCache;
|
121 | }
|
122 | const configPath = getConfigFilePath();
|
123 | logger_1.logger.debug('Configuration file found: ', configPath);
|
124 | const rawConfig = js_yaml_1.safeLoad(fs_1.readFileSync(configPath, 'utf-8'));
|
125 | _configCache = validateConfiguration(rawConfig);
|
126 | return _configCache;
|
127 | }
|
128 | exports.getConfiguration = getConfiguration;
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | function checkMinimalConfigVersion() {
|
136 | const config = getConfiguration();
|
137 | const minVersionRaw = config.minVersion;
|
138 | if (!minVersionRaw) {
|
139 | logger_1.logger.debug('No minimal version specified in the configuration, skpipping the check');
|
140 | return;
|
141 | }
|
142 | const currentVersionRaw = version_1.getPackageVersion();
|
143 | if (!currentVersionRaw) {
|
144 | throw new Error('Cannot get the current craft version');
|
145 | }
|
146 | const minVersion = version_1.parseVersion(minVersionRaw);
|
147 | if (!minVersion) {
|
148 | throw new Error(`Cannot parse the minimal version: "${minVersionRaw}"`);
|
149 | }
|
150 | const currentVersion = version_1.parseVersion(currentVersionRaw);
|
151 | if (!currentVersion) {
|
152 | throw new Error(`Cannot parse the current version: "${currentVersionRaw}"`);
|
153 | }
|
154 | if (version_1.versionGreaterOrEqualThan(currentVersion, minVersion)) {
|
155 | logger_1.logger.info(`"craft" version is compatible with the minimal version from the configuration file.`);
|
156 | }
|
157 | else {
|
158 | throw new errors_1.ConfigurationError(`Incompatible "craft" versions. Current version: ${currentVersionRaw}, minimal version: ${minVersionRaw} (taken from .craft.yml).`);
|
159 | }
|
160 | }
|
161 | exports.checkMinimalConfigVersion = checkMinimalConfigVersion;
|
162 |
|
163 |
|
164 |
|
165 | function getGlobalGithubConfig() {
|
166 |
|
167 |
|
168 | const repoGithubConfig = getConfiguration().github || {};
|
169 | if (!repoGithubConfig) {
|
170 | throw new errors_1.ConfigurationError('GitHub configuration not found in the config file');
|
171 | }
|
172 | if (!repoGithubConfig.owner) {
|
173 | throw new errors_1.ConfigurationError('GitHub target: owner not found');
|
174 | }
|
175 | if (!repoGithubConfig.repo) {
|
176 | throw new errors_1.ConfigurationError('GitHub target: repo not found');
|
177 | }
|
178 | return repoGithubConfig;
|
179 | }
|
180 | exports.getGlobalGithubConfig = getGlobalGithubConfig;
|
181 |
|
182 |
|
183 |
|
184 | function getGitTagPrefix() {
|
185 | const targets = getConfiguration().targets || [];
|
186 | const githubTarget = targets.find(target => target.name === 'github') || {};
|
187 | return githubTarget.tagPrefix || '';
|
188 | }
|
189 | exports.getGitTagPrefix = getGitTagPrefix;
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | function getArtifactProviderFromConfig() {
|
197 | var _a, _b;
|
198 | const projectConfig = getConfiguration();
|
199 | const artifactProviderName = (_a = projectConfig.artifactProvider) === null || _a === void 0 ? void 0 : _a.name;
|
200 | const artifactProviderConfig = Object.assign(Object.assign({}, (_b = projectConfig.artifactProvider) === null || _b === void 0 ? void 0 : _b.config), { repoName: projectConfig.github.repo, repoOwner: projectConfig.github.owner });
|
201 | switch (artifactProviderName) {
|
202 | case undefined:
|
203 | case "zeus" :
|
204 | return new zeus_1.ZeusArtifactProvider(artifactProviderConfig);
|
205 | case "none" :
|
206 | return new none_1.NoneArtifactProvider();
|
207 | case "gcs" :
|
208 | return new gcs_1.GCSArtifactProvider(artifactProviderConfig);
|
209 | case "github" :
|
210 | return new github_1.GithubArtifactProvider(artifactProviderConfig);
|
211 | default: {
|
212 | throw new errors_1.ConfigurationError('Invalid artifact provider');
|
213 | }
|
214 | }
|
215 | }
|
216 | exports.getArtifactProviderFromConfig = getArtifactProviderFromConfig;
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 | function getStatusProviderFromConfig() {
|
223 | const config = getConfiguration() || {};
|
224 | const githubConfig = config.github;
|
225 | const rawStatusProvider = config.statusProvider || {
|
226 | config: undefined,
|
227 | name: undefined,
|
228 | };
|
229 | const { config: statusProviderConfig, name: statusProviderName, } = rawStatusProvider;
|
230 | switch (statusProviderName) {
|
231 | case undefined:
|
232 | case "zeus" :
|
233 | return new zeus_2.ZeusStatusProvider(githubConfig.owner, githubConfig.repo, statusProviderConfig);
|
234 | case "github" :
|
235 | return new github_2.GithubStatusProvider(githubConfig.owner, githubConfig.repo, statusProviderConfig);
|
236 | default: {
|
237 | throw new errors_1.ConfigurationError('Invalid status provider');
|
238 | }
|
239 | }
|
240 | }
|
241 | exports.getStatusProviderFromConfig = getStatusProviderFromConfig;
|
242 |
|
\ | No newline at end of file |