-
Notifications
You must be signed in to change notification settings - Fork 0
/
deno.ts
159 lines (124 loc) · 4.35 KB
/
deno.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
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
149
150
151
152
153
154
155
156
157
158
159
/**
* @module
*
* UNSTABLE
*
* Programmatic apis to the Deno CLI
*/
// Untested. Logic is pretty simple. Should always "just work"
import { colors } from './deps.ts'
import { exec } from './sh.ts'
export type DenoExecPermissions = 'all' | Deno.PermissionOptions
export interface DenoExecOptions extends DenoOptions {
/**
* The permissions to execute files with
*
* NOTE: 'inherit' permissions are equivalent to 'all' */
permissions?: DenoExecPermissions
/** If true, operation be provided with the unstable apis of the Deno runtime */
unstable?: boolean
}
export interface DenoOptions {
/** If true, operation will not resolve and continue to keep Deno alive, monitoring the files for changes */
watch?: boolean
/** If true, all output is suppressed */
quiet?: boolean
}
/** Run Deno's builtin test suite */
export async function test(options: DenoExecOptions = {}): Promise<void> {
const args = ['deno', 'test', ...stringifyDenoExecOptions(options)]
await exec(args, { env: Deno.env.toObject() })
}
/** Run Deno's builtin linter */
export async function lint(options: DenoOptions = {}): Promise<void> {
const args = [
'deno',
'lint',
// We always want to lint in quiet mode because we will log an alternative success message
...stringifyDenoOptions({ ...options }),
]
await exec(args, { env: Deno.env.toObject() })
}
export interface FormatOptions extends DenoOptions {
check?: boolean
}
/** Run Deno's builtin formatter */
export async function format(options: FormatOptions = {}): Promise<void> {
const args = ['deno', 'fmt', ...stringifyDenoOptions(options)]
if (options.check) args.push('--check')
await exec(args, { env: Deno.env.toObject() })
}
/** Test, lint, and format check source code in the current working directory */
export async function check(options: DenoExecOptions = {}): Promise<void> {
await Promise.all([
test(options).then(() => {
if (!options.quiet) console.log(colors.green('Success'), 'tests passed')
}),
lint(options).then(() => {
if (!options.quiet) console.log(colors.green('Success'), 'linting passed')
}),
format({ ...options, check: true }).then(() => {
if (!options.quiet) console.log(colors.green('Success'), 'formatting is ok')
}),
])
}
function stringifyDenoOptions(options: DenoOptions) {
const args: string[] = []
if (options.watch) args.push('--watch')
if (options.quiet) args.push('--quiet')
return args
}
function stringifyDenoExecOptions(options: DenoExecOptions) {
const args: string[] = []
args.push(...stringifyDenoOptions(options))
if (options.permissions) args.push(...stringifyPermissions(options.permissions))
if (options.unstable) args.push('--unstable')
return args
}
function stringifyPermissions(permissions: DenoExecPermissions) {
if (permissions === 'all') return ['-A']
if (permissions === 'none') return []
if (permissions === 'inherit') {
permissions = {
env: 'inherit',
ffi: 'inherit',
hrtime: 'inherit',
net: 'inherit',
read: 'inherit',
run: 'inherit',
sys: 'inherit',
write: 'inherit',
}
}
const args: string[] = []
if (permissions.env) {
if (permissions.env === 'inherit' || permissions.env === true) args.push('--allow-env')
else args.push(`--allow-env="${permissions.env.join(',')}"`)
}
if (permissions.ffi) {
if (permissions.ffi === 'inherit' || permissions.ffi === true) args.push('--allow-ffi')
else args.push(`--allow-ffi=${permissions.ffi.join(',')}`)
}
if (permissions.hrtime) args.push('--allow-hrtime')
if (permissions.net) {
if (permissions.net === 'inherit' || permissions.net === true) args.push('--allow-net')
else args.push(`--allow-net=${permissions.net.join(',')}`)
}
if (permissions.read) {
if (permissions.read === 'inherit' || permissions.read === true) args.push('--allow-read')
else args.push(`--allow-read=${permissions.read.join(',')}`)
}
if (permissions.run) {
if (permissions.run === 'inherit' || permissions.run === true) args.push('--allow-run')
else args.push(`--allow-run=${permissions.run.join(',')}`)
}
if (permissions.sys) {
if (permissions.sys === 'inherit' || permissions.sys === true) args.push('--allow-sys')
else args.push(`--allow-sys=${permissions.sys.join(',')}`)
}
if (permissions.write) {
if (permissions.write === 'inherit' || permissions.write === true) args.push('--allow-write')
else args.push(`--allow-write=${permissions.write.join(',')}`)
}
return args
}