11import fs from "fs" ;
2- import path from "path" ;
32import {
43 ScrubberAction ,
54 TagNameToAction ,
65 ScrubberConfig ,
76} from "./scrubberTypes" ;
8-
9- const TAG_START_CHAR = "{" ;
10- const TAG_END_CHAR = "}" ;
11-
12- const FILE_TYPE_COMMENT : { [ key : string ] : string } = {
13- js : "//" ,
14- json : "//" ,
15- ts : "//" ,
16- py : "#" ,
17- } ;
18-
19- function scrubberActionsToDict ( actions : ScrubberAction [ ] ) : TagNameToAction {
20- const dict : TagNameToAction = { } ;
21- actions . forEach ( ( action ) => {
22- action . tags . forEach ( ( tag : string ) => {
23- dict [ tag ] = action . type ;
24- } ) ;
25- } ) ;
26- return dict ;
27- }
7+ import { scrubberActionsToDict , scrubDir } from "./scrubUtils" ;
288
299async function getConfigFile ( filename : string ) : Promise < ScrubberConfig > {
3010 try {
@@ -36,122 +16,6 @@ async function getConfigFile(filename: string): Promise<ScrubberConfig> {
3616 }
3717}
3818
39- async function scrubFile (
40- filePath : string ,
41- tags : TagNameToAction ,
42- isDryRun : boolean ,
43- ) : Promise < void > {
44- return new Promise ( ( resolve , reject ) => {
45- fs . readFile ( filePath , { encoding : "utf8" } , async ( err , text ) => {
46- if ( err ) {
47- reject ( err ) ;
48- }
49-
50- const ext = filePath . split ( "." ) . pop ( ) ;
51- const commentType = ext && FILE_TYPE_COMMENT [ ext ] ;
52- const scrubbedLines : string [ ] = [ ] ;
53- let skip = false ;
54-
55- const lines : string [ ] = text . split ( "\n" ) ;
56-
57- for ( let i = 0 ; i < lines . length ; ++ i ) {
58- const line = lines [ i ] ;
59- let tryProcessTag = true ;
60-
61- if ( line . length === 0 ) {
62- scrubbedLines . push ( line ) ;
63- continue ;
64- }
65-
66- // Split on whitespace
67- const tokens = line . trim ( ) . split ( / [ ] + / ) ;
68-
69- if ( commentType ) {
70- if ( tokens [ 0 ] !== commentType ) {
71- tryProcessTag = false ;
72- }
73- tokens . shift ( ) ;
74- }
75-
76- if ( tryProcessTag ) {
77- if ( tokens [ 0 ] in tags && tokens . length !== 2 ) {
78- console . warn (
79- `WARNING line ${
80- i + 1
81- } : possible malformed tag; tags must be on their own line preceded by '}' or followed by '{'`,
82- ) ;
83- scrubbedLines . push ( line ) ;
84- continue ;
85- }
86-
87- if ( tokens [ 0 ] in tags || tokens [ 1 ] in tags ) {
88- const tag = tokens [ 0 ] in tags ? tokens [ 0 ] : tokens [ 1 ] ;
89- const brace = tag === tokens [ 0 ] ? tokens [ 1 ] : tokens [ 0 ] ;
90-
91- if ( brace === tokens [ 1 ] && brace !== TAG_START_CHAR ) {
92- throw new Error (
93- `Malformed tag ${ filePath } :line ${
94- i + 1
95- } : expected '{' after tag name`,
96- ) ;
97- }
98-
99- if ( brace === tokens [ 0 ] && brace !== TAG_END_CHAR ) {
100- throw new Error (
101- `Malformed tag ${ filePath } :line ${
102- i + 1
103- } : expected '}' before tag name`,
104- ) ;
105- }
106-
107- // NOTE: nested tagging is not currently expected and will lead to unexpected behaviour.
108-
109- if ( tags [ tag ] === "remove" ) {
110- skip = brace === TAG_START_CHAR ;
111- }
112-
113- // We always scrub tags from the final file.
114- continue ;
115- }
116- }
117-
118- if ( skip ) {
119- if ( isDryRun ) {
120- console . log ( `Skipping line ${ i + 1 } ` ) ;
121- }
122- continue ;
123- }
124-
125- scrubbedLines . push ( line ) ;
126- }
127-
128- if ( isDryRun ) return ;
129-
130- fs . writeFileSync ( filePath , scrubbedLines . join ( "\n" ) ) ;
131-
132- resolve ( ) ;
133- } ) ;
134- } ) ;
135- }
136-
137- async function scrubDir ( dir : string , tags : TagNameToAction , isDryRun : boolean ) {
138- const files = await fs . readdirSync ( dir ) ;
139- const promises = files . map (
140- async ( name : string ) : Promise < void > => {
141- const filePath = path . join ( dir , name ) ;
142- const stat = fs . statSync ( filePath ) ;
143- if ( stat . isFile ( ) ) {
144- return scrubFile ( filePath , tags , isDryRun ) ;
145- }
146- if ( stat . isDirectory ( ) ) {
147- return scrubDir ( filePath , tags , isDryRun ) ;
148- }
149- return Promise . resolve ( ) ;
150- } ,
151- ) ;
152- await Promise . all ( promises ) ;
153- }
154-
15519class Scrubber {
15620 tags : TagNameToAction = { } ;
15721
@@ -168,11 +32,11 @@ class Scrubber {
16832 async start (
16933 actions : ScrubberAction [ ] ,
17034 isDryRun : boolean = false ,
171- ) : Promise < void > {
35+ ) : Promise < void [ ] [ ] > {
17236 const tags = { ...this . tags , ...scrubberActionsToDict ( actions ) } ;
17337
17438 // TODO: specify file extensions?
175- await Promise . all ( this . dirs . map ( ( dir ) => scrubDir ( dir , tags , isDryRun ) ) ) ;
39+ return Promise . all ( this . dirs . map ( ( dir ) => scrubDir ( dir , tags , isDryRun ) ) ) ;
17640 }
17741}
17842
0 commit comments