Skip to content

Commit

Permalink
workers/scan-file: readFile -> open
Browse files Browse the repository at this point in the history
  • Loading branch information
VandeurenGlenn committed Aug 11, 2024
1 parent 88dab32 commit e0d82e4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
54 changes: 52 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ import {scanFiles} from './stages/scan-files.js';
import {fixFiles} from './stages/fix-files.js';
import {traverseFiles} from './stages/traverse-files.js';
import {scanDependencies} from './stages/scan-dependencies.js';
import {availableParallelism} from 'node:os';

function getWantedThreads(scanSpeed: string): number {
const threads = availableParallelism();
switch (scanSpeed) {
case 'slow':
return threads * 0.25;
case 'medium':
return threads * 0.5;
case 'fast':
return threads * 0.75;
case 'fastest':
return threads;
}
return 1;
}

const availableManifests: Record<string, modReplacements.ManifestModule> = {
native: modReplacements.nativeReplacements,
Expand Down Expand Up @@ -118,6 +134,38 @@ async function runModuleReplacements(): Promise<void> {
cl.confirm({
message: 'Automatically uninstall packages?',
initialValue: false
}),
scanSpeed: () =>
cl.select({
message: 'Preferred scan speed',
options: [
{
value: 'fastest',
label: 'Fastest',
hint: 'uses all the threads, pushes cpu to 100%'
},
{
value: 'fast',
label: 'Fast',
hint: '75% of available threads'
},
{
value: 'medium',
label: 'Medium',
hint: '50% of available threads'
},
{
value: 'slow',
label: 'Slow',
hint: '25% of available threads'
},
{
value: 'slowest',
label: 'Slowest',
hint: 'disables parallelism, 1 thread'
}
],
initialValue: 'fast'
})
},
{
Expand Down Expand Up @@ -198,13 +246,15 @@ async function runModuleReplacements(): Promise<void> {

try {
const files = await traverseFiles(options.filesDir);

const threads = getWantedThreads(options.scanSpeed);
console.time('scan');
const scanFilesResult = await scanFiles(
files,
manifestReplacements,
threads,
scanSpinner
);

console.timeEnd('scan');
if (scanFilesResult.length > 0) {
dependenciesFound = true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/stages/scan-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const available = availableParallelism();
export function scanFiles(
files: string[],
replacements: modReplacements.ModuleReplacement[],
threads: number,
spinner: ReturnType<typeof cl.spinner>
): Promise<FileReplacement[]> {
return new Promise((resolve, reject) => {
Expand All @@ -21,7 +22,7 @@ export function scanFiles(
const filesLength = files.length;
const results: FileReplacement[] = [];

for (const file of files.splice(0, available)) {
for (const file of files.splice(0, threads)) {
const worker = new Worker(`${__dirname}/workers/scan-file.js`);
// todo, what todo with the errors?
worker.on('error', (error) => reject(error.message));
Expand Down
6 changes: 4 additions & 2 deletions src/stages/workers/scan-file.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as modReplacements from 'module-replacements';
import {ts as sg} from '@ast-grep/napi';
import {readFile} from 'node:fs/promises';
import dedent from 'dedent';
import pc from 'picocolors';
import {type FileReplacement} from './../../shared-types.js';
import {suggestReplacement} from './../../suggest-replacement.js';
import {parentPort} from 'node:worker_threads';
import {open} from 'node:fs/promises';

async function scanFile(
filePath: string,
Expand Down Expand Up @@ -85,9 +85,11 @@ async function scanTask(
replacements: modReplacements.ModuleReplacement[]
) {
try {
const contents = await readFile(file, 'utf8');
const fd = await open(file);
const contents = await fd.readFile({encoding: 'utf-8'});
const lines = contents.split('\n');
const scanResult = await scanFile(file, contents, lines, replacements);
await fd.close();

parentPort?.postMessage({type: 'result', value: scanResult});
} catch (err) {
Expand Down

0 comments on commit e0d82e4

Please sign in to comment.