-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
43 lines (38 loc) · 1.12 KB
/
utils.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
import { parse } from 'https://deno.land/std/encoding/yaml.ts'
import * as log from 'https://deno.land/std/log/mod.ts'
import { HasuraConfig, GeneralOptions } from './types.ts'
// TODO use Deno.dir('home') when stable
export const HOME_DIR = Deno.env.get('HOME')
export const error = (message: string): never => {
log.error(message)
Deno.exit(1)
}
export const getHasuraConfig = async (path: string): Promise<HasuraConfig> => {
try {
const decoder = new TextDecoder('utf-8')
const data = await Deno.readFile(`${path}/config.yaml`)
const config = parse(decoder.decode(data)) as HasuraConfig
if (!config.version) config.version = 2
return config
} catch (e) {
return error(`Cannot load Hasura config.yaml`)
}
}
export const defaultOptions = async ({
project,
...other
}: {
[key: string]: string
}): Promise<GeneralOptions> => {
project = project || Deno.cwd()
try {
const dirInfo = await Deno.stat(project)
if (!dirInfo.isDirectory) return error(`${project} is not a directory`)
} catch (e) {
return error(`${project} does not exist`)
}
return {
project,
...other,
}
}