1- import { existsSync } from "fs" ;
2- import { copyFile , mkdir , readdir , readFile , writeFile } from "fs/promises" ;
3- import { dirname , join } from "path" ;
1+ import { existsSync } from "node:fs" ;
2+ import {
3+ copyFile ,
4+ mkdir ,
5+ mkdtemp ,
6+ readdir ,
7+ readFile ,
8+ rm ,
9+ writeFile ,
10+ } from "node:fs/promises" ;
11+ import { tmpdir } from "node:os" ;
12+ import { dirname , join } from "node:path" ;
413import { tutorialConfig } from "../tutorial.config" ;
514import {
615 getCurrentBranch ,
@@ -24,6 +33,7 @@ export async function sync() {
2433 if ( ! currentStage ) {
2534 throw new Error ( "Current branch is not a tutorial stage" ) ;
2635 }
36+ const preservedUntrackedFiles = await preserveUntrackedFiles ( ) ;
2737
2838 // get the current commit message
2939 const commitMessage = await getCurrentCommitMessage ( ) ;
@@ -36,31 +46,39 @@ export async function sync() {
3646 ( s ) => s . order > currentStage . order ,
3747 ) ;
3848
39- for ( const stage of previousStages ) {
40- await switchBranch ( stage . name ) ;
41- await pullToThisStage ( currentBranch ) ;
42- const futurePaths = getFuturePathsFrom ( stage . name ) ;
43- await removeFuturePaths ( futurePaths ) ;
44- await commitAllChanges ( commitMessage ) ;
45- }
49+ try {
50+ for ( const stage of previousStages ) {
51+ await switchBranch ( stage . name ) ;
52+ await pullToThisStage ( currentBranch ) ;
53+ const futurePaths = getFuturePathsFrom ( stage . name ) ;
54+ await removeFuturePaths ( futurePaths ) ;
55+ await commitAllChanges ( commitMessage ) ;
56+ }
4657
47- for ( const stage of nextStages ) {
48- await switchBranch ( stage . name ) ;
49- await pullToThisStage ( currentBranch ) ;
50- const futurePaths = getFuturePathsFrom ( stage . name ) ;
51- await restoreFuturePaths ( futurePaths ) ;
52- await commitAllChanges ( commitMessage ) ;
53- }
58+ for ( const stage of nextStages ) {
59+ await switchBranch ( stage . name ) ;
60+ await pullToThisStage ( currentBranch ) ;
61+ const futurePaths = getFuturePathsFrom ( stage . name ) ;
62+ await restoreFuturePaths ( futurePaths ) ;
63+ await commitAllChanges ( commitMessage ) ;
64+ }
5465
55- if ( currentBranch !== "main" ) {
56- await switchBranch ( "main" ) ;
57- await pullToThisStage ( currentBranch ) ;
58- await commitAllChanges ( commitMessage ) ;
59- }
66+ if ( currentBranch !== "main" ) {
67+ await switchBranch ( "main" ) ;
68+ await pullToThisStage ( currentBranch ) ;
69+ await commitAllChanges ( commitMessage ) ;
70+ }
6071
61- await switchBranch ( currentBranch ) ;
72+ await switchBranch ( currentBranch ) ;
6273
63- await syncDocsContent ( ) ;
74+ await syncDocsContent ( ) ;
75+ } finally {
76+ if ( ( await getCurrentBranch ( ) ) !== currentBranch ) {
77+ await switchBranch ( currentBranch ) ;
78+ }
79+
80+ await restorePreservedUntrackedFiles ( preservedUntrackedFiles ) ;
81+ }
6482
6583 // for stages before this one
6684
@@ -86,6 +104,59 @@ export async function sync() {
86104 // git restore --source=current-stage -- .
87105}
88106
107+ type PreservedUntrackedFiles = {
108+ backupRoot : string ;
109+ files : string [ ] ;
110+ } | null ;
111+
112+ async function preserveUntrackedFiles ( ) : Promise < PreservedUntrackedFiles > {
113+ const files = await getUntrackedFiles ( ) ;
114+ if ( files . length === 0 ) {
115+ return null ;
116+ }
117+
118+ const backupRoot = await mkdtemp ( join ( tmpdir ( ) , "tutorial-sync-untracked-" ) ) ;
119+
120+ for ( const file of files ) {
121+ if ( ! existsSync ( file ) ) {
122+ continue ;
123+ }
124+
125+ const targetPath = join ( backupRoot , file ) ;
126+ await mkdir ( dirname ( targetPath ) , { recursive : true } ) ;
127+ await copyFile ( file , targetPath ) ;
128+ await rm ( file , { force : true } ) ;
129+ }
130+
131+ return { backupRoot, files } ;
132+ }
133+
134+ async function restorePreservedUntrackedFiles (
135+ preservedFiles : PreservedUntrackedFiles ,
136+ ) : Promise < void > {
137+ if ( ! preservedFiles ) {
138+ return ;
139+ }
140+
141+ try {
142+ for ( const file of preservedFiles . files ) {
143+ if ( existsSync ( file ) ) {
144+ continue ;
145+ }
146+
147+ const sourcePath = join ( preservedFiles . backupRoot , file ) ;
148+ if ( ! existsSync ( sourcePath ) ) {
149+ continue ;
150+ }
151+
152+ await mkdir ( dirname ( file ) , { recursive : true } ) ;
153+ await copyFile ( sourcePath , file ) ;
154+ }
155+ } finally {
156+ await rm ( preservedFiles . backupRoot , { recursive : true , force : true } ) ;
157+ }
158+ }
159+
89160export async function syncDocsContent ( ) {
90161 console . log ( "Syncing tutorial content to docs..." ) ;
91162
@@ -227,28 +298,45 @@ export async function pullToThisStage(fromStageBranch: string): Promise<void> {
227298}
228299
229300export async function commitAllChanges ( message : string ) : Promise < void > {
230- // Check if there are any changes to commit first
231- const checkProc = Bun . spawn ( [ "git" , "diff" , "--quiet" ] , {
301+ const addTrackedProc = Bun . spawn ( [ "git" , "add" , "-u" ] , {
232302 stdout : "pipe" ,
233303 stderr : "pipe" ,
234304 } ) ;
235- await checkProc . exited ;
305+ await addTrackedProc . exited ;
236306
237- // If exit code is 0, there are no changes, so return early
238- if ( checkProc . exitCode === 0 ) {
239- return ;
307+ if ( addTrackedProc . exitCode !== 0 ) {
308+ const error = await new Response ( addTrackedProc . stderr ) . text ( ) ;
309+ throw new Error ( `Failed to add tracked changes: ${ error } ` ) ;
240310 }
241311
242- // git add -A
243- const proc = Bun . spawn ( [ "git" , "add" , "-A" ] , {
312+ const declaredUntrackedFiles = ( await getUntrackedFiles ( ) ) . filter (
313+ isDeclaredTutorialPath ,
314+ ) ;
315+ if ( declaredUntrackedFiles . length > 0 ) {
316+ const addUntrackedProc = Bun . spawn (
317+ [ "git" , "add" , "--" , ...declaredUntrackedFiles ] ,
318+ {
319+ stdout : "pipe" ,
320+ stderr : "pipe" ,
321+ } ,
322+ ) ;
323+ await addUntrackedProc . exited ;
324+
325+ if ( addUntrackedProc . exitCode !== 0 ) {
326+ const error = await new Response ( addUntrackedProc . stderr ) . text ( ) ;
327+ throw new Error ( `Failed to add declared untracked paths: ${ error } ` ) ;
328+ }
329+ }
330+
331+ const checkProc = Bun . spawn ( [ "git" , "diff" , "--cached" , "--quiet" ] , {
244332 stdout : "pipe" ,
245333 stderr : "pipe" ,
246334 } ) ;
247- await proc . exited ;
335+ await checkProc . exited ;
248336
249- if ( proc . exitCode !== 0 ) {
250- const error = await new Response ( proc . stderr ) . text ( ) ;
251- throw new Error ( `Failed to add all changes: ${ error } ` ) ;
337+ // If exit code is 0, there are no staged changes, so return early.
338+ if ( checkProc . exitCode === 0 ) {
339+ return ;
252340 }
253341
254342 // git commit -m "Sync with previous stage"
@@ -264,3 +352,36 @@ export async function commitAllChanges(message: string): Promise<void> {
264352 throw new Error ( `Failed to commit all changes: ${ error } ` ) ;
265353 }
266354}
355+
356+ async function getUntrackedFiles ( ) : Promise < string [ ] > {
357+ const proc = Bun . spawn (
358+ [ "git" , "ls-files" , "--others" , "--exclude-standard" , "-z" ] ,
359+ {
360+ stdout : "pipe" ,
361+ stderr : "pipe" ,
362+ } ,
363+ ) ;
364+
365+ const result = await new Response ( proc . stdout ) . text ( ) ;
366+ await proc . exited ;
367+
368+ if ( proc . exitCode !== 0 ) {
369+ const error = await new Response ( proc . stderr ) . text ( ) ;
370+ throw new Error ( `Failed to list untracked files: ${ error } ` ) ;
371+ }
372+
373+ return result . split ( "\0" ) . filter ( Boolean ) ;
374+ }
375+
376+ function isDeclaredTutorialPath ( path : string ) : boolean {
377+ return tutorialConfig . stages . some ( ( stage ) =>
378+ stage . newPaths . some (
379+ ( newPath ) =>
380+ path === newPath || path . startsWith ( withTrailingSlash ( newPath ) ) ,
381+ ) ,
382+ ) ;
383+ }
384+
385+ function withTrailingSlash ( path : string ) : string {
386+ return path . endsWith ( "/" ) ? path : `${ path } /` ;
387+ }
0 commit comments