feature: server: columns: get rid of mock-require

This commit is contained in:
coderaiser 2024-03-20 12:13:17 +02:00
parent 46c8accd84
commit 98d3a4cc56
41 changed files with 233 additions and 215 deletions

View file

@ -10,6 +10,7 @@ module.exports = {
], ],
rules: { rules: {
'key-spacing': 'off', 'key-spacing': 'off',
'n/prefer-node-protocol': 'error',
}, },
overrides: [{ overrides: [{
files: ['bin/release.js'], files: ['bin/release.js'],

View file

@ -1,11 +1,11 @@
'use strict'; 'use strict';
const fs = require('fs'); const fs = require('node:fs');
const { const {
basename, basename,
extname, extname,
join, join,
} = require('path'); } = require('node:path');
const ExtractTextPlugin = require('extract-text-webpack-plugin'); const ExtractTextPlugin = require('extract-text-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin'); const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');

View file

@ -4,7 +4,7 @@ const {
resolve, resolve,
sep, sep,
join, join,
} = require('path'); } = require('node:path');
const {EnvironmentPlugin} = require('webpack'); const {EnvironmentPlugin} = require('webpack');
const WebpackBar = require('webpackbar'); const WebpackBar = require('webpackbar');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const process = require('process'); const process = require('node:process');
/* global DOM */ /* global DOM */
const Emitify = require('emitify'); const Emitify = require('emitify');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const process = require('process'); const process = require('node:process');
require('../css/main.css'); require('../css/main.css');
require('../css/nojs.css'); require('../css/nojs.css');
require('../css/columns/name-size-date.css'); require('../css/columns/name-size-date.css');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const {join} = require('path'); const {join} = require('node:path');
const {test, stub} = require('supertape'); const {test, stub} = require('supertape');
const mockRequire = require('mock-require'); const mockRequire = require('mock-require');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const {extname} = require('path'); const {extname} = require('node:path');
const currify = require('currify'); const currify = require('currify');
const testRegExp = currify((name, reg) => reg.test(name)); const testRegExp = currify((name, reg) => reg.test(name));
const getRegExp = (ext) => RegExp(`\\.${ext}$`, 'i'); const getRegExp = (ext) => RegExp(`\\.${ext}$`, 'i');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const process = require('process'); const process = require('node:process');
const codegen = require('codegen.macro'); const codegen = require('codegen.macro');
const tryToCatch = require('try-to-catch'); const tryToCatch = require('try-to-catch');
const currify = require('currify'); const currify = require('currify');

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const {join} = require('path'); const {join} = require('node:path');
const {readFileSync} = require('fs'); const {readFileSync} = require('node:fs');
const test = require('supertape'); const test = require('supertape');
const montag = require('montag'); const montag = require('montag');

View file

@ -121,6 +121,7 @@
"just-snake-case": "^1.1.0", "just-snake-case": "^1.1.0",
"markdown-it": "^14.0.0", "markdown-it": "^14.0.0",
"mellow": "^3.0.0", "mellow": "^3.0.0",
"nano-memoize": "^3.0.16",
"nomine": "^4.0.0", "nomine": "^4.0.0",
"object.omit": "^3.0.0", "object.omit": "^3.0.0",
"once": "^1.4.0", "once": "^1.4.0",
@ -171,7 +172,7 @@
"domtokenlist-shim": "^1.2.0", "domtokenlist-shim": "^1.2.0",
"emitify": "^4.0.1", "emitify": "^4.0.1",
"eslint": "^8.0.1", "eslint": "^8.0.1",
"eslint-plugin-n": "^16.0.1", "eslint-plugin-n": "^17.0.0-4",
"eslint-plugin-putout": "^22.0.0", "eslint-plugin-putout": "^22.0.0",
"extract-text-webpack-plugin": "^4.0.0-alpha.0", "extract-text-webpack-plugin": "^4.0.0-alpha.0",
"gritty": "^8.0.0", "gritty": "^8.0.0",

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const path = require('path'); const path = require('node:path');
const {test, stub} = require('supertape'); const {test, stub} = require('supertape');
const cloudcmd = require('./cloudcmd.js'); const cloudcmd = require('./cloudcmd.js');

View file

@ -1,9 +1,13 @@
'use strict'; 'use strict';
const fullstore = require('fullstore');
const process = require('process'); const process = require('process');
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const {nanomemoize} = require('nano-memoize');
const readFilesSync = require('@cloudcmd/read-files-sync'); const readFilesSync = require('@cloudcmd/read-files-sync');
const isMap = (a) => /\.map$/.test(a); const isMap = (a) => /\.map$/.test(a);
const not = (fn) => (a) => !fn(a); const not = (fn) => (a) => !fn(a);
@ -12,19 +16,26 @@ const defaultColumns = {
'name-size-date-owner-mode': '', 'name-size-date-owner-mode': '',
}; };
const isDev = process.env.NODE_ENV === 'development'; const _isDev = fullstore(process.env.NODE_ENV === 'development');
const getDist = (isDev) => isDev ? 'dist-dev' : 'dist'; const getDist = (isDev) => isDev ? 'dist-dev' : 'dist';
const dist = getDist(isDev); module.exports.isDev = _isDev;
const columnsDir = path.join(__dirname, '..', dist, 'columns');
const names = fs module.exports.getColumns = ({isDev = _isDev()} = {}) => {
.readdirSync(columnsDir) const columns = readFilesSyncMemo(isDev);
.filter(not(isMap));
const columns = readFilesSync(columnsDir, names, 'utf8'); return {
...columns,
module.exports = { ...defaultColumns,
...columns, };
...defaultColumns,
}; };
const readFilesSyncMemo = nanomemoize((isDev) => {
const dist = getDist(isDev);
const columnsDir = path.join(__dirname, '..', dist, 'columns');
const names = fs
.readdirSync(columnsDir)
.filter(not(isMap));
return readFilesSync(columnsDir, names, 'utf8');
});

37
server/columns.spec.js Normal file
View file

@ -0,0 +1,37 @@
'use strict';
const test = require('supertape');
const fs = require('fs');
const {getColumns, isDev} = require('./columns');
test('columns: prod', (t) => {
const columns = getColumns({
isDev: false,
});
t.equal(columns[''], '');
t.end();
});
test('columns: dev', (t) => {
const columns = getColumns({
isDev: true,
});
const css = fs.readFileSync(`${__dirname}/../css/columns/name-size-date.css`, 'utf8');
t.equal(columns['name-size-date'], css);
t.end();
});
test('columns: no args', (t) => {
const currentIsDev = isDev();
isDev(true);
const columns = getColumns();
const css = fs.readFileSync(`${__dirname}/../css/columns/name-size-date.css`, 'utf8');
isDev(currentIsDev);
t.equal(columns['name-size-date'], css);
t.end();
});

View file

@ -2,11 +2,11 @@
const DIR_SERVER = `${__dirname}/`; const DIR_SERVER = `${__dirname}/`;
const DIR_COMMON = '../common/'; const DIR_COMMON = '../common/';
const path = require('path'); const path = require('node:path');
const fs = require('fs'); const fs = require('node:fs');
const Emitter = require('events'); const Emitter = require('node:events');
const {homedir} = require('os'); const {homedir} = require('node:os');
const exit = require(`${DIR_SERVER}exit`); const exit = require(`${DIR_SERVER}exit`);
const CloudFunc = require(`${DIR_COMMON}cloudfunc`); const CloudFunc = require(`${DIR_COMMON}cloudfunc`);

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const {once} = require('events'); const {once} = require('node:events');
const test = require('supertape'); const test = require('supertape');
const io = require('socket.io-client'); const io = require('socket.io-client');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const process = require('process'); const process = require('node:process');
const test = require('supertape'); const test = require('supertape');
const {promisify} = require('util'); const {promisify} = require('util');
const tryToCatch = require('try-to-catch'); const tryToCatch = require('try-to-catch');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const process = require('process'); const process = require('node:process');
const test = require('supertape'); const test = require('supertape');
const env = require('./env'); const env = require('./env');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const process = require('process'); const process = require('node:process');
const getMessage = (a) => a?.message || a; const getMessage = (a) => a?.message || a;
module.exports = (...args) => { module.exports = (...args) => {

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const process = require('process'); const process = require('node:process');
const {test, stub} = require('supertape'); const {test, stub} = require('supertape');
const exit = require('./exit'); const exit = require('./exit');

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const {join} = require('path'); const {join} = require('node:path');
const {callbackify} = require('util'); const {callbackify} = require('node:util');
const pullout = require('pullout'); const pullout = require('pullout');
const ponse = require('ponse'); const ponse = require('ponse');

View file

@ -1,8 +1,8 @@
'use strict'; 'use strict';
const fs = require('fs'); const fs = require('node:fs');
const {join} = require('path'); const {join} = require('node:path');
const {promisify} = require('util'); const {promisify} = require('node:util');
const tryToCatch = require('try-to-catch'); const tryToCatch = require('try-to-catch');
const serveOnce = require('serve-once'); const serveOnce = require('serve-once');

View file

@ -1,8 +1,8 @@
'use strict'; 'use strict';
const process = require('process'); const process = require('node:process');
const net = require('net'); const net = require('node:net');
const repl = require('repl'); const repl = require('node:repl');
module.exports = net module.exports = net
.createServer((socket) => { .createServer((socket) => {

View file

@ -1,11 +1,11 @@
'use strict'; 'use strict';
const process = require('process'); const process = require('node:process');
const DIR = '../'; const DIR = '../';
const DIR_COMMON = `${DIR}../common/`; const DIR_COMMON = `${DIR}../common/`;
const path = require('path'); const path = require('node:path');
const fs = require('fs'); const fs = require('node:fs');
const root = require(`${DIR}root`); const root = require(`${DIR}root`);
const CloudFunc = require(`${DIR_COMMON}cloudfunc`); const CloudFunc = require(`${DIR_COMMON}cloudfunc`);

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const process = require('process'); const process = require('node:process');
const format = require('format-io'); const format = require('format-io');
const {version} = require('../../package'); const {version} = require('../../package');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const process = require('process'); const process = require('node:process');
const {test, stub} = require('supertape'); const {test, stub} = require('supertape');
const info = require('./info'); const info = require('./info');

View file

@ -1,9 +1,6 @@
'use strict'; 'use strict';
const DIR_SERVER = './'; const {extname} = require('node:path');
const DIR_COMMON = '../common/';
const {extname} = require('path');
const {read} = require('win32'); const {read} = require('win32');
const ponse = require('ponse'); const ponse = require('ponse');
@ -16,12 +13,13 @@ const once = require('once');
const pipe = require('pipe-io'); const pipe = require('pipe-io');
const {contentType} = require('mime-types'); const {contentType} = require('mime-types');
const root = require(`${DIR_SERVER}root`); const root = require(`./root`);
const prefixer = require(`${DIR_SERVER}prefixer`); const prefixer = require(`./prefixer`);
const CloudFunc = require(`${DIR_COMMON}cloudfunc`); const CloudFunc = require(`../common/cloudfunc`);
const Columns = require(`${DIR_SERVER}/columns`); const {getColumns} = require(`./columns`);
const Template = require(`${DIR_SERVER}/template`); const Template = require(`./template`);
const {stringify} = JSON;
const {FS} = CloudFunc; const {FS} = CloudFunc;
const sendIndex = (params, data) => { const sendIndex = (params, data) => {
@ -29,7 +27,7 @@ const sendIndex = (params, data) => {
...params, ...params,
name: 'index.html', name: 'index.html',
}; };
ponse.send(data, ponseParams); ponse.send(data, ponseParams);
}; };
@ -39,9 +37,9 @@ const getPrefix = (config) => prefixer(config('prefix'));
const getReadDir = (config) => { const getReadDir = (config) => {
if (!config('dropbox')) if (!config('dropbox'))
return read; return read;
const {readDir} = onceRequire('@cloudcmd/dropbox'); const {readDir} = onceRequire('@cloudcmd/dropbox');
return wraptile(readDir, config('dropboxToken')); return wraptile(readDir, config('dropboxToken'));
}; };
@ -51,10 +49,10 @@ const getReadDir = (config) => {
module.exports = currify((config, options, request, response, next) => { module.exports = currify((config, options, request, response, next) => {
const name = ponse.getPathName(request); const name = ponse.getPathName(request);
const isFS = RegExp(`^/$|^${FS}`).test(name); const isFS = RegExp(`^/$|^${FS}`).test(name);
if (!isFS) if (!isFS)
return next(); return next();
route({ route({
config, config,
options, options,
@ -74,36 +72,36 @@ async function route({config, options, request, response}) {
gzip, gzip,
name, name,
}; };
config('prefix', prefixer(request.baseUrl)); config('prefix', prefixer(request.baseUrl));
const rootName = name.replace(CloudFunc.FS, '') || '/'; const rootName = name.replace(CloudFunc.FS, '') || '/';
const fullPath = root(rootName, config('root')); const fullPath = root(rootName, config('root'));
const read = getReadDir(config); const read = getReadDir(config);
const [error, stream] = await tryToCatch(read, fullPath, { const [error, stream] = await tryToCatch(read, fullPath, {
root: config('root'), root: config('root'),
}); });
const {html} = options; const {html} = options;
if (error) if (error)
return ponse.sendError(error, p); return ponse.sendError(error, p);
if (stream.type === 'directory') { if (stream.type === 'directory') {
const {files} = stream; const {files} = stream;
return sendIndex(p, buildIndex(config, html, { return sendIndex(p, buildIndex(config, html, {
files, files,
path: format.addSlashToEnd(rootName), path: format.addSlashToEnd(rootName),
})); }));
} }
const {contentLength} = stream; const {contentLength} = stream;
response.setHeader('Content-Length', contentLength); response.setHeader('Content-Length', contentLength);
response.setHeader('Content-Type', contentType(extname(fullPath))); response.setHeader('Content-Type', contentType(extname(fullPath)));
await pipe([stream, response]); await pipe([stream, response]);
} }
@ -118,56 +116,56 @@ function indexProcessing(config, options) {
const noConsole = !config('console'); const noConsole = !config('console');
const noTerminal = !config('terminal'); const noTerminal = !config('terminal');
const {panel} = options; const {panel} = options;
let {data} = options; let {data} = options;
if (noKeysPanel) if (noKeysPanel)
data = hideKeysPanel(data); data = hideKeysPanel(data);
if (oneFilePanel) if (oneFilePanel)
data = data data = data
.replace('icon-move', 'icon-move none') .replace('icon-move', 'icon-move none')
.replace('icon-copy', 'icon-copy none'); .replace('icon-copy', 'icon-copy none');
if (noContact) if (noContact)
data = data.replace('icon-contact', 'icon-contact none'); data = data.replace('icon-contact', 'icon-contact none');
if (noConfig) if (noConfig)
data = data.replace('icon-config', 'icon-config none'); data = data.replace('icon-config', 'icon-config none');
if (noConsole) if (noConsole)
data = data.replace('icon-console', 'icon-console none'); data = data.replace('icon-console', 'icon-console none');
if (noTerminal) if (noTerminal)
data = data.replace('icon-terminal', 'icon-terminal none'); data = data.replace('icon-terminal', 'icon-terminal none');
const left = rendy(Template.panel, { const left = rendy(Template.panel, {
side: 'left', side: 'left',
content: panel, content: panel,
className: !oneFilePanel ? '' : 'panel-single', className: !oneFilePanel ? '' : 'panel-single',
}); });
let right = ''; let right = '';
if (!oneFilePanel) if (!oneFilePanel)
right = rendy(Template.panel, { right = rendy(Template.panel, {
side: 'right', side: 'right',
content: panel, content: panel,
className: '', className: '',
}); });
const name = config('name'); const name = config('name');
data = rendy(data, { data = rendy(data, {
title: CloudFunc.getTitle({ title: CloudFunc.getTitle({
name, name,
}), }),
fm: left + right, fm: left + right,
prefix: getPrefix(config), prefix: getPrefix(config),
config: JSON.stringify(config('*')), config: stringify(config('*')),
columns: Columns[config('columns')], columns: getColumns()[config('columns')],
}); });
return data; return data;
} }
@ -177,7 +175,7 @@ function buildIndex(config, html, data) {
prefix: getPrefix(config), prefix: getPrefix(config),
template: Template, template: Template,
}); });
return indexProcessing(config, { return indexProcessing(config, {
panel, panel,
data: html, data: html,
@ -188,14 +186,14 @@ module.exports._hideKeysPanel = hideKeysPanel;
function hideKeysPanel(html) { function hideKeysPanel(html) {
const keysPanel = '<div id="js-keyspanel" class="{{ className }}"'; const keysPanel = '<div id="js-keyspanel" class="{{ className }}"';
const keysPanelRegExp = '<div id="?js-keyspanel"? class="?{{ className }}"?'; const keysPanelRegExp = '<div id="?js-keyspanel"? class="?{{ className }}"?';
const from = rendy(keysPanelRegExp, { const from = rendy(keysPanelRegExp, {
className: 'keyspanel', className: 'keyspanel',
}); });
const to = rendy(keysPanel, { const to = rendy(keysPanel, {
className: 'keyspanel hidden', className: 'keyspanel hidden',
}); });
return html.replace(RegExp(from), to); return html.replace(RegExp(from), to);
} }

View file

@ -1,9 +1,9 @@
'use strict'; 'use strict';
const {Readable} = require('stream'); const {Readable} = require('node:stream');
const path = require('path'); const path = require('node:path');
const fs = require('fs'); const fs = require('node:fs');
const tryToCatch = require('try-to-catch'); const tryToCatch = require('try-to-catch');
const {test, stub} = require('supertape'); const {test, stub} = require('supertape');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const path = require('path'); const path = require('node:path');
const readFilesSync = require('@cloudcmd/read-files-sync'); const readFilesSync = require('@cloudcmd/read-files-sync');
const templatePath = path.join(__dirname, '..', 'tmpl/fs'); const templatePath = path.join(__dirname, '..', 'tmpl/fs');

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const fs = require('fs'); const fs = require('node:fs');
const {join} = require('path'); const {join} = require('node:path');
const {test, stub} = require('supertape'); const {test, stub} = require('supertape');

View file

@ -3,36 +3,36 @@
const tryCatch = require('try-catch'); const tryCatch = require('try-catch');
const exit = require('./exit'); const exit = require('./exit');
const columns = require('./columns'); const {getColumns} = require('./columns');
const isString = (a) => typeof a === 'string'; const isString = (a) => typeof a === 'string';
module.exports.root = (dir, config) => { module.exports.root = (dir, config) => {
if (!isString(dir)) if (!isString(dir))
throw Error('dir should be a string'); throw Error('dir should be a string');
if (dir === '/') if (dir === '/')
return; return;
if (config('dropbox')) if (config('dropbox'))
return; return;
const {statSync} = require('fs'); const {statSync} = require('fs');
const [error] = tryCatch(statSync, dir); const [error] = tryCatch(statSync, dir);
if (error) if (error)
return exit('cloudcmd --root: %s', error.message); return exit('cloudcmd --root: %s', error.message);
}; };
module.exports.editor = (name) => { module.exports.editor = (name) => {
const reg = /^(dword|edward|deepword)$/; const reg = /^(dword|edward|deepword)$/;
if (!reg.test(name)) if (!reg.test(name))
exit('cloudcmd --editor: could be "dword", "edward" or "deepword" only'); exit('cloudcmd --editor: could be "dword", "edward" or "deepword" only');
}; };
module.exports.packer = (name) => { module.exports.packer = (name) => {
const reg = /^(tar|zip)$/; const reg = /^(tar|zip)$/;
if (!reg.test(name)) if (!reg.test(name))
exit('cloudcmd --packer: could be "tar" or "zip" only'); exit('cloudcmd --packer: could be "tar" or "zip" only');
}; };
@ -40,14 +40,14 @@ module.exports.packer = (name) => {
module.exports.columns = (type) => { module.exports.columns = (type) => {
const addQuotes = (a) => `"${a}"`; const addQuotes = (a) => `"${a}"`;
const all = Object const all = Object
.keys(columns) .keys(getColumns())
.concat(''); .concat('');
const names = all const names = all
.filter(Boolean) .filter(Boolean)
.map(addQuotes) .map(addQuotes)
.join(', '); .join(', ');
if (!all.includes(type)) if (!all.includes(type))
exit(`cloudcmd --columns: can be only one of: ${names}`); exit(`cloudcmd --columns: can be only one of: ${names}`);
}; };

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const fs = require('fs'); const fs = require('node:fs');
const {test, stub} = require('supertape'); const {test, stub} = require('supertape');
@ -22,20 +22,20 @@ test('validate: root: bad', (t) => {
const config = { const config = {
root: Math.random(), root: Math.random(),
}; };
const [e] = tryCatch(cloudcmd, { const [e] = tryCatch(cloudcmd, {
config, config,
}); });
t.equal(e.message, 'dir should be a string', 'should throw'); t.equal(e.message, 'dir should be a string', 'should throw');
t.end(); t.end();
}); });
test('validate: root: config', (t) => { test('validate: root: config', (t) => {
const config = stub().returns(true); const config = stub().returns(true);
validate.root('/hello', config); validate.root('/hello', config);
t.calledWith(config, ['dropbox'], 'should call config'); t.calledWith(config, ['dropbox'], 'should call config');
t.end(); t.end();
}); });
@ -43,7 +43,7 @@ test('validate: root: config', (t) => {
test('validate: root: /', (t) => { test('validate: root: /', (t) => {
const fn = stub(); const fn = stub();
validate.root('/', fn); validate.root('/', fn);
t.notCalled(fn, 'should not call fn'); t.notCalled(fn, 'should not call fn');
t.end(); t.end();
}); });
@ -51,57 +51,57 @@ test('validate: root: /', (t) => {
test('validate: root: stat', (t) => { test('validate: root: stat', (t) => {
const fn = stub(); const fn = stub();
const {statSync} = fs; const {statSync} = fs;
const error = 'ENOENT'; const error = 'ENOENT';
fs.statSync = () => { fs.statSync = () => {
throw Error(error); throw Error(error);
}; };
mockRequire(exitPath, fn); mockRequire(exitPath, fn);
const {root} = reRequire(validatePath); const {root} = reRequire(validatePath);
root('hello', fn); root('hello', fn);
const msg = 'cloudcmd --root: %s'; const msg = 'cloudcmd --root: %s';
fs.statSync = statSync; fs.statSync = statSync;
stopAll(); stopAll();
t.calledWith(fn, [msg, error], 'should call fn'); t.calledWith(fn, [msg, error], 'should call fn');
t.end(); t.end();
}); });
test('validate: packer: not valid', (t) => { test('validate: packer: not valid', (t) => {
const fn = stub(); const fn = stub();
mockRequire(exitPath, fn); mockRequire(exitPath, fn);
const {packer} = reRequire(validatePath); const {packer} = reRequire(validatePath);
const msg = 'cloudcmd --packer: could be "tar" or "zip" only'; const msg = 'cloudcmd --packer: could be "tar" or "zip" only';
packer('hello'); packer('hello');
stopAll(); stopAll();
t.calledWith(fn, [msg], 'should call fn'); t.calledWith(fn, [msg], 'should call fn');
t.end(); t.end();
}); });
test('validate: editor: not valid', (t) => { test('validate: editor: not valid', (t) => {
const fn = stub(); const fn = stub();
mockRequire(exitPath, fn); mockRequire(exitPath, fn);
const {editor} = reRequire(validatePath); const {editor} = reRequire(validatePath);
const msg = 'cloudcmd --editor: could be "dword", "edward" or "deepword" only'; const msg = 'cloudcmd --editor: could be "dword", "edward" or "deepword" only';
editor('hello'); editor('hello');
stopAll(); stopAll();
t.calledWith(fn, [msg], 'should call fn'); t.calledWith(fn, [msg], 'should call fn');
t.end(); t.end();
}); });
@ -109,33 +109,36 @@ test('validate: editor: not valid', (t) => {
test('validate: columns', (t) => { test('validate: columns', (t) => {
const fn = stub(); const fn = stub();
mockRequire(exitPath, fn); mockRequire(exitPath, fn);
const {columns} = require(validatePath); const {columns} = require(validatePath);
columns('name-size-date'); columns('name-size-date');
stopAll(); stopAll();
t.notCalled(fn, 'should not call exit'); t.notCalled(fn, 'should not call exit');
t.end(); t.end();
}); });
test('validate: columns: wrong', (t) => { test('validate: columns: wrong', (t) => {
const fn = stub(); const fn = stub();
const getColumns = stub().returns({
mockRequire(exitPath, fn);
mockRequire(columnsPath, {
'name-size-date': '', 'name-size-date': '',
'name-size': '', 'name-size': '',
}); });
mockRequire(exitPath, fn);
mockRequire(columnsPath, {
getColumns,
});
const {columns} = reRequire(validatePath); const {columns} = reRequire(validatePath);
const msg = 'cloudcmd --columns: can be only one of: "name-size-date", "name-size"'; const msg = 'cloudcmd --columns: can be only one of: "name-size-date", "name-size"';
columns('hello'); columns('hello');
stopAll(); stopAll();
t.calledWith(fn, [msg], 'should call exit'); t.calledWith(fn, [msg], 'should call exit');
t.end(); t.end();
}); });

View file

@ -1,8 +1,8 @@
'use strict'; 'use strict';
const process = require('process'); const process = require('node:process');
const http = require('http'); const http = require('node:http');
const os = require('os'); const os = require('node:os');
const express = require('express'); const express = require('express');
const io = require('socket.io'); const io = require('socket.io');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const fs = require('fs'); const fs = require('node:fs');
const tryCatch = require('try-catch'); const tryCatch = require('try-catch');
const DIR = `${__dirname}/../../`; const DIR = `${__dirname}/../../`;

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const {mkdirSync} = require('fs'); const {mkdirSync} = require('node:fs');
const {join} = require('path'); const {join} = require('node:path');
const test = require('supertape'); const test = require('supertape');
const rimraf = require('rimraf'); const rimraf = require('rimraf');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const fs = require('fs'); const fs = require('node:fs');
const test = require('supertape'); const test = require('supertape');
const {Volume} = require('memfs'); const {Volume} = require('memfs');

View file

@ -1,8 +1,8 @@
'use strict'; 'use strict';
const fs = require('fs'); const fs = require('node:fs');
const {join} = require('path'); const {join} = require('node:path');
const {promisify} = require('util'); const {promisify} = require('node:util');
const {reRequire} = require('mock-require'); const {reRequire} = require('mock-require');
const test = require('supertape'); const test = require('supertape');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const fs = require('fs'); const fs = require('node:fs');
const test = require('supertape'); const test = require('supertape');
const {Volume} = require('memfs'); const {Volume} = require('memfs');
@ -19,45 +19,45 @@ test('cloudcmd: rest: rename', async (t) => {
'/fixture/mv.txt': 'hello', '/fixture/mv.txt': 'hello',
'/fixture/tmp/a.txt': 'a', '/fixture/tmp/a.txt': 'a',
}; };
const vol = Volume.fromJSON(volume, '/'); const vol = Volume.fromJSON(volume, '/');
const unionFS = ufs const unionFS = ufs
.use(vol) .use(vol)
.use(fs); .use(fs);
mockRequire('fs', unionFS); mockRequire('node:fs', unionFS);
reRequire('@cloudcmd/rename-files'); reRequire('@cloudcmd/rename-files');
reRequire('@cloudcmd/move-files'); reRequire('@cloudcmd/move-files');
reRequire(restPath); reRequire(restPath);
const cloudcmd = reRequire(cloudcmdPath); const cloudcmd = reRequire(cloudcmdPath);
const {createConfigManager} = cloudcmd; const {createConfigManager} = cloudcmd;
const configManager = createConfigManager(); const configManager = createConfigManager();
configManager('auth', false); configManager('auth', false);
configManager('root', '/'); configManager('root', '/');
const {request} = serveOnce(cloudcmd, { const {request} = serveOnce(cloudcmd, {
configManager, configManager,
}); });
const files = { const files = {
from: '/fixture/mv.txt', from: '/fixture/mv.txt',
to: '/fixture/tmp/mv.txt', to: '/fixture/tmp/mv.txt',
}; };
const {body} = await request.put(`/api/v1/rename`, { const {body} = await request.put(`/api/v1/rename`, {
body: files, body: files,
}); });
mockRequire.stop('fs'); mockRequire.stopAll();
const expected = 'rename: ok("{"from":"/fixture/mv.txt","to":"/fixture/tmp/mv.txt"}")'; const expected = 'rename: ok("{"from":"/fixture/mv.txt","to":"/fixture/tmp/mv.txt"}")';
stopAll(); stopAll();
t.equal(body, expected, 'should move'); t.equal(body, expected, 'should move');
t.end(); t.end();
}); });
@ -65,23 +65,23 @@ test('cloudcmd: rest: rename', async (t) => {
test('cloudcmd: rest: rename: no from', async (t) => { test('cloudcmd: rest: rename: no from', async (t) => {
const cloudcmd = reRequire(cloudcmdPath); const cloudcmd = reRequire(cloudcmdPath);
const {createConfigManager} = cloudcmd; const {createConfigManager} = cloudcmd;
const configManager = createConfigManager(); const configManager = createConfigManager();
configManager('auth', false); configManager('auth', false);
configManager('root', '/'); configManager('root', '/');
const {request} = serveOnce(cloudcmd, { const {request} = serveOnce(cloudcmd, {
configManager, configManager,
}); });
const files = {}; const files = {};
const {body} = await request.put(`/api/v1/rename`, { const {body} = await request.put(`/api/v1/rename`, {
body: files, body: files,
}); });
const expected = '"from" should be filled'; const expected = '"from" should be filled';
t.equal(body, expected); t.equal(body, expected);
t.end(); t.end();
}); });
@ -89,25 +89,25 @@ test('cloudcmd: rest: rename: no from', async (t) => {
test('cloudcmd: rest: rename: no to', async (t) => { test('cloudcmd: rest: rename: no to', async (t) => {
const cloudcmd = reRequire(cloudcmdPath); const cloudcmd = reRequire(cloudcmdPath);
const {createConfigManager} = cloudcmd; const {createConfigManager} = cloudcmd;
const configManager = createConfigManager(); const configManager = createConfigManager();
configManager('auth', false); configManager('auth', false);
configManager('root', '/'); configManager('root', '/');
const {request} = serveOnce(cloudcmd, { const {request} = serveOnce(cloudcmd, {
configManager, configManager,
}); });
const files = { const files = {
from: '/', from: '/',
}; };
const {body} = await request.put(`/api/v1/rename`, { const {body} = await request.put(`/api/v1/rename`, {
body: files, body: files,
}); });
const expected = '"to" should be filled'; const expected = '"to" should be filled';
t.equal(body, expected); t.equal(body, expected);
t.end(); t.end();
}); });

View file

@ -1,33 +0,0 @@
'use strict';
const process = require('process');
const test = require('supertape');
const fs = require('fs');
const {reRequire} = require('mock-require');
const columnsPath = '../../server/columns';
test('columns: prod', (t) => {
const {NODE_ENV} = process.env;
process.env.NODE_ENV = '';
const columns = reRequire(columnsPath);
process.env.NODE_ENV = NODE_ENV;
t.equal(columns[''], '');
t.end();
});
test('columns: dev', (t) => {
const {NODE_ENV} = process.env;
process.env.NODE_ENV = 'development';
const columns = reRequire(columnsPath);
const css = fs.readFileSync(`${__dirname}/../../css/columns/name-size-date.css`, 'utf8');
process.env.NODE_ENV = NODE_ENV;
t.equal(columns['name-size-date'], css);
t.end();
});

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const path = require('path'); const path = require('node:path');
const {once} = require('events'); const {once} = require('node:events');
const test = require('supertape'); const test = require('supertape');
const io = require('socket.io-client'); const io = require('socket.io-client');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const process = require('process'); const process = require('node:process');
const test = require('supertape'); const test = require('supertape');
const env = require('../../server/env'); const env = require('../../server/env');

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const {join} = require('path'); const {join} = require('node:path');
const {test, stub} = require('supertape'); const {test, stub} = require('supertape');
const cloudcmdPath = join(__dirname, '..', '..'); const cloudcmdPath = join(__dirname, '..', '..');