/
state-setup.ts
68 lines (60 loc) · 2.61 KB
/
state-setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import readlineSync from "readline-sync"
import supportsHyperlinks from "supports-hyperlinks"
import hyperLinker from "hyperlinker"
import chalk from "chalk"
import { basename } from "path"
import { setTimeout } from "timers"
import * as fs from "fs"
import { getRepoSlug } from "./get-repo-slug"
import { InitState, InitUI } from "./interfaces"
export const createUI = (state: InitState, app: any): InitUI => {
const say = (msg: string) => console.log(msg)
const fancyLink = (name: string, href: string) => hyperLinker(name, href)
const inlineLink = (_name: string, href: string) => chalk.underline(href)
const linkToUse = state.supportsHLinks ? fancyLink : inlineLink
return {
say,
header: (msg: string) => say(chalk.bold("\n## " + msg + "\n")),
command: (command: string) => say("> " + chalk.white.bold(command) + " \n"),
link: (name: string, href: string) => linkToUse(name, href),
pause: async (secs: number) => new Promise(done => setTimeout(done, secs * 1000)),
waitForReturn: () => (app.impatient ? Promise.resolve() : readlineSync.question("\n↵ ")),
askWithAnswers: (_message: string, answers: string[]) => {
const a = readlineSync.keyInSelect(answers, "", { defaultInput: answers[0] })
return answers[a]
},
}
}
export const generateInitialState = (osProcess: NodeJS.Process): InitState => {
const isMac = osProcess.platform === "darwin"
const isWindows = osProcess.platform === "win32"
const folderName = capitalizeFirstLetter(camelCase(basename(osProcess.cwd())))
const isTypeScript = checkForTypeScript()
const isBabel = checkForBabel()
const hasTravis = fs.existsSync(".travis.yml")
const hasCircle = fs.existsSync("circle.yml")
const ciType = hasTravis ? "travis" : hasCircle ? "circle" : "unknown"
const repoSlug = getRepoSlug()
const isGitHub = !!repoSlug
return {
isMac,
isWindows,
isTypeScript,
isBabel,
isAnOSSRepo: true,
supportsHLinks: supportsHyperlinks.stdout,
filename: isTypeScript ? "dangerfile.ts" : "dangerfile.js",
botName: folderName + "Bot",
hasSetUpAccount: false,
hasCreatedDangerfile: false,
hasSetUpAccountToken: false,
repoSlug,
ciType,
isGitHub,
}
}
const checkForTypeScript = () => fs.existsSync("node_modules/typescript/package.json")
const checkForBabel = () =>
fs.existsSync("node_modules/babel-core/package.json") || fs.existsSync("node_modules/@babel/core/package.json")
const capitalizeFirstLetter = (string: string) => string.charAt(0).toUpperCase() + string.slice(1)
const camelCase = (str: string) => str.split("-").reduce((a, b) => a + b.charAt(0).toUpperCase() + b.slice(1))