You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by er...@apache.org on 2020/04/02 09:57:01 UTC

[cordova-serve] branch master updated: refactor: transform var to let/const (#32)

This is an automated email from the ASF dual-hosted git repository.

erisu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-serve.git


The following commit(s) were added to refs/heads/master by this push:
     new 0f13dff  refactor: transform var to let/const (#32)
0f13dff is described below

commit 0f13dff7f826f0ef75981e07d95e14e576e5805d
Author: エリス <er...@users.noreply.github.com>
AuthorDate: Thu Apr 2 18:56:51 2020 +0900

    refactor: transform var to let/const (#32)
---
 spec/browser.spec.js |  8 ++++----
 spec/main.spec.js    |  4 ++--
 spec/server.spec.js  |  4 ++--
 src/browser.js       | 56 ++++++++++++++++++++++++++--------------------------
 src/exec.js          |  6 +++---
 src/main.js          | 12 +++++------
 src/platform.js      | 12 +++++------
 src/server.js        | 20 +++++++++----------
 src/util.js          | 22 ++++++++++-----------
 9 files changed, 72 insertions(+), 72 deletions(-)

diff --git a/spec/browser.spec.js b/spec/browser.spec.js
index 44d91f8..124f8ed 100644
--- a/spec/browser.spec.js
+++ b/spec/browser.spec.js
@@ -15,7 +15,7 @@
     under the License.
 */
 
-var rewire = require('rewire');
+const rewire = require('rewire');
 
 function expectPromise (obj) {
     // 3 slightly different ways of verifying a promise
@@ -37,7 +37,7 @@ describe('browser', function () {
     });
 
     it('should return a promise', function () {
-        var result = browser();
+        const result = browser();
         expect(result).toBeDefined();
         expectPromise(result);
 
@@ -45,9 +45,9 @@ describe('browser', function () {
     });
 
     it('should call open() when target is `default`', function () {
-        var mockUrl = 'this is the freakin url';
+        const mockUrl = 'this is the freakin url';
 
-        var result = browser({ target: 'default', url: mockUrl });
+        const result = browser({ target: 'default', url: mockUrl });
         expect(result).toBeDefined();
         expectPromise(result);
 
diff --git a/spec/main.spec.js b/spec/main.spec.js
index 714f144..502fe8a 100644
--- a/spec/main.spec.js
+++ b/spec/main.spec.js
@@ -15,7 +15,7 @@
     under the License.
 */
 
-var main = require('..');
+const main = require('..');
 
 describe('main', function () {
     it('exists and has expected properties', function () {
@@ -25,7 +25,7 @@ describe('main', function () {
     });
 
     it('is creatable', function () {
-        var instance = main();
+        const instance = main();
         expect(instance.servePlatform).toBeDefined();
         expect(typeof instance.servePlatform).toBe('function');
 
diff --git a/spec/server.spec.js b/spec/server.spec.js
index 651423d..1ff8633 100644
--- a/spec/server.spec.js
+++ b/spec/server.spec.js
@@ -15,7 +15,7 @@
     under the License.
 */
 
-var server = require('../src/server');
+const server = require('../src/server');
 
 function expectPromise (obj) {
     // 3 slightly different ways of verifying a promise
@@ -31,7 +31,7 @@ describe('server', function () {
     });
 
     it('should return a promise', function () {
-        var result = server({ port: 8008, noServerInfo: 1 });
+        const result = server({ port: 8008, noServerInfo: 1 });
         expect(result).toBeDefined();
         expectPromise(result);
         return result;
diff --git a/src/browser.js b/src/browser.js
index 2973c89..1499910 100644
--- a/src/browser.js
+++ b/src/browser.js
@@ -19,14 +19,14 @@
 
 /* globals Promise: true */
 
-var child_process = require('child_process');
-var fs = require('fs');
-var open = require('open');
-var which = require('which');
-var exec = require('./exec');
+const child_process = require('child_process');
+const fs = require('fs');
+const open = require('open');
+const which = require('which');
+const exec = require('./exec');
 
-var NOT_INSTALLED = 'The browser target is not installed: %target%';
-var NOT_SUPPORTED = 'The browser target is not supported: %target%';
+const NOT_INSTALLED = 'The browser target is not installed: %target%';
+const NOT_SUPPORTED = 'The browser target is not supported: %target%';
 
 /**
  * Launches the specified browser with the given URL.
@@ -39,8 +39,8 @@ var NOT_SUPPORTED = 'The browser target is not supported: %target%';
  */
 module.exports = function (opts) {
     opts = opts || {};
-    var target = opts.target || 'default';
-    var url = opts.url || '';
+    let target = opts.target || 'default';
+    const url = opts.url || '';
 
     target = target.toLowerCase();
     if (target === 'default') {
@@ -48,8 +48,8 @@ module.exports = function (opts) {
         return Promise.resolve();
     } else {
         return getBrowser(target, opts.dataDir).then(function (browser) {
-            var args;
-            var urlAdded = false;
+            let args;
+            let urlAdded = false;
 
             switch (process.platform) {
             case 'darwin':
@@ -85,8 +85,8 @@ module.exports = function (opts) {
             if (!urlAdded) {
                 args.push(url);
             }
-            var command = args.join(' ');
-            var result = exec(command);
+            const command = args.join(' ');
+            const result = exec(command);
             result.catch(function () {
                 // Assume any error means that the browser is not installed and display that as a more friendly error.
                 throw new Error(NOT_INSTALLED.replace('%target%', target));
@@ -104,8 +104,8 @@ module.exports = function (opts) {
 function getBrowser (target, dataDir) {
     dataDir = dataDir || 'temp_chrome_user_data_dir_for_cordova';
 
-    var chromeArgs = ' --user-data-dir=/tmp/' + dataDir;
-    var browsers = {
+    const chromeArgs = ' --user-data-dir=/tmp/' + dataDir;
+    const browsers = {
         win32: {
             ie: 'iexplore',
             chrome: 'chrome --user-data-dir=%TEMP%\\' + dataDir,
@@ -129,7 +129,7 @@ function getBrowser (target, dataDir) {
     };
 
     if (target in browsers[process.platform]) {
-        var browser = browsers[process.platform][target];
+        const browser = browsers[process.platform][target];
         return checkBrowserExistsWindows(browser, target).then(function () {
             return Promise.resolve(browser);
         });
@@ -141,7 +141,7 @@ function getBrowser (target, dataDir) {
 // err might be null, in which case defaultMsg is used.
 // target MUST be defined or an error is thrown.
 function getErrorMessage (err, target, defaultMsg) {
-    var errMessage;
+    let errMessage;
     if (err) {
         errMessage = err.toString();
     } else {
@@ -151,7 +151,7 @@ function getErrorMessage (err, target, defaultMsg) {
 }
 
 function checkBrowserExistsWindows (browser, target) {
-    var promise = new Promise(function (resolve, reject) {
+    const promise = new Promise(function (resolve, reject) {
         // Windows displays a dialog if the browser is not installed. We'd prefer to avoid that.
         if (process.platform === 'win32') {
             if (target === 'edge') {
@@ -159,7 +159,7 @@ function checkBrowserExistsWindows (browser, target) {
                     resolve();
                 })
                     .catch(function (err) {
-                        var errMessage = getErrorMessage(err, target, NOT_INSTALLED);
+                        const errMessage = getErrorMessage(err, target, NOT_INSTALLED);
                         reject(errMessage);
                     });
             } else {
@@ -167,7 +167,7 @@ function checkBrowserExistsWindows (browser, target) {
                     resolve();
                 })
                     .catch(function (err) {
-                        var errMessage = getErrorMessage(err, target, NOT_INSTALLED);
+                        const errMessage = getErrorMessage(err, target, NOT_INSTALLED);
                         reject(errMessage);
                     });
             }
@@ -179,12 +179,12 @@ function checkBrowserExistsWindows (browser, target) {
 }
 
 function edgeSupported () {
-    var prom = new Promise(function (resolve, reject) {
+    const prom = new Promise(function (resolve, reject) {
         child_process.exec('ver', function (err, stdout, stderr) {
             if (err || stderr) {
                 reject(err || stderr);
             } else {
-                var windowsVersion = stdout.match(/([0-9.])+/g)[0];
+                const windowsVersion = stdout.match(/([0-9.])+/g)[0];
                 if (parseInt(windowsVersion) < 10) {
                     reject(new Error('The browser target is not supported on this version of Windows: %target%'));
                 } else {
@@ -196,27 +196,27 @@ function edgeSupported () {
     return prom;
 }
 
-var regItemPattern = /\s*\([^)]+\)\s+(REG_SZ)\s+([^\s].*)\s*/;
+const regItemPattern = /\s*\([^)]+\)\s+(REG_SZ)\s+([^\s].*)\s*/;
 function browserInstalled (browser) {
     // On Windows, the 'start' command searches the path then 'App Paths' in the registry.
     // We do the same here. Note that the start command uses the PATHEXT environment variable
     // for the list of extensions to use if no extension is provided. We simplify that to just '.EXE'
     // since that is what all the supported browsers use. Check path (simple but usually won't get a hit)
 
-    var promise = new Promise(function (resolve, reject) {
+    const promise = new Promise(function (resolve, reject) {
         if (which.sync(browser, { nothrow: true })) {
             return resolve();
         } else {
-            var regQPre = 'reg QUERY "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\';
-            var regQPost = '.EXE" /v ""';
-            var regQuery = regQPre + browser.split(' ')[0] + regQPost;
+            const regQPre = 'reg QUERY "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\';
+            const regQPost = '.EXE" /v ""';
+            const regQuery = regQPre + browser.split(' ')[0] + regQPost;
 
             child_process.exec(regQuery, function (err, stdout, stderr) {
                 if (err) {
                     // The registry key does not exist, which just means the app is not installed.
                     reject(err);
                 } else {
-                    var result = regItemPattern.exec(stdout);
+                    const result = regItemPattern.exec(stdout);
                     if (fs.existsSync(trimRegPath(result[2]))) {
                         resolve();
                     } else {
diff --git a/src/exec.js b/src/exec.js
index a76a83d..f97a60b 100644
--- a/src/exec.js
+++ b/src/exec.js
@@ -19,7 +19,7 @@
 
 /* globals Promise: true */
 
-var child_process = require('child_process');
+const child_process = require('child_process');
 
 /**
  * Executes the command specified.
@@ -30,8 +30,8 @@ var child_process = require('child_process');
 module.exports = function (cmd, opt_cwd) {
     return new Promise(function (resolve, reject) {
         try {
-            var opt = { cwd: opt_cwd, maxBuffer: 1024000 };
-            var timerID = 0;
+            const opt = { cwd: opt_cwd, maxBuffer: 1024000 };
+            let timerID = 0;
             if (process.platform === 'linux') {
                 timerID = setTimeout(function () {
                     resolve('linux-timeout');
diff --git a/src/main.js b/src/main.js
index 996798d..9210345 100644
--- a/src/main.js
+++ b/src/main.js
@@ -17,9 +17,9 @@
  under the License.
  */
 
-var chalk = require('chalk');
-var compression = require('compression');
-var express = require('express');
+const chalk = require('chalk');
+const compression = require('compression');
+const express = require('express');
 
 module.exports = function () {
     return new CordovaServe();
@@ -31,9 +31,9 @@ function CordovaServe () {
     // Attach this before anything else to provide status output
     this.app.use(function (req, res, next) {
         res.on('finish', function () {
-            var color = this.statusCode === '404' ? chalk.red : chalk.green;
-            var msg = color(this.statusCode) + ' ' + this.req.originalUrl;
-            var encoding = this._headers && this._headers['content-encoding'];
+            const color = this.statusCode === '404' ? chalk.red : chalk.green;
+            let msg = color(this.statusCode) + ' ' + this.req.originalUrl;
+            const encoding = this._headers && this._headers['content-encoding'];
             if (encoding) {
                 msg += chalk.gray(' (' + encoding + ')');
             }
diff --git a/src/platform.js b/src/platform.js
index 7d14170..9cb4823 100644
--- a/src/platform.js
+++ b/src/platform.js
@@ -19,8 +19,8 @@
 
 /* globals Promise: true */
 
-var fs = require('fs');
-var util = require('./util');
+const fs = require('fs');
+const util = require('./util');
 
 /**
  * Launches a server where the root points to the specified platform in a Cordova project.
@@ -33,13 +33,13 @@ var util = require('./util');
 module.exports = function (platform, opts) {
     // note: `this` is actually an instance of main.js CordovaServe
     // this module is a mixin
-    var that = this;
-    var retPromise = new Promise(function (resolve, reject) {
+    const that = this;
+    const retPromise = new Promise(function (resolve, reject) {
         if (!platform) {
             reject(new Error('Error: A platform must be specified'));
         } else {
             opts = opts || {};
-            var projectRoot = findProjectRoot(opts.root);
+            const projectRoot = findProjectRoot(opts.root);
             that.projectRoot = projectRoot;
             opts.root = util.getPlatformWwwRoot(projectRoot, platform);
 
@@ -54,7 +54,7 @@ module.exports = function (platform, opts) {
 };
 
 function findProjectRoot (path) {
-    var projectRoot = util.cordovaProjectRoot(path);
+    const projectRoot = util.cordovaProjectRoot(path);
     if (!projectRoot) {
         if (!path) {
             throw new Error('Current directory does not appear to be in a Cordova project.');
diff --git a/src/server.js b/src/server.js
index 58a8868..5a360f2 100644
--- a/src/server.js
+++ b/src/server.js
@@ -19,8 +19,8 @@
 
 /* globals Promise: true */
 
-var chalk = require('chalk');
-var express = require('express');
+const chalk = require('chalk');
+const express = require('express');
 
 /**
  * @desc Launches a server with the specified options and optional custom handlers.
@@ -28,12 +28,12 @@ var express = require('express');
  * @returns {*|promise}
  */
 module.exports = function (opts) {
-    var that = this;
-    var promise = new Promise(function (resolve, reject) {
+    const that = this;
+    const promise = new Promise(function (resolve, reject) {
         opts = opts || {};
-        var port = opts.port || 8000;
+        let port = opts.port || 8000;
 
-        var log = module.exports.log = function (msg) {
+        const log = module.exports.log = function (msg) {
             if (!opts.noLogOutput) {
                 if (opts.events) {
                     opts.events.emit('log', msg);
@@ -43,8 +43,8 @@ module.exports = function (opts) {
             }
         };
 
-        var app = that.app;
-        var server = require('http').Server(app);
+        const app = that.app;
+        const server = require('http').Server(app);
         that.server = server;
 
         if (opts.router) {
@@ -63,10 +63,10 @@ module.exports = function (opts) {
             app.use(express.static(that.projectRoot));
         }
 
-        var listener = server.listen(port);
+        const listener = server.listen(port);
         listener.on('listening', function () {
             that.port = port;
-            var message = 'Static file server running on: ' + chalk.green('http://localhost:' + port) + ' (CTRL + C to shut down)';
+            const message = 'Static file server running on: ' + chalk.green('http://localhost:' + port) + ' (CTRL + C to shut down)';
             if (!opts.noServerInfo) {
                 log(message);
             }
diff --git a/src/util.js b/src/util.js
index fdb92fa..32c5d0a 100644
--- a/src/util.js
+++ b/src/util.js
@@ -17,14 +17,14 @@
  under the License.
  */
 
-var fs = require('fs');
-var path = require('path');
+const fs = require('fs');
+const path = require('path');
 
 // Some helpful utility stuff copied from cordova-lib. This is a bit nicer than taking a dependency on cordova-lib just
 // to get this minimal stuff. Hopefully we won't need the platform stuff (finding platform www_dir) once it is moved
 // into the actual platform.
 
-var platforms = {
+const platforms = {
     amazon_fireos: { www_dir: 'assets/www' },
     android: { www_dir: 'assets/www' },
     blackberry10: { www_dir: 'www' },
@@ -44,24 +44,24 @@ var platforms = {
 function cordovaProjectRoot (dir) {
     if (!dir) {
         // Prefer PWD over cwd so that symlinked dirs within your PWD work correctly.
-        var pwd = process.env.PWD;
-        var cwd = process.cwd();
+        const pwd = process.env.PWD;
+        const cwd = process.cwd();
         if (pwd && pwd !== cwd && pwd !== 'undefined') {
             return cordovaProjectRoot(pwd) || cordovaProjectRoot(cwd);
         }
         return cordovaProjectRoot(cwd);
     }
 
-    var bestReturnValueSoFar = null;
-    for (var i = 0; i < 1000; ++i) {
-        var result = isRootDir(dir);
+    let bestReturnValueSoFar = null;
+    for (let i = 0; i < 1000; ++i) {
+        const result = isRootDir(dir);
         if (result === 2) {
             return dir;
         }
         if (result === 1) {
             bestReturnValueSoFar = dir;
         }
-        var parentDir = path.normalize(path.join(dir, '..'));
+        const parentDir = path.normalize(path.join(dir, '..'));
         // Detect fs root.
         if (parentDir === dir) {
             return bestReturnValueSoFar;
@@ -72,13 +72,13 @@ function cordovaProjectRoot (dir) {
 }
 
 function getPlatformWwwRoot (cordovaProjectRoot, platformName) {
-    var platform = platforms[platformName];
+    const platform = platforms[platformName];
     if (!platform) {
         throw new Error('Unrecognized platform: ' + platformName);
     }
 
     try {
-        var Api = require(path.join(cordovaProjectRoot, 'platforms', platformName, 'cordova/Api'));
+        const Api = require(path.join(cordovaProjectRoot, 'platforms', platformName, 'cordova/Api'));
         return new Api().locations.www;
     } catch (e) {
         // Fallback on hardcoded paths if platform api not found


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org