feature(cloudcmd) convert commonjs to esm

This commit is contained in:
coderaiser 2020-04-08 00:51:09 +03:00
parent 00a7575c99
commit db1cb84597
9 changed files with 88 additions and 60 deletions

View file

@ -1,4 +1,7 @@
{ {
"rules": {
"convert-commonjs-to-esm": "on"
},
"match": { "match": {
"server": { "server": {
"remove-process-exit": true "remove-process-exit": true

View file

@ -1,26 +1,26 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict'; import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const Info = require('../package'); const Info = require('../package.json');
const DIR_SERVER = '../server/'; const DIR_SERVER = '../server';
const {promisify} = require('util'); import {promisify} from 'util';
const exit = require(DIR_SERVER + 'exit'); import exit from '../server/exit.js';
const { const {
createConfig, createConfig,
configPath, configPath,
} = require(DIR_SERVER + 'config'); } = require('../server/config.js');
const config = createConfig({ const config = createConfig({
configPath, configPath,
}); });
const env = require(DIR_SERVER + 'env'); import env from '../server/env.js';
const prefixer = require(DIR_SERVER + '/prefixer'); import prefixer from '../server/prefixer.js';
import superImport from '../server/super-import.js';
const noop = () => {};
const choose = (a, b) => { const choose = (a, b) => {
if (a === undefined) if (a === undefined)
@ -31,8 +31,10 @@ const choose = (a, b) => {
process.on('unhandledRejection', exit); process.on('unhandledRejection', exit);
import minimist from 'minimist';
const {argv} = process; const {argv} = process;
const args = require('minimist')(argv.slice(2), { const args = minimist(argv.slice(2), {
string: [ string: [
'name', 'name',
'port', 'port',
@ -149,7 +151,7 @@ async function main() {
if (args.repl) if (args.repl)
repl(); repl();
checkUpdate(); await checkUpdate();
port(args.port); port(args.port);
config('name', args.name); config('name', args.name);
@ -188,7 +190,7 @@ async function main() {
config('dropbox', args['dropbox']); config('dropbox', args['dropbox']);
config('dropboxToken', args['dropbox-token'] || ''); config('dropboxToken', args['dropbox-token'] || '');
readConfig(args.config); await readConfig(args.config);
const options = { const options = {
root: config('root'), root: config('root'),
@ -202,14 +204,14 @@ async function main() {
const password = env('password') || args.password; const password = env('password') || args.password;
if (password) if (password)
config('password', getPassword(password)); config('password', await getPassword(password));
validateRoot(options.root, config); validateRoot(options.root, config);
if (args['show-config']) if (args['show-config'])
showConfig(); await showConfig();
const {default: distribute} = await import('../server/distribute/index.js'); const distribute = await superImport('../server/distribute');
const importConfig = promisify(distribute.import); const importConfig = promisify(distribute.import);
await importConfig(config) await importConfig(config)
@ -220,8 +222,8 @@ async function main() {
start(options, config); start(options, config);
} }
function validateRoot(root, config) { async function validateRoot(root, config) {
const validate = require(DIR_SERVER + 'validate'); const validate = await superImport(DIR_SERVER + 'validate');
validate.root(root, config); validate.root(root, config);
if (root === '/') if (root === '/')
@ -230,8 +232,8 @@ function validateRoot(root, config) {
console.log(`root: ${root}`); console.log(`root: ${root}`);
} }
function getPassword(password) { async function getPassword(password) {
const criton = require('criton'); const criton = await superImport('criton');
return criton(password, config('algo')); return criton(password, config('algo'));
} }
@ -239,13 +241,13 @@ function version() {
console.log('v' + Info.version); console.log('v' + Info.version);
} }
function start(options, config) { async function start(options, config) {
const SERVER = DIR_SERVER + 'server'; const SERVER = DIR_SERVER + 'server';
if (!args.server) if (!args.server)
return; return;
const server = require(SERVER); const server = await superImport(SERVER);
server(options, config); server(options, config);
} }
@ -258,70 +260,71 @@ function port(arg) {
exit('cloudcmd --port: should be a number'); exit('cloudcmd --port: should be a number');
} }
function showConfig() { async function showConfig() {
const show = require('../server/show-config'); const show = await superImport(`${DIR_SERVER}/show-config`);
const data = show(config('*')); const data = show(config('*'));
console.log(data); console.log(data);
} }
function readConfig(name) { async function readConfig(name) {
if (!name) if (!name)
return; return;
const fs = require('fs'); const fs = await superImport('fs');
const tryCatch = require('try-catch'); const tryCatch = await superImport('try-catch');
const jju = require('jju'); const jju = await superImport('jju');
const forEachKey = require('for-each-key'); const forEachKey = await superImport('for-each-key');
const readjsonSync = (name) => jju.parse(fs.readFileSync(name, 'utf8'), { const readjsonSync = (name) => jju.parse(fs.readFileSync(name, 'utf8'), {
mode: 'json', mode: 'json',
}); });
const [error, data] = tryCatch(readjsonSync, name); const [error, data] = tryCatch(readjsonSync, name);
if (error) if (error)
return exit(error.message); return exit(error.message);
forEachKey(config, data); forEachKey(config, data);
} }
function help() { async function help() {
const bin = require('../json/help'); const bin = await superImport('../json/help.json');
const forEachKey = require('for-each-key'); const forEachKey = await superImport('for-each-key');
const currify = require('currify'); const currify = await superImport('currify');
const usage = 'Usage: cloudcmd [options]'; const usage = 'Usage: cloudcmd [options]';
const url = Info.homepage; const url = Info.homepage;
const log = currify((a, b, c) => console.log(a, b, c)); const log = currify((a, b, c) => console.log(a, b, c));
console.log(usage); console.log(usage);
console.log('Options:'); console.log('Options:');
forEachKey(log(' %s %s'), bin); forEachKey(log(' %s %s'), bin);
console.log('\nGeneral help using Cloud Commander: <%s>', url); console.log('\nGeneral help using Cloud Commander: <%s>', url);
} }
function repl() { async function repl() {
console.log('REPL mode enabled (telnet localhost 1337)'); console.log('REPL mode enabled (telnet localhost 1337)');
require(DIR_SERVER + 'repl'); await superImport(DIR_SERVER + 'repl');
} }
async function checkUpdate() { async function checkUpdate() {
const load = require('package-json'); const load = await superImport('package-json');
const {version} = await load(Info.name, 'latest') const {version} = await load(Info.name, 'latest')
showUpdateInfo(version); await showUpdateInfo(version);
} }
function showUpdateInfo(version) { async function showUpdateInfo(version) {
if (version === Info.version) if (version === Info.version)
return; return;
const chalk = require('chalk'); const chalk = await superImport('chalk');
const latestVersion = chalk.green.bold('v' + version); const latestVersion = chalk.green.bold('v' + version);
const latest = `update available: ${latestVersion}`; const latest = `update available: ${latestVersion}`;
const current = chalk.dim(`(current: v${Info.version})`); const current = chalk.dim(`(current: v${Info.version})`);
console.log('%s %s', latest, current); console.log('%s %s', latest, current);
} }

View file

@ -1,14 +1,12 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict';
const DIR = '../'; const DIR = '../';
const Info = require(DIR + 'package'); const Info = require(DIR + 'package');
const minor = require('minor'); import minor from 'minor';
const place = require('place'); import place from 'place';
const rendy = require('rendy'); import rendy from 'rendy';
const shortdate = require('shortdate'); import shortdate from 'shortdate';
const ERROR = Error('ERROR: version is missing. release --patch|--minor|--major'); const ERROR = Error('ERROR: version is missing. release --patch|--minor|--major');

View file

@ -1,6 +1,7 @@
{ {
"name": "cloudcmd", "name": "cloudcmd",
"version": "14.3.8", "version": "14.3.8",
"type": "module",
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)", "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
"description": "File manager for the web with console and editor", "description": "File manager for the web with console and editor",
"homepage": "http://cloudcmd.io", "homepage": "http://cloudcmd.io",

View file

@ -3,8 +3,8 @@
const {env} = process; const {env} = process;
const up = (a) => a.toUpperCase(); const up = (a) => a.toUpperCase();
module.exports = parse; export default parse;
module.exports.bool = (name) => { export const bool = (name) => {
const value = parse(name); const value = parse(name);
if (value === 'true') if (value === 'true')

View file

@ -2,7 +2,7 @@
const getMessage = (a) => a && a.message || a; const getMessage = (a) => a && a.message || a;
module.exports = (...args) => { export default (...args) => {
const messages = args.map(getMessage); const messages = args.map(getMessage);
console.error(...messages); console.error(...messages);

View file

@ -1,6 +1,6 @@
'use strict'; 'use strict';
module.exports = (value) => { export default (value) => {
if (typeof value !== 'string') if (typeof value !== 'string')
return ''; return '';

23
server/super-import.js Normal file
View file

@ -0,0 +1,23 @@
import tryToCatch from 'try-to-catch';
export default async function superImport(str) {
const tryToCatch = await expandDefault('try-to-catch');
const [eJs, resultJs] = await tryToCatch(expandDefault, `${str}.js`);
if (!eJs)
return resultJs;
const [eIndex, resultIndex] = await tryToCatch(expandDefault, `${str}/index.js`);
if (!eIndex)
return resultIndex;
return await expandDefault(str);
}
async function expandDefault(a) {
const result = await import(a);
return result.default;
}