From 0e7627de1590664bdc3a0a53b66839a0912ee18e Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Sun, 21 Jul 2019 17:53:35 +0930 Subject: [PATCH] refactor: es6-fy --- lib/deployer.js | 64 ++++++++++++++++++++++++------------------------- test/index.js | 56 +++++++++++++++++++++---------------------- 2 files changed, 60 insertions(+), 60 deletions(-) diff --git a/lib/deployer.js b/lib/deployer.js index fb58038..ad8b28b 100644 --- a/lib/deployer.js +++ b/lib/deployer.js @@ -1,32 +1,32 @@ 'use strict'; -var pathFn = require('path'); -var fs = require('hexo-fs'); -var chalk = require('chalk'); -var swig = require('swig'); -var moment = require('moment'); -var util = require('hexo-util'); -var spawn = util.spawn; +const pathFn = require('path'); +const fs = require('hexo-fs'); +const chalk = require('chalk'); +const swig = require('swig'); +const moment = require('moment'); +const util = require('hexo-util'); +const spawn = util.spawn; -var assetDir = pathFn.join(__dirname, '../assets'); +const assetDir = pathFn.join(__dirname, '../assets'); -var swigHelpers = { +const swigHelpers = { now: function(format) { return moment().format(format); } }; module.exports = function(args) { - var baseDir = this.base_dir; - var publicDir = this.public_dir; - var deployDir = pathFn.join(baseDir, '.deploy_heroku'); - var deployPubDir = pathFn.join(deployDir, 'public'); - var log = this.log; - var message = commitMessage(args); - var verbose = !args.silent; + const baseDir = this.base_dir; + const publicDir = this.public_dir; + const deployDir = pathFn.join(baseDir, '.deploy_heroku'); + const deployPubDir = pathFn.join(deployDir, 'public'); + const log = this.log; + const message = commitMessage(args); + const verbose = !args.silent; if (!args.repo && !args.repository) { - var help = ''; + let help = ''; help += 'You have to configure the deployment settings in _config.yml first!\n\n'; help += 'Example:\n'; @@ -41,10 +41,10 @@ module.exports = function(args) { } function git() { - var len = arguments.length; - var args = new Array(len); + const len = arguments.length; + const args = new Array(len); - for (var i = 0; i < len; i++) { + for (let i = 0; i < len; i++) { args[i] = arguments[i]; } @@ -56,46 +56,46 @@ module.exports = function(args) { function setup() { // Copy assets - return fs.copyDir(assetDir, deployDir).then(function() { + return fs.copyDir(assetDir, deployDir).then(() => { return git('init'); - }).then(function() { + }).then(() => { return git('add', '-A'); - }).then(function() { + }).then(() => { return git('commit', '-m', 'First commit'); }); } function push(repo) { - return git('add', '-A').then(function() { - return git('commit', '-m', message).catch(function() { + return git('add', '-A').then(() => { + return git('commit', '-m', message).catch(() => { // Do nothing. It's OK if nothing to commit. }); - }).then(function() { + }).then(() => { return git('push', '-u', repo, 'master', '--force'); }); } - return fs.exists(deployDir).then(function(exist) { + return fs.exists(deployDir).then(exist => { if (exist) return; log.info('Setting up Heroku deployment...'); return setup(); - }).then(function() { + }).then(() => { return fs.exists(deployPubDir); - }).then(function(exist) { + }).then(exist => { if (!exist) return; log.info('Clearing .deploy folder...'); return fs.emptyDir(deployPubDir); - }).then(function() { + }).then(() => { log.info('Copying files from public folder...'); return fs.copyDir(publicDir, deployPubDir); - }).then(function() { + }).then(() => { return push(args.repo || args.repository); }); }; function commitMessage(args) { - var message = args.m || args.msg || args.message || 'Site updated: {{ now(\'YYYY-MM-DD HH:mm:ss\') }}'; + const message = args.m || args.msg || args.message || 'Site updated: {{ now(\'YYYY-MM-DD HH:mm:ss\') }}'; return swig.compile(message)(swigHelpers); } diff --git a/test/index.js b/test/index.js index 724b39b..b75a8fc 100644 --- a/test/index.js +++ b/test/index.js @@ -1,20 +1,20 @@ 'use strict'; -var should = require('chai').should(); // eslint-disable-line -var pathFn = require('path'); -var spawn = require('hexo-util/lib/spawn'); -var fs = require('hexo-fs'); -var Promise = require('bluebird'); +const should = require('chai').should(); // eslint-disable-line +const pathFn = require('path'); +const spawn = require('hexo-util/lib/spawn'); +const fs = require('hexo-fs'); +const Promise = require('bluebird'); -var assetDir = pathFn.join(__dirname, '../assets'); +const assetDir = pathFn.join(__dirname, '../assets'); -describe('Heroku deployer', function() { - var baseDir = pathFn.join(__dirname, 'deployer_test'); - var publicDir = pathFn.join(baseDir, 'public'); - var fakeRemote = pathFn.join(baseDir, 'remote'); - var validateDir = pathFn.join(baseDir, 'validate'); +describe('Heroku deployer', () => { + const baseDir = pathFn.join(__dirname, 'deployer_test'); + const publicDir = pathFn.join(baseDir, 'public'); + const fakeRemote = pathFn.join(baseDir, 'remote'); + const validateDir = pathFn.join(baseDir, 'validate'); - var ctx = { + const ctx = { base_dir: baseDir, public_dir: publicDir, log: { @@ -22,34 +22,34 @@ describe('Heroku deployer', function() { } }; - var deployer = require('../lib/deployer').bind(ctx); + const deployer = require('../lib/deployer').bind(ctx); function compareFile(a, b) { return Promise.all([ fs.readFile(a), fs.readFile(b) - ]).then(function(result) { + ]).then(result => { result[0].should.eql(result[1]); }); } - before(function() { + before(() => { return fs.writeFile(pathFn.join(publicDir, 'foo.txt'), 'foo'); }); - beforeEach(function() { + beforeEach(() => { // Create a bare repo as a fake remote repo - return fs.mkdirs(fakeRemote).then(function() { + return fs.mkdirs(fakeRemote).then(() => { return spawn('git', ['init', '--bare', fakeRemote]); }); }); - after(function() { + after(() => { return fs.rmdir(baseDir); }); - afterEach(function() { - return fs.rmdir(fakeRemote).then(function() { + afterEach(() => { + return fs.rmdir(fakeRemote).then(() => { return fs.rmdir(validateDir); }); }); @@ -60,10 +60,10 @@ describe('Heroku deployer', function() { } function validate() { - return clone().then(function() { + return clone().then(() => { // Check files return fs.readFile(pathFn.join(validateDir, 'public', 'foo.txt')); - }).then(function(result) { + }).then(result => { result.should.eql('foo'); // Check Procfile @@ -71,27 +71,27 @@ describe('Heroku deployer', function() { }); } - it('default', function() { + it('default', () => { return deployer({ repo: fakeRemote, silent: true - }).then(function() { + }).then(() => { return validate(); }); }); it('server test'); - it.skip('custom message', function() { + it.skip('custom message', () => { return deployer({ repo: fakeRemote, message: 'custom message', silent: true - }).then(function() { + }).then(() => { return validate(); - }).then(function() { + }).then(() => { return spawn('git', ['log', '-1', '--pretty=format:%s'], {cwd: validateDir}); - }).then(function(content) { + }).then(content => { content.should.eql('custom message'); }); });