Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@digitak/esrun

Package Overview
Dependencies
Maintainers
1
Versions
70
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@digitak/esrun - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

src/main.d.ts

5

package.json
{
"name": "@digitak/esrun",
"version": "1.0.3",
"version": "1.0.4",
"description": "Execute directly your Typescript/Javascript(with ES modules) files using Esbuild",

@@ -11,4 +11,3 @@ "files": ["src/**/*"],

"scripts": {
"test": "./src/bin.js test/test.ts",
"publish": "npm publish --access public"
"test": "./src/bin.js test"
},

@@ -15,0 +14,0 @@ "repository": {

81

src/main.js

@@ -1,21 +0,74 @@

const { resolve } = require('path')
const { resolve, basename } = require('path')
const { build } = require('esbuild')
const { existsSync, statSync, readFileSync } = require('fs')
async function esrun(inputFile, args=[]) {
if (!inputFile) return console.log(`Missing input file`)
inputFile = resolve(inputFile)
/**
* Intelligent function that looks for the right file to load
* If the file does not exist, try to add a '.ts' and a '.js' extension
* If the inputFile is a directory, check for a entry point inside the directory
*/
function findInputFile(path) {
if (!existsSync(path)) {
if (existsSync(`${path}.ts`))
path = `${path}.ts`
else if (existsSync(`${path}.js`))
path = `${path}.js`
else throw `Path '${path}' does not exist`
}
const buildResult = await build({
entryPoints: [inputFile],
bundle: true,
write: false,
platform: 'node',
})
const code = buildResult.outputFiles[0].text
const evaluator = new Function('process', 'require', code)
process.argv = [process.argv[0], inputFile, ...args]
const stat = statSync(path)
if (stat.isFile())
return path
else if (stat.isDirectory()) {
// first we check if there is a package.json file with a `main` key
const packageFile = resolve(path, 'package.json')
if (existsSync(packageFile) && statSync(packageFile).isFile()) {
const package = JSON.parse(readFileSync(packageFile, 'utf8'))
if (package.main) return findInputFile(resolve(path, package.main))
}
return evaluator(process, require)
// otherwise we look for a default entry point
const name = basename(path)
for (const subpath of [
resolve(path, 'index.ts'),
resolve(path, name),
resolve(path, `${name}.ts`),
resolve(path, 'main.ts'),
resolve(path, 'index.js'),
resolve(path, `${name}.js`),
resolve(path, 'main.js'),
]) if (existsSync(subpath) && statSync(subpath).isFile())
return subpath
throw `Could not resolve an entry point in folder '${path}`
}
else throw `Path '${path}' should be a file or a directory`
}
/**
* Run a .ts or .js file
*/
async function esrun(inputFile, args=[]) {
try {
if (!inputFile) throw `Missing input file`
inputFile = findInputFile(resolve(inputFile))
const buildResult = await build({
entryPoints: [inputFile],
bundle: true,
write: false,
platform: 'node',
})
const code = buildResult.outputFiles[0].text
const evaluator = new Function('process', 'require', code)
process.argv = [process.argv[0], inputFile, ...args]
return evaluator(process, require)
}
catch (error) {
console.error(error)
return 1
}
}
module.exports = esrun
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc