Comparing version 0.0.2 to 0.0.3
@@ -8,32 +8,12 @@ #!/usr/bin/env node | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const liyad = require('liyad'); | ||
const io = require('./io'); | ||
const help = require('./help').help; | ||
function help() { | ||
console.log( | ||
`Usage: liyad [options] [ -- ] [ -e script | script.lisp | - ] [ -- ] [arguments] | ||
liyad [options] | ||
Options: | ||
- script read from stdin (default; interactive mode if a tty) | ||
-- indicate the end of CLI options / script files | ||
-p, --profile=... select interpreter profile | ||
S S-expression parser | ||
L, lisp (default) lisp interpreter (return single value) | ||
LM lisp interpreter (return multiple values) | ||
lisp_async lisp interpreter (return single value, enable async) | ||
LM_async lisp interpreter (return multiple values, enable async) | ||
-e, --eval=... evaluate script | ||
-i, --interactive always enter the REPL even if stdin does not appear to be a terminal | ||
-h, --help print command line options | ||
-v, --version print version informations | ||
` | ||
) | ||
} | ||
exports.help = help; | ||
function cli() { | ||
const cwd = process.cwd(); | ||
let profileName = 'lisp'; | ||
@@ -43,2 +23,3 @@ let evalCode = void 0; | ||
let interactive = false; | ||
let safe= false; | ||
@@ -70,2 +51,5 @@ ParseOpts: for (; optsPos < process.argv.length; optsPos++) { | ||
break; | ||
case '--safe': | ||
safe = true; | ||
break; | ||
default: | ||
@@ -150,6 +134,48 @@ if (x.startsWith('--profile=')) { | ||
const appOpts = process.argv.slice(optsPos); | ||
liyad.lisp.appendGlobals({ | ||
'$argv': appOpts, | ||
}); | ||
const idMap = new Map(); | ||
function getRequire(baseDir) { | ||
return ((name) => { | ||
let id = void 0; | ||
if (name.startsWith('./') || name.startsWith('../')) { | ||
id = path.join(baseDir, name); | ||
} else { | ||
id = path.join(cwd, 'lisp_modules', name); | ||
} | ||
if (! id.endsWith('.lisp')) { | ||
let p = path.join(id, '__init__.lisp'); | ||
if (! fs.existsSync(p)) { | ||
p = id + '.lisp'; | ||
} | ||
id = p; | ||
} | ||
if (idMap.has(id)) { | ||
return idMap.get(id); | ||
} else { | ||
const c = io.readFromFileSync(id); | ||
lisp.setGlobals(getGlobals(path.dirname(id), path.basename(id))); | ||
const r = lisp(c); | ||
idMap.set(id, r); | ||
return r; | ||
} | ||
}); | ||
} | ||
function getGlobals(baseDir, moduleName) { | ||
return Object.assign({}, { | ||
'$argv': appOpts, | ||
'$__dirname': baseDir, | ||
'$__filename': path.join(baseDir, moduleName), | ||
}, safe ? {} : { | ||
'$require': getRequire(baseDir), | ||
'$node-require': (name) => { | ||
let id = name; | ||
if (name.startsWith('./') || name.startsWith('../')) { | ||
id = path.join(baseDir, name); | ||
} | ||
return require(id); | ||
}, | ||
}); | ||
} | ||
if (interactive || (!evalCode && fileNames.filter(x => x !== '-').length === 0 && process.stdin.isTTY)) { | ||
@@ -160,2 +186,3 @@ const repl = require('./repl'); | ||
const codes = await getCodes(); | ||
lisp.setGlobals(getGlobals(cwd, '__entrypoint__')); | ||
await repl.startRepl(liyad.S, lisp, codes); | ||
@@ -171,2 +198,3 @@ } catch (e) { | ||
const codes = await getCodes(); | ||
lisp.setGlobals(getGlobals(cwd, '__entrypoint__')); | ||
let r = await lisp(codes); | ||
@@ -173,0 +201,0 @@ switch (typeof r) { |
@@ -49,1 +49,7 @@ // Copyright (c) 2018, Shellyl_N and Authors | ||
exports.readFromFile = readFromFile; | ||
function readFromFileSync(inputPath) { | ||
return fs.readFileSync(inputPath, { encoding: 'utf8' }); | ||
} | ||
exports.readFromFileSync = readFromFileSync; |
{ | ||
"name": "liyad-cli", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "CLI and REPL for Liyad (Lisp yet another DSL interpreter).", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
# Liyad CLI | ||
### CLI and REPL for [Liyad](https://github.com/shellyln/liyad) (Lisp yet another DSL interpreter). | ||
[](https://shellyln.github.io/liyad/) | ||
@@ -33,2 +34,9 @@ [](https://www.npmjs.com/package/liyad-cli) | ||
-- indicate the end of CLI options / script files | ||
-p, --profile=... select interpreter profile | ||
S S-expression parser | ||
L, lisp (default) lisp interpreter (return single value) | ||
LM lisp interpreter (return multiple values) | ||
L_async, lisp_async lisp interpreter (return single value, enable async) | ||
LM_async lisp interpreter (return multiple values, enable async) | ||
--safe run as safe mode (disable '$require' and '$node-require') | ||
-e, --eval=... evaluate script | ||
@@ -35,0 +43,0 @@ -i, --interactive always enter the REPL even if stdin does not appear to be a terminal |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
21698
13
460
63
4