'use strict';
var fs = require('fs');
var path = require('path');
var envVar = require('../environment-variables');
/**
* @class PublishGithubPages
* @module AutodocsPublish
*/
/**
* Initialisation step for Github Pages
*
* Used to check/ set any Github Pages environment variables
*
* @method init
* @for PublishGithubPages
*/
function environmentVariablesGithub() {
/**
* @property GH_TOKEN
* @type String (Environment Variable)
* @default None - throws when not set
*/
envVar.require('GH_TOKEN');
/**
* @property REPO_SLUG
* @type String (Environment Variable)
* @default None - throws when not set
*/
envVar.require('REPO_SLUG');
if (!envVar.exists('GH_USER') ||
!envVar.exists('GH_REPO')) {
var tokens = process.env.REPO_SLUG.split('/');
/**
* @property GH_USER
* @type String (Environment Variable)
* @default First half of `REPO_SLUG`
*/
envVar.default('GH_USER', tokens[0]);
/**
* @property GH_REPO
* @type String (Environment Variable)
* @default Second half of `REPO_SLUG`
*/
envVar.default('GH_REPO', tokens[1]);
process.env.SCRIPT_DIR = __dirname;
}
}
/**
* Performs the required steps for publishing to Github pages
*
* - runs `npm run generatedocs`
* - `git fetch`es the `gh-pages` branch of the project into a throwaway git repository
* - copies the documentation generated by the `generatedocs` script into the `gh-pages` branch
* - copies any additional files required into the gh-pages branch
* - if any files have changed, `git commit`s `git push`es on the `gh-pages` branch
* - this is when the files actually succeed in publishing
* - if necessary, cleans up the throwaway git repository
*
* @method run
* @for PublishGithubPages
*/
function publishGithubPages() {
var childProcess = require('child_process');
var script = childProcess.spawn(path.join(__dirname, 'github-pages.sh'), [], {
stdio: 'inherit',
});
script.on('close', function(code) {
outputUrls();
process.exit(code);
});
}
/**
* Outputs the base URL and the API URL at which the documentation should be published.
*
* Uses `CNAME` file based on best guess,
* otherwise defaults to `*.github/*`
*
* @method outputUrls
* @for PublishGithubPages
* @private
*/
function outputUrls() {
var CNAME;
if (process.env.FLAG_COPY_ASSETS === 'true' &&
process.env.DOCUMENT_ASSETS.split(' ').indexOf('CNAME') >= 0) {
// Best guess that CNAME file was copied in
try {
var CNAMEfile = path.join(process.env.PROJECT_DIR, 'CNAME');
CNAME = fs.readFileSync(CNAMEfile).toString().trim();
}
catch (e) {
// Do nothing
}
}
CNAME = CNAME || (process.env.GH_USER+'.github.io');
var publishDomain = 'http://'+CNAME;
var publishUrl = publishDomain+'/'+process.env.GH_REPO;
var publishApiUrl = publishUrl+'/'+process.env.DOCUMENT_PUBLISH_FOLDER;
console.log('Base URL: '+publishUrl);
console.log('API URL: '+publishApiUrl);
}
module.exports = {
init: environmentVariablesGithub,
run: publishGithubPages,
};