feat: update package.json to include mocha and chai for testing, add ansi-colors and fastest-levenshtein dependencies
refactor: modify sliceAndWriteCalls function to accept folderPath parameter and update driver function to use it fix: enhance isNodeModule function to utilize builtinModules for better module detection chore: remove unused constants file from utils test: clean up instantiation.spec.js by removing commented assertions
This commit is contained in:
@@ -11,8 +11,9 @@ import { LibraryTypesRecorder } from './libcalls.mjs';
|
||||
/**
|
||||
*
|
||||
* @param {ReturnType<LibraryTypesRecorder['generateAllArgumentsForRecordedCalls']>} calls
|
||||
* @param {string} folderPath
|
||||
*/
|
||||
export async function sliceAndWriteCalls(calls) {
|
||||
export async function sliceAndWriteCalls(calls, folderPath) {
|
||||
const writePromises = [];
|
||||
|
||||
for (const [moduleName, callBox] of calls) {
|
||||
@@ -24,8 +25,9 @@ export async function sliceAndWriteCalls(calls) {
|
||||
|
||||
// const relatedModuleNamePath = import.meta.resolve(moduleName);
|
||||
// console.log(`Related module path`, relatedModuleNamePath);
|
||||
|
||||
const relatedModuleNamePath = await wpCompress(moduleName)
|
||||
console.log("[wp] Compressing module", moduleName);
|
||||
// throw Error("Module slicing not implemented yet");
|
||||
const relatedModuleNamePath = await wpCompress(moduleName,folderPath );
|
||||
const fileSource = readFileSync(relatedModuleNamePath).toString('utf-8');
|
||||
// continue; // TODO - handle relative modules
|
||||
const { slicedCode } = getSliceAndInfoSync(fileSource, (moduleExports) => {
|
||||
@@ -66,11 +68,12 @@ export async function sliceAndWriteCalls(calls) {
|
||||
}).catch(console.log);
|
||||
}
|
||||
|
||||
// is-glob WORKED
|
||||
/**
|
||||
*
|
||||
* @param {string} filePath
|
||||
*/
|
||||
function driver(folderPath = './test_src/anymatch') {
|
||||
function driver(folderPath = './candidates/braces') {
|
||||
// const FILE_PATH = './test_src/index.cjs';
|
||||
|
||||
const project = new Project({ compilerOptions: { allowJs: true, checkJs: false, } });
|
||||
@@ -96,15 +99,19 @@ function driver(folderPath = './test_src/anymatch') {
|
||||
const callMap = libraryTypesRecorder.generateAllArgumentsForRecordedCalls();
|
||||
|
||||
|
||||
logCallList(callMap, 'FakeModuleName');
|
||||
sliceAndWriteCalls(callMap).then(() => {
|
||||
logCallList(callMap, folderPath);
|
||||
sliceAndWriteCalls(callMap, folderPath).then(() => {
|
||||
console.log("Slicing and writing calls done");
|
||||
});
|
||||
}
|
||||
|
||||
if (process.argv[1] === import.meta.filename) {
|
||||
console.log("[SafeImport] started");
|
||||
driver();
|
||||
if(process.argv.length >2 && process.argv[2] !== '') {
|
||||
console.log(`[SafeImport] started ${process.argv[2]}`);
|
||||
driver(process.argv[2]);
|
||||
}else{
|
||||
console.log("[SafeImport] started");
|
||||
driver();}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -2,6 +2,7 @@
|
||||
import path from 'path';
|
||||
import tsm, { Identifier, ImportSpecifier, StringLiteral, SyntaxKind, ts, } from 'ts-morph';
|
||||
import { LibraryTypesRecorder } from './libcalls.mjs';
|
||||
import {builtinModules} from 'node:module'
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -50,6 +51,7 @@ export function getImportCallsAndArgumentTypes(importDecls, checker, mainFilePat
|
||||
// const importArgs = importDecl.getArguments();
|
||||
|
||||
const parent = importDecl.getParent();
|
||||
console.log("Parent of import call", parent?.getKindName(), parent?.getText());
|
||||
if (parent?.isKind(SyntaxKind.VariableDeclaration)) {
|
||||
// this is a variable declaration
|
||||
const varDecl = parent;
|
||||
@@ -76,9 +78,6 @@ export function getImportCallsAndArgumentTypes(importDecls, checker, mainFilePat
|
||||
console.log("Variable name", varName);
|
||||
// check if declaration is identifier or object pattern
|
||||
}
|
||||
console.log("Require arguments. Skipping");
|
||||
continue;
|
||||
throw Error("Not implemented yet");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,8 +297,11 @@ export function isRelativeModule(moduleName) {
|
||||
* @returns
|
||||
*/
|
||||
export function isNodeModule(moduleName) {
|
||||
if (moduleName.startsWith('node:')) return true;
|
||||
const nodeModules = ['fs', 'fs/promises', 'path', 'http', 'https', 'os', 'crypto','assert'];
|
||||
return nodeModules.includes(moduleName);
|
||||
if (builtinModules.includes(moduleName) ) return true;
|
||||
if (moduleName.startsWith('node:')) {
|
||||
return builtinModules.includes(moduleName.substring(5));// strip node: prefix
|
||||
}
|
||||
// const nodeModules = ['fs', 'fs/promises', 'path', 'http', 'https', 'os', 'crypto','assert'];
|
||||
// return nodeModules.includes(moduleName);
|
||||
}
|
||||
|
||||
|
@@ -1,3 +0,0 @@
|
||||
|
||||
export const prependString = `(function(exports, require, module, __filename, __dirname) {\n`
|
||||
export const appendString = `\n});`
|
Reference in New Issue
Block a user