-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
132 lines (97 loc) · 3.01 KB
/
bundle.js
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
import { addSchema, addMediaTypePlugin } from "@hyperjump/json-schema/draft-2020-12";
import { bundle } from "@hyperjump/json-schema/bundle";
import * as path from 'path'
import * as yaml from 'yaml'
import { globSync } from 'glob'
import { parse as parseYaml } from 'yaml';
import { readFileSync } from 'node:fs'
import { existsSync, mkdirSync, rmSync, writeFileSync } from "fs";
// -----------------------------------------------------------------------------
// Prepare Dependencies
addMediaTypePlugin("application/octet-stream", {
parse: async (response) => [parseYaml(await response.text()), undefined],
matcher: (path) => path.endsWith(".yml") || path.endsWith(".yaml")
});
// turn of logging here
// console.log = () => {}
// -----------------------------------------------------------------------------
// Constants
const SOURCE = path.resolve('schema')
const TARGET = path.resolve('bundled')
// -----------------------------------------------------------------------------
// Functions
let prepFilesystem = () => {
try {
mkdirSync('bundled')
} catch (error) {
rmSync('bundled', { force: true, recursive: true })
prepFilesystem()
}
}
let withGlobbedFiles = (files) => {
console.log('File list:', files)
files.forEach(iterateOverSchemaForAdding)
files.every(iterateOverFileForBundling)
}
let iterateOverSchemaForAdding = (file) => {
console.log('Iteration: ' + file)
try {
let contents = readFileSync(file, { encoding: 'utf-8' })
addSchemaForContents(contents, file)
} catch (error) {
console.error(error)
}
}
let addSchemaForContents = (contents, filePath) => {
let schema = parseYaml(contents)
let retrievalURI = 'file://' + filePath
console.log('File Schema:', schema)
console.log('Retrieval URI:', retrievalURI)
addSchema(schema, retrievalURI)
}
let iterateOverFileForBundling = (file) => {
try {
ensureTargetFilePath(file)
bundleSchemaToFile(file)
} catch (error) {
return false
}
return true
}
let ensureTargetFilePath = (file) => {
let targetPath = path.parse(file).dir.replace(SOURCE, TARGET)
console.log('Target Path:', targetPath)
if (!existsSync(targetPath)) {
mkdirSync(targetPath, { recursive: true })
}
}
let bundleSchemaToFile = (file) => {
let sourceFile = 'file://' + file
console.log('Source File:', sourceFile)
bundle(sourceFile).then((schema) => { writeBundledFile(schema, file) }, reportFailedBundle)
}
let writeBundledFile = (bundled, file) => {
let yamlString = yaml.stringify(bundled)
let targetFile = file.replace(SOURCE, TARGET)
console.log(bundled)
console.log(yamlString)
console.log('Target File:', targetFile)
try {
writeFileSync(
targetFile,
yamlString,
{ encoding: 'utf-8', flag: 'w' }
)
} catch (error) {
console.error(error)
}
}
let reportFailedBundle = (error) => {
console.error(error)
}
// -----------------------------------------------------------------------------
// Runtime
prepFilesystem()
withGlobbedFiles(
globSync(path.join(SOURCE, '**', '*.yml'))
)