Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ jobs:
- name: Build All Packages
run: bun run buildc all

- name: Check cspell word order
run: bun run cspell-order:check

- name: Basic Checks
run: bun run check

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
],
"scripts": {
"check": "check && bun run --sequential --workspaces check",
"cspell-order:check": "bun run scripts/check-cspell-order.ts",
"cspell-order:format": "bun run scripts/format-cspell-order.ts",
Comment on lines 11 to +13

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"check": "check && bun run --sequential --workspaces check",
"cspell-order:check": "bun run scripts/check-cspell-order.ts",
"cspell-order:format": "bun run scripts/format-cspell-order.ts",
"check": "cspell-order:check && check && bun run --sequential --workspaces check",
"cspell-order:check": "bun run scripts/check-cspell-order.ts",
"cspell-order:format": "bun run scripts/format-cspell-order.ts",

This way, you can avoid adding a separate Check cspell word order step to validate.yml, since the Basic Checks step will automatically run the cspell word-order check

"test": "bun run --workspaces test run",
"test:coverage": "bun run --workspaces test:coverage",
"prepare": "simple-git-hooks",
Expand Down
46 changes: 46 additions & 0 deletions scripts/check-cspell-order.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const configPath = 'cspell.yml';
const lines = (await Bun.file(configPath).text()).split('\n');
const wordsSectionIndex = lines.findIndex((line) => /^words:\s*$/.test(line));

if (wordsSectionIndex === -1) {
throw new Error(`Could not find the words section in ${configPath}`);
}

const words: { line: number; value: string }[] = [];

for (let index = wordsSectionIndex + 1; index < lines.length; index++) {
const line = lines[index];
const wordMatch = line.match(/^ {2}- (.*)$/);

if (wordMatch) {
words.push({ line: index + 1, value: wordMatch[1] });
continue;
}

if (line.trim() && !line.startsWith(' ')) break;
}

const outOfOrder = words.flatMap((word, index) => {
if (index === 0) return [];

const previous = words[index - 1];
return previous.value.toLowerCase() > word.value.toLowerCase()
? [{ previous, word }]
: [];
});

if (outOfOrder.length > 0) {
console.error(
`${configPath}: words must be sorted alphabetically (case-insensitive):`,
);
for (const { previous, word } of outOfOrder) {
console.error(
` line ${word.line} "${word.value.slice(0, 40)}" is before line ${previous.line} "${previous.value.slice(0, 40)}"`,
);
}
process.exitCode = 1;
} else {
console.log(`${configPath}: words are sorted alphabetically.`);
}

export default {};
43 changes: 43 additions & 0 deletions scripts/format-cspell-order.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const configPath = 'cspell.yml';
const originalConfig = await Bun.file(configPath).text();
const lines = originalConfig.split('\n');
const wordsSectionIndex = lines.findIndex((line) => /^words:\s*$/.test(line));

if (wordsSectionIndex === -1) {
throw new Error(`Could not find the words section in ${configPath}`);
}

const wordLines: { index: number; line: string; value: string }[] = [];

for (let index = wordsSectionIndex + 1; index < lines.length; index++) {
const line = lines[index];
const wordMatch = line.match(/^ {2}- (.*)$/);

if (wordMatch) {
wordLines.push({ index, line, value: wordMatch[1] });
continue;
}

if (line.trim() && !line.startsWith(' ')) break;
}

const sortedWordLines = [...wordLines].sort((a, b) => {
const first = a.value.toLowerCase();
const second = b.value.toLowerCase();
return first < second ? -1 : first > second ? 1 : 0;
});

for (const [index, wordLine] of wordLines.entries()) {
lines[wordLine.index] = sortedWordLines[index].line;
}

const sortedConfig = lines.join('\n');

if (sortedConfig !== originalConfig) {
await Bun.write(configPath, sortedConfig);
console.log(`${configPath}: sorted words alphabetically.`);
} else {
console.log(`${configPath}: words are already sorted alphabetically.`);
}

export default {};
Loading