Skip to content

Commit

Permalink
Add advanced flag & scanSpeed option
Browse files Browse the repository at this point in the history
  • Loading branch information
VandeurenGlenn committed Aug 11, 2024
1 parent 8f31de4 commit 57bb3a0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ The interactive CLI can guide you through the following steps:
npx esperf
```

## Advanced Usage

The interactive CLI will show more steps:

- Set scan speed

```sh
npx esperf --advanced
```

## License

MIT
60 changes: 59 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ 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';

let advanced = false;

for (let i = 0; i < process.argv.length; ++i) {
if (process.argv[i] === '--advanced') advanced = true;
}

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,7 +140,41 @@ async function runModuleReplacements(): Promise<void> {
cl.confirm({
message: 'Automatically uninstall packages?',
initialValue: false
})
}),
scanSpeed: () =>
advanced
? 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: 'medium'
})
: Promise.resolve('medium')
},
{
onCancel: () => {
Expand Down Expand Up @@ -198,10 +254,12 @@ async function runModuleReplacements(): Promise<void> {

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

const scanFilesResult = await scanFiles(
files,
manifestReplacements,
threads,
scanSpinner
);

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

0 comments on commit 57bb3a0

Please sign in to comment.