Skip to content

Commit

Permalink
fix: check docs links existance before linking (#17)
Browse files Browse the repository at this point in the history
Fixes: #16
  • Loading branch information
MoLow authored Oct 28, 2024
1 parent 5edac5c commit 7d9ab71
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
12 changes: 8 additions & 4 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ class Linker {
#dirs = []
#baseDir
#docsDir
constructor ({ baseDir, docsDir }) {
#validateDocsLink
constructor ({ baseDir, docsDir, validateDocsLink }) {
this.#baseDir = baseDir
this.#docsDir = docsDir
this.#validateDocsLink = validateDocsLink ?? (() => true)
}

async getLinks (allDirectories, readDir) {
Expand All @@ -38,7 +40,7 @@ class Linker {
})
.filter(Boolean)

this.#makeDocsLinks(allDirs.map((d) => d.raw))
await this.#makeDocsLinks(allDirs.map((d) => d.raw))

this.#dirs = allDirs.filter((d) => semver.satisfies(d, '~0.10 || ~0.12 || >= 1.0')).map((d) => d.raw)
this.#dirs.sort((d1, d2) => semver.compare(d1, d2))
Expand Down Expand Up @@ -70,15 +72,17 @@ class Linker {
return this.#links
}

#makeDocsLinks (versions) {
async #makeDocsLinks (versions) {
if (!this.#docsDir) {
return
}

for (const version of versions) {
const src = path.join(this.#baseDir, version, 'docs')
const dst = path.join(this.#docsDir, version)
this.#links.set(dst, src)
if (await this.#validateDocsLink(src)) {
this.#links.set(dst, src)
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions latest-linker.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ if (process.argv.length < 3) {
}

const dir = path.resolve(process.argv[2])
const docsDir = process.argv[3] && path.resolve(process.argv[3]);
const docsDir = process.argv[3] && path.resolve(process.argv[3])

async function validateDocsLink (src) {
try {
const stat = await fs.stat(src)
return stat.isDirectory()
} catch {
return false
}
}

(async function main () {
/* c8 ignore next 3 */
Expand All @@ -26,7 +35,7 @@ const docsDir = process.argv[3] && path.resolve(process.argv[3]);
}

const allDirs = (await fs.readdir(dir, { withFileTypes: true })).filter((d) => d.isDirectory()).map((d) => d.name)
const linker = new Linker({ baseDir: dir, docsDir })
const linker = new Linker({ baseDir: dir, docsDir, validateDocsLink })
const links = await linker.getLinks(allDirs, fs.readdir)
for (const [dest, src] of links) {
await fs.unlink(dest).catch(() => {})
Expand Down

0 comments on commit 7d9ab71

Please sign in to comment.