Skip to content

Commit

Permalink
Version 3 with local modules support
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawel committed Feb 28, 2022
1 parent afa9559 commit 0851224
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 31 deletions.
66 changes: 51 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,79 @@
#! /usr/bin/env node
const path = require('path');
const fs = require('fs');
const moduleName = process.argv[2]
const fnName = process.argv[3]
const params = process.argv.slice(4);

if (!process.argv[2]) {
if (!moduleName) {
console.error('File name not provided i.e. run-func ./index.js');
process.exit();
}

if (!process.argv[3]) {
if (!fnName) {
console.error('Function name not provided i.e. run-func ./index.js default');
process.exit();
}

let isEs6 = path.extname(process.argv[2]) === '.mjs';
const filePath = path.join(process.cwd(), process.argv[2])
const packageJsonPath = path.join(path.dirname(filePath), 'package.json')
const filePath = path.join(process.cwd(), moduleName)
const isLocalFile = path.extname(moduleName) !== ''

if (fs.existsSync(packageJsonPath)) {
const packageJson = fs.readFileSync(packageJsonPath)
isEs6 = JSON.parse(packageJson).type === 'module'
try {
if (!isLocalFile && !fs.existsSync(require.resolve(moduleName))) {
console.error('Module is not installed:', moduleName);
process.exit();
}
} catch(e) {
console.error('Module is not installed:', moduleName);
process.exit();
}

if (isEs6) {
import('file://' + path.resolve(filePath)).then((userModule) => {
executeInModule(userModule, process.argv[3], params);
if (isES6()) {
import(isLocalFile ? 'file://' + path.resolve(filePath) : moduleName).then((userModule) => {
executeInModule(userModule, fnName, params);
});
} else {
const userModule = require(filePath);
executeInModule(userModule, process.argv[3], params);
const userModule = require(isLocalFile ? filePath : moduleName);
executeInModule(userModule, fnName, params);
}

async function executeInModule(userMod, fnName, fnParams) {
if (typeof userMod === 'function') {
console.log(await userMod(...fnParams))
return
}

function executeInModule(userMod, fnName, fnParams) {
if (!userMod) {
throw new Error(`Module ${userMod} does not exists`);
}
if (!userMod[fnName]) {
throw new Error(`Function ${fnName} is not present or exported from module`);
}
userMod[fnName](...fnParams);
const result = userMod[fnName](...fnParams);
if (typeof result === 'object' && result.then) {
result.then(res => {
typeof res !== 'undefined' && console.log(res);
})
} else if (typeof result !== 'undefined') {
console.log(result);
}
}

function isES6() {
const isLocalFile = path.extname(moduleName) !== ''
const filePath = isLocalFile ? path.join(process.cwd(), moduleName) : require.resolve(moduleName)
let isEs6 = path.extname(moduleName) === '.mjs'

for(var i = 0; i < 10; i ++) {
const dirsAbove = path.join(...new Array(i).fill('..'))
const dir = path.join(path.dirname(filePath), dirsAbove)
const packageJsonPath = path.join(dir, 'package.json')
if (fs.existsSync(packageJsonPath)) {
const packageJson = fs.readFileSync(packageJsonPath)
isEs6 = JSON.parse(packageJson).type === 'module'
break
}
}

return isEs6
}
66 changes: 51 additions & 15 deletions mem.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,79 @@
#! /usr/bin/env node --max-old-space-size=4096
const path = require('path');
const fs = require('fs');
const moduleName = process.argv[2]
const fnName = process.argv[3]
const params = process.argv.slice(4);

if (!process.argv[2]) {
if (!moduleName) {
console.error('File name not provided i.e. run-func ./index.js');
process.exit();
}

if (!process.argv[3]) {
if (!fnName) {
console.error('Function name not provided i.e. run-func ./index.js default');
process.exit();
}

let isEs6 = path.extname(process.argv[2]) === '.mjs';
const filePath = path.join(process.cwd(), process.argv[2])
const packageJsonPath = path.join(path.dirname(filePath), 'package.json')
const filePath = path.join(process.cwd(), moduleName)
const isLocalFile = path.extname(moduleName) !== ''

if (fs.existsSync(packageJsonPath)) {
const packageJson = fs.readFileSync(packageJsonPath)
isEs6 = JSON.parse(packageJson).type === 'module'
try {
if (!isLocalFile && !fs.existsSync(require.resolve(moduleName))) {
console.error('Module is not installed:', moduleName);
process.exit();
}
} catch(e) {
console.error('Module is not installed:', moduleName);
process.exit();
}

if (isEs6) {
import('file://' + path.resolve(filePath)).then((userModule) => {
executeInModule(userModule, process.argv[3], params);
if (isES6()) {
import(isLocalFile ? 'file://' + path.resolve(filePath) : moduleName).then((userModule) => {
executeInModule(userModule, fnName, params);
});
} else {
const userModule = require(filePath);
executeInModule(userModule, process.argv[3], params);
const userModule = require(isLocalFile ? filePath : moduleName);
executeInModule(userModule, fnName, params);
}

async function executeInModule(userMod, fnName, fnParams) {
if (typeof userMod === 'function') {
console.log(await userMod(...fnParams))
return
}

function executeInModule(userMod, fnName, fnParams) {
if (!userMod) {
throw new Error(`Module ${userMod} does not exists`);
}
if (!userMod[fnName]) {
throw new Error(`Function ${fnName} is not present or exported from module`);
}
userMod[fnName](...fnParams);
const result = userMod[fnName](...fnParams);
if (typeof result === 'object' && result.then) {
result.then(res => {
typeof res !== 'undefined' && console.log(res);
})
} else if (typeof result !== 'undefined') {
console.log(result);
}
}

function isES6() {
const isLocalFile = path.extname(moduleName) !== ''
const filePath = isLocalFile ? path.join(process.cwd(), moduleName) : require.resolve(moduleName)
let isEs6 = path.extname(moduleName) === '.mjs'

for(var i = 0; i < 10; i ++) {
const dirsAbove = path.join(...new Array(i).fill('..'))
const dir = path.join(path.dirname(filePath), dirsAbove)
const packageJsonPath = path.join(dir, 'package.json')
if (fs.existsSync(packageJsonPath)) {
const packageJson = fs.readFileSync(packageJsonPath)
isEs6 = JSON.parse(packageJson).type === 'module'
break
}
}

return isEs6
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"version": "2.1.0",
"version": "3.0.0",
"repository": {
"type": "git",
"url": "https://github.com/DVLP/run-func.git"
Expand Down

0 comments on commit 0851224

Please sign in to comment.