From db1cb84597e03d779ed67f20830c82db891bf3ad Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 8 Apr 2020 00:51:09 +0300 Subject: [PATCH] feature(cloudcmd) convert commonjs to esm --- .eslintrc.js => .eslintrc.cjs | 0 .putout.json | 3 + bin/cloudcmd.js | 103 +++++++++++++++++----------------- bin/release.js | 10 ++-- package.json | 1 + server/env.js | 4 +- server/exit.js | 2 +- server/prefixer.js | 2 +- server/super-import.js | 23 ++++++++ 9 files changed, 88 insertions(+), 60 deletions(-) rename .eslintrc.js => .eslintrc.cjs (100%) create mode 100644 server/super-import.js diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 100% rename from .eslintrc.js rename to .eslintrc.cjs diff --git a/.putout.json b/.putout.json index e8ed3cf0..8e75e393 100644 --- a/.putout.json +++ b/.putout.json @@ -1,4 +1,7 @@ { + "rules": { + "convert-commonjs-to-esm": "on" + }, "match": { "server": { "remove-process-exit": true diff --git a/bin/cloudcmd.js b/bin/cloudcmd.js index 1897cfd5..3c160abf 100755 --- a/bin/cloudcmd.js +++ b/bin/cloudcmd.js @@ -1,26 +1,26 @@ #!/usr/bin/env node -'use strict'; +import { createRequire } from 'module'; +const require = createRequire(import.meta.url); -const Info = require('../package'); -const DIR_SERVER = '../server/'; +const Info = require('../package.json'); +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 { createConfig, configPath, -} = require(DIR_SERVER + 'config'); +} = require('../server/config.js'); const config = createConfig({ configPath, }); -const env = require(DIR_SERVER + 'env'); -const prefixer = require(DIR_SERVER + '/prefixer'); - -const noop = () => {}; +import env from '../server/env.js'; +import prefixer from '../server/prefixer.js'; +import superImport from '../server/super-import.js'; const choose = (a, b) => { if (a === undefined) @@ -31,8 +31,10 @@ const choose = (a, b) => { process.on('unhandledRejection', exit); +import minimist from 'minimist'; + const {argv} = process; -const args = require('minimist')(argv.slice(2), { +const args = minimist(argv.slice(2), { string: [ 'name', 'port', @@ -149,7 +151,7 @@ async function main() { if (args.repl) repl(); - checkUpdate(); + await checkUpdate(); port(args.port); config('name', args.name); @@ -188,7 +190,7 @@ async function main() { config('dropbox', args['dropbox']); config('dropboxToken', args['dropbox-token'] || ''); - readConfig(args.config); + await readConfig(args.config); const options = { root: config('root'), @@ -202,14 +204,14 @@ async function main() { const password = env('password') || args.password; if (password) - config('password', getPassword(password)); + config('password', await getPassword(password)); validateRoot(options.root, 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); await importConfig(config) @@ -220,8 +222,8 @@ async function main() { start(options, config); } -function validateRoot(root, config) { - const validate = require(DIR_SERVER + 'validate'); +async function validateRoot(root, config) { + const validate = await superImport(DIR_SERVER + 'validate'); validate.root(root, config); if (root === '/') @@ -230,8 +232,8 @@ function validateRoot(root, config) { console.log(`root: ${root}`); } -function getPassword(password) { - const criton = require('criton'); +async function getPassword(password) { + const criton = await superImport('criton'); return criton(password, config('algo')); } @@ -239,13 +241,13 @@ function version() { console.log('v' + Info.version); } -function start(options, config) { +async function start(options, config) { const SERVER = DIR_SERVER + 'server'; if (!args.server) return; - const server = require(SERVER); + const server = await superImport(SERVER); server(options, config); } @@ -258,70 +260,71 @@ function port(arg) { exit('cloudcmd --port: should be a number'); } -function showConfig() { - const show = require('../server/show-config'); +async function showConfig() { + const show = await superImport(`${DIR_SERVER}/show-config`); const data = show(config('*')); - + console.log(data); } -function readConfig(name) { +async function readConfig(name) { if (!name) return; - - const fs = require('fs'); - const tryCatch = require('try-catch'); - const jju = require('jju'); - const forEachKey = require('for-each-key'); - + + const fs = await superImport('fs'); + const tryCatch = await superImport('try-catch'); + const jju = await superImport('jju'); + const forEachKey = await superImport('for-each-key'); + const readjsonSync = (name) => jju.parse(fs.readFileSync(name, 'utf8'), { mode: 'json', }); - + const [error, data] = tryCatch(readjsonSync, name); - + if (error) return exit(error.message); - + forEachKey(config, data); } -function help() { - const bin = require('../json/help'); - const forEachKey = require('for-each-key'); - const currify = require('currify'); +async function help() { + const bin = await superImport('../json/help.json'); + const forEachKey = await superImport('for-each-key'); + const currify = await superImport('currify'); + const usage = 'Usage: cloudcmd [options]'; const url = Info.homepage; const log = currify((a, b, c) => console.log(a, b, c)); - + console.log(usage); console.log('Options:'); forEachKey(log(' %s %s'), bin); console.log('\nGeneral help using Cloud Commander: <%s>', url); } -function repl() { +async function repl() { console.log('REPL mode enabled (telnet localhost 1337)'); - require(DIR_SERVER + 'repl'); + await superImport(DIR_SERVER + 'repl'); } async function checkUpdate() { - const load = require('package-json'); - + const load = await superImport('package-json'); + const {version} = await load(Info.name, 'latest') - showUpdateInfo(version); + await showUpdateInfo(version); } -function showUpdateInfo(version) { +async function showUpdateInfo(version) { if (version === Info.version) return; - - const chalk = require('chalk'); - + + const chalk = await superImport('chalk'); + const latestVersion = chalk.green.bold('v' + version); const latest = `update available: ${latestVersion}`; const current = chalk.dim(`(current: v${Info.version})`); - + console.log('%s %s', latest, current); } diff --git a/bin/release.js b/bin/release.js index f3e6bf93..a872ca52 100755 --- a/bin/release.js +++ b/bin/release.js @@ -1,14 +1,12 @@ #!/usr/bin/env node -'use strict'; - const DIR = '../'; const Info = require(DIR + 'package'); -const minor = require('minor'); -const place = require('place'); -const rendy = require('rendy'); -const shortdate = require('shortdate'); +import minor from 'minor'; +import place from 'place'; +import rendy from 'rendy'; +import shortdate from 'shortdate'; const ERROR = Error('ERROR: version is missing. release --patch|--minor|--major'); diff --git a/package.json b/package.json index 0cc36bea..21590dd6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "name": "cloudcmd", "version": "14.3.8", + "type": "module", "author": "coderaiser (https://github.com/coderaiser)", "description": "File manager for the web with console and editor", "homepage": "http://cloudcmd.io", diff --git a/server/env.js b/server/env.js index bec9ec30..0097dcbb 100644 --- a/server/env.js +++ b/server/env.js @@ -3,8 +3,8 @@ const {env} = process; const up = (a) => a.toUpperCase(); -module.exports = parse; -module.exports.bool = (name) => { +export default parse; +export const bool = (name) => { const value = parse(name); if (value === 'true') diff --git a/server/exit.js b/server/exit.js index f01cd130..ec01608c 100644 --- a/server/exit.js +++ b/server/exit.js @@ -2,7 +2,7 @@ const getMessage = (a) => a && a.message || a; -module.exports = (...args) => { +export default (...args) => { const messages = args.map(getMessage); console.error(...messages); diff --git a/server/prefixer.js b/server/prefixer.js index 3b5f3a95..71573f4d 100644 --- a/server/prefixer.js +++ b/server/prefixer.js @@ -1,6 +1,6 @@ 'use strict'; -module.exports = (value) => { +export default (value) => { if (typeof value !== 'string') return ''; diff --git a/server/super-import.js b/server/super-import.js new file mode 100644 index 00000000..7d951f4a --- /dev/null +++ b/server/super-import.js @@ -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; +} +