Skip to content

Commit 694f5ca

Browse files
authored
fix(astro): sub folders not working on Windows (#225)
1 parent ab1abf2 commit 694f5ca

File tree

8 files changed

+116
-32
lines changed

8 files changed

+116
-32
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
branches:
66
- main
77
pull_request:
8+
workflow_dispatch:
89

910
jobs:
1011
test:

packages/astro/src/default/utils/content.ts

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
1-
import type {
2-
ChapterSchema,
3-
FilesRefList,
4-
Lesson,
5-
LessonSchema,
6-
PartSchema,
7-
Tutorial,
8-
TutorialSchema,
9-
} from '@tutorialkit/types';
10-
import { folderPathToFilesRef, interpolateString } from '@tutorialkit/types';
1+
import type { ChapterSchema, Lesson, LessonSchema, PartSchema, Tutorial, TutorialSchema } from '@tutorialkit/types';
2+
import { interpolateString } from '@tutorialkit/types';
113
import { getCollection } from 'astro:content';
12-
import glob from 'fast-glob';
134
import path from 'node:path';
14-
import { IGNORED_FILES } from './constants';
155
import { DEFAULT_LOCALIZATION } from './content/default-localization';
166
import { squash } from './content/squash.js';
177
import { logger } from './logger';
188
import { joinPaths } from './url';
19-
20-
const CONTENT_DIR = path.join(process.cwd(), 'src/content/tutorial');
9+
import { getFilesRefList } from './content/files-ref';
2110

2211
export async function getTutorial(): Promise<Tutorial> {
2312
const collection = sortCollection(await getCollection('tutorial'));
@@ -331,24 +320,6 @@ function getSlug(entry: CollectionEntryTutorial) {
331320
return slug;
332321
}
333322

334-
async function getFilesRefList(pathToFolder: string): Promise<FilesRefList> {
335-
const root = path.join(CONTENT_DIR, pathToFolder);
336-
337-
const filePaths = (
338-
await glob(`${glob.convertPathToPattern(root)}/**/*`, {
339-
onlyFiles: true,
340-
ignore: IGNORED_FILES,
341-
dot: true,
342-
})
343-
).map((filePath) => `/${path.relative(root, filePath)}`);
344-
345-
filePaths.sort();
346-
347-
const filesRef = folderPathToFilesRef(pathToFolder);
348-
349-
return [filesRef, filePaths];
350-
}
351-
352323
interface CollectionEntryTutorial {
353324
id: string;
354325
slug: string;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { expect, test } from 'vitest';
2+
3+
import { getFilesRefList } from './files-ref';
4+
5+
test('getFilesRefList returns files', async () => {
6+
const files = await getFilesRefList('test/fixtures/files', '');
7+
8+
expect(files).toMatchInlineSnapshot(`
9+
[
10+
"test-fixtures-files.json",
11+
[
12+
"/first.js",
13+
"/nested/directory/second.ts",
14+
],
15+
]
16+
`);
17+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { FilesRefList } from '@tutorialkit/types';
2+
import { folderPathToFilesRef } from '@tutorialkit/types';
3+
import glob from 'fast-glob';
4+
import path from 'node:path';
5+
import { IGNORED_FILES } from '../constants';
6+
7+
const CONTENT_DIR = path.join(process.cwd(), 'src/content/tutorial');
8+
9+
export async function getFilesRefList(pathToFolder: string, base = CONTENT_DIR): Promise<FilesRefList> {
10+
const root = path.join(base, pathToFolder);
11+
12+
const filePaths = (
13+
await glob(`${glob.convertPathToPattern(root)}/**/*`, {
14+
onlyFiles: true,
15+
ignore: IGNORED_FILES,
16+
dot: true,
17+
})
18+
).map((filePath) => `/${path.relative(root, filePath).replaceAll(path.sep, '/')}`);
19+
20+
filePaths.sort();
21+
22+
const filesRef = folderPathToFilesRef(pathToFolder);
23+
24+
return [filesRef, filePaths];
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'Example JS file';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'Example TS file';

packages/cli/tests/__snapshots__/create-tutorial.test.ts.snap

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,58 @@ exports[`create a project 1`] = `
9494
]
9595
`;
9696

97+
exports[`create and build a project > built project file references 1`] = `
98+
{
99+
"1-basics-1-introduction-1-welcome-files.json": [
100+
"/src/index.js",
101+
"/src/test/bar.js",
102+
],
103+
"1-basics-1-introduction-1-welcome-solution.json": [
104+
"/src/index.js",
105+
],
106+
"1-basics-1-introduction-2-foo-files.json": [
107+
"/bar/styles.css",
108+
"/src/index.html",
109+
"/src/unicorn.js",
110+
"/src/windows_xp.png",
111+
],
112+
"1-basics-1-introduction-2-foo-solution.json": [
113+
"/src/index.html",
114+
],
115+
"1-basics-1-introduction-3-bar-files.json": [
116+
"/src/index.html",
117+
],
118+
"template-default.json": [
119+
"/package-lock.json",
120+
"/package.json",
121+
"/src/index.js",
122+
],
123+
"template-vite-app-2.json": [
124+
"/.gitignore",
125+
"/counter.js",
126+
"/foo.txt",
127+
"/index.html",
128+
"/javascript.svg",
129+
"/main.js",
130+
"/package-lock.json",
131+
"/package.json",
132+
"/public/vite.svg",
133+
"/style.css",
134+
],
135+
"template-vite-app.json": [
136+
"/.gitignore",
137+
"/counter.js",
138+
"/index.html",
139+
"/javascript.svg",
140+
"/main.js",
141+
"/package-lock.json",
142+
"/package.json",
143+
"/public/vite.svg",
144+
"/style.css",
145+
],
146+
}
147+
`;
148+
97149
exports[`create and build a project 1`] = `
98150
[
99151
"1-basics",
@@ -253,6 +305,7 @@ exports[`create and eject a project 1`] = `
253305
"src/utils/content",
254306
"src/utils/content.ts",
255307
"src/utils/content/default-localization.ts",
308+
"src/utils/content/files-ref.ts",
256309
"src/utils/content/squash.ts",
257310
"src/utils/logger.ts",
258311
"src/utils/nav.ts",

packages/cli/tests/create-tutorial.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { execa } from 'execa';
22
import fs from 'node:fs/promises';
3+
import { readFileSync } from 'node:fs';
34
import { tmpdir } from 'node:os';
45
import path from 'node:path';
56
import { afterAll, beforeAll, expect, test } from 'vitest';
@@ -67,6 +68,20 @@ test('create and build a project', async (context) => {
6768
const distFiles = await fs.readdir(path.join(dest, 'dist'), { recursive: true });
6869

6970
expect(distFiles.map(normaliseSlash).sort()).toMatchSnapshot();
71+
72+
// create snapshot of lesson, solution and template file reference JSONs
73+
const lessonJsons = distFiles.filter((file) => file.endsWith('-files.json'));
74+
const solutionJsons = distFiles.filter((file) => file.endsWith('-solution.json'));
75+
const templateJsons = distFiles.filter((file) => file.startsWith('template-') && file.endsWith('.json'));
76+
77+
const contents = [...lessonJsons, ...solutionJsons, ...templateJsons].reduce((jsons, current) => {
78+
const fileJson = JSON.parse(readFileSync(path.join(dest, 'dist', current), 'utf8'));
79+
const filenames = Object.keys(fileJson);
80+
81+
return { ...jsons, [current]: filenames };
82+
}, {});
83+
84+
expect(contents).toMatchSnapshot('built project file references');
7085
});
7186

7287
test('create and eject a project', async (context) => {

0 commit comments

Comments
 (0)