-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
182 lines (152 loc) · 4.1 KB
/
utils.js
File metadata and controls
182 lines (152 loc) · 4.1 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const acorn = require('acorn')
const envPaths = require('env-paths').default
const fs = require('fs').promises
const path = require('path')
const paths = envPaths('jsq', { suffix: '' })
const cacheDir = paths.cache
const cacheFile = path.join(cacheDir, process.env.CACHE || 'cache.json')
const hasStdin = process.env.STDIN === undefined ? !process.stdin.isTTY : process.env.STDIN === '1'
module.exports = {
cacheFile,
debug,
delCache,
expandShorthands,
fileExists,
hasStdin,
parse,
readCache,
readStdin,
writeCache,
}
function debug(...args) {
if (process.env.DEBUG === '1') {
console.error(...args)
}
}
function readStdin() {
if (!hasStdin) {
debug('\nNothing to read from stdin')
return
}
return new Promise((resolve, reject) => {
debug('\nReading stdin...')
let body = ''
process.stdin.setEncoding('utf8')
process.stdin.on('data', chunk => {
if (process.env.DEBUG === '1') {
process.stderr.write(chunk)
}
body += chunk
})
process.stdin.on('end', () => {
debug('\n...Finished reading stdin')
resolve(body)
})
process.stdin.on('error', reject)
process.on('SIGINT', function onSigint() {
debug('\nReceived SIGINT, ending input')
process.stdin.emit('end')
process.off('SIGINT', onSigint)
})
})
}
async function fileExists(path) {
try {
await fs.access(path)
return true
} catch {
return false
}
}
function parse(text, { fallbackRaw = false } = {}) {
if (text == null || typeof text !== 'string' || text.trim() === '') {
return text
}
try {
return JSON.parse(text)
} catch (err) {
if (!(err instanceof SyntaxError)) {
throw err
}
try {
// Support NDJSON
return text
.trim()
.split('\n')
.map(line => JSON.parse(line))
} catch (err) {
if (!(err instanceof SyntaxError)) {
throw err
}
if (fallbackRaw) {
return text
}
// Could not parse JSON
console.error(text)
console.error('Input above is not valid JSON:', err.message)
console.error('If you meant to pass a string, make sure it is wrapped with double quotes')
console.error('If you piped jsq into itself, use --json on the first command')
process.exit(1)
}
}
}
async function readCache() {
if (!(await fileExists(cacheFile))) {
return {}
}
try {
return JSON.parse((await fs.readFile(cacheFile, 'utf8')) || '{}')
} catch (err) {
console.error('Cannot get cache, see error below:')
console.error(err)
return {}
}
}
async function writeCache(cache) {
const json = JSON.stringify(cache, null, 2)
const dir = path.dirname(cacheFile)
await fs.mkdir(dir, { recursive: true })
await fs.writeFile(cacheFile, json)
}
async function delCache() {
try {
await fs.unlink(cacheFile)
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
}
}
}
const NON_SHORTHAND_LABELS = new Set(['name', 'num', ']', ')', '}', 'null', 'true', 'false'])
function expandShorthands(expression, replacement) {
const tokens = [...acorn.tokenizer(expression, { ecmaVersion: 'latest' })]
let result = ''
let cursor = 0
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i]
const next = tokens[i + 1]
const prev = tokens[i - 1]
if (token.type.label !== '.') {
continue
}
// Ignore foo.bar
const isPropertyAccess =
next?.type.label === 'name' &&
NON_SHORTHAND_LABELS.has(prev?.type.label)
// Ignore 1.5
const isDecimalNumber =
next?.type.label === 'num' &&
expression.slice(token.end, next.start).trim() === ''
if (isPropertyAccess || isDecimalNumber) {
continue
}
result += expression.slice(cursor, token.start) + replacement
// `.prop` keep the dot, `.` don't keep the dot
cursor = next?.type.label === 'name' ? token.start : token.end
// Next token needs to see this one as a name
token.type = { ...token.type, label: 'name' }
}
// copy-paste the end of the expression that doesn't contain a dot
result += expression.slice(cursor)
return result
}