1
- /* @flow */
2
- const { execSync } = require ( `child_process` )
3
- const execa = require ( `execa` )
4
- const hostedGitInfo = require ( `hosted-git-info` )
5
- const fs = require ( `fs-extra` )
6
- const sysPath = require ( `path` )
7
- const report = require ( `./reporter` )
8
- const url = require ( `url` )
9
- const isValid = require ( `is-valid-path` )
10
- const existsSync = require ( `fs-exists-cached` ) . sync
11
- const { trackCli, trackError } = require ( `gatsby-telemetry` )
12
- const prompts = require ( `prompts` )
13
- const opn = require ( `better-opn` )
14
-
15
- const {
16
- getPackageManager,
17
- promptPackageManager,
18
- } = require ( `./util/configstore` )
19
- const isTTY = require ( `./util/is-tty` )
20
- const spawn = ( cmd : string , options : any ) => {
1
+ import opn from "better-opn"
2
+ import { execSync } from "child_process"
3
+ import execa from "execa"
4
+ import { sync as existsSync } from "fs-exists-cached"
5
+ import fs from "fs-extra"
6
+ import { trackCli , trackError } from "gatsby-telemetry"
7
+ import hostedGitInfo from "hosted-git-info"
8
+ import isValid from "is-valid-path"
9
+ import sysPath from "path"
10
+ import prompts from "prompts"
11
+ import url from "url"
12
+
13
+ import report from "./reporter"
14
+ import { getPackageManager , promptPackageManager } from "./util/configstore"
15
+ import isTTY from "./util/is-tty"
16
+
17
+ const spawnWithArgs = (
18
+ file : string ,
19
+ args : string [ ] ,
20
+ options ?: any
21
+ ) : execa . ExecaChildProcess =>
22
+ execa ( file , args , { stdio : `inherit` , preferLocal : false , ...options } )
23
+
24
+ const spawn = ( cmd : string , options ?: any ) : execa . ExecaChildProcess => {
21
25
const [ file , ...args ] = cmd . split ( / \s + / )
22
26
return spawnWithArgs ( file , args , options )
23
27
}
24
- const spawnWithArgs = ( file : string , args : string [ ] , options : any ) =>
25
- execa ( file , args , { stdio : `inherit` , preferLocal : false , ...options } )
26
-
27
28
// Checks the existence of yarn package and user preference if it exists
28
29
// We use yarnpkg instead of yarn to avoid conflict with Hadoop yarn
29
30
// Refer to https://github.com/yarnpkg/yarn/issues/673
30
- const shouldUseYarn = async ( ) => {
31
+ const shouldUseYarn = async ( ) : Promise < boolean > => {
31
32
try {
32
33
execSync ( `yarnpkg --version` , { stdio : `ignore` } )
33
34
@@ -49,7 +50,7 @@ const shouldUseYarn = async () => {
49
50
}
50
51
}
51
52
52
- const isAlreadyGitRepository = async ( ) => {
53
+ const isAlreadyGitRepository = async ( ) : Promise < boolean > => {
53
54
try {
54
55
return await spawn ( `git rev-parse --is-inside-work-tree` , {
55
56
stdio : `pipe` ,
@@ -60,14 +61,16 @@ const isAlreadyGitRepository = async () => {
60
61
}
61
62
62
63
// Initialize newly cloned directory as a git repo
63
- const gitInit = async rootPath => {
64
+ const gitInit = async (
65
+ rootPath : string
66
+ ) : Promise < execa . ExecaReturnBase < string > > => {
64
67
report . info ( `Initialising git in ${ rootPath } ` )
65
68
66
69
return await spawn ( `git init` , { cwd : rootPath } )
67
70
}
68
71
69
72
// Create a .gitignore file if it is missing in the new directory
70
- const maybeCreateGitIgnore = async rootPath => {
73
+ const maybeCreateGitIgnore = async ( rootPath : string ) : Promise < void > => {
71
74
if ( existsSync ( sysPath . join ( rootPath , `.gitignore` ) ) ) {
72
75
return
73
76
}
@@ -80,7 +83,10 @@ const maybeCreateGitIgnore = async rootPath => {
80
83
}
81
84
82
85
// Create an initial git commit in the new directory
83
- const createInitialGitCommit = async ( rootPath , starterUrl ) => {
86
+ const createInitialGitCommit = async (
87
+ rootPath : string ,
88
+ starterUrl : string
89
+ ) : Promise < void > => {
84
90
report . info ( `Create initial git commit in ${ rootPath } ` )
85
91
86
92
await spawn ( `git add -A` , { cwd : rootPath } )
@@ -98,7 +104,7 @@ const createInitialGitCommit = async (rootPath, starterUrl) => {
98
104
}
99
105
100
106
// Executes `npm install` or `yarn install` in rootPath.
101
- const install = async rootPath => {
107
+ const install = async ( rootPath : string ) : Promise < void > => {
102
108
const prevDir = process . cwd ( )
103
109
104
110
report . info ( `Installing packages...` )
@@ -117,13 +123,17 @@ const install = async rootPath => {
117
123
}
118
124
}
119
125
120
- const ignored = path => ! / ^ \. ( g i t | h g ) $ / . test ( sysPath . basename ( path ) )
126
+ const ignored = ( path : string ) : boolean =>
127
+ ! / ^ \. ( g i t | h g ) $ / . test ( sysPath . basename ( path ) )
121
128
122
129
// Copy starter from file system.
123
- const copy = async ( starterPath : string , rootPath : string ) => {
130
+ const copy = async (
131
+ starterPath : string ,
132
+ rootPath : string
133
+ ) : Promise < boolean > => {
124
134
// Chmod with 755.
125
135
// 493 = parseInt('755', 8)
126
- await fs . mkdirp ( rootPath , { mode : 493 } )
136
+ await fs . ensureDir ( rootPath , { mode : 493 } )
127
137
128
138
if ( ! existsSync ( starterPath ) ) {
129
139
throw new Error ( `starter ${ starterPath } doesn't exist` )
@@ -152,8 +162,8 @@ const copy = async (starterPath: string, rootPath: string) => {
152
162
}
153
163
154
164
// Clones starter from URI.
155
- const clone = async ( hostInfo : any , rootPath : string ) => {
156
- let url
165
+ const clone = async ( hostInfo : any , rootPath : string ) : Promise < void > => {
166
+ let url : string
157
167
// Let people use private repos accessed over SSH.
158
168
if ( hostInfo . getDefaultRepresentation ( ) === `sshurl` ) {
159
169
url = hostInfo . ssh ( { noCommittish : true } )
@@ -183,7 +193,16 @@ const clone = async (hostInfo: any, rootPath: string) => {
183
193
if ( ! isGit ) await createInitialGitCommit ( rootPath , url )
184
194
}
185
195
186
- const getPaths = async ( starterPath : string , rootPath : string ) => {
196
+ interface IGetPaths {
197
+ starterPath : string
198
+ rootPath : string
199
+ selectedOtherStarter : boolean
200
+ }
201
+
202
+ const getPaths = async (
203
+ starterPath : string ,
204
+ rootPath : string
205
+ ) : Promise < IGetPaths > => {
187
206
let selectedOtherStarter = false
188
207
189
208
// if no args are passed, prompt user for path and starter
@@ -230,11 +249,11 @@ const getPaths = async (starterPath: string, rootPath: string) => {
230
249
return { starterPath, rootPath, selectedOtherStarter }
231
250
}
232
251
233
- type InitOptions = {
234
- rootPath ? : string ,
252
+ interface IInitOptions {
253
+ rootPath : string
235
254
}
236
255
237
- const successMessage = path => {
256
+ const successMessage = ( path : string ) : void => {
238
257
report . info ( `
239
258
Your new Gatsby site has been successfully bootstrapped. Start developing it by running:
240
259
@@ -246,7 +265,10 @@ Your new Gatsby site has been successfully bootstrapped. Start developing it by
246
265
/**
247
266
* Main function that clones or copies the starter.
248
267
*/
249
- module . exports = async ( starter : string , options : InitOptions = { } ) => {
268
+ export default async (
269
+ starter : string ,
270
+ options : IInitOptions
271
+ ) : Promise < void > => {
250
272
const { starterPath, rootPath, selectedOtherStarter } = await getPaths (
251
273
starter ,
252
274
options . rootPath
0 commit comments