From d6d587af66dc77459052750e373da11a956d2657 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 22 Dec 2020 13:23:01 +0200 Subject: [PATCH] feature(static) converted to ESM --- .eslintrc.js => .eslintrc.cjs | 22 +++++--- .putout.json | 14 ++--- bin/cloudcmd.js | 99 ++++++++++++++++++----------------- bin/release.js | 13 +++-- package.json | 3 +- static/user-menu.js | 8 ++- static/user-menu.spec.js | 29 +++++----- 7 files changed, 93 insertions(+), 95 deletions(-) rename .eslintrc.js => .eslintrc.cjs (58%) diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 58% rename from .eslintrc.js rename to .eslintrc.cjs index bc2b16c7..1a484e56 100644 --- a/.eslintrc.js +++ b/.eslintrc.cjs @@ -1,7 +1,19 @@ -'use strict'; - module.exports = { + parser: '@babel/eslint-parser', + parserOptions: { + sourceType: 'module', + babelOptions: { + sourceType: 'module', + plugins: [ + '@babel/plugin-syntax-top-level-await', + ], + }, + }, + rules: { + 'node/no-unsupported-features/es-syntax': 'off', + }, extends: [ + 'plugin:node/recommended', 'plugin:putout/recommended', ], plugins: [ @@ -14,17 +26,11 @@ module.exports = { 'no-console': 'off', 'node/shebang': 'off', }, - extends: [ - 'plugin:node/recommended', - ], }, { files: ['bin/cloudcmd.js'], rules: { 'no-console': 'off', }, - extends: [ - 'plugin:node/recommended', - ], }, { files: ['{client,common,static}/**/*.js'], env: { diff --git a/.putout.json b/.putout.json index 8fdaaf23..d0cd4c06 100644 --- a/.putout.json +++ b/.putout.json @@ -1,10 +1,12 @@ { "ignore": [ "fixture*", - "dist*", "app.json", "fontello.json" ], + "rules": { + "convert-commonjs-to-esm": "on" + }, "match": { "{import.spec.js,console.js}": { "remove-skip": "off" @@ -25,16 +27,6 @@ "client/{client,cloudcmd,load-module}.js": { "remove-console": "off" }, - "client/modules/config/index.js": { - "apply-shorthand-properties": [{ - "ignore": [ - "ONE_MINUTE" - ] - }] - }, - "view.js": { - "apply-shorthand-properties": "off" - }, "test/common/cloudfunc.js": { "remove-console": "off" }, diff --git a/bin/cloudcmd.js b/bin/cloudcmd.js index 201331af..a4f3ae64 100755 --- a/bin/cloudcmd.js +++ b/bin/cloudcmd.js @@ -1,25 +1,26 @@ #!/usr/bin/env node -'use strict'; +import readjson from 'readjson'; -const Info = require('../package'); -const DIR_SERVER = '../server/'; +import {promisify} from 'util'; +import tryToCatch from 'try-to-catch'; -const {promisify} = require('util'); -const tryToCatch = require('try-to-catch'); - -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'; + +import minimist from 'minimist'; + +const Info = await readjson(new URL('../package.json', import.meta.url)); +console.log(Info); + +const {argv} = process; const choose = (a, b) => { if (a === undefined) @@ -28,10 +29,11 @@ const choose = (a, b) => { return a; }; +console.log('---->', env); + process.on('unhandledRejection', exit); -const {argv} = process; -const args = require('minimist')(argv.slice(2), { +const args = minimist(argv.slice(2), { string: [ 'name', 'port', @@ -142,11 +144,11 @@ if (args.version) else if (args.help) help(); else - main(); + await main(); async function main() { if (args.repl) - repl(); + await repl(); port(args.port); @@ -186,7 +188,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 +202,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 importConfig = promisify(distribute.import); + const {default: distributeImport} = await import('../server/distribute/import.js'); + const importConfig = promisify(distributeImport); - start(options, config); + await start(options, config); if (args.save) config.write(); @@ -219,8 +221,9 @@ async function main() { await importConfig(config); } -function validateRoot(root, config) { - const validate = require(DIR_SERVER + 'validate'); +async function validateRoot(root, config) { + const validate = await import('../server/validate.js'); + validate.root(root, config); if (root === '/') @@ -229,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 import('criton'); return criton(password, config('algo')); } @@ -238,13 +241,11 @@ function version() { console.log('v' + Info.version); } -function start(options, config) { - const SERVER = DIR_SERVER + 'server'; - +async function start(options, config) { if (!args.server) return; - const server = require(SERVER); + const server = await import('../server/server.js'); server(options, config); } @@ -257,21 +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 import('../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 import('fs'); + const tryCatch = await import('try-catch'); + const jju = await import('jju'); + const forEachKey = await import('for-each-key'); const readjsonSync = (name) => jju.parse(fs.readFileSync(name, 'utf8'), { mode: 'json', @@ -285,10 +286,10 @@ 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 = await import('../json/help'); + const forEachKey = await import('for-each-key'); + const currify = await import('currify'); const usage = 'Usage: cloudcmd [options]'; const url = Info.homepage; const log = currify((a, b, c) => console.log(a, b, c)); @@ -299,23 +300,23 @@ function help() { 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 import('../server/repl'); } async function checkUpdate() { - const load = require('package-json'); + const load = await import('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 import('chalk'); const latestVersion = chalk.green.bold('v' + version); const latest = `update available: ${latestVersion}`; diff --git a/bin/release.js b/bin/release.js index f3e6bf93..c6db0a7b 100755 --- a/bin/release.js +++ b/bin/release.js @@ -1,14 +1,13 @@ #!/usr/bin/env node -'use strict'; +import readjson from 'readjson'; -const DIR = '../'; -const Info = require(DIR + 'package'); +import minor from 'minor'; +import place from 'place'; +import rendy from 'rendy'; +import shortdate from 'shortdate'; -const minor = require('minor'); -const place = require('place'); -const rendy = require('rendy'); -const shortdate = require('shortdate'); +const Info = await readjson('../package'); const ERROR = Error('ERROR: version is missing. release --patch|--minor|--major'); diff --git a/package.json b/package.json index 94a75a8d..b43e6068 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "type": "git", "url": "git://github.com/coderaiser/cloudcmd.git" }, + "type": "module", "keywords": [ "console", "terminal", @@ -225,7 +226,7 @@ "yaspeller": "^7.0.0" }, "engines": { - "node": ">=12" + "node": ">=14" }, "license": "MIT", "main": "server/cloudcmd.js", diff --git a/static/user-menu.js b/static/user-menu.js index 2b4b2625..6329a82b 100644 --- a/static/user-menu.js +++ b/static/user-menu.js @@ -1,8 +1,6 @@ -'use strict'; - const RENAME_FILE = 'Rename file'; -module.exports = { +export default { '__settings': { select: [ RENAME_FILE, @@ -78,7 +76,7 @@ async function readDefaultMenu({prefix}) { return data; } -module.exports._selectNames = selectNames; +export const _selectNames = selectNames; function selectNames(names, panel, {selectFile, getCurrentByName}) { for (const name of names) { const file = getCurrentByName(name, panel); @@ -86,7 +84,7 @@ function selectNames(names, panel, {selectFile, getCurrentByName}) { } } -module.exports._compare = compare; +export const _compare = compare; function compare(a, b) { const result = []; diff --git a/static/user-menu.spec.js b/static/user-menu.spec.js index a1b196a0..16cd0977 100644 --- a/static/user-menu.spec.js +++ b/static/user-menu.spec.js @@ -1,12 +1,11 @@ -'use strict'; +import autoGlobals from 'auto-globals'; +import supertape from 'supertape'; +import stub from '@cloudcmd/stub'; +import tryToCatch from 'try-to-catch'; +import wraptile from 'wraptile'; -const autoGlobals = require('auto-globals'); -const test = autoGlobals(require('supertape')); -const stub = require('@cloudcmd/stub'); -const tryToCatch = require('try-to-catch'); -const wraptile = require('wraptile'); - -const defaultMenu = require('./user-menu'); +import defaultMenu from './user-menu.js'; +console.log(defaultMenu); const {create} = autoGlobals; @@ -15,6 +14,8 @@ const reject = wraptile(async (a) => { throw Error(a); }); +const test = autoGlobals(supertape); + test('cloudcmd: static: user menu: Rename', async (t) => { const name = 'F2 - Rename file'; const DOM = getDOM(); @@ -124,8 +125,8 @@ test('cloudcmd: static: user menu: compare directories', async (t) => { t.end(); }); -test('cloudcmd: static: user menu: compare directories: select names', (t) => { - const {_selectNames} = defaultMenu; +test('cloudcmd: static: user menu: compare directories: select names', async (t) => { + const {_selectNames} = await import('./user-menu.js'); const selectFile = stub(); const file = {}; const getCurrentByName = stub().returns(file); @@ -142,8 +143,8 @@ test('cloudcmd: static: user menu: compare directories: select names', (t) => { t.end(); }); -test('cloudcmd: static: user menu: compare directories: select names: getCurrentByName', (t) => { - const {_selectNames} = defaultMenu; +test('cloudcmd: static: user menu: compare directories: select names: getCurrentByName', async (t) => { + const {_selectNames} = await import('./user-menu.js'); const selectFile = stub(); const getCurrentByName = stub(); @@ -160,8 +161,8 @@ test('cloudcmd: static: user menu: compare directories: select names: getCurrent t.end(); }); -test('cloudcmd: static: user menu: compare directories: select names: compare', (t) => { - const {_compare} = defaultMenu; +test('cloudcmd: static: user menu: compare directories: select names: compare', async (t) => { + const {_compare} = await import('./user-menu.js'); const a = [1, 2]; const b = [1, 3];