mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
refactor(user-menu) menuName
This commit is contained in:
parent
a727d6f6ea
commit
1dbcf92336
3 changed files with 18 additions and 9 deletions
|
|
@ -224,7 +224,10 @@ function cloudcmd(prefix, plugins, modules) {
|
|||
root,
|
||||
}),
|
||||
|
||||
userMenu,
|
||||
userMenu({
|
||||
menuName: '.cloudcmd.menu.js',
|
||||
}),
|
||||
|
||||
rest,
|
||||
route({
|
||||
html: defaultHtml,
|
||||
|
|
|
|||
|
|
@ -6,25 +6,24 @@ const {join} = require('path');
|
|||
const {promisify} = require('util');
|
||||
|
||||
const tryToCatch = require('try-to-catch');
|
||||
const currify = require('currify');
|
||||
const findUp = require('find-up');
|
||||
|
||||
const readFile = promisify(fs.readFile);
|
||||
const menuName = '.cloudcmd.menu.js';
|
||||
const homeMenuPath = join(homedir(), menuName);
|
||||
|
||||
module.exports = async (req, res, next) => {
|
||||
module.exports = currify(async({menuName}, req, res, next) => {
|
||||
if (req.url.indexOf('/api/v1/user-menu'))
|
||||
return next();
|
||||
|
||||
const {method} = req;
|
||||
|
||||
if (method === 'GET')
|
||||
return onGET(req.query, res);
|
||||
return onGET(menuName, req.query, res);
|
||||
|
||||
next();
|
||||
};
|
||||
});
|
||||
|
||||
async function onGET({dir}, res) {
|
||||
async function onGET(menuName, {dir}, res) {
|
||||
const [errorFind, currentMenuPath] = await tryToCatch(findUp, [
|
||||
menuName,
|
||||
], {cwd: dir});
|
||||
|
|
@ -37,6 +36,7 @@ async function onGET({dir}, res) {
|
|||
if (errorFind && errorFind.code === 'ENOENT')
|
||||
return res.send('');
|
||||
|
||||
const homeMenuPath = join(homedir(), menuName);
|
||||
const menuPath = currentMenuPath || homeMenuPath;
|
||||
const [e, data] = await tryToCatch(readFile, menuPath, 'utf8');
|
||||
|
||||
|
|
|
|||
|
|
@ -7,13 +7,19 @@ const test = require('supertape');
|
|||
const serveOnce = require('serve-once');
|
||||
|
||||
const userMenu = require('./user-menu');
|
||||
const {request} = serveOnce(() => userMenu);
|
||||
const {request} = serveOnce(userMenu);
|
||||
|
||||
const userMenuPath = join(__dirname, '..', '.cloudcmd.menu.js');
|
||||
const userMenuFile = fs.readFileSync(userMenuPath, 'utf8');
|
||||
|
||||
test('cloudcmd: user menu', async (t) => {
|
||||
const {body} = await request.get(`/api/v1/user-menu?dir=${__dirname}`);
|
||||
const options = {
|
||||
menuName: '.cloudcmd.menu.js',
|
||||
};
|
||||
|
||||
const {body} = await request.get(`/api/v1/user-menu?dir=${__dirname}`, {
|
||||
options,
|
||||
});
|
||||
|
||||
t.equal(userMenuFile, body, 'should equal');
|
||||
t.end();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue