Skip to content

Commit eb6cf09

Browse files
test(cli): add test for creating and building a project (#60)
1 parent 79cb18d commit eb6cf09

File tree

19 files changed

+1094
-236
lines changed

19 files changed

+1094
-236
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.github/workflows/ci.yaml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,32 @@ on:
77
jobs:
88
test:
99
name: Test
10-
runs-on: ubuntu-latest
10+
runs-on: ${{ matrix.os }}
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
os: [ubuntu-latest]
15+
node-version: [18, 20, 22]
16+
include:
17+
- os: macos-latest
18+
node_version: 20
19+
- os: windows-latest
20+
node_version: 20.13.1 # 20.14.0 keeps causing a native `node::SetCppgcReference+18123` error in Vitest
1121
steps:
22+
- uses: actions/setup-node@v4
23+
with:
24+
node-version: ${{ matrix.node-version }}
1225
- name: Setup
1326
uses: pnpm/action-setup@v4
1427
with:
1528
version: 8
1629
- name: Checkout
17-
uses: actions/checkout@v3
30+
uses: actions/checkout@v4
1831
- name: Install dependencies
1932
run: pnpm install
2033
- name: Lint
2134
run: pnpm prettier --check .
2235
- name: Build
2336
run: pnpm build
37+
- name: Test
38+
run: pnpm test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ dist-ssr
2020
.pnpm-store
2121
/tutorialkit/template
2222
tsconfig.tsbuildinfo
23+
.tmp

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"build": "pnpm run --filter=@tutorialkit/* --filter=tutorialkit --filter=create-tutorial build",
55
"template:dev": "TUTORIALKIT_DEV=true pnpm run build && pnpm run --filter=tutorialkit-starter dev",
66
"template:build": "pnpm run build && pnpm run --filter=tutorialkit-starter build",
7+
"test": "pnpm run --filter=tutorialkit test",
78
"prepare": "is-ci || husky install",
89
"clean": "./scripts/clean.sh"
910
},

packages/astro/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,16 @@
4747
"unocss": "^0.59.4",
4848
"zod": "3.23.8"
4949
},
50-
"peerDependencies": {
51-
"astro": "^4.8.6"
52-
},
5350
"devDependencies": {
5451
"@tutorialkit/types": "workspace:*",
5552
"@types/mdast": "^4.0.3",
5653
"esbuild": "^0.20.2",
5754
"esbuild-node-externals": "^1.13.0",
55+
"execa": "^9.2.0",
5856
"typescript": "^5.4.5",
5957
"vite-plugin-inspect": "0.8.4"
58+
},
59+
"peerDependencies": {
60+
"astro": "^4.10.2"
6061
}
6162
}

packages/astro/scripts/build.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import assert from 'node:assert';
22
import { existsSync } from 'node:fs';
3-
import { spawnSync } from 'node:child_process';
43
import { cp, rm } from 'node:fs/promises';
4+
import { execa } from 'execa';
55
import esbuild from 'esbuild';
66
import { nodeExternalsPlugin } from 'esbuild-node-externals';
77

88
// clean dist
99
await rm('dist', { recursive: true, force: true });
1010

1111
// only do typechecking and emit the type declarations with tsc
12-
spawnSync('tsc', ['--emitDeclarationOnly', '--project', './tsconfig.build.json'], { stdio: 'inherit' });
12+
execa('tsc', ['--emitDeclarationOnly', '--project', './tsconfig.build.json'], {
13+
stdio: 'inherit',
14+
preferLocal: true,
15+
});
1316

