Skip to content

Commit 3415e84

Browse files
committed
ADD: node/config/Output: get|toRawText|Buffer
1 parent bbbbe81 commit 3415e84

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

SPEC.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
+ 📄 [source/node/cache/staleCheck.js](source/node/cache/staleCheck.js)
5959
- `describeStaleReport`, `loadStatFile`, `saveStatFile`, `staleCheckCalcReport`, `staleCheckMark`, `staleCheckSetup`
6060
+ 📄 [source/node/config/Output.js](source/node/config/Output.js)
61-
- `outputConfig`, `outputConfigMap`
61+
- `getRawBuffer`, `getRawText`, `outputConfig`, `outputConfigMap`, `toRawBuffer`, `toRawText`
6262
+ 📄 [source/node/config/YAML.js](source/node/config/YAML.js)
6363
- `GET_YAML`, `USE_YAML`, `parseYAML`, `readYAML`, `readYAMLSync`, `stringifyYAML`, `writeYAML`, `writeYAMLSync`
6464
+ 📄 [source/node/export/generate.js](source/node/export/generate.js)

source/node/config/Output.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
import { resolve, dirname } from 'node:path'
22
import { binary } from '@dr-js/core/module/common/format.js'
3+
import { isString } from '@dr-js/core/module/common/check.js'
34
import { writeBuffer } from '@dr-js/core/module/node/fs/File.js'
45
import { createDirectory } from '@dr-js/core/module/node/fs/Directory.js'
56

67
import { stringifyYAML } from './YAML.js'
78
import { stringifyNginxConf } from 'source/common/config/Nginx.js'
89

10+
const __RAW = Symbol('CFG:RAW') // mark raw config
11+
const getRawText = (config) => config.__RAW === __RAW && isString(config.text) ? config.text : undefined
12+
const toRawText = (text) => ({ __RAW, text })
13+
const getRawBuffer = (config) => config.__RAW === __RAW && Buffer.isBuffer(config.buffer) ? config.buffer : undefined
14+
const toRawBuffer = (buffer) => ({ __RAW, buffer })
15+
916
const outputConfig = async (
1017
pathCombo = '', // 'file-path.json|file-path.yml|file-path.yaml'
1118
config = {},
1219
{ fromRoot, onOutput }
1320
) => {
1421
for (const path of pathCombo.split('|')) {
15-
const buffer = (path.endsWith('.yml') || path.endsWith('.yaml')) ? Buffer.from(stringifyYAML(config))
16-
: path.endsWith('.json') ? Buffer.from(JSON.stringify(config, null, 2))
17-
: path.endsWith('.nginx.conf') ? Buffer.from(stringifyNginxConf(config))
18-
: null
22+
const buffer = getRawText(config) !== undefined ? Buffer.from(getRawText(config))
23+
: getRawBuffer(config) !== undefined ? getRawBuffer(config)
24+
: (path.endsWith('.yml') || path.endsWith('.yaml')) ? Buffer.from(stringifyYAML(config))
25+
: path.endsWith('.json') ? Buffer.from(JSON.stringify(config, null, 2))
26+
: path.endsWith('.nginx.conf') ? Buffer.from(stringifyNginxConf(config))
27+
: null
1928
if (!buffer) throw new Error(`invalid output path: ${path}`)
2029
await createDirectory(dirname(fromRoot(path)))
2130
await writeBuffer(fromRoot(path), buffer)
@@ -40,6 +49,8 @@ const outputConfigMap = async (
4049
}
4150

4251
export {
52+
getRawText, toRawText,
53+
getRawBuffer, toRawBuffer,
4354
outputConfig,
4455
outputConfigMap
4556
}

source/node/config/Output.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { readText } from '@dr-js/core/module/node/fs/File.js'
44
import { deleteDirectory, resetDirectory } from '@dr-js/core/module/node/fs/Directory.js'
55

66
import {
7+
toRawText,
8+
toRawBuffer,
79
// outputConfig,
810
outputConfigMap
911
} from './Output.js'
@@ -21,9 +23,12 @@ const verifyFile = async (file, expectString) => strictEqual(
2123
expectString.trim()
2224
)
2325

26+
const TEST_TEXT = '\r\n\t'
2427
const TEST_OBJECT = { a: 1, b: 2, c: { d: 42 } }
2528

2629
const TEST_OUTPUT_CONFIG = {
30+
'rawT.txt|rawT.json': toRawText(TEST_TEXT),
31+
'rawB.txt|rawB.json': toRawBuffer(Buffer.from(TEST_TEXT)),
2732
'basic.json': TEST_OBJECT,
2833
'combo.json|combo.yml|combo.yaml|combo.nginx.conf': TEST_OBJECT
2934
}
@@ -53,6 +58,10 @@ describe('Node.Config.Output', () => {
5358
it('outputConfigMap()', async () => {
5459
await outputConfigMap(TEST_OUTPUT_CONFIG, { outputRoot: TEST_ROOT, log: info })
5560

61+
await verifyFile(fromRoot('rawT.txt'), TEST_TEXT)
62+
await verifyFile(fromRoot('rawT.txt'), TEST_TEXT)
63+
await verifyFile(fromRoot('rawB.txt'), TEST_TEXT)
64+
await verifyFile(fromRoot('rawB.txt'), TEST_TEXT)
5665
await verifyFile(fromRoot('basic.json'), TEST_OUTPUT_JSON)
5766
await verifyFile(fromRoot('combo.json'), TEST_OUTPUT_JSON)
5867
await verifyFile(fromRoot('combo.yml'), TEST_OUTPUT_YAML)

0 commit comments

Comments
 (0)