Skip to content

Commit

Permalink
refactor: es6-fy
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Jul 21, 2019
1 parent 3d2f5b4 commit 0e7627d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 60 deletions.
64 changes: 32 additions & 32 deletions lib/deployer.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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];
}

Expand All @@ -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);
}
56 changes: 28 additions & 28 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
'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: {
info: 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);
});
});
Expand All @@ -60,38 +60,38 @@ 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
return compareFile(pathFn.join(assetDir, 'Procfile'), pathFn.join(validateDir, 'Procfile'));
});
}

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');
});
});
Expand Down

0 comments on commit 0e7627d

Please sign in to comment.