|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import { |
| 4 | + ScrubberAction, |
| 5 | + TagNameToAction, |
| 6 | + ScrubberConfig, |
| 7 | +} 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 | +} |
| 28 | + |
| 29 | +async function getConfigFile(filename: string): Promise<ScrubberConfig> { |
| 30 | + try { |
| 31 | + const configString = await fs.readFileSync(filename, "utf8"); |
| 32 | + return JSON.parse(configString); |
| 33 | + } catch (err) { |
| 34 | + console.error("Failed to read file ", filename); |
| 35 | + throw err; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 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 | + |
| 155 | +class Scrubber { |
| 156 | + tags: TagNameToAction = {}; |
| 157 | + |
| 158 | + dirs: string[] = []; |
| 159 | + |
| 160 | + async parseConfig(filename: string): Promise<void> { |
| 161 | + // TODO validate config (e.g.properly formed tag names) |
| 162 | + const config = await getConfigFile(filename); |
| 163 | + this.tags = scrubberActionsToDict(config.actions); |
| 164 | + this.dirs = config.dirs; |
| 165 | + } |
| 166 | + |
| 167 | + // Scrub files |
| 168 | + async start( |
| 169 | + actions: ScrubberAction[], |
| 170 | + isDryRun: boolean = false, |
| 171 | + ): Promise<void> { |
| 172 | + const tags = { ...this.tags, ...scrubberActionsToDict(actions) }; |
| 173 | + |
| 174 | + // TODO: specify file extensions? |
| 175 | + await Promise.all(this.dirs.map((dir) => scrubDir(dir, tags, isDryRun))); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +export default Scrubber; |
0 commit comments