Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- main
pull_request:
workflow_dispatch:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is useful for contributors. It allows them (and me) to run CI manually on specific branch on forks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's a really nice addition! 😃


jobs:
test:
Expand Down
35 changes: 3 additions & 32 deletions packages/astro/src/default/utils/content.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
import type {
ChapterSchema,
FilesRefList,
Lesson,
LessonSchema,
PartSchema,
Tutorial,
TutorialSchema,
} from '@tutorialkit/types';
import { folderPathToFilesRef, interpolateString } from '@tutorialkit/types';
import type { ChapterSchema, Lesson, LessonSchema, PartSchema, Tutorial, TutorialSchema } from '@tutorialkit/types';
import { interpolateString } from '@tutorialkit/types';
import { getCollection } from 'astro:content';
import glob from 'fast-glob';
import path from 'node:path';
import { IGNORED_FILES } from './constants';
import { DEFAULT_LOCALIZATION } from './content/default-localization';
import { squash } from './content/squash.js';
import { logger } from './logger';
import { joinPaths } from './url';

const CONTENT_DIR = path.join(process.cwd(), 'src/content/tutorial');
import { getFilesRefList } from './content/files-ref';

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

async function getFilesRefList(pathToFolder: string): Promise<FilesRefList> {
const root = path.join(CONTENT_DIR, pathToFolder);

const filePaths = (
await glob(`${glob.convertPathToPattern(root)}/**/*`, {
onlyFiles: true,
ignore: IGNORED_FILES,
dot: true,
})
).map((filePath) => `/${path.relative(root, filePath)}`);

filePaths.sort();

const filesRef = folderPathToFilesRef(pathToFolder);

return [filesRef, filePaths];
}

interface CollectionEntryTutorial {
id: string;
slug: string;
Expand Down
17 changes: 17 additions & 0 deletions packages/astro/src/default/utils/content/files-ref.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect, test } from 'vitest';

import { getFilesRefList } from './files-ref';

test('getFilesRefList returns files', async () => {
const files = await getFilesRefList('test/fixtures/files', '');

expect(files).toMatchInlineSnapshot(`
[
"test-fixtures-files.json",
[
"/first.js",
"/nested/directory/second.ts",
],
]
`);
});
Comment on lines +5 to +17
Copy link
Member Author

@AriPerkkio AriPerkkio Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failure on Windows CI before fix:

 ❯ src/default/utils/content/files-ref.spec.ts  (1 test | 1 failed) 26ms
   ❯ src/default/utils/content/files-ref.spec.ts > getFilesRefList returns files
     → Snapshot `getFilesRefList returns files 1` mismatched
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
 FAIL  src/default/utils/content/files-ref.spec.ts > getFilesRefList returns files
Error: Snapshot `getFilesRefList returns files 1` mismatched
- Expected
+ Received
  [
    "test-fixtures-files.json",
    [
      "/first.js",
-     "/nested/directory/second.ts",
+     "/nested\directory\second.ts",
    ],
  ]

https://github.com/stackblitz/tutorialkit/actions/runs/10287876681/job/28471979082?pr=225

25 changes: 25 additions & 0 deletions packages/astro/src/default/utils/content/files-ref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { FilesRefList } from '@tutorialkit/types';
import { folderPathToFilesRef } from '@tutorialkit/types';
import glob from 'fast-glob';
import path from 'node:path';
import { IGNORED_FILES } from '../constants';

const CONTENT_DIR = path.join(process.cwd(), 'src/content/tutorial');

export async function getFilesRefList(pathToFolder: string, base = CONTENT_DIR): Promise<FilesRefList> {
const root = path.join(base, pathToFolder);

const filePaths = (
await glob(`${glob.convertPathToPattern(root)}/**/*`, {
onlyFiles: true,
ignore: IGNORED_FILES,
dot: true,
})
).map((filePath) => `/${path.relative(root, filePath).replaceAll(path.sep, '/')}`);

filePaths.sort();

const filesRef = folderPathToFilesRef(pathToFolder);

return [filesRef, filePaths];
}
1 change: 1 addition & 0 deletions packages/astro/test/fixtures/files/first.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'Example JS file';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'Example TS file';
53 changes: 53 additions & 0 deletions packages/cli/tests/__snapshots__/create-tutorial.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,58 @@ exports[`create a project 1`] = `
]
`;

exports[`create and build a project > built project file references 1`] = `
{
"1-basics-1-introduction-1-welcome-files.json": [
"/src/index.js",
"/src/test/bar.js",
],
"1-basics-1-introduction-1-welcome-solution.json": [
"/src/index.js",
],
"1-basics-1-introduction-2-foo-files.json": [
"/bar/styles.css",
"/src/index.html",
"/src/unicorn.js",
"/src/windows_xp.png",
],
"1-basics-1-introduction-2-foo-solution.json": [
"/src/index.html",
],
"1-basics-1-introduction-3-bar-files.json": [
"/src/index.html",
],
"template-default.json": [
"/package-lock.json",
"/package.json",
"/src/index.js",
],
"template-vite-app-2.json": [
"/.gitignore",
"/counter.js",
"/foo.txt",
"/index.html",
"/javascript.svg",
"/main.js",
"/package-lock.json",
"/package.json",
"/public/vite.svg",
"/style.css",
],
"template-vite-app.json": [
"/.gitignore",
"/counter.js",
"/index.html",
"/javascript.svg",
"/main.js",
"/package-lock.json",
"/package.json",
"/public/vite.svg",
"/style.css",
],
}
`;

exports[`create and build a project 1`] = `
[
"1-basics",
Expand Down Expand Up @@ -253,6 +305,7 @@ exports[`create and eject a project 1`] = `
"src/utils/content",
"src/utils/content.ts",
"src/utils/content/default-localization.ts",
"src/utils/content/files-ref.ts",
"src/utils/content/squash.ts",
"src/utils/logger.ts",
"src/utils/nav.ts",
Expand Down
15 changes: 15 additions & 0 deletions packages/cli/tests/create-tutorial.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { execa } from 'execa';
import fs from 'node:fs/promises';
import { readFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { afterAll, beforeAll, expect, test } from 'vitest';
Expand Down Expand Up @@ -67,6 +68,20 @@ test('create and build a project', async (context) => {
const distFiles = await fs.readdir(path.join(dest, 'dist'), { recursive: true });

expect(distFiles.map(normaliseSlash).sort()).toMatchSnapshot();

// create snapshot of lesson, solution and template file reference JSONs
const lessonJsons = distFiles.filter((file) => file.endsWith('-files.json'));
const solutionJsons = distFiles.filter((file) => file.endsWith('-solution.json'));
const templateJsons = distFiles.filter((file) => file.startsWith('template-') && file.endsWith('.json'));

const contents = [...lessonJsons, ...solutionJsons, ...templateJsons].reduce((jsons, current) => {
const fileJson = JSON.parse(readFileSync(path.join(dest, 'dist', current), 'utf8'));
const filenames = Object.keys(fileJson);

return { ...jsons, [current]: filenames };
}, {});

expect(contents).toMatchSnapshot('built project file references');
});

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