diff --git a/bin/cloudcmd.js b/bin/cloudcmd.mjs similarity index 82% rename from bin/cloudcmd.js rename to bin/cloudcmd.mjs index 201331af..85e43a08 100755 --- a/bin/cloudcmd.js +++ b/bin/cloudcmd.mjs @@ -1,25 +1,21 @@ #!/usr/bin/env node -'use strict'; - -const Info = require('../package'); const DIR_SERVER = '../server/'; -const {promisify} = require('util'); -const tryToCatch = require('try-to-catch'); +import {createRequire} from 'module'; +import {promisify} from 'util'; +import tryToCatch from 'try-to-catch'; +import {createSimport} from 'simport'; -const exit = require(DIR_SERVER + 'exit'); -const { - createConfig, - configPath, -} = require(DIR_SERVER + 'config'); +import exit from '../server/exit.js'; +import {createConfig, configPath} from '../server/config.js'; const config = createConfig({ configPath, }); -const env = require(DIR_SERVER + 'env'); -const prefixer = require(DIR_SERVER + '/prefixer'); +import env from '../server/env.js'; +import prefixer from '../server/prefixer.js'; const choose = (a, b) => { if (a === undefined) @@ -30,6 +26,11 @@ const choose = (a, b) => { process.on('unhandledRejection', exit); +const simport = createSimport(import.meta.url); +const require = createRequire(import.meta.url); + +const Info = require('../package.json'); + const {argv} = process; const args = require('minimist')(argv.slice(2), { string: [ @@ -186,7 +187,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'), @@ -200,17 +201,17 @@ async function main() { const password = env('password') || args.password; if (password) - config('password', getPassword(password)); + config('password', await getPassword(password)); - validateRoot(options.root, config); + await validateRoot(options.root, config); if (args['show-config']) - showConfig(); + await showConfig(); - const distribute = require('../server/distribute'); + const distribute = await simport('../server/distribute/index.js'); const importConfig = promisify(distribute.import); - start(options, config); + await start(options, config); if (args.save) config.write(); @@ -219,8 +220,8 @@ async function main() { await importConfig(config); } -function validateRoot(root, config) { - const validate = require(DIR_SERVER + 'validate'); +async function validateRoot(root, config) { + const validate = await simport(DIR_SERVER + 'validate.js'); validate.root(root, config); if (root === '/') @@ -229,8 +230,8 @@ function validateRoot(root, config) { console.log(`root: ${root}`); } -function getPassword(password) { - const criton = require('criton'); +async function getPassword(password) { + const criton = await import('criton'); return criton(password, config('algo')); } @@ -238,13 +239,13 @@ function version() { console.log('v' + Info.version); } -function start(options, config) { - const SERVER = DIR_SERVER + 'server'; +async function start(options, config) { + const SERVER = DIR_SERVER + 'server.js'; if (!args.server) return; - const server = require(SERVER); + const server = await simport(SERVER); server(options, config); } @@ -257,27 +258,21 @@ function port(arg) { exit('cloudcmd --port: should be a number'); } -function showConfig() { - const show = require('../server/show-config'); +async function showConfig() { + const show = await simport('../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 tryToCatch = await simport('try-to-catch'); + const forEachKey = await import('for-each-key'); - const readjsonSync = (name) => jju.parse(fs.readFileSync(name, 'utf8'), { - mode: 'json', - }); - - const [error, data] = tryCatch(readjsonSync, name); + const [error, data] = await tryToCatch(simport, name); if (error) return exit(error.message); @@ -285,10 +280,11 @@ function readConfig(name) { 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 = require('../json/help.json'); + const forEachKey = await simport('for-each-key'); + const currify = await simport('currify'); + const usage = 'Usage: cloudcmd [options]'; const url = Info.homepage; const log = currify((a, b, c) => console.log(a, b, c)); @@ -305,17 +301,17 @@ function repl() { } async function checkUpdate() { - const load = require('package-json'); + const load = await simport('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 simport('chalk'); const latestVersion = chalk.green.bold('v' + version); const latest = `update available: ${latestVersion}`; diff --git a/bin/release.js b/bin/release.mjs similarity index 89% rename from bin/release.js rename to bin/release.mjs index f3e6bf93..b2870780 100755 --- a/bin/release.js +++ b/bin/release.mjs @@ -1,14 +1,11 @@ #!/usr/bin/env node -'use strict'; +import Info from '../package.js'; -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 a73d66ca..08e47b9e 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "server" ], "bin": { - "cloudcmd": "bin/cloudcmd.js" + "cloudcmd": "bin/cloudcmd.mjs" }, "nyc": { "exclude": [ @@ -152,6 +152,7 @@ "restafary": "^8.0.0", "restbox": "^2.0.0", "shortdate": "^2.0.0", + "simport": "^1.0.1", "socket.io": "^3.0.1", "socket.io-client": "^3.0.1", "squad": "^3.0.0",