projects/scripts/em-dashes.mjs
// @ts-check
// Em-dash lint for one or more folders or files: node scripts/em-dashes.mjs [target ...]
// Repository prose uses no em dashes, so every tracked or new text file in each target fails on one, with its position.
// Fixtures that copy Fluxer's own text keep it exactly, and an empty file list fails so a broken scan cannot pass
import { execFileSync } from "node:child_process"
import { readFileSync, statSync } from "node:fs"
import { relative, resolve } from "node:path"
import { fileURLToPath } from "node:url"
const dash = String.fromCharCode(0x2014)
// Upstream text kept exactly as Fluxer publishes it
const exactUpstream = new Set([
"tests/contract/fixtures/gateway-docs.json",
"tests/contract/fixtures/openapi-examples.json",
"projects/sdk/tests/contract/fixtures/gateway-docs.json",
"projects/sdk/tests/contract/fixtures/openapi-examples.json",
])
const binary = /\.(?:png|jpe?g|gif|webp|avif|ico|woff2?|ttf|otf|glb|gltf|bin|zip|gz|tgz|pdf|wasm|mp4|webm)$/i
/**
* Scan one or more folders or files for em dashes
* @param {readonly string[]} targets
* @param {string} [cwd]
*/
export function scanEmDashes(targets, cwd = process.cwd()) {
const resolvedTargets = (targets.length === 0 ? ["."] : targets).map((target) => resolve(cwd, target))
/** @type {string[]} */
const found = []
let checked = 0
for (const target of resolvedTargets) {
const stats = statSync(target)
/** @type {{ display: string, absolute: string }[]} */
let entries
if (stats.isFile()) {
const rel = relative(cwd, target).replaceAll("\\", "/")
entries = binary.test(rel) || exactUpstream.has(rel) ? [] : [{ display: rel, absolute: target }]
} else {
const listed = execFileSync("git", ["ls-files", "--cached", "--others", "--exclude-standard", "-z"], {
cwd: target,
encoding: "utf8",
maxBuffer: 64 * 1024 * 1024,
})
.split("\0")
.filter((file) => file !== "" && !binary.test(file) && !exactUpstream.has(file))
if (listed.length === 0) throw new Error(`No files to check in ${target}`)
entries = listed.map((file) => ({
display:
resolvedTargets.length === 1 ? file : relative(cwd, resolve(target, file)).replaceAll("\\", "/"),
absolute: resolve(target, file),
}))
}
for (const { display, absolute } of entries) {
let text
try {
text = readFileSync(absolute, "utf8")
} catch (error) {
// A file deleted from the working tree but still tracked has nothing left to check
if (/** @type {NodeJS.ErrnoException} */ (error).code === "ENOENT") continue
throw error
}
checked++
if (text.includes("\0") || !text.includes(dash)) continue
text.split("\n").forEach((line, index) => {
const column = line.indexOf(dash)
if (column !== -1) found.push(`${display}:${index + 1}:${column + 1}`)
})
}
}
return { checked, found }
}
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
const { checked, found } = scanEmDashes(process.argv.slice(2))
if (found.length > 0) {
console.error(
`Em dashes found. Rephrase with a comma, colon, parentheses or two sentences:\n${found.join("\n")}`,
)
process.exit(1)
}
console.log(`No em dashes in ${checked} files`)
}