-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathvalidation.ts
50 lines (44 loc) · 1.68 KB
/
validation.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
import {Writable} from 'stream'
import {checkFile} from '../../helpers/validation'
import {Sourcemap} from './interfaces'
import {renderMinifiedPathPrefixMisusage} from './renderer'
import {extractRepeatedPath} from './utils'
export class InvalidPayload extends Error {
public reason: string
constructor(reason: string, message?: string) {
super(message)
this.reason = reason
}
}
export const validatePayload = (sourcemap: Sourcemap, stdout: Writable) => {
// Check existence of sourcemap file
const sourcemapCheck = checkFile(sourcemap.sourcemapPath)
if (!sourcemapCheck.exists) {
// This case should not happen as all collected sourcemaps should point to correct files
throw new InvalidPayload('missing_sourcemap', `Skipping missing sourcemap (${sourcemap.sourcemapPath})`)
}
if (sourcemapCheck.empty) {
throw new InvalidPayload('empty_sourcemap', `Skipping empty sourcemap (${sourcemap.sourcemapPath})`)
}
// Check existence of minified file
const minifiedFileCheck = checkFile(sourcemap.minifiedFilePath)
if (!minifiedFileCheck.exists) {
throw new InvalidPayload(
'missing_js',
`Missing corresponding JS file for sourcemap (${sourcemap.minifiedFilePath})`
)
}
if (minifiedFileCheck.empty) {
throw new InvalidPayload(
'empty_js',
`Skipping sourcemap (${sourcemap.sourcemapPath}) due to ${sourcemap.minifiedFilePath} being empty`
)
}
// Check for --minified-path-prefix flag misuages.
if (sourcemap.minifiedPathPrefix) {
const repeated = extractRepeatedPath(sourcemap.minifiedPathPrefix, sourcemap.relativePath)
if (repeated) {
stdout.write(renderMinifiedPathPrefixMisusage(sourcemap, repeated))
}
}
}