This repository has been archived by the owner on May 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Parallel Jetifier, scale up to cpu count (#24)
* feat: parallel files jetifying * fix: typo * removed unnecessary console.logs
- Loading branch information
Showing
5 changed files
with
62 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,19 @@ | ||
const { readFileSync, writeFileSync } = require('fs'); | ||
const { loadCSV, readDir } = require('./src/utils'); | ||
const { fork } = require('child_process'); | ||
const { join } = require('path'); | ||
const { getClassesMapping, readDir, chunk } = require('./src/utils'); | ||
|
||
const cpus = require('os').cpus().length; | ||
|
||
const arg = process.argv.slice(2)[0]; | ||
const mode = arg && ((arg === 'reverse') || (arg === '-r')) ? 'reverse' : 'forward'; | ||
|
||
const SEARCH_DIR = 'node_modules'; | ||
|
||
const csv = loadCSV(); | ||
const classesMapping = getClassesMapping(); | ||
const files = readDir(SEARCH_DIR); | ||
|
||
for (const file of files) { | ||
let data = readFileSync(file, { encoding: 'utf8' }); | ||
for (const c in csv) { | ||
if (data.includes(mode === 'forward' ? c : csv[c])) { | ||
console.log(`${mode}-jetifying: ${file}`); | ||
data = mode === 'forward' ? data.replace(new RegExp(c, 'g'), csv[c]) : data.replace(new RegExp(csv[c], 'g'), c); | ||
writeFileSync(file, data, { encoding: 'utf8' }); | ||
} | ||
} | ||
} | ||
console.log(`Jetifier found ${files.length} file(s) to ${mode}-jetify. Using ${cpus} workers...`); | ||
|
||
for (const filesChunk of chunk(files, cpus)) { | ||
const worker = fork(join(__dirname, 'src', 'worker.js')); | ||
worker.send({ filesChunk, classesMapping, mode }); | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { readFileSync, writeFileSync } = require('fs'); | ||
|
||
process.on('message', ({ filesChunk, classesMapping, mode }) => { | ||
|
||
for (const file of filesChunk) { | ||
let data = readFileSync(file, { encoding: 'utf8' }); | ||
for (const [oldClass, newClass] of classesMapping) { | ||
if (data.includes(mode === 'forward' ? oldClass : newClass)) { | ||
data = mode === 'forward' ? | ||
data.replace(new RegExp(oldClass, 'g'), newClass) : | ||
data.replace(new RegExp(newClass, 'g'), oldClass); | ||
writeFileSync(file, data, { encoding: 'utf8' }); | ||
} | ||
} | ||
} | ||
|
||
process.exit(0); | ||
}); |