Skip to content

Commit

Permalink
Work on the worker path fix
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdee committed Jan 24, 2022
1 parent 464a4d9 commit 0c4d5c2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
38 changes: 33 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "denomo-cli",
"private": false,
"version": "1.0.0",
"version": "1.0.1",
"description": "Denomo is a CLI utility that deletes nested 'node_modules' directories",
"author": {
"name": "Peter Dyumin",
Expand All @@ -16,7 +16,7 @@
"lint": "eslint",
"prepublishOnly": "npm run build",
"pretest": "npm run build",
"test": "mocha \"build/tests/*.spec.js\""
"test": "cross-env TESTING=true mocha \"build/tests/*.spec.js\""
},
"homepage": "https://github.com/julyskies/denomo-cli",
"license": "MIT",
Expand All @@ -42,6 +42,7 @@
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"chai": "^4.3.4",
"cross-env": "^7.0.3",
"eslint": "^8.6.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.25.4",
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const ERROR_MESSAGES = {
couldNotAccessTheModule: 'Could not access the module!',
couldNotAccessTheProvidedPath: 'Could not access the provided path!',
pathIsRequired: 'Path is required!',
pleaseProvideThePath: 'Please provide the path!',
Expand Down
22 changes: 19 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { ChildProcess, fork } from 'child_process';
import { ChildProcess, exec, fork } from 'child_process';
import { cpus } from 'os';
import { promisify } from 'util';
import { stat } from 'fs/promises';

import { ERROR_MESSAGES, HANDLER_TYPES } from './constants';
import logger from './logger';
import { MessageToParent, NodeError } from './types';

const execPromise = promisify(exec);

const DIRECTORIES: string[] = [];
const MAX_WORKERS: number = cpus().length;

let START_TIME: number;
let WORKER_PATH: string;
let WORKERS: ChildProcess[] = [];

/**
Expand All @@ -34,7 +38,7 @@ function handler(type: string, payload: ChildProcess | string[]): never | void {

// eslint-disable-next-line
for (const path of paths) {
const newWorker = fork(`${process.cwd()}/build/worker.js`);
const newWorker = fork(WORKER_PATH);
newWorker.send({ path });
newWorker.on(
HANDLER_TYPES.close,
Expand Down Expand Up @@ -64,6 +68,18 @@ function handler(type: string, payload: ChildProcess | string[]): never | void {
export default async function main(): Promise<Error | void> {
START_TIME = Date.now();

try {
const { stdout = '', stderr = '' } = await execPromise('npm root -g');
if (stderr || !stdout) {
throw new Error(ERROR_MESSAGES.couldNotAccessTheModule);
}
WORKER_PATH = process.env.TESTING
? `${process.cwd()}/build/worker.js`
: `${stdout.trim()}/denomo-cli/build/worker.js`;
} catch {
throw new Error(ERROR_MESSAGES.couldNotAccessTheModule);
}

const [, , entryPoint = ''] = process.argv;
if (!entryPoint) {
throw new Error(ERROR_MESSAGES.pleaseProvideThePath);
Expand All @@ -90,7 +106,7 @@ export default async function main(): Promise<Error | void> {
}

if (DIRECTORIES.length > 0) {
const entryWorker = fork(`${process.cwd()}/build/worker.js`);
const entryWorker = fork(WORKER_PATH);
entryWorker.send({ path: DIRECTORIES[0] });
DIRECTORIES.splice(0);
entryWorker.on(
Expand Down

0 comments on commit 0c4d5c2

Please sign in to comment.