@@ -3,20 +3,51 @@ import { exec, exists } from '../node'
33
44const gitOrigin = 'coderoad'
55
6+ const stashAllFiles = async ( ) => {
7+ console . log ( 'stashAllFiles' )
8+ // stash files including untracked (eg. newly created file)
9+ const { stdout, stderr } = await exec ( `git stash --include-untracked` )
10+ if ( stderr ) {
11+ console . error ( stderr )
12+ throw new Error ( 'Error stashing files' )
13+ }
14+ }
15+
16+ const cherryPickCommit = async ( commit : string , count = 0 ) : Promise < void > => {
17+ if ( count > 1 ) {
18+ console . warn ( 'cherry-pick failed' )
19+ return
20+ }
21+ try {
22+ const { stdout } = await exec ( `git cherry-pick ${ commit } ` )
23+ if ( ! stdout ) {
24+ throw new Error ( 'No cherry-pick output' )
25+ }
26+ } catch ( error ) {
27+ console . log ( 'cherry-pick-commit failed' )
28+ // stash all files if cherry-pick fails
29+ await stashAllFiles ( )
30+ return cherryPickCommit ( commit , ++ count )
31+ }
32+ }
33+
34+ const errorMessages = {
35+ js : {
36+ 'node-gyp' : 'Error running npm setup command'
37+ }
38+ }
39+
640/*
741 SINGLE git cherry-pick %COMMIT%
8- MULTIPLE git cherry-pick %COMMIT_START%..%COMMIT_END%
9- if shell, run shell
42+ if fails, will stash all and retry
1043*/
1144export async function gitLoadCommits ( actions : CR . TutorialAction , dispatch : CR . EditorDispatch ) : Promise < void > {
1245 const { commits, commands, files } = actions
1346
1447 for ( const commit of commits ) {
15- const { stdout, stderr } = await exec ( `git cherry-pick ${ commit } ` )
16- if ( stderr ) {
17- console . error ( stderr )
18- throw new Error ( 'Error loading commit' )
19- }
48+ // pull a commit from tutorial repo
49+ console . log ( `try cherry-pick ${ commit } ` )
50+ await cherryPickCommit ( commit )
2051 }
2152
2253 if ( commands ) {
@@ -25,10 +56,12 @@ export async function gitLoadCommits(actions: CR.TutorialAction, dispatch: CR.Ed
2556 const { stdout, stderr } = await exec ( command )
2657 if ( stderr ) {
2758 console . error ( stderr )
28-
29- if ( stderr . match ( / n o d e - g y p / ) ) {
30- // ignored error
31- throw new Error ( 'Error running setup command' )
59+ // langauge specific error messages from running commands
60+ for ( const message of Object . keys ( errorMessages . js ) ) {
61+ if ( stderr . match ( message ) ) {
62+ // ignored error
63+ throw new Error ( 'Error running setup command' )
64+ }
3265 }
3366 }
3467 console . log ( `run command: ${ command } ` , stdout )
0 commit comments