1- import { execFile } from "child_process"
21import { join } from "path"
2+ const minijsonLib = join ( __dirname , `${ process . platform } -${ process . arch } ` , "minijson.node" )
3+
4+ const nativeLib = require ( minijsonLib ) // eslint-disable-line @typescript-eslint/no-var-requires
35
46/**
57 * Minify all the given JSON files in place. It minifies the files in parallel.
@@ -14,19 +16,7 @@ export async function minifyFiles(files: string[], hasComment = false): Promise<
1416 if ( filesNum === 0 ) {
1517 return Promise . resolve ( )
1618 }
17-
18- const args = [ ...files ]
19- const spliceUpper = 2 * filesNum - 2
20-
21- for ( let iSplice = 0 ; iSplice <= spliceUpper ; iSplice += 2 ) {
22- args . splice ( iSplice , 0 , "--file" )
23- }
24-
25- if ( hasComment ) {
26- args . push ( "--comment" )
27- }
28-
29- await spawnMinijson ( args )
19+ nativeLib . minifyFiles ( files , hasComment )
3020}
3121
3222/**
@@ -38,36 +28,6 @@ export async function minifyFiles(files: string[], hasComment = false): Promise<
3828 * @throws {Promise<string | Error> } The promise is rejected with the reason for failure
3929 */
4030export async function minifyString ( jsonString : string , hasComment = false ) : Promise < string > {
41- const args = [ "--string" , jsonString ]
42- if ( hasComment ) {
43- args . push ( "--comment" )
44- }
4531 // trim is needed due to using stdout for interop
46- return ( await spawnMinijson ( args ) ) . trim ( )
47- }
48-
49- const exeExtention = process . platform === "win32" ? ".exe" : ""
50- const binName = `minijson${ exeExtention } `
51-
52- const minijsonBin = join ( __dirname , `${ process . platform } -${ process . arch } ` , binName )
53-
54- /**
55- * Spawn minijson with the given arguments
56- *
57- * @param args An array of arguments
58- * @returns {Promise<string> } Returns a promise that resolves to stdout output string when the operation finishes
59- * @throws {Promise<string | Error> } The promise is rejected with the reason for failure
60- */
61- export function spawnMinijson ( args : string [ ] ) : Promise < string > {
62- return new Promise < string > ( ( resolve , reject ) => {
63- execFile ( minijsonBin , args , ( err , stdout , stderr ) => {
64- if ( err ) {
65- reject ( err )
66- }
67- if ( stderr !== "" ) {
68- reject ( stderr )
69- }
70- resolve ( stdout )
71- } )
72- } )
32+ return nativeLib . minifyFiles ( jsonString , hasComment )
7333}
0 commit comments