1- import path from 'node:path ' ;
1+ import { execa } from 'execa ' ;
22import fs from 'node:fs/promises' ;
3+ import { tmpdir } from 'node:os' ;
4+ import path from 'node:path' ;
35import { afterAll , beforeAll , expect , test } from 'vitest' ;
4- import { execa } from 'execa' ;
56
6- const tmpDir = path . join ( __dirname , '.tmp' ) ;
7+ // on CI on windows we want to make sure to use the same drive, so we use a custom logic
8+ const tmpDir =
9+ process . platform === 'win32'
10+ ? path . join ( path . resolve ( __dirname , '../../../..' ) , '.tmp' )
11+ : await fs . mkdtemp ( path . join ( tmpdir ( ) , 'tk-test-' ) ) ;
712const baseDir = path . resolve ( __dirname , '../../..' ) ;
813
914const cli = path . join ( baseDir , 'packages/cli/dist/index.js' ) ;
@@ -14,7 +19,9 @@ beforeAll(async () => {
1419} ) ;
1520
1621afterAll ( async ( ) => {
17- await fs . rm ( tmpDir , { force : true , recursive : true } ) ;
22+ if ( process . platform !== 'win32' || ! process . env . CI ) {
23+ await fs . rm ( tmpDir , { force : true , recursive : true } ) ;
24+ }
1825} ) ;
1926
2027test ( 'cannot create project without installing but with starting' , async ( context ) => {
@@ -23,9 +30,6 @@ test('cannot create project without installing but with starting', async (contex
2330 await expect (
2431 execa ( 'node' , [ cli , 'create' , name , '--no-install' , '--start' ] , {
2532 cwd : tmpDir ,
26- env : {
27- TK_DIRECTORY : baseDir ,
28- } ,
2933 } ) ,
3034 ) . rejects . toThrow ( 'Cannot start project without installing dependencies.' ) ;
3135} ) ;
@@ -36,9 +40,6 @@ test('create a project', async (context) => {
3640
3741 await execa ( 'node' , [ cli , 'create' , name , '--no-install' , '--no-git' , '--defaults' ] , {
3842 cwd : tmpDir ,
39- env : {
40- TK_DIRECTORY : baseDir ,
41- } ,
4243 } ) ;
4344
4445 const projectFiles = await fs . readdir ( dest , { recursive : true } ) ;
@@ -50,14 +51,13 @@ test('create and build a project', async (context) => {
5051 const name = context . task . id ;
5152 const dest = path . join ( tmpDir , name ) ;
5253
53- await execa ( 'node' , [ cli , 'create' , name , '--no-git' , '--no-start' , '--defaults' ] , {
54+ await execa ( 'node' , [ cli , 'create' , name , '--no-git' , '--no-install' , '--no- start', '--defaults' ] , {
5455 cwd : tmpDir ,
55- env : {
56- TK_DIRECTORY : baseDir ,
57- } ,
5856 } ) ;
5957
60- await execa ( 'npm' , [ 'run' , 'build' ] , {
58+ await runPnpmInstall ( dest , baseDir ) ;
59+
60+ await execa ( 'pnpm' , [ 'run' , 'build' ] , {
6161 cwd : dest ,
6262 } ) ;
6363
@@ -73,24 +73,25 @@ test('create and eject a project', async (context) => {
7373 const name = context . task . id ;
7474 const dest = path . join ( tmpDir , name ) ;
7575
76- await execa ( 'node' , [ cli , 'create' , name , '--no-git' , '--no-start' , '--defaults' ] , {
76+ await execa ( 'node' , [ cli , 'create' , name , '--no-git' , '--no-install' , '--no- start', '--defaults' ] , {
7777 cwd : tmpDir ,
78- env : {
79- TK_DIRECTORY : baseDir ,
80- } ,
8178 } ) ;
8279
80+ await runPnpmInstall ( dest , baseDir ) ;
81+
8382 await execa ( 'node' , [ cli , 'eject' , name , '--force' , '--defaults' ] , {
8483 cwd : tmpDir ,
85- env : {
86- TK_DIRECTORY : baseDir ,
87- } ,
8884 } ) ;
8985
90- // remove `node_modules` before taking the snapshot
91- await fs . rm ( path . join ( dest , 'node_modules' ) , { force : true , recursive : true } ) ;
86+ if ( process . platform !== 'win32' ) {
87+ await fs . rm ( path . join ( dest , 'node_modules' ) , { force : true , recursive : true , maxRetries : 5 } ) ;
88+ }
9289
93- const projectFiles = await fs . readdir ( dest , { recursive : true } ) ;
90+ let projectFiles = await fs . readdir ( dest , { recursive : true } ) ;
91+
92+ if ( process . platform === 'win32' ) {
93+ projectFiles = projectFiles . filter ( ( filePath ) => ! filePath . startsWith ( 'node_modules' ) ) ;
94+ }
9495
9596 expect ( projectFiles . map ( normaliseSlash ) . sort ( ) ) . toMatchSnapshot ( ) ;
9697 expect ( await fs . readFile ( path . join ( dest , 'astro.config.ts' ) , 'utf-8' ) ) . toMatchSnapshot ( ) ;
@@ -100,25 +101,21 @@ test('create, eject and build a project', async (context) => {
100101 const name = context . task . id ;
101102 const dest = path . join ( tmpDir , name ) ;
102103
103- await execa ( 'node' , [ cli , 'create' , name , '--no-git' , '--no-start' , '--defaults' ] , {
104+ await execa ( 'node' , [ cli , 'create' , name , '--no-git' , '--no-install' , '--no- start', '--defaults' ] , {
104105 cwd : tmpDir ,
105- env : {
106- TK_DIRECTORY : baseDir ,
107- } ,
108106 } ) ;
109107
108+ await runPnpmInstall ( dest , baseDir ) ;
109+
110110 await execa ( 'node' , [ cli , 'eject' , name , '--force' , '--defaults' ] , {
111111 cwd : tmpDir ,
112- env : {
113- TK_DIRECTORY : baseDir ,
114- } ,
115112 } ) ;
116113
117- await execa ( 'npm ' , [ 'install' ] , {
114+ await execa ( 'pnpm ' , [ 'install' , '--no-frozen-lockfile '] , {
118115 cwd : dest ,
119116 } ) ;
120117
121- await execa ( 'npm ' , [ 'run' , 'build' ] , {
118+ await execa ( 'pnpm ' , [ 'run' , 'build' ] , {
122119 cwd : dest ,
123120 } ) ;
124121
@@ -139,9 +136,6 @@ test('cannot eject on an empty folder', async (context) => {
139136 await expect (
140137 execa ( 'node' , [ cli , 'eject' , name , '--force' , '--defaults' ] , {
141138 cwd : tmpDir ,
142- env : {
143- TK_DIRECTORY : baseDir ,
144- } ,
145139 } ) ,
146140 ) . rejects . toThrow ( 'package.json does not exists!' ) ;
147141} ) ;
@@ -157,9 +151,6 @@ test('cannot eject on a node project that is not an Astro project', async (conte
157151 await expect (
158152 execa ( 'node' , [ cli , 'eject' , name , '--force' , '--defaults' ] , {
159153 cwd : tmpDir ,
160- env : {
161- TK_DIRECTORY : baseDir ,
162- } ,
163154 } ) ,
164155 ) . rejects . toThrow ( 'astro.config.ts does not exists!' ) ;
165156} ) ;
@@ -188,9 +179,6 @@ test('cannot eject on an astro project that is not using TutorialKit', async (co
188179 await expect (
189180 execa ( 'node' , [ cli , 'eject' , name , '--force' , '--defaults' ] , {
190181 cwd : tmpDir ,
191- env : {
192- TK_DIRECTORY : baseDir ,
193- } ,
194182 } ) ,
195183 ) . rejects . toThrow ( `@tutorialkit${ path . sep } astro does not exists!` ) ;
196184} ) ;
@@ -216,20 +204,39 @@ test('cannot eject on an astro project that is not using TutorialKit 2', async (
216204 ` ,
217205 ) ;
218206
219- await execa ( 'npm ' , [ 'install' ] , {
207+ await execa ( 'pnpm ' , [ 'install' ] , {
220208 cwd : dest ,
221209 } ) ;
222210
223211 await expect (
224212 execa ( 'node' , [ cli , 'eject' , name , '--force' , '--defaults' ] , {
225213 cwd : tmpDir ,
226- env : {
227- TK_DIRECTORY : baseDir ,
228- } ,
229214 } ) ,
230215 ) . rejects . toThrow ( `Could not find import to '@tutorialkit/astro'` ) ;
231216} ) ;
232217
233218function normaliseSlash ( filePath : string ) {
234219 return filePath . replace ( / \\ / g, '/' ) ;
235220}
221+
222+ async function runPnpmInstall ( dest : string , baseDir : string ) {
223+ const packageJsonPath = path . join ( dest , 'package.json' ) ;
224+ const packageJson = JSON . parse ( await fs . readFile ( packageJsonPath , 'utf-8' ) ) ;
225+
226+ packageJson . pnpm = {
227+ overrides : {
228+ '@astrojs/language-server' : '2.11.1' ,
229+ '@tutorialkit/astro' : `file:${ baseDir } /packages/astro` ,
230+ '@tutorialkit/components-react' : `file:${ baseDir } /packages/components/react` ,
231+ '@tutorialkit/runtime' : `file:${ baseDir } /packages/runtime` ,
232+ '@tutorialkit/theme' : `file:${ baseDir } /packages/theme` ,
233+ '@tutorialkit/types' : `file:${ baseDir } /packages/types` ,
234+ } ,
235+ } ;
236+
237+ await fs . writeFile ( packageJsonPath , JSON . stringify ( packageJson , undefined , 2 ) , 'utf8' ) ;
238+
239+ await execa ( 'pnpm' , [ 'install' ] , {
240+ cwd : dest ,
241+ } ) ;
242+ }
0 commit comments