From 629967b7d11505ad9fe228af8344811bfa9d143c Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 14 Nov 2016 14:28:13 +0200 Subject: [PATCH] feature(cloudcmd) es2015-ify --- .babelrc | 6 ++ .gitignore | 2 + .npmignore | 3 + bin/cloudcmd.js | 195 +++++++++++++++++++++++------------------------- package.json | 11 ++- 5 files changed, 114 insertions(+), 103 deletions(-) create mode 100644 .babelrc diff --git a/.babelrc b/.babelrc new file mode 100644 index 00000000..f94f27f5 --- /dev/null +++ b/.babelrc @@ -0,0 +1,6 @@ +{ + "presets": [ + "es2015" + ] +} + diff --git a/.gitignore b/.gitignore index 6804981a..49fbaab9 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ modules/fancybox/sprite.psd *.swp +bin-legacy + diff --git a/.npmignore b/.npmignore index 99c4331d..a3253557 100644 --- a/.npmignore +++ b/.npmignore @@ -10,5 +10,8 @@ app.json circle.yml bin/release.js + legacy +bin-legacy/release.js + diff --git a/bin/cloudcmd.js b/bin/cloudcmd.js index 6d1259ce..237d882b 100755 --- a/bin/cloudcmd.js +++ b/bin/cloudcmd.js @@ -2,73 +2,69 @@ 'use strict'; -var Info = require('../package'), - DIR = __dirname + '/../', - DIR_LIB = DIR + 'lib/', - DIR_SERVER = DIR_LIB + 'server/', +const Info = require('../package'); +const DIR = __dirname + '/../'; +const DIR_LIB = DIR + 'lib/'; +const DIR_SERVER = DIR_LIB + 'server/'; - exit = require(DIR_SERVER + 'exit'), - config = require(DIR_SERVER + 'config'), - - options, - argv = process.argv, - - args = require('minimist')(argv.slice(2), { - string: [ - 'port', - 'password', - 'username', - 'config', - 'editor', - 'root', - 'prefix' - ], - boolean: [ - 'auth', - 'repl', - 'save', - 'server', - 'online', - 'open', - 'minify', - 'progress', - 'html-dialogs', - 'console', - 'config-dialog', - 'one-panel-mode' - ], - default: { - server : true, - auth : config('auth'), - port : config('port'), - minify : config('minify'), - online : config('online'), - open : config('open'), - editor : config('editor') || 'edward', - username : config('username'), - root : config('root') || '/', - prefix : config('prefix') || '', - progress : config('progress'), - console : defaultTrue(config('console')), - - 'html-dialogs': config('htmlDialogs'), - 'config-dialog': defaultTrue(config('configDialog')), - 'one-panel-mode': config('onePanelMode'), - }, - alias: { - v: 'version', - h: 'help', - p: 'password', - o: 'online', - u: 'username', - s: 'save', - a: 'auth', - c: 'config' - }, - unknown: function(cmd) { - exit('\'%s\' is not a cloudcmd option. See \'cloudcmd --help\'.', cmd); - } - }); +const exit = require(DIR_SERVER + 'exit'); +const config = require(DIR_SERVER + 'config'); + +const argv = process.argv; +const args = require('minimist')(argv.slice(2), { + string: [ + 'port', + 'password', + 'username', + 'config', + 'editor', + 'root', + 'prefix' + ], + boolean: [ + 'auth', + 'repl', + 'save', + 'server', + 'online', + 'open', + 'minify', + 'progress', + 'config-dialog', + 'console', + 'one-panel-mode' + ], + default: { + server : true, + auth : config('auth'), + port : config('port'), + minify : config('minify'), + online : config('online'), + open : config('open'), + editor : config('editor') || 'edward', + username : config('username'), + root : config('root') || '/', + prefix : config('prefix') || '', + progress : config('progress'), + console : defaultTrue(config('console')), + + 'config-dialog': defaultTrue(config('configDialog')), + 'one-panel-mode': config('onePanelMode'), + }, + alias: { + v: 'version', + h: 'help', + p: 'password', + o: 'online', + u: 'username', + s: 'save', + a: 'auth', + c: 'config' + }, + unknown: (cmd) => { + exit('\'%s\' is not a cloudcmd option. See \'cloudcmd --help\'.', cmd); + } +}); if (args.version) { version(); @@ -97,7 +93,7 @@ if (args.version) { readConfig(args.config); - options = { + const options = { root: args.root, editor: args.editor, prefix: args.prefix @@ -111,7 +107,7 @@ if (args.version) { if (!args.save) start(options); else - config.save(function() { + config.save(() => { start(options); }); } @@ -124,12 +120,12 @@ function defaultTrue(value) { } function validateRoot(root) { - var validate = require('../lib/server/validate'); + const validate = require('../lib/server/validate'); validate.root(root, console.log); } function getPassword(password) { - var criton = require('criton'); + const criton = require('criton'); return criton(password, config('algo')); } @@ -139,14 +135,14 @@ function version() { } function start(config) { - var SERVER = '../lib/server'; + const SERVER = '../lib/server'; if (args.server) require(SERVER)(config); } function port(arg) { - var number = parseInt(arg, 10); + const number = parseInt(arg, 10); if (!isNaN(number)) config('port', number); @@ -155,34 +151,35 @@ function port(arg) { } function readConfig(name) { - var fs, data, error, tryCatch; + if (!name) + return; - if (name) { - fs = require('fs'); - tryCatch = require('try-catch'); - error = tryCatch(function() { - var json = fs.readFileSync(name); - data = JSON.parse(json); + const tryCatch = require('try-catch'); + const readjson = require('readjson'); + + let data; + + const error = tryCatch(() => { + data = readjson.sync(name); + }); + + if (error) + exit(error.message); + else + Object.keys(data).forEach((item) => { + config(item, data[item]); }); - - if (error) - exit(error.message); - else - Object.keys(data).forEach(function(item) { - config(item, data[item]); - }); - } } function help() { - var bin = require('../json/help'), - usage = 'Usage: cloudcmd [options]', - url = Info.homepage; + const bin = require('../json/help'); + const usage = 'Usage: cloudcmd [options]'; + const url = Info.homepage; console.log(usage); console.log('Options:'); - Object.keys(bin).forEach(function(name) { + Object.keys(bin).forEach((name) => { console.log(' %s %s', name, bin[name]); }); @@ -195,21 +192,19 @@ function repl() { } function checkUpdate() { - var load = require('package-json'), - chalk = require('chalk'), - rendy = require('rendy'); + const load = require('package-json'); + const chalk = require('chalk'); + const rendy = require('rendy'); - load(Info.name, 'latest').then(function(data) { - var latest, - current, - version = data.version; + load(Info.name, 'latest').then((data) => { + const version = data.version; if (version !== Info.version) { - latest = rendy('update available: {{ latest }}', { + const latest = rendy('update available: {{ latest }}', { latest: chalk.green.bold('v' + version), }); - current = chalk.dim(rendy('(current: v{{ current }})', { + const current = chalk.dim(rendy('(current: v{{ current }})', { current: Info.version })); diff --git a/package.json b/package.json index 83749a1e..016f3aa2 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "server" ], "bin": { - "cloudcmd": "bin/cloudcmd.js" + "cloudcmd": "bin-legacy/cloudcmd.js", + "cloudcmd+": "bin/cloudcmd.js" }, "config": { "dirs_legacy": "bin lib", @@ -59,7 +60,7 @@ "test": "tape 'test/**/*.js'", "watch:test": "nodemon -w lib -w test -x \"npm run test\"", "spell": "yaspeller .", - "wisdom": "npm run docker:rm-old; bin/release.js", + "wisdom": "npm run build; npm run docker:rm-old; bin/release.js", "postpublish": "redrun --parallel docker docker:alpine", "docker:pull:node": "docker pull node", "docker:pull:alpine": "docker pull mhart/alpine-node", @@ -77,7 +78,9 @@ "docker:rm:latest-alpine": "docker rmi coderaiser/cloudcmd:latest-alpine", "docker:rm-old": "redrun --parallel docker:rm:*", "coverage": "nyc npm test", - "report": "nyc report --reporter=text-lcov | coveralls" + "report": "nyc report --reporter=text-lcov | coveralls", + "6to5:bin": "babel bin -d bin-legacy", + "build": "redrun 6to5:*" }, "directories": { "man": "man" @@ -125,6 +128,8 @@ "writejson": "^1.1.0" }, "devDependencies": { + "babel-cli": "^6.18.0", + "babel-preset-es2015": "^6.18.0", "coveralls": "^2.11.6", "es6-promisify": "^5.0.0", "eslint": "^3.1.1",