Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/src/gitpod-file-parser.ts
2498 views
1
/**
2
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { injectable } from "inversify";
8
import * as yaml from "js-yaml";
9
import Ajv from "ajv";
10
import { log } from "./util/logging";
11
import { WorkspaceConfig, PortRangeConfig } from "./protocol";
12
13
export type MaybeConfig = WorkspaceConfig | undefined;
14
15
const schema = require("../data/gitpod-schema.json");
16
const validate = new Ajv().compile(schema as object);
17
const defaultParseOptions = {
18
acceptPortRanges: false,
19
};
20
21
export interface ParseResult {
22
config: WorkspaceConfig;
23
parsedConfig?: WorkspaceConfig;
24
validationErrors?: string[];
25
}
26
27
@injectable()
28
export class GitpodFileParser {
29
public parse(content: string, parseOptions = {}, defaultConfig: WorkspaceConfig = {}): ParseResult {
30
const options = {
31
...defaultParseOptions,
32
...parseOptions,
33
};
34
try {
35
const parsedConfig = yaml.safeLoad(content) as any;
36
// eslint-disable-next-line @typescript-eslint/no-floating-promises
37
validate(parsedConfig);
38
const validationErrors = validate.errors ? validate.errors.map((e) => e.message || e.keyword) : undefined;
39
if (validationErrors && validationErrors.length > 0) {
40
return {
41
config: defaultConfig,
42
parsedConfig,
43
validationErrors,
44
};
45
}
46
const overrides = {} as any;
47
if (!options.acceptPortRanges && Array.isArray(parsedConfig.ports)) {
48
overrides.ports = parsedConfig.ports.filter((port: any) => !PortRangeConfig.is(port));
49
}
50
return {
51
config: {
52
...defaultConfig,
53
...parsedConfig,
54
...overrides,
55
},
56
parsedConfig,
57
};
58
} catch (err) {
59
log.error("Unparsable Gitpod configuration", err, { content });
60
return {
61
config: defaultConfig,
62
validationErrors: ["Unparsable Gitpod configuration: " + err.toString()],
63
};
64
}
65
}
66
}
67
68