| 1 | /** |
| 2 | * @file Generate config.yaml file from template. Also intended to run as a child process. |
| 3 | */ |
| 4 | |
| 5 | const { fork } = require("child_process"); |
| 6 | const fs = require("fs"); |
| 7 | const path = require("path"); |
| 8 | |
| 9 | const { execCmdSync } = require("../../../scripts/util"); |
| 10 | |
| 11 | const { continueDir } = require("./utils"); |
| 12 | |
| 13 | async function generateConfigYamlSchema() { |
| 14 | process.chdir(path.join(continueDir, "packages", "config-yaml")); |
| 15 | execCmdSync("npm install"); |
| 16 | execCmdSync("npm run build"); |
| 17 | execCmdSync("npm run generate-schema"); |
| 18 | fs.copyFileSync( |
| 19 | path.join("schema", "config-yaml-schema.json"), |
| 20 | path.join(continueDir, "extensions", "vscode", "config-yaml-schema.json"), |
| 21 | ); |
| 22 | console.log("[info] Generated config.yaml schema"); |
| 23 | } |
| 24 | |
| 25 | async function copyConfigSchema() { |
| 26 | process.chdir(path.join(continueDir, "extensions", "vscode")); |
| 27 | // Modify and copy for .continuerc.json |
| 28 | const schema = JSON.parse(fs.readFileSync("config_schema.json", "utf8")); |
| 29 | schema.$defs.SerializedContinueConfig.properties.mergeBehavior = { |
| 30 | type: "string", |
| 31 | enum: ["merge", "overwrite"], |
| 32 | default: "merge", |
| 33 | title: "Merge behavior", |
| 34 | markdownDescription: |
| 35 | "If set to 'merge', .continuerc.json will be applied on top of config.json (arrays and objects are merged). If set to 'overwrite', then every top-level property of .continuerc.json will overwrite that property from config.json.", |
| 36 | "x-intellij-html-description": |
| 37 | "<p>If set to <code>merge</code>, <code>.continuerc.json</code> will be applied on top of <code>config.json</code> (arrays and objects are merged). If set to <code>overwrite</code>, then every top-level property of <code>.continuerc.json</code> will overwrite that property from <code>config.json</code>.</p>", |
| 38 | }; |
| 39 | fs.writeFileSync("continue_rc_schema.json", JSON.stringify(schema, null, 2)); |
| 40 | |
| 41 | // Copy config schemas to intellij |
| 42 | fs.copyFileSync( |
| 43 | "config_schema.json", |
| 44 | path.join( |
| 45 | "..", |
| 46 | "intellij", |
| 47 | "src", |
| 48 | "main", |
| 49 | "resources", |
| 50 | "config_schema.json", |
| 51 | ), |
| 52 | ); |
| 53 | fs.copyFileSync( |
| 54 | "continue_rc_schema.json", |
| 55 | path.join( |
| 56 | "..", |
| 57 | "intellij", |
| 58 | "src", |
| 59 | "main", |
| 60 | "resources", |
| 61 | "continue_rc_schema.json", |
| 62 | ), |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | process.on("message", (msg) => { |
| 67 | const { operation } = msg.payload; |
| 68 | if (operation === "generate") { |
| 69 | generateConfigYamlSchema() |
| 70 | .then(() => process.send({ done: true })) |
| 71 | .catch((error) => { |
| 72 | console.error(error); // show the error in the parent process |
| 73 | process.send({ error: true }); |
| 74 | }); |
| 75 | } |
| 76 | if (operation === "copy") { |
| 77 | copyConfigSchema() |
| 78 | .then(() => process.send({ done: true })) |
| 79 | .catch((error) => { |
| 80 | console.error(error); // show the error in the parent process |
| 81 | process.send({ error: true }); |
| 82 | }); |
| 83 | } |
| 84 | }); |
| 85 | |
| 86 | async function generateAndCopyConfigYamlSchema() { |
| 87 | // Generate and copy over config-yaml-schema.json |
| 88 | const generateConfigYamlChild = fork( |
| 89 | path.join(__dirname, "generate-copy-config.js"), |
| 90 | { |
| 91 | stdio: "inherit", |
| 92 | }, |
| 93 | ); |
| 94 | generateConfigYamlChild.send({ payload: { operation: "generate" } }); |
| 95 | |
| 96 | await new Promise((resolve, reject) => { |
| 97 | generateConfigYamlChild.on("message", (msg) => { |
| 98 | if (msg.error) { |
| 99 | reject(); |
| 100 | } |
| 101 | resolve(); |
| 102 | }); |
| 103 | }); |
| 104 | |
| 105 | // Copy config schemas to intellij |
| 106 | const copyConfigSchemaChild = fork( |
| 107 | path.join(__dirname, "generate-copy-config.js"), |
| 108 | { |
| 109 | stdio: "inherit", |
| 110 | }, |
| 111 | ); |
| 112 | copyConfigSchemaChild.send({ payload: { operation: "copy" } }); |
| 113 | |
| 114 | await new Promise((resolve, reject) => { |
| 115 | copyConfigSchemaChild.on("message", (msg) => { |
| 116 | if (msg.error) { |
| 117 | reject(); |
| 118 | } |
| 119 | resolve(); |
| 120 | }); |
| 121 | }); |
| 122 | } |
| 123 | |
| 124 | module.exports = { |
| 125 | generateAndCopyConfigYamlSchema, |
| 126 | }; |