mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
feature(user-menu) add ability to used EcmaScript Modules, improve error handling
This commit is contained in:
parent
bba2f7b570
commit
9301506d82
2 changed files with 32 additions and 9 deletions
|
|
@ -111,6 +111,7 @@
|
|||
"@cloudcmd/read-files-sync": "^2.0.0",
|
||||
"apart": "^2.0.0",
|
||||
"chalk": "^2.0.1",
|
||||
"compression": "^1.7.4",
|
||||
"console-io": "^10.0.0",
|
||||
"copymitter": "^4.0.2",
|
||||
"criton": "^2.0.0",
|
||||
|
|
@ -144,6 +145,7 @@
|
|||
"package-json": "^6.0.0",
|
||||
"ponse": "^3.0.0",
|
||||
"pullout": "^3.0.0",
|
||||
"putout": "^5.5.0",
|
||||
"rendy": "^2.0.0",
|
||||
"restafary": "^6.0.0",
|
||||
"restbox": "^1.0.1",
|
||||
|
|
@ -200,7 +202,6 @@
|
|||
"optimize-css-assets-webpack-plugin": "^5.0.0",
|
||||
"philip": "^2.0.0",
|
||||
"place": "^1.1.4",
|
||||
"putout": "^5.2.1",
|
||||
"readjson": "^1.1.3",
|
||||
"request": "^2.76.0",
|
||||
"rimraf": "^3.0.0",
|
||||
|
|
|
|||
|
|
@ -6,9 +6,12 @@ const fs = require('fs');
|
|||
const {join} = require('path');
|
||||
const {promisify} = require('util');
|
||||
|
||||
const tryCatch = require('try-catch');
|
||||
const tryToCatch = require('try-to-catch');
|
||||
const currify = require('currify');
|
||||
const findUp = require('find-up');
|
||||
const putout = require('putout');
|
||||
const {codeframe} = putout;
|
||||
|
||||
const readFile = promisify(fs.readFile);
|
||||
|
||||
|
|
@ -49,19 +52,38 @@ async function onGET({req, res, menuName}) {
|
|||
|
||||
const homeMenuPath = join(homedir(), menuName);
|
||||
const menuPath = currentMenuPath || homeMenuPath;
|
||||
const [e, data] = await tryToCatch(readFile, menuPath, 'utf8');
|
||||
const [e, source] = await tryToCatch(readFile, menuPath, 'utf8');
|
||||
|
||||
if (!e)
|
||||
return res
|
||||
.type('js')
|
||||
.send(data);
|
||||
|
||||
if (e.code !== 'ENOENT')
|
||||
if (e && e.code !== 'ENOENT')
|
||||
return res
|
||||
.status(404)
|
||||
.send(e.message);
|
||||
|
||||
sendDefaultMenu(res);
|
||||
if (e)
|
||||
return sendDefaultMenu(res);
|
||||
|
||||
const [parseError, result] = tryCatch(putout, source, {
|
||||
plugins: [
|
||||
'convert-esm-to-commonjs',
|
||||
'strict-mode',
|
||||
],
|
||||
});
|
||||
|
||||
if (parseError)
|
||||
return res
|
||||
.type('js')
|
||||
.send(`
|
||||
throw Error(\`<pre>path: ${menuPath}\n\n${codeframe({
|
||||
error: parseError,
|
||||
source,
|
||||
highlightCode: false,
|
||||
})}
|
||||
</pre>\`);
|
||||
`);
|
||||
|
||||
res
|
||||
.type('js')
|
||||
.send(result.code);
|
||||
}
|
||||
|
||||
function sendDefaultMenu(res) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue