UNPKG

6.79 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const path_1 = require("@stoplight/path");
5const types_1 = require("@stoplight/types");
6const eol = require("eol");
7const lodash_1 = require("lodash");
8const filesystem_1 = require("../../backends/filesystem");
9const graph_1 = require("../../graph");
10const utils_1 = require("../../graph/utils");
11const notifier_1 = require("../../notifier");
12const scheduler_1 = require("../../scheduler");
13const taskHandler_1 = require("../../scheduler/taskHandler");
14const pathsMatcher_1 = require("../../utils/pathsMatcher");
15const abstract_lifecycle_1 = require("../abstract-lifecycle");
16const consts_1 = require("./consts");
17const deserialize_1 = require("./handlers/deserialize");
18const serialize_1 = require("./handlers/serialize");
19const parse_1 = require("./parse");
20const types_2 = require("./types");
21const addNode_1 = require("./utils/addNode");
22const createFilter_1 = require("./utils/createFilter");
23const groupReadableByFormat_1 = require("./utils/groupReadableByFormat");
24const mergeConfig_1 = require("./utils/mergeConfig");
25exports.mergeConfig = mergeConfig_1.mergeConfig;
26const toGraphitePaths_1 = require("./utils/toGraphitePaths");
27const validator_1 = require("./validator");
28var consts_2 = require("./consts");
29exports.DEFAULT_CONFIG = consts_2.DEFAULT_CONFIG;
30exports.DEFAULT_PATHS = consts_2.DEFAULT_PATHS;
31exports.STOPLIGHT_CONFIG_PATH = consts_2.STOPLIGHT_CONFIG_PATH;
32exports.STOPLIGHT_CONFIG_PLUGIN_ID = consts_2.STOPLIGHT_CONFIG_PLUGIN_ID;
33var mergeFormats_1 = require("./utils/mergeFormats");
34exports.mergeFormats = mergeFormats_1.mergeFormats;
35tslib_1.__exportStar(require("./types"), exports);
36exports.createStoplightConfigPlugin = (props) => new StoplightConfigPlugin(props);
37class StoplightConfigPlugin extends abstract_lifecycle_1.AbstractLifecyclePlugin {
38 constructor({ cwd, readFile, defaultConfig = consts_1.DEFAULT_CONFIG }) {
39 super();
40 this.id = consts_1.STOPLIGHT_CONFIG_PLUGIN_ID;
41 this.specProvider = {
42 path: consts_1.STOPLIGHT_CONFIG_PATH_REGEXP,
43 spec: graph_1.Specs.Stoplight,
44 };
45 this.tasks = [
46 {
47 operation: scheduler_1.GraphTaskOp.DeserializeSourceNode,
48 handler: taskHandler_1.createTaskHandler({
49 selector: node => this.selector(node),
50 run: deserialize_1.deserialize,
51 }, `${consts_1.STOPLIGHT_CONFIG_PLUGIN_ID}-deserializer`),
52 },
53 {
54 operation: scheduler_1.GraphTaskOp.SerializeSourceNode,
55 handler: taskHandler_1.createTaskHandler({
56 selector: node => this.selector(node),
57 run: serialize_1.serialize,
58 }, `${consts_1.STOPLIGHT_CONFIG_PLUGIN_ID}-serializer`),
59 },
60 ];
61 this.events = [
62 {
63 name: notifier_1.GraphiteEvent.DidPatch,
64 handler({ operations }) {
65 if (operations.length > 0 && operations[0].op === graph_1.GraphOp.AddNode && operations[0].node.path === this.cwd) {
66 if (typeof this.fileContents === 'string') {
67 const stoplightConfigNode = addNode_1.addNode(this.graphite.graph, operations[0].node.id);
68 this.graphite.graph.setSourceNodeProp(stoplightConfigNode.id, 'data.original', eol.lf(this.fileContents));
69 if (/\r\n/.test(this.fileContents)) {
70 this.graphite.graph.setSourceNodeProp(stoplightConfigNode.id, 'data.eol', filesystem_1.EOL.CRLF);
71 }
72 deserialize_1.populateNodeData(this.graphite.graph, stoplightConfigNode);
73 }
74 else if (this.fileError && this.fileError.code !== 'ENOENT') {
75 const stoplightConfigNode = addNode_1.addNode(this.graphite.graph, operations[0].node.id);
76 this.graphite.graph.setSourceNodeDiagnostics(stoplightConfigNode.id, types_2.StoplightConfigError.IO, [
77 {
78 severity: types_1.DiagnosticSeverity.Error,
79 message: this.fileError.message,
80 range: Object.assign({}, validator_1.DEFAULT_RANGE),
81 },
82 ]);
83 }
84 }
85 },
86 },
87 ];
88 this.cwd = cwd;
89 this.readFile = readFile;
90 this.defaultConfig = defaultConfig;
91 this.uri = path_1.join(cwd, consts_1.STOPLIGHT_CONFIG_PATH);
92 this.selector = (node) => utils_1.isSourceNode(node) && node.uri === this.uri;
93 }
94 async doActivate() {
95 super.doActivate();
96 try {
97 this.fileContents = await this.readFile(this.uri, 'utf8');
98 this.fileError = null;
99 }
100 catch (ex) {
101 this.fileContents = null;
102 this.fileError = ex;
103 this.config = this.defaultConfig;
104 this.graphite.paths = toGraphitePaths_1.toGraphitePaths(this.config);
105 this.paths = groupReadableByFormat_1.groupReadableByFormat(this.config);
106 return;
107 }
108 try {
109 const config = parse_1.parseConfig(this.fileContents);
110 validator_1.assertValidConfig(config);
111 this.config = mergeConfig_1.mergeConfig(lodash_1.cloneDeep(this.defaultConfig), config.data);
112 }
113 catch (_a) {
114 this.config = this.defaultConfig;
115 }
116 this.graphite.paths = toGraphitePaths_1.toGraphitePaths(this.config);
117 this.paths = groupReadableByFormat_1.groupReadableByFormat(this.config);
118 }
119 async doDeactivate() {
120 await super.doDeactivate();
121 this.fileContents = void 0;
122 this.fileError = void 0;
123 this.config = void 0;
124 this.paths = void 0;
125 }
126 getNodes() {
127 return this.graphite.graph.nodeValues.filter(this.createFilter(this.getPaths(), new WeakMap()));
128 }
129 createFilter(paths = this.getPaths(), cache = null) {
130 return createFilter_1.createFilter(paths, pathsMatcher_1.createPathsMatcher(this.graphite, this.cwd), cache);
131 }
132 getPaths() {
133 if (!this.paths) {
134 throw new Error('Config plugin not initialized');
135 }
136 return this.paths;
137 }
138 getConfig() {
139 if (!this.config) {
140 throw new Error('Config plugin not initialized');
141 }
142 return this.config;
143 }
144}
145exports.StoplightConfigPlugin = StoplightConfigPlugin;
146//# sourceMappingURL=index.js.map
\No newline at end of file