Comparing version 0.0.6 to 0.0.7
@@ -31,2 +31,3 @@ #!/usr/bin/env node | ||
let profileName = 'lisp'; | ||
let lsxBootPath = void 0; | ||
let evalCode = void 0; | ||
@@ -37,2 +38,35 @@ let optsPos = 2; | ||
function getLsxBoot(lsxBootFileName) { | ||
const id = require.resolve(lsxBootFileName, {paths: [ | ||
cwd | ||
]}); | ||
return require(id); | ||
} | ||
function getInterpreter(profile, lsxBootFileName) { | ||
switch (profile) { | ||
case 'S': | ||
return liyad.S; | ||
case 'L': case 'lisp': | ||
return liyad.lisp; | ||
case 'LM': | ||
return liyad.LM; | ||
case 'L_async': case 'lisp_async': | ||
return liyad.lisp_async; | ||
case 'LM_async': | ||
return liyad.LM_async; | ||
case 'LSX': case 'LSX_async': | ||
{ | ||
const lsxBoot = getLsxBoot(lsxBootFileName); | ||
return liyad.LSX({ | ||
jsx: lsxBoot.dom, | ||
jsxFlagment: lsxBoot.flagment, | ||
components: lsxBoot.components, | ||
}); | ||
} | ||
default: | ||
throw new Error(`Unknown profile name is specified: ${profile} .`); | ||
} | ||
} | ||
ParseOpts: for (; optsPos < process.argv.length; optsPos++) { | ||
@@ -67,2 +101,6 @@ const x = process.argv[optsPos]; | ||
break; | ||
case '--lsx-boot': | ||
optsPos++; | ||
lsxBootPath = process.argv[optsPos]; | ||
break; | ||
case '--safe': | ||
@@ -84,22 +122,7 @@ safe = true; | ||
let lisp = void 0; | ||
switch (profileName) { | ||
case 'S': | ||
lisp = liyad.S; | ||
break; | ||
case 'L': case 'lisp': | ||
lisp = liyad.lisp; | ||
break; | ||
case 'LM': | ||
lisp = liyad.LM; | ||
break; | ||
case 'L_async': case 'lisp_async': | ||
lisp = liyad.lisp_async; | ||
break; | ||
case 'LM_async': | ||
lisp = liyad.LM_async; | ||
break; | ||
default: | ||
console.error(`Unknown profile name is specified: ${profileName} .`); | ||
try { | ||
lisp = getInterpreter(profileName, lsxBootPath); | ||
} catch (e) { | ||
console.error(e.message); | ||
process.exit(-1); | ||
break; | ||
} | ||
@@ -154,3 +177,4 @@ | ||
function getRequire(baseDir) { | ||
return ((name) => { | ||
return ((name, profile) => { | ||
const lisp = getInterpreter(profile || profileName, lsxBootPath); | ||
let id = void 0; | ||
@@ -197,2 +221,19 @@ if (name.startsWith('./') || name.startsWith('../')) { | ||
}, | ||
'$render': (c, cb) => { | ||
try { | ||
const lsxBoot = getLsxBoot(lsxBootPath); | ||
const p = lsxBoot.render(c); | ||
if (p instanceof Promise) { | ||
p.then(html => { | ||
cb(null, html); | ||
}, e => { | ||
cb(e, null); | ||
}); | ||
} else { | ||
cb(null, lsxBoot.render(c)); | ||
} | ||
} catch (err) { | ||
cb(err, null); | ||
} | ||
}, | ||
}); | ||
@@ -216,4 +257,7 @@ } | ||
try { | ||
const fns = fileNames.filter(x => x !== '-'); | ||
const codes = await getCodes(); | ||
lisp.setGlobals(getGlobals(cwd, '__entrypoint__')); | ||
lisp.setGlobals(fns.length > 0 ? | ||
getGlobals(path.dirname(fns[fns.length - 1]), path.basename(fns[fns.length - 1])) : | ||
getGlobals(cwd, '__entrypoint__')); | ||
let r = await lisp(codes); | ||
@@ -220,0 +264,0 @@ switch (typeof r) { |
@@ -23,2 +23,5 @@ #!/usr/bin/env node | ||
LM_async lisp interpreter (return multiple values, enable async) | ||
LSX lisp interpreter (return single value, enable LSX) | ||
LSX_async lisp interpreter (return single value, enable LSX, enable async) | ||
--lsx-boot lsxboot.js LSX bootstrap JavaScript file; required if profile LSX or LSX_async is selected. | ||
--safe run as safe mode (disable '$require' and '$node-require') | ||
@@ -25,0 +28,0 @@ -e, --eval=... evaluate script |
{ | ||
"name": "liyad-cli", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "CLI and REPL for Liyad (Lisp yet another DSL interpreter).", | ||
@@ -43,3 +43,4 @@ "keywords": [ | ||
"author": "shellyln", | ||
"license": "ISC" | ||
"license": "ISC", | ||
"devDependencies": {} | ||
} |
@@ -40,2 +40,5 @@ # Liyad CLI | ||
LM_async lisp interpreter (return multiple values, enable async) | ||
LSX lisp interpreter (return single value, enable LSX) | ||
LSX_async lisp interpreter (return single value, enable LSX, enable async) | ||
--lsx-boot lsxboot.js LSX bootstrap JavaScript file; required if profile LSX or LSX_async is selected. | ||
--safe run as safe mode (disable '$require' and '$node-require') | ||
@@ -49,2 +52,26 @@ -e, --eval=... evaluate script | ||
## LSX bootstrap file example | ||
lsxboot.js | ||
```javascript | ||
const React = require('react'); | ||
const ReactDOMServer = require('react-dom/server'); | ||
class Hello extends React.Component { | ||
render() { | ||
return (React.createElement("div", {}, "hello")); | ||
} | ||
} | ||
exports.dom = React.createElement; | ||
exports.flagment = React.Fragment; | ||
exports.render = ReactDOMServer.renderToStaticMarkup; | ||
exports.components = { | ||
Hello, | ||
}; | ||
``` | ||
## Packaging to the single executable file. | ||
@@ -107,6 +134,8 @@ Use [pkg](https://www.npmjs.com/package/pkg). | ||
* `$require(id)` | ||
* `$require(id [, profile])` | ||
* Load lisp code from other file. | ||
* returns: Exported functions and variables. | ||
* `id`: Load from relative path if `id` starts with `./` or `../`. Otherwise load from local or global `node_modules`. | ||
* `profile`: (optional) interpreter profile (S/L/lisp/LM/L_async/lisp_async/LM_async). | ||
* default value is selected by CLI option `-p` or `--profile`. | ||
* `$node-require(id)` | ||
@@ -113,0 +142,0 @@ * Load JavaScript code from other file. |
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
29543
16
542
148
7