-
Notifications
You must be signed in to change notification settings - Fork 390
/
Copy pathcli-state.ts
125 lines (103 loc) · 3.57 KB
/
cli-state.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
import fs from 'fs'
import path from 'path'
import process from 'process'
import { deleteProperty, getProperty, hasProperty, setProperty } from 'dot-prop'
import { findUpSync } from 'find-up'
import writeFileAtomic from 'write-file-atomic'
import { getPathInProject } from '../lib/settings.js'
const STATE_PATH = getPathInProject(['state.json'])
const permissionError = "You don't have access to this file."
/**
* Finds location of `.netlify/state.json`
*/
const findStatePath = (cwd: string): string => {
const statePath = findUpSync([STATE_PATH], { cwd })
if (!statePath) {
return path.join(cwd, STATE_PATH)
}
return statePath
}
export default class CLIState {
private path: string
constructor(cwd: string) {
this.path = findStatePath(cwd)
}
get all() {
try {
// @ts-expect-error TS(2345) FIXME: Argument of type 'Buffer' is not assignable to par... Remove this comment to see the full error message
return JSON.parse(fs.readFileSync(this.path))
} catch (error) {
// Don't create if it doesn't exist
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error.code === 'ENOENT' || error.code === 'ENOTDIR') {
return {}
}
// Improve the message of permission errors
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error.code === 'EACCES') {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
error.message = `${error.message}\n${permissionError}\n`
}
// Empty the file if it encounters invalid JSON
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error.name === 'SyntaxError') {
writeFileAtomic.sync(this.path, '')
return {}
}
throw error
}
}
set all(val) {
try {
// Make sure the folder exists as it could have been deleted in the meantime
fs.mkdirSync(path.dirname(this.path), { recursive: true })
writeFileAtomic.sync(this.path, JSON.stringify(val, null, '\t'))
} catch (error) {
// Improve the message of permission errors
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
if (error.code === 'EACCES') {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
error.message = `${error.message}\n${permissionError}\n`
}
throw error
}
}
get size() {
return Object.keys(this.all || {}).length
}
// @ts-expect-error TS(7006) FIXME: Parameter 'key' implicitly has an 'any' type.
get(key) {
if (key === 'siteId' && process.env.NETLIFY_SITE_ID) {
// TODO figure out cleaner way of grabbing ENV vars
return process.env.NETLIFY_SITE_ID
}
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
return getProperty(this.all, key)
}
// @ts-expect-error TS(7019) FIXME: Rest parameter 'args' implicitly has an 'any[]' ty... Remove this comment to see the full error message
set(...args) {
const [key, val] = args
const config = this.all
if (args.length === 1) {
Object.entries(key).forEach(([keyPart, value]) => {
setProperty(config, keyPart, value)
})
} else {
setProperty(config, key, val)
}
this.all = config
}
// @ts-expect-error TS(7006) FIXME: Parameter 'key' implicitly has an 'any' type.
has(key) {
return hasProperty(this.all, key)
}
// @ts-expect-error TS(7006) FIXME: Parameter 'key' implicitly has an 'any' type.
delete(key) {
const config = this.all
deleteProperty(config, key)
this.all = config
}
clear() {
this.all = {}
}
}