把 pub.js (部署到 github pages)代码合并到 deployer 库里,删除原来的 scripts/*
This commit is contained in:
@@ -1,45 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const rRepoURL = /^(?:(?:git|https?|git\+https|git\+ssh):\/\/)?(?:[^@]+@)?([^\/]+?)[\/:](.+?)\.git$/; // eslint-disable-line no-useless-escape
|
||||
const rGithubPage = /\.github\.(io|com)$/;
|
||||
|
||||
function parseRepo(repo) {
|
||||
const split = repo.split(',');
|
||||
const url = split.shift();
|
||||
let branch = split[0];
|
||||
|
||||
if (!branch && rRepoURL.test(url)) {
|
||||
const match = url.match(rRepoURL);
|
||||
const host = match[1];
|
||||
const path = match[2];
|
||||
|
||||
if (host === 'github.com') {
|
||||
branch = rGithubPage.test(path) ? 'master' : 'gh-pages';
|
||||
} else if (host === 'coding.net') {
|
||||
branch = 'coding-pages';
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
url: url,
|
||||
branch: branch || 'master'
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = function(args) {
|
||||
const repo = args.repo || args.repository;
|
||||
if (!repo) throw new TypeError('repo is required!');
|
||||
|
||||
if (typeof repo === 'string') {
|
||||
const data = parseRepo(repo);
|
||||
data.branch = args.branch || data.branch;
|
||||
|
||||
return [data];
|
||||
}
|
||||
|
||||
const result = Object.keys(repo).map(key => {
|
||||
return parseRepo(repo[key]);
|
||||
});
|
||||
|
||||
return result;
|
||||
};
|
||||
161
scripts/pub.js
161
scripts/pub.js
@@ -1,161 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const pathFn = require('path');
|
||||
const fs = require('hexo-fs');
|
||||
const chalk = require('chalk');
|
||||
const swig = require('swig-templates');
|
||||
const moment = require('moment');
|
||||
const Promise = require('bluebird');
|
||||
const spawn = require('hexo-util/lib/spawn');
|
||||
const parseConfig = require('./config');
|
||||
|
||||
const swigHelpers = {
|
||||
now: function(format) {
|
||||
return moment().format(format);
|
||||
}
|
||||
};
|
||||
|
||||
const args = {
|
||||
repo: 'git@github.com:vichome/webapp',
|
||||
name: 'Limo Saplf',
|
||||
email: 'limosaplf@gmail.com',
|
||||
};
|
||||
|
||||
function exec() {
|
||||
const baseDir = '';
|
||||
const deployDir = pathFn.join(baseDir, '.deploy_git');
|
||||
const publicDir = 'dist';
|
||||
let extendDirs = args.extend_dirs;
|
||||
const ignoreHidden = args.ignore_hidden;
|
||||
const ignorePattern = args.ignore_pattern;
|
||||
const message = commitMessage(args);
|
||||
const verbose = !args.silent;
|
||||
|
||||
if (!args.repo && process.env.HEXO_DEPLOYER_REPO) {
|
||||
args.repo = process.env.HEXO_DEPLOYER_REPO;
|
||||
}
|
||||
|
||||
if (!args.repo && !args.repository) {
|
||||
let help = '';
|
||||
|
||||
help += 'You have to configure the deployment settings in _config.yml first!\n\n';
|
||||
help += 'Example:\n';
|
||||
help += ' deploy:\n';
|
||||
help += ' type: git\n';
|
||||
help += ' repo: <repository url>\n';
|
||||
help += ' branch: [branch]\n';
|
||||
help += ' message: [message]\n\n';
|
||||
help += ' extend_dirs: [extend directory]\n\n';
|
||||
help += 'For more help, you can check the docs: ' + chalk.underline('http://hexo.io/docs/deployment.html');
|
||||
|
||||
console.log(help);
|
||||
return;
|
||||
}
|
||||
|
||||
function git(...args) {
|
||||
return spawn('git', args, {
|
||||
cwd: deployDir,
|
||||
verbose: verbose,
|
||||
stdio: 'inherit'
|
||||
});
|
||||
}
|
||||
|
||||
function setup() {
|
||||
const userName = args.name || args.user || args.userName || '';
|
||||
const userEmail = args.email || args.userEmail || '';
|
||||
|
||||
// Create a placeholder for the first commit
|
||||
return fs.writeFile(pathFn.join(deployDir, 'placeholder'), '').then(() => {
|
||||
return git('init');
|
||||
}).then(() => {
|
||||
return userName && git('config', 'user.name', userName);
|
||||
}).then(() => {
|
||||
return userEmail && git('config', 'user.email', userEmail);
|
||||
}).then(() => {
|
||||
return git('add', '-A');
|
||||
}).then(() => {
|
||||
return git('commit', '-m', 'First commit');
|
||||
});
|
||||
}
|
||||
|
||||
function push(repo) {
|
||||
return git('add', '-A').then(() => {
|
||||
return git('commit', '-m', message).catch(() => {
|
||||
// Do nothing. It's OK if nothing to commit.
|
||||
});
|
||||
}).then(() => {
|
||||
return git('push', '-u', repo.url, 'HEAD:' + repo.branch, '--force');
|
||||
});
|
||||
}
|
||||
|
||||
return fs.exists(deployDir).then(function(exist) {
|
||||
if (exist) return;
|
||||
|
||||
// log.info('Setting up Git deployment...');
|
||||
return setup();
|
||||
}).then(() => {
|
||||
// log.info('Clearing .deploy_git folder...');
|
||||
return fs.emptyDir(deployDir);
|
||||
}).then(() => {
|
||||
const opts = {};
|
||||
// log.info('Copying files from public folder...');
|
||||
if (typeof ignoreHidden === 'object') {
|
||||
opts.ignoreHidden = ignoreHidden.public;
|
||||
} else {
|
||||
opts.ignoreHidden = ignoreHidden;
|
||||
}
|
||||
|
||||
if (typeof ignorePattern === 'string') {
|
||||
opts.ignorePattern = new RegExp(ignorePattern);
|
||||
} else if (typeof ignorePattern === 'object' && Reflect.apply(Object.prototype.hasOwnProperty, ignorePattern, ['public'])) {
|
||||
opts.ignorePattern = new RegExp(ignorePattern.public);
|
||||
}
|
||||
|
||||
return fs.copyDir(publicDir, deployDir, opts);
|
||||
}).then(() => {
|
||||
// log.info('Copying files from extend dirs...');
|
||||
|
||||
if (!extendDirs) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof extendDirs === 'string') {
|
||||
extendDirs = [extendDirs];
|
||||
}
|
||||
|
||||
const mapFn = function(dir) {
|
||||
const opts = {};
|
||||
const extendPath = pathFn.join(baseDir, dir);
|
||||
const extendDist = pathFn.join(deployDir, dir);
|
||||
|
||||
if (typeof ignoreHidden === 'object') {
|
||||
opts.ignoreHidden = ignoreHidden[dir];
|
||||
} else {
|
||||
opts.ignoreHidden = ignoreHidden;
|
||||
}
|
||||
|
||||
if (typeof ignorePattern === 'string') {
|
||||
opts.ignorePattern = new RegExp(ignorePattern);
|
||||
} else if (typeof ignorePattern === 'object' && Reflect.apply(Object.prototype.hasOwnProperty, ignorePattern, [dir])) {
|
||||
opts.ignorePattern = new RegExp(ignorePattern[dir]);
|
||||
}
|
||||
|
||||
return fs.copyDir(extendPath, extendDist, opts);
|
||||
};
|
||||
|
||||
return Promise.map(extendDirs, mapFn, {
|
||||
concurrency: 2
|
||||
});
|
||||
}).then(() => {
|
||||
return parseConfig(args);
|
||||
}).each(function(repo) {
|
||||
return push(repo);
|
||||
});
|
||||
};
|
||||
|
||||
function commitMessage(args) {
|
||||
const message = args.m || args.msg || args.message || 'Site updated: {{ now(\'YYYY-MM-DD HH:mm:ss\') }}';
|
||||
return swig.compile(message)(swigHelpers);
|
||||
}
|
||||
|
||||
exec();
|
||||
Reference in New Issue
Block a user