@qiwi/semantic-release-gh-pages-plugin
Version:
gh-pages publishing plugin for semantic-release
143 lines (142 loc) • 5.75 kB
JavaScript
/** @module semantic-release-gh-pages-plugin */
import { __awaiter } from "tslib";
import AggregateError from 'aggregate-error';
import dbg from 'debug';
import gitParse from 'git-url-parse';
import { castArray, omit } from 'lodash';
import readPkg from 'read-pkg';
import request from 'then-request';
import { DEFAULT_BRANCH, DEFAULT_DST, DEFAULT_ENTERPRISE, DEFAULT_MSG, DEFAULT_PATTERN, DEFAULT_PULL_TAGS_BRANCH, DEFAULT_SRC, PLUGIN_PATH } from './defaults';
import { anyDefined, catchToSmth } from './util';
const debug = dbg('semantic-release:gh-pages');
export { DEFAULT_BRANCH, DEFAULT_SRC, DEFAULT_MSG, DEFAULT_DST, DEFAULT_ENTERPRISE, PLUGIN_PATH, DEFAULT_PULL_TAGS_BRANCH, DEFAULT_PATTERN, } from './defaults';
const gitUrlParse = catchToSmth(gitParse, {});
export const GITIO_REPO_PATTERN = /^https:\/\/git\.io\/[\dA-Za-z-]+$/;
/**
* @private
*/
export const extractRepoName = (repoUrl) => {
return gitUrlParse(repoUrl).full_name;
};
/**
* @private
*/
export const extractRepoDomain = (repoUrl) => {
return gitUrlParse(repoUrl).resource;
};
/**
* @private
*/
export const extractRepoToken = (repoUrl) => {
const repo = gitUrlParse(repoUrl);
return repo.token || repo.user;
};
/**
* @private
*/
export const getRepoUrl = (pluginConfig, context, enterprise) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
const { env } = context;
const urlFromEnv = getRepoUrlFromEnv(env);
const urlFromStepOpts = pluginConfig.repositoryUrl;
const urlFromOpts = ((_a = context === null || context === void 0 ? void 0 : context.options) === null || _a === void 0 ? void 0 : _a.repositoryUrl) || '';
const urlFromPackage = getUrlFromPackage();
const reassemble = !!urlFromStepOpts || !urlFromOpts;
let url = urlFromStepOpts || urlFromOpts || urlFromEnv || urlFromPackage;
debug('getRepoUrl:');
debug('urlFromEnv= %s', urlFromEnv);
debug('urlFromStepOpts= %s', urlFromStepOpts);
debug('urlFromOpts= %s', urlFromOpts);
debug('urlFromPackage= %s', urlFromPackage);
if (GITIO_REPO_PATTERN.test(url)) {
const res = yield request('GET', urlFromOpts, { followRedirects: false, timeout: 5000 });
url = res.headers.location;
}
if (reassemble) {
url = reassembleRepoUrl(url, context);
}
if (enterprise && extractRepoDomain(url) === 'github.com') {
throw new AggregateError(['repo refers to `github.com` but enterprise url is expected']);
}
return url;
});
/**
* @private
*/
const getRepoUrlFromEnv = (env) => env.REPO_URL;
/**
* @private
*/
export const getUrlFromPackage = () => {
var _a;
const pkg = readPkg.sync();
return String(((_a = pkg === null || pkg === void 0 ? void 0 : pkg.repository) === null || _a === void 0 ? void 0 : _a.url) || (pkg === null || pkg === void 0 ? void 0 : pkg.repository) || '');
};
/**
* @private
*/
export const getToken = (env, repoUrl) => env.GH_TOKEN || env.GITHUB_TOKEN || extractRepoToken(repoUrl);
/**
* @private
*/
export const reassembleRepoUrl = (redirectedUrl, context) => {
const { env } = context;
const repoName = extractRepoName(redirectedUrl);
const repoDomain = extractRepoDomain(redirectedUrl);
const token = getToken(env, redirectedUrl);
return `https://${token}@${repoDomain}/${repoName}.git`;
};
/**
* @private
*/
export const resolveConfig = (pluginConfig_1, context_1, ...args_1) => __awaiter(void 0, [pluginConfig_1, context_1, ...args_1], void 0, function* (pluginConfig, context, path = PLUGIN_PATH, step) {
var _a, _b;
const opts = resolveOptions(pluginConfig, context, path, step);
const { branches = opts._branches, branch = DEFAULT_BRANCH, msg = DEFAULT_MSG, src = DEFAULT_SRC, dst = DEFAULT_DST, pattern: _pattern = DEFAULT_PATTERN, add, dotfiles } = opts;
const enterprise = Boolean(opts.enterprise || pluginConfig.enterprise || DEFAULT_ENTERPRISE);
const repo = yield getRepoUrl(pluginConfig, context, enterprise);
const ciBranch = (_a = context === null || context === void 0 ? void 0 : context.branch) === null || _a === void 0 ? void 0 : _a.name;
const docsBranch = ((_b = branches === null || branches === void 0 ? void 0 : branches.find(([from]) => from === ciBranch)) === null || _b === void 0 ? void 0 : _b[1]) || branch;
const pullTagsBranch = anyDefined(opts.pullTagsBranch, ciBranch, opts._branch, DEFAULT_PULL_TAGS_BRANCH);
const token = getToken(context.env, repo);
const pattern = _pattern.includes(':') ? _pattern.split(':') : _pattern;
debug('resolveConfig args:');
debug('pluginConfig= %j', pluginConfig);
debug('path= %s', path);
debug('step= %s', step);
debug('ciBranch= %s', ciBranch);
debug('docsBranch= %s', docsBranch);
debug('pullTagsBranch= %s', pullTagsBranch);
debug('pattern = %s', pattern);
return {
src,
dst,
msg,
ciBranch,
pullTagsBranch,
docsBranch,
enterprise,
repo,
token,
add,
dotfiles,
pattern,
};
});
/**
* @private
*/
export const resolveOptions = (pluginConfig, context, path = PLUGIN_PATH, step) => {
const { options } = context;
const base = omit(pluginConfig, 'branch', 'branches');
const extra = step && options[step] && castArray(options[step])
.map(config => {
if (Array.isArray(config)) {
const [path, opts] = config;
return Object.assign(Object.assign({}, opts), { path });
}
return config;
})
.find(config => (config === null || config === void 0 ? void 0 : config.path) === path) || {};
return Object.assign(Object.assign({ _branch: pluginConfig.branch, _branches: pluginConfig.branches }, base), extra);
};