(filePath)
| 69 | } |
| 70 | |
| 71 | async function readWorkspaceConfigFile(filePath) { |
| 72 | const { |
| 73 | default: fs |
| 74 | } = await import("graceful-fs"); |
| 75 | const {promisify} = await import("node:util"); |
| 76 | const readFile = promisify(fs.readFile); |
| 77 | const jsyaml = await import("js-yaml"); |
| 78 | |
| 79 | let fileContent; |
| 80 | try { |
| 81 | fileContent = await readFile(filePath, {encoding: "utf8"}); |
| 82 | } catch (err) { |
| 83 | throw new Error( |
| 84 | `Failed to load workspace configuration from path ${filePath}: ${err.message}`, { |
| 85 | cause: err |
| 86 | }); |
| 87 | } |
| 88 | let configs; |
| 89 | try { |
| 90 | configs = jsyaml.loadAll(fileContent, undefined, { |
| 91 | filename: filePath, |
| 92 | }); |
| 93 | } catch (err) { |
| 94 | throw new Error(`Failed to parse workspace configuration at ${filePath}\nError: ${err.message}`); |
| 95 | } |
| 96 | |
| 97 | if (!configs || !configs.length) { |
| 98 | // No configs found => exit here |
| 99 | log.verbose(`Found empty workspace configuration file at ${filePath}`); |
| 100 | return configs; |
| 101 | } |
| 102 | |
| 103 | // Validate found configurations with schema |
| 104 | // Validation is done again in the Workspace class. But here we can reference the YAML file |
| 105 | // which adds helpful information like the line number |
| 106 | const validationResults = await Promise.all( |
| 107 | configs.map(async (config, documentIndex) => { |
| 108 | // Catch validation errors to ensure proper order of rejections within Promise.all |
| 109 | try { |
| 110 | await validateWorkspace({ |
| 111 | config, |
| 112 | yaml: { |
| 113 | path: filePath, |
| 114 | source: fileContent, |
| 115 | documentIndex |
| 116 | } |
| 117 | }); |
| 118 | } catch (error) { |
| 119 | return error; |
| 120 | } |
| 121 | }) |
| 122 | ); |
| 123 | |
| 124 | const validationErrors = validationResults.filter(($) => $); |
| 125 | |
| 126 | if (validationErrors.length > 0) { |
| 127 | // Throw any validation errors |
| 128 | // For now just throw the error of the first invalid document |
no test coverage detected