Skip to content

Commit 00f902e

Browse files
committed
fix: get this working on windows again
1 parent 9cd4c56 commit 00f902e

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/lib/server/content.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { posixify } from '$lib/utils.js';
12
import fs from 'node:fs';
23
import path from 'node:path';
34
import glob from 'tiny-glob/sync.js';
@@ -32,16 +33,19 @@ function is_valid(dir) {
3233
* @returns {import('$lib/types').PartStub[]}
3334
*/
3435
export function get_index() {
35-
const parts = fs.readdirSync('content/tutorial').filter(is_valid);
36+
const parts = fs.readdirSync('content/tutorial').filter(is_valid).map(posixify);
3637

3738
return parts.map((part) => {
38-
const chapters = fs.readdirSync(`content/tutorial/${part}`).filter(is_valid);
39+
const chapters = fs.readdirSync(`content/tutorial/${part}`).filter(is_valid).map(posixify);
3940

4041
return {
4142
slug: part,
4243
title: json(`content/tutorial/${part}/meta.json`).title,
4344
chapters: chapters.map((chapter) => {
44-
const exercises = fs.readdirSync(`content/tutorial/${part}/${chapter}`).filter(is_valid);
45+
const exercises = fs
46+
.readdirSync(`content/tutorial/${part}/${chapter}`)
47+
.filter(is_valid)
48+
.map(posixify);
4549

4650
return {
4751
slug: chapter,
@@ -71,7 +75,7 @@ export function get_index() {
7175
export function get_exercise(slug) {
7276
const exercises = glob('[0-9][0-9]-*/[0-9][0-9]-*/[0-9][0-9]-*/README.md', {
7377
cwd: 'content/tutorial'
74-
});
78+
}).map(posixify);
7579

7680
/** @type {string[]} */
7781
const chain = [];
@@ -112,7 +116,7 @@ export function get_exercise(slug) {
112116
const b_ = /** @type {import('$lib/types').FileStub} */ (b[key]);
113117

114118
if (a_.contents === b_.contents) {
115-
throw new Error(`duplicate file: ${exercise_slug} ${key}`)
119+
throw new Error(`duplicate file: ${exercise_slug} ${key}`);
116120
}
117121
}
118122

@@ -192,7 +196,7 @@ export function get_exercise(slug) {
192196
}
193197

194198
// ensure every code block for an exercise with multiple files has a `/// file:` annotation
195-
const filtered = Object.values(solution).filter(item => {
199+
const filtered = Object.values(solution).filter((item) => {
196200
return item.type === 'file' && item.name.startsWith(scope.prefix);
197201
});
198202

@@ -208,7 +212,9 @@ export function get_exercise(slug) {
208212
const all_files = { ...a, ...solution };
209213
const filenames = new Set(
210214
Object.keys(all_files)
211-
.filter((filename) => filename.startsWith(scope.prefix) && all_files[filename].type === 'file')
215+
.filter(
216+
(filename) => filename.startsWith(scope.prefix) && all_files[filename].type === 'file'
217+
)
212218
.map((filename) => filename.slice(scope.prefix.length))
213219
);
214220

@@ -287,14 +293,14 @@ function walk(cwd, options = {}) {
287293
* @param {number} depth
288294
*/
289295
function walk_dir(dir, depth) {
290-
const files = fs.readdirSync(path.join(cwd, dir));
296+
const files = fs.readdirSync(path.join(cwd, dir)).map(posixify);
291297

292298
for (const basename of files) {
293299
if (excluded.has(basename)) continue;
294300

295301
const name = dir + basename;
296302

297-
if (options.exclude?.some((exclude) => name.replace(/\\/g, '/').endsWith(exclude))) continue;
303+
if (options.exclude?.some((exclude) => posixify(name).endsWith(exclude))) continue;
298304

299305
const resolved = path.join(cwd, name);
300306
const stats = fs.statSync(resolved);

src/lib/utils.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ const chars = {
1313
/** @param {string} html */
1414
export function escape_html(html) {
1515
return html.replace(/[&<>]/g, (c) => chars[c]);
16-
}
16+
}
17+
18+
/** @param {string} path */
19+
export function posixify(path) {
20+
return path.replace(/\\/g, '/');
21+
}

0 commit comments

Comments
 (0)