Skip to content
Merged
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
149 changes: 145 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"ci:testing": "npm run test:coverage && npm run test:coverage:check && npm run ci:workflows",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build",
"docs:validate-drift": "node scripts/validate-docs-drift.mjs",
"docs:api:generate": "node scripts/generate-api-docs.mjs",
"docs:validate-drift": "node scripts/validate-docs-drift.mjs",
"api:start": "node api/server.js"
Expand All @@ -55,6 +56,7 @@
"@sentry/react": "8.54.0",
"@stellar/stellar-sdk": "^12.3.0",
"@tensorflow/tfjs": "^4.22.0",
"@tensorflow/tfjs-node": "4.22.0",
"@tensorflow/tfjs-node": "^4.22.0",
"d3-array": "^3.2.4",
"d3-force-3d": "^3.0.6",
Expand Down Expand Up @@ -84,6 +86,7 @@
},
"optionalDependencies": {
"@ledgerhq/hw-transport-webhid": "^6.29.4",
"@ledgerhq/hw-transport-webusb": "^6.29.4"
"@ledgerhq/hw-transport-webusb": "^6.29.4",
"@stellar/ledger": "^1.0.0",
"ioredis": "^5.4.0"
Expand Down
161 changes: 161 additions & 0 deletions scripts/validate-docs-drift.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,164 @@
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const repoRoot = path.resolve(__dirname, '..');
const docsRoot = path.join(repoRoot, 'docs');
const rootPackage = path.join(repoRoot, 'package.json');

const externalLinkPattern = /^(?:[a-zA-Z][a-zA-Z0-9+.-]*:|\/\/)/;
const markdownLinkPattern = /!?\[[^\]]*\]\(\s*([^\s)]+)(?:\s+"[^"]*")?\s*\)/g;
const npmRunPattern = /npm run\s+([\w:-]+)/g;

async function collectMarkdownFiles(dir) {
const entries = await fs.readdir(dir, { withFileTypes: true });
const files = [];
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
files.push(...await collectMarkdownFiles(fullPath));
} else if (entry.isFile() && (entry.name.endsWith('.md') || entry.name.endsWith('.mdx'))) {
files.push(fullPath);
}
}
return files;
}

function parseMarkdownLinks(content) {
const links = [];
let match;
while ((match = markdownLinkPattern.exec(content)) !== null) {
links.push(match[1]);
}
return links;
}

function parseNpmRunCommands(content) {
const scripts = [];
let match;
while ((match = npmRunPattern.exec(content)) !== null) {
scripts.push(match[1]);
}
return scripts;
}

function isExternalLink(href) {
return externalLinkPattern.test(href) || href.startsWith('mailto:') || href.startsWith('tel:');
}

function normalizeLinkHref(href) {
return href.replace(/^\.+\//, '').replace(/#.*$/, '');
}

async function fileExists(filePath) {
try {
const stat = await fs.stat(filePath);
return stat.isFile();
} catch {
return false;
}
}

async function resolveLink(sourceFile, rawHref) {
const href = rawHref.split('#', 1)[0];
if (!href || href.startsWith('#')) {
return null;
}

const candidates = [];
const isAbsoluteDocs = href.startsWith('docs/') || href.startsWith('/docs/') || href.startsWith('./docs/') || href.startsWith('../docs/');

const sourceDir = path.dirname(sourceFile);
const referenced = path.normalize(href);

if (!path.isAbsolute(referenced)) {
candidates.push(path.join(sourceDir, referenced));
if (isAbsoluteDocs) {
const relativeToRoot = path.join(repoRoot, referenced.replace(/^\/*/, ''));
candidates.push(relativeToRoot);
}
} else {
candidates.push(path.join(repoRoot, referenced));
}

for (const candidate of candidates) {
const normalized = path.normalize(candidate);
if (await fileExists(normalized)) return normalized;
const withMd = `${normalized}.md`;
const withMdx = `${normalized}.mdx`;
const indexMd = path.join(normalized, 'index.md');
const readmeMd = path.join(normalized, 'README.md');
if (await fileExists(withMd)) return withMd;
if (await fileExists(withMdx)) return withMdx;
if (await fileExists(indexMd)) return indexMd;
if (await fileExists(readmeMd)) return readmeMd;
}

return null;
}

async function loadPackageScripts() {
try {
const pkgJson = JSON.parse(await fs.readFile(rootPackage, 'utf8'));
return new Set(Object.keys(pkgJson.scripts || {}));
} catch (error) {
console.error('Unable to read package.json for npm script validation:', error.message);
return new Set();
}
}

async function main() {
const files = await collectMarkdownFiles(docsRoot);
const packageScripts = await loadPackageScripts();
const errors = [];

for (const file of files) {
const content = await fs.readFile(file, 'utf8');
const links = parseMarkdownLinks(content);
for (const link of links) {
if (isExternalLink(link) || link.startsWith('#')) continue;
const resolved = await resolveLink(file, link);
if (!resolved) {
errors.push({ type: 'missing-file', file, link, resolved: path.join(path.dirname(file), link) });
}
}

const scripts = parseNpmRunCommands(content);
for (const script of scripts) {
if (!packageScripts.has(script)) {
errors.push({ type: 'missing-script', file, script });
}
}
}

if (errors.length === 0) {
console.log('=== Documentation Drift Detection ===');
console.log(`Scanned ${files.length} documentation files.`);
console.log('No missing docs links or npm scripts were found.');
process.exit(0);
}

console.log('=== Documentation Drift Detection ===');
console.log(`Scanned ${files.length} documentation files.`);
console.log();
for (const error of errors) {
if (error.type === 'missing-file') {
console.log(`ERROR: File not found: "${error.link}" (resolved: ${error.resolved}) — referenced from ${path.relative(repoRoot, error.file)}`);
} else if (error.type === 'missing-script') {
console.log(`ERROR: Script not found: "npm run ${error.script}" — referenced from ${path.relative(repoRoot, error.file)}`);
}
}
console.log();
console.log(`Errors: ${errors.length}`);
process.exit(1);
}

main().catch((error) => {
console.error('Unexpected error during docs drift validation:', error);
process.exit(1);
});
#!/usr/bin/env node
import fs from "fs";
import path from "path";
Expand Down
Loading
Loading