@@ -4,7 +4,7 @@ import { CreateFileFromTemplate } from '#functions/CreateFileFromTemplate';
4
4
import { fileExists } from '#functions/FileExists' ;
5
5
import { PromptNew , PromptNewObjectKeys } from '#prompts/PromptNew' ;
6
6
import { Spinner } from '@favware/colorette-spinner' ;
7
- import { fromAsync , isErr , isOk } from '@sapphire/result' ;
7
+ import { Result } from '@sapphire/result' ;
8
8
import { blueBright , red } from 'colorette' ;
9
9
import { execa } from 'execa' ;
10
10
import { cp , readFile , rm , writeFile } from 'node:fs/promises' ;
@@ -18,24 +18,18 @@ async function editPackageJson(location: string, name: string) {
18
18
19
19
output . name = name ;
20
20
21
- const result = await fromAsync ( ( ) => writeFile ( pjLocation , JSON . stringify ( output , null , 2 ) ) ) ;
21
+ const result = await Result . fromAsync ( ( ) => writeFile ( pjLocation , JSON . stringify ( output , null , 2 ) ) ) ;
22
22
23
- return isOk ( result ) ;
23
+ return result . isOk ( ) ;
24
24
}
25
25
26
26
async function installDeps ( location : string , pm : string , verbose : boolean ) {
27
- const result = await fromAsync ( ( ) =>
28
- execa ( pm . toLowerCase ( ) , [ 'install' ] , {
29
- stdio : verbose ? 'inherit' : undefined ,
30
- cwd : `./${ location } /`
31
- } )
32
- ) ;
33
-
34
- if ( isErr ( result ) ) {
35
- throw result . error ;
36
- }
27
+ const value = await execa ( pm . toLowerCase ( ) , [ 'install' ] , {
28
+ stdio : verbose ? 'inherit' : undefined ,
29
+ cwd : `./${ location } /`
30
+ } ) ;
37
31
38
- if ( result . value . exitCode !== 0 ) {
32
+ if ( value . exitCode !== 0 ) {
39
33
throw new Error ( 'An unknown error occurred while installing the dependencies. Try running Sapphire CLI with "--verbose" flag.' ) ;
40
34
}
41
35
@@ -50,23 +44,16 @@ async function installDeps(location: string, pm: string, verbose: boolean) {
50
44
51
45
async function configureYarnRc ( location : string , name : string , value : string ) {
52
46
await execa ( 'yarn' , [ 'config' , 'set' , name , value ] , { cwd : `./${ location } /` } ) ;
53
-
54
47
return true ;
55
48
}
56
49
57
50
async function installYarnV3 ( location : string , verbose : boolean ) {
58
- const result = await fromAsync ( ( ) =>
59
- execa ( 'yarn' , [ 'set' , 'version' , 'berry' ] , {
60
- stdio : verbose ? 'inherit' : undefined ,
61
- cwd : `./${ location } /`
62
- } )
63
- ) ;
64
-
65
- if ( isErr ( result ) ) {
66
- throw result . error ;
67
- }
51
+ const value = await execa ( 'yarn' , [ 'set' , 'version' , 'berry' ] , {
52
+ stdio : verbose ? 'inherit' : undefined ,
53
+ cwd : `./${ location } /`
54
+ } ) ;
68
55
69
- if ( result . value . exitCode !== 0 ) {
56
+ if ( value . exitCode !== 0 ) {
70
57
throw new Error ( 'An unknown error occurred while installing Yarn v3. Try running Sapphire CLI with "--verbose" flag.' ) ;
71
58
}
72
59
@@ -81,43 +68,34 @@ async function installYarnV3(location: string, verbose: boolean) {
81
68
82
69
async function installYarnTypescriptPlugin ( location : string ) {
83
70
await execa ( 'yarn' , [ 'plugin' , 'import' , 'typescript' ] , { cwd : `./${ location } /` } ) ;
84
-
85
71
return true ;
86
72
}
87
73
88
74
async function initializeGitRepo ( location : string ) {
89
75
await execa ( 'git' , [ 'init' ] , { cwd : `./${ location } /` } ) ;
90
-
91
76
return true ;
92
77
}
93
78
94
79
async function runJob ( job : ( ) => Promise < any > , name : string ) {
95
80
const spinner = new Spinner ( name ) . start ( ) ;
96
81
97
- const result = await fromAsync ( async ( ) => job ( ) ) ;
98
-
99
- if ( isErr ( result ) ) {
100
- spinner . error ( { text : red ( ( result . error as Error ) . message ) } ) ;
101
- console . error ( red ( ( result . error as Error ) . message ) ) ;
102
- throw result . error ;
103
- }
104
-
105
- spinner . success ( ) ;
106
- return true ;
82
+ const result = await Result . fromAsync < any , Error > ( ( ) => job ( ) ) ;
83
+ return result . match ( {
84
+ ok : ( ) => {
85
+ spinner . success ( ) ;
86
+ return true ;
87
+ } ,
88
+ err : ( error ) => {
89
+ spinner . error ( { text : red ( error . message ) } ) ;
90
+ console . error ( red ( error . message ) ) ;
91
+ throw error ;
92
+ }
93
+ } ) ;
107
94
}
108
95
109
96
async function cloneRepo ( location : string , verbose : boolean ) {
110
- const result = await fromAsync ( async ( ) =>
111
- execa ( 'git' , [ 'clone' , repoUrl , `${ location } /ghr` ] , {
112
- stdio : verbose ? 'inherit' : undefined
113
- } )
114
- ) ;
115
-
116
- if ( isErr ( result ) ) {
117
- throw result . error ;
118
- }
119
-
120
- if ( result . value . exitCode !== 0 ) {
97
+ const value = await execa ( 'git' , [ 'clone' , repoUrl , `${ location } /ghr` ] , { stdio : verbose ? 'inherit' : undefined } ) ;
98
+ if ( value . exitCode !== 0 ) {
121
99
throw new Error ( 'An unknown error occurred while cloning the repository. Try running Sapphire CLI with "--verbose" flag.' ) ;
122
100
}
123
101
0 commit comments