Skip to content

Commit ac63886

Browse files
mottox2MichaelDeBoey
andauthoredMar 10, 2020
chore(gatsby-cli): Convert init-starter to typescript (#22086)
* chore(gatsby-cli): Convert init-starter to typescript * Fix ts errors and remove conditional args * Update packages/gatsby-cli/src/init-starter.ts Co-Authored-By: Michaël De Boey <info@michaeldeboey.be> * Update packages/gatsby-cli/src/init-starter.ts Co-Authored-By: Michaël De Boey <info@michaeldeboey.be> * Format Co-authored-by: Michaël De Boey <info@michaeldeboey.be>
1 parent 2123158 commit ac63886

File tree

1 file changed

+61
-39
lines changed

1 file changed

+61
-39
lines changed
 

‎packages/gatsby-cli/src/init-starter.js ‎packages/gatsby-cli/src/init-starter.ts

+61-39
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
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 => {
2125
const [file, ...args] = cmd.split(/\s+/)
2226
return spawnWithArgs(file, args, options)
2327
}
24-
const spawnWithArgs = (file: string, args: string[], options: any) =>
25-
execa(file, args, { stdio: `inherit`, preferLocal: false, ...options })
26-
2728
// Checks the existence of yarn package and user preference if it exists
2829
// We use yarnpkg instead of yarn to avoid conflict with Hadoop yarn
2930
// Refer to https://github.com/yarnpkg/yarn/issues/673
30-
const shouldUseYarn = async () => {
31+
const shouldUseYarn = async (): Promise<boolean> => {
3132
try {
3233
execSync(`yarnpkg --version`, { stdio: `ignore` })
3334

@@ -49,7 +50,7 @@ const shouldUseYarn = async () => {
4950
}
5051
}
5152

52-
const isAlreadyGitRepository = async () => {
53+
const isAlreadyGitRepository = async (): Promise<boolean> => {
5354
try {
5455
return await spawn(`git rev-parse --is-inside-work-tree`, {
5556
stdio: `pipe`,
@@ -60,14 +61,16 @@ const isAlreadyGitRepository = async () => {
6061
}
6162

6263
// 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>> => {
6467
report.info(`Initialising git in ${rootPath}`)
6568

6669
return await spawn(`git init`, { cwd: rootPath })
6770
}
6871

6972
// 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> => {
7174
if (existsSync(sysPath.join(rootPath, `.gitignore`))) {
7275
return
7376
}
@@ -80,7 +83,10 @@ const maybeCreateGitIgnore = async rootPath => {
8083
}
8184

8285
// 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> => {
8490
report.info(`Create initial git commit in ${rootPath}`)
8591

8692
await spawn(`git add -A`, { cwd: rootPath })
@@ -98,7 +104,7 @@ const createInitialGitCommit = async (rootPath, starterUrl) => {
98104
}
99105

100106
// Executes `npm install` or `yarn install` in rootPath.
101-
const install = async rootPath => {
107+
const install = async (rootPath: string): Promise<void> => {
102108
const prevDir = process.cwd()
103109

104110
report.info(`Installing packages...`)
@@ -117,13 +123,17 @@ const install = async rootPath => {
117123
}
118124
}
119125

120-
const ignored = path => !/^\.(git|hg)$/.test(sysPath.basename(path))
126+
const ignored = (path: string): boolean =>
127+
!/^\.(git|hg)$/.test(sysPath.basename(path))
121128

122129
// 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> => {
124134
// Chmod with 755.
125135
// 493 = parseInt('755', 8)
126-
await fs.mkdirp(rootPath, { mode: 493 })
136+
await fs.ensureDir(rootPath, { mode: 493 })
127137

128138
if (!existsSync(starterPath)) {
129139
throw new Error(`starter ${starterPath} doesn't exist`)
@@ -152,8 +162,8 @@ const copy = async (starterPath: string, rootPath: string) => {
152162
}
153163

154164
// 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
157167
// Let people use private repos accessed over SSH.
158168
if (hostInfo.getDefaultRepresentation() === `sshurl`) {
159169
url = hostInfo.ssh({ noCommittish: true })
@@ -183,7 +193,16 @@ const clone = async (hostInfo: any, rootPath: string) => {
183193
if (!isGit) await createInitialGitCommit(rootPath, url)
184194
}
185195

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> => {
187206
let selectedOtherStarter = false
188207

189208
// if no args are passed, prompt user for path and starter
@@ -230,11 +249,11 @@ const getPaths = async (starterPath: string, rootPath: string) => {
230249
return { starterPath, rootPath, selectedOtherStarter }
231250
}
232251

233-
type InitOptions = {
234-
rootPath?: string,
252+
interface IInitOptions {
253+
rootPath: string
235254
}
236255

237-
const successMessage = path => {
256+
const successMessage = (path: string): void => {
238257
report.info(`
239258
Your new Gatsby site has been successfully bootstrapped. Start developing it by running:
240259
@@ -246,7 +265,10 @@ Your new Gatsby site has been successfully bootstrapped. Start developing it by
246265
/**
247266
* Main function that clones or copies the starter.
248267
*/
249-
module.exports = async (starter: string, options: InitOptions = {}) => {
268+
export default async (
269+
starter: string,
270+
options: IInitOptions
271+
): Promise<void> => {
250272
const { starterPath, rootPath, selectedOtherStarter } = await getPaths(
251273
starter,
252274
options.rootPath

0 commit comments

Comments
 (0)
Please sign in to comment.