-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Version 3 with local modules support
- Loading branch information
Pawel
committed
Feb 28, 2022
1 parent
afa9559
commit 0851224
Showing
3 changed files
with
103 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters