🛠️ Update various documentation, scripts, and configuration templates to enhance clarity, functionality, and maintainability across the project
This commit is contained in:
@@ -1,18 +1,23 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import test from 'node:test';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
|
||||
const repoRoot = path.resolve(scriptDir, '..');
|
||||
const mergeScript = path.join(scriptDir, 'merge-vscode-settings.mjs');
|
||||
const templateFile = path.join(repoRoot, 'config', 'vscode', 'settings.template.jsonc');
|
||||
const repoRoot = path.resolve(scriptDir, "..");
|
||||
const mergeScript = path.join(scriptDir, "merge-vscode-settings.mjs");
|
||||
const templateFile = path.join(
|
||||
repoRoot,
|
||||
"config",
|
||||
"vscode",
|
||||
"settings.template.jsonc",
|
||||
);
|
||||
|
||||
function stripJsonComments(input) {
|
||||
let output = '';
|
||||
let output = "";
|
||||
let inString = false;
|
||||
let escaping = false;
|
||||
let inLineComment = false;
|
||||
@@ -23,7 +28,7 @@ function stripJsonComments(input) {
|
||||
const next = input[index + 1];
|
||||
|
||||
if (inLineComment) {
|
||||
if (char === '\n') {
|
||||
if (char === "\n") {
|
||||
inLineComment = false;
|
||||
output += char;
|
||||
}
|
||||
@@ -31,10 +36,10 @@ function stripJsonComments(input) {
|
||||
}
|
||||
|
||||
if (inBlockComment) {
|
||||
if (char === '*' && next === '/') {
|
||||
if (char === "*" && next === "/") {
|
||||
inBlockComment = false;
|
||||
index += 1;
|
||||
} else if (char === '\n' || char === '\r') {
|
||||
} else if (char === "\n" || char === "\r") {
|
||||
output += char;
|
||||
}
|
||||
continue;
|
||||
@@ -44,7 +49,7 @@ function stripJsonComments(input) {
|
||||
output += char;
|
||||
if (escaping) {
|
||||
escaping = false;
|
||||
} else if (char === '\\') {
|
||||
} else if (char === "\\") {
|
||||
escaping = true;
|
||||
} else if (char === '"') {
|
||||
inString = false;
|
||||
@@ -58,13 +63,13 @@ function stripJsonComments(input) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === '/' && next === '/') {
|
||||
if (char === "/" && next === "/") {
|
||||
inLineComment = true;
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === '/' && next === '*') {
|
||||
if (char === "/" && next === "*") {
|
||||
inBlockComment = true;
|
||||
index += 1;
|
||||
continue;
|
||||
@@ -77,7 +82,7 @@ function stripJsonComments(input) {
|
||||
}
|
||||
|
||||
function stripTrailingCommas(input) {
|
||||
let output = '';
|
||||
let output = "";
|
||||
let inString = false;
|
||||
let escaping = false;
|
||||
|
||||
@@ -88,7 +93,7 @@ function stripTrailingCommas(input) {
|
||||
output += char;
|
||||
if (escaping) {
|
||||
escaping = false;
|
||||
} else if (char === '\\') {
|
||||
} else if (char === "\\") {
|
||||
escaping = true;
|
||||
} else if (char === '"') {
|
||||
inString = false;
|
||||
@@ -102,12 +107,12 @@ function stripTrailingCommas(input) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === ',') {
|
||||
if (char === ",") {
|
||||
let lookahead = index + 1;
|
||||
while (lookahead < input.length && /\s/.test(input[lookahead])) {
|
||||
lookahead += 1;
|
||||
}
|
||||
if (input[lookahead] === '}' || input[lookahead] === ']') {
|
||||
if (input[lookahead] === "}" || input[lookahead] === "]") {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -127,23 +132,23 @@ function runMerge(targetFile) {
|
||||
process.execPath,
|
||||
[
|
||||
mergeScript,
|
||||
'--target',
|
||||
"--target",
|
||||
targetFile,
|
||||
'--template',
|
||||
"--template",
|
||||
templateFile,
|
||||
'--set',
|
||||
'COPILOT_RESOURCES_HOME=/repo/home',
|
||||
"--set",
|
||||
"COPILOT_RESOURCES_HOME=/repo/home",
|
||||
],
|
||||
{
|
||||
cwd: repoRoot,
|
||||
encoding: 'utf8',
|
||||
}
|
||||
encoding: "utf8",
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
test('preserves comments and custom nested entries while inserting managed settings', () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'copilot-settings-'));
|
||||
const targetFile = path.join(tempDir, 'settings.json');
|
||||
test("preserves comments and custom nested entries while inserting managed settings", () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "copilot-settings-"));
|
||||
const targetFile = path.join(tempDir, "settings.json");
|
||||
|
||||
fs.writeFileSync(
|
||||
targetFile,
|
||||
@@ -163,13 +168,13 @@ test('preserves comments and custom nested entries while inserting managed setti
|
||||
}
|
||||
}
|
||||
`,
|
||||
'utf8'
|
||||
"utf8",
|
||||
);
|
||||
|
||||
const result = runMerge(targetFile);
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
|
||||
const output = fs.readFileSync(targetFile, 'utf8');
|
||||
const output = fs.readFileSync(targetFile, "utf8");
|
||||
assert.match(output, /\/\/ keep this comment/);
|
||||
assert.match(output, /\/\/ preserve nested comments/);
|
||||
assert.match(output, /\/\/ custom agents stay/);
|
||||
@@ -177,18 +182,32 @@ test('preserves comments and custom nested entries while inserting managed setti
|
||||
assert.match(output, /"\/repo\/home\/resources\/agents": true/);
|
||||
|
||||
const parsed = parseJsonc(output);
|
||||
assert.equal(parsed['workbench.colorTheme'], 'GitHub Dark Mode');
|
||||
assert.equal(parsed['chat.agentFilesLocations']['/custom/agents'], true);
|
||||
assert.equal(parsed['chat.agentFilesLocations']['/repo/home/resources/agents'], true);
|
||||
assert.equal(parsed['chat.agentFilesLocations']['~/.copilot/agents'], true);
|
||||
assert.equal(parsed['chat.instructionsFilesLocations']['/old/instructions'], true);
|
||||
assert.equal(parsed['chat.instructionsFilesLocations']['/repo/home/resources/instructions'], true);
|
||||
assert.equal(parsed['chat.instructionsFilesLocations']['~/.claude/rules'], true);
|
||||
assert.equal(parsed["workbench.colorTheme"], "GitHub Dark Mode");
|
||||
assert.equal(parsed["chat.agentFilesLocations"]["/custom/agents"], true);
|
||||
assert.equal(
|
||||
parsed["chat.agentFilesLocations"]["/repo/home/resources/agents"],
|
||||
true,
|
||||
);
|
||||
assert.equal(parsed["chat.agentFilesLocations"]["~/.copilot/agents"], true);
|
||||
assert.equal(
|
||||
parsed["chat.instructionsFilesLocations"]["/old/instructions"],
|
||||
true,
|
||||
);
|
||||
assert.equal(
|
||||
parsed["chat.instructionsFilesLocations"][
|
||||
"/repo/home/resources/instructions"
|
||||
],
|
||||
true,
|
||||
);
|
||||
assert.equal(
|
||||
parsed["chat.instructionsFilesLocations"]["~/.claude/rules"],
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
test('second run is idempotent and keeps the file text unchanged', () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'copilot-settings-'));
|
||||
const targetFile = path.join(tempDir, 'settings.json');
|
||||
test("second run is idempotent and keeps the file text unchanged", () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "copilot-settings-"));
|
||||
const targetFile = path.join(tempDir, "settings.json");
|
||||
|
||||
fs.writeFileSync(
|
||||
targetFile,
|
||||
@@ -199,18 +218,40 @@ test('second run is idempotent and keeps the file text unchanged', () => {
|
||||
}
|
||||
}
|
||||
`,
|
||||
'utf8'
|
||||
"utf8",
|
||||
);
|
||||
|
||||
const firstRun = runMerge(targetFile);
|
||||
assert.equal(firstRun.status, 0, firstRun.stderr);
|
||||
const firstOutput = fs.readFileSync(targetFile, 'utf8');
|
||||
const firstOutput = fs.readFileSync(targetFile, "utf8");
|
||||
|
||||
const secondRun = runMerge(targetFile);
|
||||
assert.equal(secondRun.status, 0, secondRun.stderr);
|
||||
assert.match(secondRun.stdout, /already up to date/);
|
||||
|
||||
const secondOutput = fs.readFileSync(targetFile, 'utf8');
|
||||
const secondOutput = fs.readFileSync(targetFile, "utf8");
|
||||
assert.equal(secondOutput, firstOutput);
|
||||
assert.match(secondOutput, /\/\/ user comment/);
|
||||
});
|
||||
});
|
||||
|
||||
test("creates a valid settings file from an empty target", () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "copilot-settings-"));
|
||||
const targetFile = path.join(tempDir, "settings.json");
|
||||
|
||||
fs.writeFileSync(targetFile, "{}\n", "utf8");
|
||||
|
||||
const result = runMerge(targetFile);
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
|
||||
const parsed = parseJsonc(fs.readFileSync(targetFile, "utf8"));
|
||||
assert.equal(
|
||||
parsed["chat.agentFilesLocations"]["/repo/home/resources/agents"],
|
||||
true,
|
||||
);
|
||||
assert.equal(
|
||||
parsed["chat.instructionsFilesLocations"][
|
||||
"/repo/home/resources/instructions"
|
||||
],
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user