1- /* eslint-disable no-console */
2- import klawSync from "klaw-sync" ;
3- import path from "path" ;
4- import fs from "fs-extra" ;
1+ import {
2+ cpSync ,
3+ existsSync ,
4+ readdirSync ,
5+ readFileSync ,
6+ rmSync ,
7+ writeFileSync
8+ } from "node:fs" ;
9+ import path from "node:path" ;
10+ import { fileURLToPath } from "node:url" ;
511import prettier from "prettier" ;
6- import url from "url" ;
712
8- const __dirname = path . dirname ( url . fileURLToPath ( import . meta. url ) ) ;
13+ const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
914
1015const updateTestOutput = async ( ) => {
1116 let samplesDir = path . resolve ( __dirname , "../test/unit-test" ) ;
12- let originalSamplesDir = samplesDir ;
1317 if ( process . argv . indexOf ( "-single" ) > - 1 ) {
1418 samplesDir = path . resolve ( __dirname , "./single-printer-run" ) ;
1519 } else if ( process . argv . indexOf ( "-repository" ) > - 1 ) {
1620 const testSamples = path . resolve ( __dirname , "../test-samples" ) ;
17- originalSamplesDir = path . resolve (
21+ const originalSamplesDir = path . resolve (
1822 __dirname ,
1923 process . argv [ process . argv . indexOf ( "-repository" ) + 1 ]
2024 ) ;
2125 samplesDir = path . resolve ( testSamples , path . basename ( originalSamplesDir ) ) ;
22- if ( fs . existsSync ( samplesDir ) ) {
23- fs . removeSync ( samplesDir ) ;
26+ if ( existsSync ( samplesDir ) ) {
27+ rmSync ( samplesDir , { recursive : true } ) ;
2428 }
2529 console . log ( `start copy ${ originalSamplesDir } to ${ samplesDir } ` ) ;
26- fs . copySync ( originalSamplesDir , samplesDir ) ;
30+ cpSync ( originalSamplesDir , samplesDir ) ;
2731 console . log ( `end copy ${ originalSamplesDir } to ${ samplesDir } ` ) ;
2832 }
2933
@@ -32,30 +36,35 @@ const updateTestOutput = async () => {
3236 numberOfTime = process . argv [ process . argv . indexOf ( "-times" ) + 1 ] ;
3337 }
3438
35- const sampleFiles = klawSync ( samplesDir , { nodir : true } ) ;
36- const javaSampleFiles = sampleFiles . filter ( fileDesc => {
37- if ( fileDesc . path . includes ( "node_modules" ) ) {
38- return false ;
39- }
40- if ( process . argv . indexOf ( "-repository" ) > - 1 ) {
41- return fileDesc . path . endsWith ( ".java" ) ;
42- }
43- return fileDesc . path . endsWith ( "input.java" ) ;
39+ const sampleFiles = readdirSync ( samplesDir , {
40+ encoding : "utf-8" ,
41+ recursive : true
4442 } ) ;
43+ const javaSampleFiles = sampleFiles
44+ . filter ( filePath => {
45+ if ( filePath . includes ( "node_modules" ) ) {
46+ return false ;
47+ }
48+ if ( process . argv . indexOf ( "-repository" ) > - 1 ) {
49+ return filePath . endsWith ( ".java" ) ;
50+ }
51+ return filePath . endsWith ( "input.java" ) ;
52+ } )
53+ . map ( filePath => path . join ( samplesDir , filePath ) ) ;
4554
4655 let failures = 0 ;
4756 await Promise . all (
48- javaSampleFiles . map ( async fileDesc => {
49- const javaFileText = fs . readFileSync ( fileDesc . path , "utf8" ) ;
57+ javaSampleFiles . map ( async filePath => {
58+ const javaFileText = readFileSync ( filePath , "utf8" ) ;
5059
5160 try {
52- console . log ( `Reading <${ fileDesc . path } >` ) ;
61+ console . log ( `Reading <${ filePath } >` ) ;
5362 let newExpectedText = javaFileText ;
5463
55- const testDir = path . dirname ( fileDesc . path ) ;
64+ const testDir = path . dirname ( filePath ) ;
5665 const optionsPath = path . join ( testDir , ".prettierrc.json" ) ;
57- const testOptions = fs . existsSync ( optionsPath )
58- ? fs . readJsonSync ( optionsPath )
66+ const testOptions = existsSync ( optionsPath )
67+ ? JSON . parse ( readFileSync ( optionsPath , "utf-8" ) )
5968 : { } ;
6069
6170 for ( let i = 0 ; i < numberOfTime ; i ++ ) {
@@ -78,18 +87,15 @@ const updateTestOutput = async () => {
7887 ...testOptions
7988 } ) ;
8089 }
81- let outputFilePath = fileDesc . path . replace (
82- / i n p u t .j a v a $ / ,
83- "output.java"
84- ) ;
90+ let outputFilePath = filePath . replace ( / i n p u t .j a v a $ / , "output.java" ) ;
8591 if ( process . argv . indexOf ( "-repository" ) > - 1 ) {
86- outputFilePath = fileDesc . path ;
92+ outputFilePath = filePath ;
8793 }
8894 console . log ( `writing <${ outputFilePath } >` ) ;
89- fs . writeFileSync ( outputFilePath , newExpectedText ) ;
95+ writeFileSync ( outputFilePath , newExpectedText ) ;
9096 } catch ( e ) {
9197 failures ++ ;
92- console . log ( `Failed parsing: <${ fileDesc . path } >` ) ;
98+ console . log ( `Failed parsing: <${ filePath } >` ) ;
9399 console . log ( e ) ;
94100 }
95101 } )
0 commit comments