[add] add LibraryCallsRecorder, integrate faker for argument generation
This commit is contained in:
83
src/bundle/index.mjs
Normal file
83
src/bundle/index.mjs
Normal file
@@ -0,0 +1,83 @@
|
||||
import wp from 'webpack';
|
||||
import path from 'node:path'
|
||||
|
||||
|
||||
if (process.argv[1] === import.meta.filename) {
|
||||
console.log("[SafePack] started");
|
||||
main();
|
||||
console.log("done");
|
||||
}
|
||||
|
||||
function main() {
|
||||
const ls = [
|
||||
'classnames',
|
||||
'semver',
|
||||
'ansi-styles',
|
||||
// 'debug',
|
||||
// 'supports-color',
|
||||
'chalk',
|
||||
'ms',
|
||||
'minimatch',
|
||||
'strip-ansi',
|
||||
'tslib',
|
||||
'has-flag',
|
||||
'ansi-regex',
|
||||
'color-convert',
|
||||
'color-name',
|
||||
// 'type-fest',
|
||||
'string-width'
|
||||
];
|
||||
|
||||
ls.forEach(l => {
|
||||
|
||||
wpCompress(l).then(outputFileLocation => {
|
||||
console.log("[wp] success", outputFileLocation);
|
||||
}).catch(err => {
|
||||
console.error("[failed wp]", l);
|
||||
console.error("[wp] error");
|
||||
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
export function wpCompress(l, outputPath = path.resolve('./output/')) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const libraryLocation = import.meta.resolve(l);
|
||||
console.log(libraryLocation);
|
||||
const outputFile = l + '.bundle.cjs';
|
||||
// throw Error("5");
|
||||
wp({
|
||||
entry: libraryLocation,
|
||||
mode: 'production',
|
||||
optimization: {
|
||||
mangleExports: false,
|
||||
avoidEntryIife: true,
|
||||
minimize: false,
|
||||
|
||||
},
|
||||
output: {
|
||||
path: outputPath,
|
||||
filename: outputFile,
|
||||
clean: false,
|
||||
iife: false,
|
||||
library: {
|
||||
type: 'commonjs2',
|
||||
// name: l
|
||||
}
|
||||
// module: true
|
||||
},
|
||||
}, (err, stats) => {
|
||||
if (err || stats.hasErrors()) {
|
||||
// console.log(err?.stack);
|
||||
// console.log(stats?.hasErrors());
|
||||
// console.log(stats?.toJson());
|
||||
reject(err || stats);
|
||||
}else{
|
||||
resolve(path.resolve(outputPath, outputFile));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@@ -1,16 +1,16 @@
|
||||
|
||||
|
||||
import { readFileSync ,realpathSync ,mkdirSync} from 'node:fs';
|
||||
import { readFileSync ,mkdirSync} from 'node:fs';
|
||||
import { writeFile } from 'node:fs/promises';
|
||||
import tsm, { Project, SyntaxKind ,ts} from 'ts-morph';
|
||||
import { Project} from 'ts-morph';
|
||||
import {getSliceAndInfoSync} from 'slice-js/src/slice-code/test/helpers/utils.js';
|
||||
import path, { dirname,join } from 'node:path';
|
||||
import path from 'node:path';
|
||||
import { getImportCallsAndArgumentTypes } from './tsCalls.mjs';
|
||||
import { LibraryCallsRecorder } from './libcalls.mjs';
|
||||
import { wpCompress } from '../src_bundle/index.mjs';
|
||||
import { wpCompress } from './bundle/index.mjs';
|
||||
import { LibraryTypesRecorder } from './libcalls.mjs';
|
||||
/**
|
||||
*
|
||||
* @param {LibraryCallsRecorder['calls']} calls
|
||||
* @param {ReturnType<LibraryTypesRecorder['generateAllArgumentsForRecordedCalls']>} calls
|
||||
* @param {string} FILE_PATH
|
||||
*/
|
||||
export async function sliceAndWriteCalls(calls, FILE_PATH) {
|
||||
|
30
src/js/LibraryCallsRecorder.mjs
Normal file
30
src/js/LibraryCallsRecorder.mjs
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Record library calls
|
||||
*/
|
||||
|
||||
export class LibraryCallsRecorder {
|
||||
/**
|
||||
* @type {Map<string,Map<string,GenericLiteralType[][]>>}
|
||||
*/
|
||||
#calls = new Map();
|
||||
/**
|
||||
*
|
||||
* @param {string} moduleName
|
||||
* @param {string} libraryFunctionSegment
|
||||
* @param {any[]} argumentsCalled
|
||||
*/
|
||||
pushToMap(moduleName, libraryFunctionSegment, argumentsCalled) {
|
||||
const modulePortion = this.#calls.get(moduleName) ?? new Map();
|
||||
|
||||
const defArgs = modulePortion.get(libraryFunctionSegment) ?? [];
|
||||
defArgs.push(argumentsCalled);
|
||||
|
||||
modulePortion.set(libraryFunctionSegment, defArgs);
|
||||
this.#calls.set(moduleName, modulePortion);
|
||||
}
|
||||
|
||||
get calls() {
|
||||
return this.#calls;
|
||||
}
|
||||
|
||||
}
|
205
src/libcalls.mjs
205
src/libcalls.mjs
@@ -1,82 +1,64 @@
|
||||
|
||||
//@ts-check
|
||||
/**
|
||||
* @typedef {import('estree').Literal["value"]} GenericLiteralType
|
||||
*/
|
||||
|
||||
import tsm, { Type } from 'ts-morph';
|
||||
|
||||
/**
|
||||
* Record library calls
|
||||
*/
|
||||
export class LibraryCallsRecorder{
|
||||
/**
|
||||
* @type {Map<string,Map<string,GenericLiteralType[][]>>}
|
||||
*/
|
||||
#calls = new Map();
|
||||
/**
|
||||
*
|
||||
* @param {string} moduleName
|
||||
* @param {string} libraryFunctionSegment
|
||||
* @param {any[]} argumentsCalled
|
||||
*/
|
||||
pushToMap(moduleName, libraryFunctionSegment, argumentsCalled){
|
||||
const modulePortion = this.#calls.get(moduleName)?? new Map();
|
||||
|
||||
const defArgs = modulePortion.get(libraryFunctionSegment) ?? [];
|
||||
defArgs.push(argumentsCalled);
|
||||
|
||||
modulePortion.set(libraryFunctionSegment,defArgs);
|
||||
this.#calls.set(moduleName, modulePortion);
|
||||
}
|
||||
|
||||
get calls(){
|
||||
return this.#calls;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class LibraryTypesRecorder{
|
||||
import { simpleFaker, faker } from '@faker-js/faker'
|
||||
export class LibraryTypesRecorder {
|
||||
/**
|
||||
* @type {Map<string,Map<string,Type[][]>>}
|
||||
*/
|
||||
#calls = new Map();
|
||||
/**
|
||||
* @param {tsm.TypeChecker} checker
|
||||
* @type {tsm.TypeChecker} checker
|
||||
*/
|
||||
checker;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {tsm.TypeChecker} checker
|
||||
*/
|
||||
constructor(checker) {
|
||||
this.checker = checker;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param {string} moduleName
|
||||
* @param {string} libraryFunctionSegment
|
||||
* @param {Type[]} argumentsCalled
|
||||
*/
|
||||
pushToMap(moduleName, libraryFunctionSegment, argumentsCalled){
|
||||
const modulePortion = this.#calls.get(moduleName)?? new Map();
|
||||
|
||||
pushToMap(moduleName, libraryFunctionSegment, argumentsCalled) {
|
||||
const modulePortion = this.#calls.get(moduleName) ?? new Map();
|
||||
|
||||
const defArgs = modulePortion.get(libraryFunctionSegment) ?? [];
|
||||
defArgs.push(argumentsCalled);
|
||||
|
||||
modulePortion.set(libraryFunctionSegment,defArgs);
|
||||
modulePortion.set(libraryFunctionSegment, defArgs);
|
||||
this.#calls.set(moduleName, modulePortion);
|
||||
}
|
||||
|
||||
get calls(){
|
||||
get calls() {
|
||||
return this.#calls;
|
||||
}
|
||||
|
||||
generateAllArgumentsForRecordedCalls(){
|
||||
generateAllArgumentsForRecordedCalls() {
|
||||
/**
|
||||
* @type {Map<string,Map<string,(GenericLiteralType|null|undefined|{})[][]>>}
|
||||
*/
|
||||
const callMap = new Map();
|
||||
for(const [moduleName, modulePortion] of this.#calls){
|
||||
for (const [moduleName, modulePortion] of this.#calls) {
|
||||
/**
|
||||
* @type {Map<string,(GenericLiteralType|null|undefined|{})[][]>}
|
||||
*/
|
||||
const moduleCallMap = new Map();// todo refactor
|
||||
for(const [libraryFunctionSegment, argsList] of modulePortion){
|
||||
const argsForFunction = argsList.map(args=>args.map(arg=>this.instantiateType(arg)));
|
||||
moduleCallMap.set(libraryFunctionSegment,argsForFunction);
|
||||
for (const [libraryFunctionSegment, argsList] of modulePortion) {
|
||||
const argsForFunctionSimple = argsList.map(args => args.map(arg => this.instantiateType(arg)));
|
||||
const argsForFunction = argsList.flatMap(args => simpleFaker.helpers.multiple(()=> args.map(arg => this.instantiateFakerOnType(arg))));
|
||||
|
||||
moduleCallMap.set(libraryFunctionSegment, argsForFunction);
|
||||
}
|
||||
callMap.set(moduleName,moduleCallMap);
|
||||
callMap.set(moduleName, moduleCallMap);
|
||||
}
|
||||
return callMap;
|
||||
}
|
||||
@@ -87,50 +69,137 @@ export class LibraryTypesRecorder{
|
||||
* @param {string} libraryFunctionSegment
|
||||
* @returns {(GenericLiteralType|null|undefined|{})[][]|undefined}
|
||||
*/
|
||||
generateArgumentsForCall(moduleName, libraryFunctionSegment){
|
||||
generateArgumentsForCall(moduleName, libraryFunctionSegment) {
|
||||
const modulePortion = this.#calls.get(moduleName);
|
||||
if(modulePortion===undefined){
|
||||
if (modulePortion === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
||||
const argsTypesForFunctionCalls = modulePortion.get(libraryFunctionSegment);
|
||||
if(argsTypesForFunctionCalls===undefined){
|
||||
if (argsTypesForFunctionCalls === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
return argsTypesForFunctionCalls.map(argTypeForSingleCall=>{
|
||||
return argTypeForSingleCall.map(type=>{
|
||||
return LibraryTypesRecorder.instantiateType(type);
|
||||
|
||||
return argsTypesForFunctionCalls.map(argTypeForSingleCall => {
|
||||
return argTypeForSingleCall.map(type => {
|
||||
return this.instantiateType(type);
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param {Type} type
|
||||
*/
|
||||
instantiateFakerOnType(type) {
|
||||
const literalValue = type.getLiteralValue();
|
||||
if (literalValue !== undefined) {
|
||||
return literalValue;
|
||||
} else if (type.isUndefined()) {
|
||||
return undefined;
|
||||
} else if (type.isString()) {
|
||||
return simpleFaker.string.alphanumeric();
|
||||
} else if (type.isNumber()) {
|
||||
return simpleFaker.number.int();
|
||||
} else if (type.isBoolean()) {
|
||||
return simpleFaker.datatype.boolean();
|
||||
} else if (type.isArray()) {
|
||||
return []// TODO - handle arrays;
|
||||
} else if (type.isObject()) {
|
||||
const newObj = {};
|
||||
for (const prop of type.getProperties()) {
|
||||
const propName = prop.getName();
|
||||
const declarations = prop.getDeclarations();
|
||||
let propType = prop.getDeclaredType();
|
||||
if (declarations.length !== 1) {
|
||||
console.warn("Multiple declarations for property", propName, "in type", type.getText());
|
||||
} else {
|
||||
propType = this.checker.getTypeOfSymbolAtLocation(prop, declarations[0]);
|
||||
}
|
||||
newObj[propName] = this.instantiateFakerOnType(propType);
|
||||
}
|
||||
// TODO - handle functions
|
||||
return newObj;
|
||||
} else {
|
||||
console.warn("Unknown type to instantiate", type.getText());
|
||||
if (type.isAny()) {
|
||||
return simpleFaker.helpers.arrayElement([
|
||||
simpleFaker.string.sample(),
|
||||
simpleFaker.number.int(),
|
||||
simpleFaker.datatype.boolean(),
|
||||
{},
|
||||
[]
|
||||
]);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param {Type} type
|
||||
* @returns
|
||||
*/
|
||||
instantiateType(type){
|
||||
if(type.isStringLiteral()){
|
||||
return type.getLiteralValue();
|
||||
}else if(type.isNumberLiteral()){
|
||||
return Number(type.getText());
|
||||
}else if(type.isBooleanLiteral()){
|
||||
return type.getText() === 'true';
|
||||
}else if(type.isString()){
|
||||
instantiateType(type) {
|
||||
const literalValue = type.getLiteralValue();
|
||||
if (literalValue !== undefined) {
|
||||
return literalValue;
|
||||
} else if (type.isUndefined()) {
|
||||
return undefined;
|
||||
} else if (type.isString()) {
|
||||
return "";
|
||||
}else if(type.isNumber()){
|
||||
} else if (type.isNumber()) {
|
||||
return 0;
|
||||
}else if(type.isBoolean()){
|
||||
} else if (type.isBoolean()) {
|
||||
return false;// BAD IDEA
|
||||
}else if(type.isArray()){
|
||||
} else if (type.isArray()) {
|
||||
return [];
|
||||
}else if(type.isObject()){
|
||||
} else if (type.isObject()) {
|
||||
const newObj = {};
|
||||
for (const prop of type.getProperties()) {
|
||||
const propName = prop.getName();
|
||||
const declarations = prop.getDeclarations();
|
||||
let propType = prop.getDeclaredType();
|
||||
if (declarations.length !== 1) {
|
||||
console.warn("Multiple declarations for property", propName, "in type", type.getText());
|
||||
} else {
|
||||
propType = this.checker.getTypeOfSymbolAtLocation(prop, declarations[0]);
|
||||
}
|
||||
newObj[propName] = this.instantiateType(propType);
|
||||
}
|
||||
// TODO - handle functions
|
||||
return {};
|
||||
}else{
|
||||
console.warn("Unknown type to instantiate",type.getText());
|
||||
return newObj;
|
||||
} else {
|
||||
console.warn("Unknown type to instantiate", type.getText());
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param {Type} type
|
||||
*/
|
||||
instantiateMultipleFromType(type) {
|
||||
if (type.isStringLiteral()) {
|
||||
return [type.getLiteralValue()];
|
||||
} else if (type.isNumberLiteral()) {
|
||||
return [Number(type.getText())];
|
||||
} else if (type.isBooleanLiteral()) {
|
||||
return [type.getText() === 'true'];
|
||||
} else if (type.is) {
|
||||
|
||||
} else if (type.isString()) {
|
||||
return ["", "a", "b"];
|
||||
} else if (type.isNumber()) {
|
||||
return [0, 1, 2];
|
||||
} else if (type.isBoolean()) {
|
||||
return [false, true];
|
||||
} else if (type.isArray()) {
|
||||
return [[]];
|
||||
} else if (type.isObject()) {
|
||||
// TODO - handle functions
|
||||
return [{}];
|
||||
}
|
||||
console.warn("Unknown type to instantiate", type.getText());
|
||||
return [];
|
||||
}
|
||||
}
|
@@ -11,7 +11,7 @@ import { LibraryTypesRecorder } from './libcalls.mjs';
|
||||
* @returns {LibraryTypesRecorder} instance of recorded library calls
|
||||
*/
|
||||
export function getImportCallsAndArgumentTypes(importDecls, checker, mainFilePath) {
|
||||
const libraryCallsRecorder = new LibraryTypesRecorder();
|
||||
const libraryCallsRecorder = new LibraryTypesRecorder(checker);
|
||||
for (const importStringDecl of importDecls) {
|
||||
// console.log(importStringDecl);
|
||||
const importDecl = importStringDecl.getFirstAncestor();
|
||||
|
Reference in New Issue
Block a user