Skip to content

Commit

Permalink
fix: decomment code with parser in worker thread
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzhao committed Dec 4, 2020
1 parent 57aa3d1 commit 5e3a588
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 22 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@octokit/rest": "^16.43.1",
"@sentry/electron": "^2.0.1",
"classnames": "^2.2.6",
"decomment": "^0.9.3",
"electron-default-menu": "^1.0.2",
"electron-devtools-installer": "^3.1.1",
"electron-squirrel-startup": "^1.0.0",
Expand Down Expand Up @@ -71,6 +72,7 @@
"@octokit/action": "^2.0.0",
"@types/classnames": "^2.2.10",
"@types/enzyme": "^3.10.7",
"@types/decomment": "^0.9.0",
"@types/fs-extra": "^9.0.2",
"@types/jest": "^26.0.14",
"@types/log-symbols": "^3.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/main/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function getMainWindowOptions(): Electron.BrowserWindowConstructorOptions
webviewTag: false,
nodeIntegration: true,
enableRemoteModule: true,
nodeIntegrationInWorker: true,
},
};
}
Expand Down
44 changes: 29 additions & 15 deletions src/renderer/npm.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EditorValues } from '../interfaces';
import { exec } from '../utils/exec';

const { builtinModules } = require('module');
import { builtinModules } from 'module';

export type IPackageManager = 'npm' | 'yarn';

Expand All @@ -22,7 +22,7 @@ const ignoredModules: Array<string> = [
];

/* regular expression to both match and extract module names */
const requiregx = /^.*require\(['"](.*?)['"]\)/gm;
const requiregx = /require\(['"](.*?)['"]\)/gm;

/*
Quick and dirty filter functions for filtering module names
Expand Down Expand Up @@ -73,20 +73,21 @@ export async function getIsPackageManagerInstalled(
/**
* Finds npm modules in editor values, returning an array of modules.
*/
export function findModulesInEditors(values: EditorValues) {
export async function findModulesInEditors(values: EditorValues) {
const files = [values.main, values.renderer, values.preload];
const modules = files.reduce(
(agg, file) => [...agg, ...findModules(file)],
[],
);
const modules: Array<string> = [];

for (const file of files) {
modules.push(...(await findModules(file)));
}

console.log('Modules Found:', modules);

return Array.from(new Set(modules));
}

/**
* Uses a simple regex to find `require()` statements in a string.
* Finds `require()` statements in a string.
* Tries to exclude electron and Node built-ins as well as file-path
* references. Also will try to install base packages of modules
* that have a slash in them, for example: `lodash/fp` as the actual package
Expand All @@ -98,18 +99,18 @@ export function findModulesInEditors(values: EditorValues) {
* @param {string} input
* @returns {Array<string>}
*/
export function findModules(input: string): Array<string> {
export async function findModules(input: string) {
/* container definitions */
const modules: Array<string> = [];
let match: RegExpMatchArray | null;

/* decomment code with the esprima parser */
const code = await decommentWithWorker(input);

/* grab all global require matches in the text */
while ((match = requiregx.exec(input) || null)) {
// ensure commented-out requires aren't downloaded
if (!match[0].startsWith('//')) {
const mod = match[1];
modules.push(mod);
}
while ((match = requiregx.exec(code) || null)) {
const mod = match[1];
modules.push(mod);
}

/* map and reduce */
Expand Down Expand Up @@ -159,3 +160,16 @@ export function packageRun(
): Promise<string> {
return exec(dir, `${packageManager} run ${command}`);
}

function decommentWithWorker(input: string): Promise<string> {
return new Promise((resolve, reject) => {
const worker = new Worker('../utils/decomment.ts');
worker.postMessage(input);
worker.onmessage = function (event: MessageEvent<string>) {
resolve(event.data);
};
worker.onerror = function (e) {
reject(e);
};
});
}
5 changes: 5 additions & 0 deletions src/utils/decomment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import decomment from 'decomment';

onmessage = function (event: MessageEvent<string>) {
postMessage(decomment(event.data));
};
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"sourceMap": true,
"lib": [
"es2017",
"dom"
"dom",
"WebWorker"
],
"noImplicitAny": true,
"noImplicitReturns": true,
Expand Down Expand Up @@ -41,4 +42,4 @@
"indentSize": 2,
"tabSize": 2
}
}
}
22 changes: 17 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,11 @@
resolved "https://registry.yarnpkg.com/@types/classnames/-/classnames-2.2.10.tgz#cc658ca319b6355399efc1f5b9e818f1a24bf999"
integrity sha512-1UzDldn9GfYYEsWWnn/P4wkTlkZDH7lDb0wBMGbtIQc9zXEQq7FlKBdZUn6OBqD8sKZZ2RQO2mAjGpXiDGoRmQ==

"@types/decomment@^0.9.0":
version "0.9.0"
resolved "https://registry.yarnpkg.com/@types/decomment/-/decomment-0.9.0.tgz#ef4701e474eef774e9ddd65e4e85f6c3dab5ce9f"
integrity sha512-3K8zqMlLQREJtIMboTH9GyuX/CSL5cFg+B1gVezUdEJ9lm8IPIJzO+WH0L1sQnkHIdU8Wz65rzfrXb+NojWr1Q==

"@types/dom4@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@types/dom4/-/dom4-2.0.1.tgz#506d5781b9bcab81bd9a878b198aec7dee2a6033"
Expand Down Expand Up @@ -3974,6 +3979,13 @@ decode-uri-component@^0.2.0:
resolved "https://registry.yarnpkg.com/decode-uri-component/-/decode-uri-component-0.2.0.tgz#eb3913333458775cb84cd1a1fae062106bb87545"
integrity sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU=

decomment@^0.9.3:
version "0.9.3"
resolved "https://registry.yarnpkg.com/decomment/-/decomment-0.9.3.tgz#b913f32e5fe1113848f516caa5c7afefa9544d38"
integrity sha512-5skH5BfUL3n09RDmMVaHS1QGCiZRnl2nArUwmsE9JRY93Ueh3tihYl5wIrDdAuXnoFhxVis/DmRWREO2c6DG3w==
dependencies:
esprima "4.0.1"

decompress-response@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/decompress-response/-/decompress-response-3.3.0.tgz#80a4dd323748384bfa248083622aedec982adff3"
Expand Down Expand Up @@ -4982,16 +4994,16 @@ espree@^7.3.0:
acorn-jsx "^5.2.0"
eslint-visitor-keys "^1.3.0"

esprima@4.0.1, esprima@^4.0.0, esprima@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==

esprima@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-3.1.3.tgz#fdca51cee6133895e3c88d535ce49dbff62a4633"
integrity sha1-/cpRzuYTOJXjyI1TXOSdv/YqRjM=

esprima@^4.0.0, esprima@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==

esquery@^1.0.1, esquery@^1.2.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.3.1.tgz#b78b5828aa8e214e29fb74c4d5b752e1c033da57"
Expand Down

0 comments on commit 5e3a588

Please sign in to comment.