1
1
import { resolve , dirname } from 'node:path'
2
2
import { binary } from '@dr-js/core/module/common/format.js'
3
+ import { isString } from '@dr-js/core/module/common/check.js'
3
4
import { writeBuffer } from '@dr-js/core/module/node/fs/File.js'
4
5
import { createDirectory } from '@dr-js/core/module/node/fs/Directory.js'
5
6
6
7
import { stringifyYAML } from './YAML.js'
7
8
import { stringifyNginxConf } from 'source/common/config/Nginx.js'
8
9
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
+
9
16
const outputConfig = async (
10
17
pathCombo = '' , // 'file-path.json|file-path.yml|file-path.yaml'
11
18
config = { } ,
12
19
{ fromRoot, onOutput }
13
20
) => {
14
21
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
19
28
if ( ! buffer ) throw new Error ( `invalid output path: ${ path } ` )
20
29
await createDirectory ( dirname ( fromRoot ( path ) ) )
21
30
await writeBuffer ( fromRoot ( path ) , buffer )
@@ -40,6 +49,8 @@ const outputConfigMap = async (
40
49
}
41
50
42
51
export {
52
+ getRawText , toRawText ,
53
+ getRawBuffer , toRawBuffer ,
43
54
outputConfig ,
44
55
outputConfigMap
45
56
}
0 commit comments