Skip to content

Commit 98ce638

Browse files
committed
fix(build): correct writeFileSync usage; harden is-client SSR check; minor cleanup
- src/scripts/build-content-tree.mjs: replace incorrect fs.writeFileSync callback with try/catch. The synchronous API does not accept a callback; passing one throws a TypeError and breaks `npm run content`. Now the script logs success and sets a non‑zero exit code on failure. - src/utilities/is-client.js: avoid ReferenceError in non‑browser contexts by guarding with `typeof window !== 'undefined'` and switch to a consistent ESM default export. This prevents crashes during SSR/SSG or tooling that evaluates the module outside the browser. - src/utilities/flatten-content-tree.mjs: use `forEach` instead of `map` when not using the returned array. This clarifies intent and avoids linter warnings about the unused result of `map`. These changes are internal, do not alter runtime behavior of the website, and improve correctness, stability, and readability. Signed-off-by: shubham shukla <shubhushukla586@gmail.com>
1 parent 0dac331 commit 98ce638

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

src/scripts/build-content-tree.mjs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@ function buildContentTree(source, output) {
4141
dir: source,
4242
});
4343

44-
fs.writeFileSync(
45-
path.resolve(output),
46-
JSON.stringify(content, null, 2),
47-
(error) => {
48-
if (error) {
49-
console.log('scripts/build-content-tree', error);
50-
} else {
51-
console.log('Successfully built content tree file at ' + output);
52-
}
53-
}
54-
);
44+
// fs.writeFileSync does not accept a callback; wrap with try/catch and
45+
// log a clear success or error instead.
46+
try {
47+
fs.writeFileSync(
48+
path.resolve(output),
49+
JSON.stringify(content, null, 2)
50+
);
51+
console.log('Successfully built content tree file at ' + output);
52+
} catch (error) {
53+
console.error('scripts/build-content-tree', error);
54+
process.exitCode = 1;
55+
}
5556
}

src/utilities/flatten-content-tree.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ export default (tree) => {
77
}
88

99
if ('children' in node) {
10-
node.children.map(crawl);
10+
node.children.forEach(crawl);
1111
}
1212
};
1313

14-
tree.children.map(crawl);
14+
if (Array.isArray(tree.children)) {
15+
tree.children.forEach(crawl);
16+
}
1517

1618
return paths;
1719
};

src/utilities/is-client.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
const isClient = window !== undefined && window.document !== undefined;
1+
// Guard against ReferenceError in non-browser/SSR environments
2+
// and use a consistent ESM default export.
3+
const isClient =
4+
typeof window !== 'undefined' && typeof window.document !== 'undefined';
25

3-
module.exports = isClient;
6+
export default isClient;

0 commit comments

Comments
 (0)