1417
// build with esbuild
1518
esbuild.build({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ async function getFilesRef(pathToFolder: string): Promise<FilesRef> {
222222
const root = path.join(CONTENT_DIR, pathToFolder);
223223

224224
const filePaths = (
225-
await glob(`${root}/**/*`, {
225+
await glob(`${glob.convertPathToPattern(root)}/**/*`, {
226226
onlyFiles: true,
227227
})
228228
).map((filePath) => `/${path.relative(root, filePath)}`);

packages/astro/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ export default function createPlugin({ defaultRoutes = true, isolation, enterpri
117117
_config = config;
118118
},
119119
'astro:server:setup'(options) {
120+
if (!_config) {
121+
return;
122+
}
123+
120124
const { server, logger } = options;
121125
const projectRoot = fileURLToPath(_config.root);
122126

packages/astro/src/webcontainer-files.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export class WebContainerFiles {
2020
const cache = new FileMapCache(logger, server, { contentDir, templatesDir });
2121

2222
this._watcher = watch([
23-
`${contentDir}/**/${FILES_FOLDER_NAME}/**/*`,
24-
`${contentDir}/**/${SOLUTION_FOLDER_NAME}/**/*`,
23+
path.join(contentDir, `**/${FILES_FOLDER_NAME}/**/*`),
24+
path.join(contentDir, `**/${SOLUTION_FOLDER_NAME}/**/*`),
2525
templatesDir,
2626
]);
2727

@@ -57,12 +57,18 @@ export class WebContainerFiles {
5757
const { contentDir, templatesDir } = this._folders(projectRoot);
5858

5959
const folders = await glob(
60-
[`${contentDir}/**/${FILES_FOLDER_NAME}`, `${contentDir}/**/${SOLUTION_FOLDER_NAME}`, `${templatesDir}/*`],
60+
[
61+
`${glob.convertPathToPattern(contentDir)}/**/${FILES_FOLDER_NAME}`,
62+
`${glob.convertPathToPattern(contentDir)}/**/${SOLUTION_FOLDER_NAME}`,
63+
`${glob.convertPathToPattern(templatesDir)}/*`,
64+
],
6165
{ onlyDirectories: true },
6266
);
6367

6468
await Promise.all(
6569
folders.map(async (folder) => {
70+
folder = path.normalize(folder);
71+
6672
const fileRef = getFilesRef(folder, { contentDir, templatesDir });
6773
const dest = fileURLToPath(new URL(fileRef, dir));
6874

@@ -198,7 +204,7 @@ class FileMapCache {
198204
}
199205

200206
async function createFileMap(dir: string) {
201-
const filePaths = await glob(`${dir}/**/*`, {
207+
const filePaths = await glob(`${glob.convertPathToPattern(dir)}/**/*`, {
202208
onlyFiles: true,
203209
});
204210

@@ -261,5 +267,5 @@ function getFilesRef(pathToFolder: string, { contentDir, templatesDir }: Content
261267
pathToFolder = 'template' + pathToFolder.slice(templatesDir.length);
262268
}
263269

264-
return encodeURIComponent(pathToFolder.replaceAll('/', '-').replaceAll('_', '')) + '.json';
270+
return encodeURIComponent(pathToFolder.replaceAll(/[\/\\]+/g, '-').replaceAll('_', '')) + '.json';
265271
}

packages/cli/package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616
"scripts": {
1717
"build": "node scripts/build.js",
1818
"prepack": "node scripts/pre-pack.js",
19-
"postpack": "node scripts/cleanup.js"
19+
"postpack": "node scripts/cleanup.js",
20+
"test": "vitest --testTimeout=180000"
2021
},
2122
"files": [
2223
"dist",
2324
"template",
2425
"template/.gitignore"
2526
],
2627
"dependencies": {
27-
"@babel/parser": "7.24.5",
2828
"@babel/generator": "7.24.5",
29+
"@babel/parser": "7.24.5",
2930
"@babel/traverse": "7.24.5",
3031
"@babel/types": "7.24.5",
3132
"@clack/prompts": "^0.7.0",
@@ -42,7 +43,9 @@
4243
"@types/yargs-parser": "^21.0.3",
4344
"esbuild": "^0.20.2",
4445
"esbuild-node-externals": "^1.13.0",
45-
"fs-extra": "^11.2.0"
46+
"execa": "^9.2.0",
47+
"fs-extra": "^11.2.0",
48+
"vitest": "^1.6.0"
4649
},
4750
"engines": {
4851
"node": ">=18.18.0"

0 commit comments

Comments
 (0)