-
Notifications
You must be signed in to change notification settings - Fork 0
/
Output.js
56 lines (51 loc) · 2.12 KB
/
Output.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
import { resolve, dirname } from 'node:path'
import { binary } from '@dr-js/core/module/common/format.js'
import { isString } from '@dr-js/core/module/common/check.js'
import { writeBuffer } from '@dr-js/core/module/node/fs/File.js'
import { createDirectory } from '@dr-js/core/module/node/fs/Directory.js'
import { stringifyYAML } from './YAML.js'
import { stringifyNginxConf } from 'source/common/config/Nginx.js'
const __RAW = Symbol('CFG:RAW') // mark raw config
const getRawText = (config) => config.__RAW === __RAW && isString(config.text) ? config.text : undefined
const toRawText = (text) => ({ __RAW, text })
const getRawBuffer = (config) => config.__RAW === __RAW && Buffer.isBuffer(config.buffer) ? config.buffer : undefined
const toRawBuffer = (buffer) => ({ __RAW, buffer })
const outputConfig = async (
pathCombo = '', // 'file-path.json|file-path.yml|file-path.yaml'
config = {},
{ fromRoot, onOutput }
) => {
for (const path of pathCombo.split('|')) {
const buffer = getRawText(config) !== undefined ? Buffer.from(getRawText(config))
: getRawBuffer(config) !== undefined ? getRawBuffer(config)
: (path.endsWith('.yml') || path.endsWith('.yaml')) ? Buffer.from(stringifyYAML(config))
: path.endsWith('.json') ? Buffer.from(JSON.stringify(config, null, 2))
: path.endsWith('.nginx.conf') ? Buffer.from(stringifyNginxConf(config))
: null
if (!buffer) throw new Error(`invalid output path: ${path}`)
await createDirectory(dirname(fromRoot(path)))
await writeBuffer(fromRoot(path), buffer)
onOutput && onOutput(path, buffer)
}
}
const outputConfigMap = async (
configMap = {},
{
outputRoot = process.cwd(),
fromRoot = (...args) => resolve(outputRoot, ...args),
log = () => {},
onOutput = (path, buffer) => log(`- ${path} (${binary(buffer.length)}B)`)
} = {}
) => {
const option = { fromRoot, onOutput }
for (const [ pathCombo, config ] of Object.entries(configMap)) {
log(`[${pathCombo}]`)
await outputConfig(pathCombo, config, option)
}
}
export {
getRawText, toRawText,
getRawBuffer, toRawBuffer,
outputConfig,
outputConfigMap
}