forked from sanity-io/sanity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·148 lines (125 loc) · 3.98 KB
/
cli.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* eslint-disable no-console, no-process-exit, no-sync */
// eslint-disable-next-line import/no-unassigned-import
import path from 'path'
import chalk from 'chalk'
import fse from 'fs-extra'
import neatStack from 'neat-stack'
import resolveFrom from 'resolve-from'
import {resolveProjectRoot} from '@sanity/resolver'
import pkg from '../package.json'
import updateNotifier from './util/updateNotifier'
import parseArguments from './util/parseArguments'
import mergeCommands from './util/mergeCommands'
import {getCliRunner} from './CommandRunner'
import baseCommands from './commands'
const sanityEnv = process.env.SANITY_INTERNAL_ENV || 'production' // eslint-disable-line no-process-env
const knownEnvs = ['development', 'staging', 'production']
module.exports = async function runCli(cliRoot) {
installUnhandledRejectionsHandler()
const args = parseArguments()
const isInit = args.groupOrCommand === 'init' && args.argsWithoutOptions[0] !== 'plugin'
const cwd = checkCwdPresence()
const workDir = isInit ? process.cwd() : resolveRootDir(cwd)
await updateNotifier({pkg, cwd, workDir}).notify()
const options = {
cliRoot: cliRoot,
workDir: workDir,
corePath: getCoreModulePath(workDir),
}
warnOnNonProductionEnvironment()
warnOnInferredProjectDir(isInit, cwd, workDir)
const core = args.coreOptions
const commands = mergeCommands(baseCommands, options.corePath)
if (core.v || core.version) {
console.log(`${pkg.name} version ${pkg.version}`)
process.exit()
}
// Translate `sanity -h <command>` to `sanity help <command>`
if (core.h || core.help) {
if (args.groupOrCommand) {
args.argsWithoutOptions.unshift(args.groupOrCommand)
}
args.groupOrCommand = 'help'
}
const cliRunner = getCliRunner(commands)
cliRunner.runCommand(args.groupOrCommand, args, options).catch((err) => {
const error = typeof err.details === 'string' ? err.details : err
// eslint-disable-next-line no-console
console.error(`\n${error.stack ? neatStack(err) : error}`)
// eslint-disable-next-line no-process-exit
process.exit(1)
})
}
function getCoreModulePath(workDir) {
const pkgPath = resolveFrom.silent(workDir, '@sanity/core')
if (pkgPath) {
return pkgPath
}
const hasManifest = fse.existsSync(path.join(workDir, 'sanity.json'))
if (hasManifest && process.argv.indexOf('install') === -1) {
console.warn(
chalk.yellow(
[
'@sanity/core not installed in current project',
'Project-specific commands not available until you run `sanity install`',
].join('\n')
)
)
}
return undefined
}
// Weird edge case where the folder the terminal is currently in has been removed
function checkCwdPresence() {
let pwd
try {
pwd = process.cwd()
} catch (err) {
if (err.code === 'ENOENT') {
console.error('[ERR] Could not resolve working directory, does the current folder exist?')
process.exit(1)
} else {
throw err
}
}
return pwd
}
function resolveRootDir(cwd) {
// Resolve project root directory
try {
return (
resolveProjectRoot({
basePath: cwd,
sync: true,
}) || cwd
)
} catch (err) {
console.warn(
chalk.red(['Error occured trying to resolve project root:', err.message].join('\n'))
)
process.exit(1)
}
return false
}
function installUnhandledRejectionsHandler() {
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled rejection:', reason.stack)
})
}
function warnOnInferredProjectDir(isInit, cwd, workDir) {
if (isInit || cwd === workDir) {
return
}
console.log(`Not in project directory, assuming context of project at ${workDir}`)
}
function warnOnNonProductionEnvironment() {
if (sanityEnv === 'production') {
return
}
console.warn(
chalk.yellow(
knownEnvs.includes(sanityEnv)
? `[WARN] Running in ${sanityEnv} environment mode\n`
: `[WARN] Running in ${chalk.red('UNKNOWN')} "${sanityEnv}" environment mode\n`
)
)
}