New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

hpw

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hpw - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

#!/usr/bin/env node
'use strict';
const os = require('os');
const PARSING_TIMEOUT = 1000;
const EXECUTION_TIMEOUT = 5000;
const vm = require('vm');
const fs = require('fs').promises;
const concolor = require('concolor');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const curDir = process.cwd();
let commandName, command;
const parameters = process.argv;
const dir = curDir + (curDir.includes('/Exercises') ? '' : '/Exercises');
const doExit = () => {
rl.close();
process.chdir(curDir);
process.exit(0);
const prepareSandbox = () => {
const context = { module: {}, console };
context.global = context;
const sandbox = vm.createContext(context);
return sandbox;
};
const showHelp = () => {
console.log(
concolor.b('Syntax:') +
concolor.white(' hpw test <file>\n')
);
doExit();
const loadFile = async file => {
const fileName = dir + '/' + file;
const data = await fs.readFile(fileName, 'utf8');
const isTest = file.includes('.test');
const src = isTest ? `() => ( ${data} );` : `() => { ${data} };`;
const options = { timeout: PARSING_TIMEOUT };
let script;
try {
script = new vm.Script(src, options);
} catch (e) {
console.dir(e);
console.log('Parsing error');
process.exit(1);
}
const sandbox = prepareSandbox();
let exported, result;
try {
const f = script.runInNewContext(sandbox, options);
result = f();
exported = sandbox.module.exports;
} catch (e) {
console.dir(e);
console.log('Execution timeout');
process.exit(1);
}
return exported ? exported : result;
};
// Commands
//
const commands = {
version() {
console.log(
' Node.js: ' + process.versions.node + '\n' +
' v8: ' + process.versions.v8 + '\n' +
' libuv: ' + process.versions.uv + '\n' +
' OS: ' + os.type() + ' ' + os.release() + ' ' + os.arch()
);
doExit();
},
const executeTest = async file => {
const jsFile = `./${file}.js`;
const js = await loadFile(jsFile);
const testFile = `./${file}.test`;
const test = await loadFile(testFile);
const target = js[test.name];
if (!target) throw new Error('No test target detected');
const targetLength = target.toString().length;
const [minLength, maxLength] = test.length;
if (targetLength > maxLength) throw new Error('Solution is too short');
if (targetLength < minLength) throw new Error('Solution is too long');
console.dir({ js, test });
};
test(file) {
console.log('Testing file: ' + file);
doExit();
},
help() {
console.log('Usage: hpw <command>\n');
console.log('List of available commands:');
for (commandName in commands) console.log(commandName);
doExit();
(async () => {
console.log(concolor.white('How Programming Works'));
console.log(concolor.info('Labs Auto Checker\n'));
const files = await fs.readdir(dir);
const tests = files
.filter(file => file.endsWith('.test'))
.map(file => file.substring(0, file.length - '.test'.length));
for (const test of tests) {
console.log(concolor`\nTest ${test}(b,white)`);
try {
await executeTest(test);
} catch (e) {
console.log(concolor` Error: ${e.message}(b,red)`);
}
}
};
console.log('How Programming Works');
console.log(concolor.info('Labs Auto Checker\n'));
if (parameters.length < 3) {
showHelp();
} else {
parameters.shift();
parameters.shift();
commandName = parameters.shift();
command = commands[commandName];
if (!command) showHelp();
else command(...parameters);
}
})();

@@ -8,2 +8,3 @@ 'use strict';

if (isWin) console.log(concolor.error('Please install Linux'));
require('./hpw');
console.log('\nUsage:');
console.log(' npm run test');
{
"name": "hpw",
"version": "0.1.1",
"version": "0.1.2",
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>",

@@ -27,4 +27,3 @@ "description": "Labs Auto Checker",

"install": "node ./bin/install.js",
"start": "node ./bin/hpw.js",
"test": "npm run lint",
"test": "node ./bin/hpw.js",
"lint": "eslint ."

@@ -31,0 +30,0 @@ },