@@ -3,20 +3,51 @@ import { exec, exists } from '../node'
3
3
4
4
const gitOrigin = 'coderoad'
5
5
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
+
6
40
/*
7
41
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
10
43
*/
11
44
export async function gitLoadCommits ( actions : CR . TutorialAction , dispatch : CR . EditorDispatch ) : Promise < void > {
12
45
const { commits, commands, files } = actions
13
46
14
47
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 )
20
51
}
21
52
22
53
if ( commands ) {
@@ -25,10 +56,12 @@ export async function gitLoadCommits(actions: CR.TutorialAction, dispatch: CR.Ed
25
56
const { stdout, stderr } = await exec ( command )
26
57
if ( stderr ) {
27
58
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
+ }
32
65
}
33
66
}
34
67
console . log ( `run command: ${ command } ` , stdout )
0 commit comments