From e919066af76933f389ecae6678dee93b19d03d48 Mon Sep 17 00:00:00 2001 From: Hesham Meneisi Date: Sun, 26 Apr 2020 00:19:54 +0700 Subject: [PATCH] feat(config): add pullTagsBranch customization (#83) Relates: #83 #48 --- src/main/ts/config.ts | 15 ++++++++++----- src/main/ts/defaults.ts | 1 + src/main/ts/ghpages.ts | 7 ++++++- src/main/ts/interface.ts | 3 ++- src/main/ts/util.ts | 2 ++ src/test/ts/config.ts | 16 +++++++++++----- src/test/ts/index.ts | 9 ++++++--- 7 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/main/ts/config.ts b/src/main/ts/config.ts index 3d3235f..b3b9674 100644 --- a/src/main/ts/config.ts +++ b/src/main/ts/config.ts @@ -5,14 +5,15 @@ import { castArray, omit } from 'lodash' import readPkg from 'read-pkg' import request from 'sync-request' import { IGhpagesPluginConfig, TAnyMap, TContext } from './interface' -import { catchToSmth } from './util' +import { anyDefined, catchToSmth } from './util' import { DEFAULT_BRANCH, DEFAULT_DST, DEFAULT_MSG, DEFAULT_SRC, PLUGIN_PATH, - DEFAULT_ENTERPRISE + DEFAULT_ENTERPRISE, + DEFAULT_PULL_TAGS_BRANCH } from './defaults' export { @@ -21,7 +22,8 @@ export { DEFAULT_MSG, DEFAULT_DST, DEFAULT_ENTERPRISE, - PLUGIN_PATH + PLUGIN_PATH, + DEFAULT_PULL_TAGS_BRANCH } const gitUrlParse = catchToSmth(gitParse, {}) @@ -138,6 +140,7 @@ export const resolveConfig = (pluginConfig: TAnyMap, context: TContext, path = P const repo = getRepo(pluginConfig, context, enterprise) const repoUrl = getRepoUrl(pluginConfig, context) const token = getToken(env, repoUrl) + const pullTagsBranch = anyDefined(opts.pullTagsBranch, opts._branch, DEFAULT_PULL_TAGS_BRANCH) if (process.env.DEBUG) { logger.log('resolveConfig args:') @@ -145,6 +148,7 @@ export const resolveConfig = (pluginConfig: TAnyMap, context: TContext, path = P logger.log('pluginConfig=', JSON.stringify(pluginConfig, null, 2)) logger.log('path=', path) logger.log('step=', step) + logger.log('pullTagsBranch=', pullTagsBranch) } return { @@ -154,7 +158,8 @@ export const resolveConfig = (pluginConfig: TAnyMap, context: TContext, path = P branch: opts.branch || DEFAULT_BRANCH, enterprise, token, - repo + repo, + pullTagsBranch } } @@ -176,5 +181,5 @@ export const resolveOptions = (pluginConfig: TAnyMap, context: TContext, path = }) .find(config => config?.path === path) || {} - return { ...base, ...extra } + return { ...base, ...extra, _branch: pluginConfig.branch } } diff --git a/src/main/ts/defaults.ts b/src/main/ts/defaults.ts index 9907e13..143abf3 100644 --- a/src/main/ts/defaults.ts +++ b/src/main/ts/defaults.ts @@ -6,3 +6,4 @@ export const DEFAULT_SRC: string = 'docs' export const DEFAULT_DST: string = '.' export const DEFAULT_MSG: string = 'docs updated {{=it.nextRelease.gitTag}}' export const DEFAULT_ENTERPRISE: boolean = false +export const DEFAULT_PULL_TAGS_BRANCH = 'master' diff --git a/src/main/ts/ghpages.ts b/src/main/ts/ghpages.ts index 2347e01..2bfaa9f 100644 --- a/src/main/ts/ghpages.ts +++ b/src/main/ts/ghpages.ts @@ -13,13 +13,18 @@ export const OK = { status: 'OK' } * @private */ export const pullTags = (opts: IPushOpts): Promise => { + if (opts.pullTagsBranch === '') { + return Promise.resolve() + } + const repo = '' + opts.repo + const pullTagsBranch = '' + opts.pullTagsBranch const execaOpts = { env: opts.env, cwd: opts.cwd } - return execa('git', ['pull', '--tags', '--force', repo], execaOpts) + return execa('git', ['pull', '--tags', '--force', repo, pullTagsBranch], execaOpts) } /** diff --git a/src/main/ts/interface.ts b/src/main/ts/interface.ts index 1f4ec48..886e9fd 100644 --- a/src/main/ts/interface.ts +++ b/src/main/ts/interface.ts @@ -26,7 +26,8 @@ export interface IGhpagesPluginConfig { msg: string, token?: string, repo?: string, - enterprise?: boolean + enterprise?: boolean, + pullTagsBranch?: string } export interface IPushOpts extends IGhpagesPluginConfig { diff --git a/src/main/ts/util.ts b/src/main/ts/util.ts index 0965930..eba22cc 100644 --- a/src/main/ts/util.ts +++ b/src/main/ts/util.ts @@ -8,3 +8,5 @@ export const catchToSmth = (fn: Function, smth?: any) => { } } } + +export const anyDefined = (...args: any[]) => args.find(item => typeof item !== 'undefined') diff --git a/src/test/ts/config.ts b/src/test/ts/config.ts index def5526..cdde847 100644 --- a/src/test/ts/config.ts +++ b/src/test/ts/config.ts @@ -5,6 +5,7 @@ import { DEFAULT_MSG, DEFAULT_SRC, DEFAULT_ENTERPRISE, + DEFAULT_PULL_TAGS_BRANCH, resolveOptions, resolveConfig, extractRepoName, @@ -36,7 +37,8 @@ describe('config', () => { PLUGIN_PATH, DEFAULT_DST, DEFAULT_MSG, - DEFAULT_SRC + DEFAULT_SRC, + DEFAULT_PULL_TAGS_BRANCH ]).forEach(v => expect(v).toEqual(expect.any(String))) }) @@ -101,7 +103,8 @@ describe('config', () => { baz: 'qux', msg: 'doc update', branch: 'master', // NOTE must be omitted, - repositoryUrl: 'https://enterprise.com/org/repo.git' + repositoryUrl: 'https://enterprise.com/org/repo.git', + pullTagsBranch: 'dev' } const extra = { enterprise: true, @@ -129,7 +132,8 @@ describe('config', () => { branch: DEFAULT_BRANCH, msg: 'doc update', token, - repo: `https://${token}@enterprise.com/org/repo.git` + repo: `https://${token}@enterprise.com/org/repo.git`, + pullTagsBranch: 'dev' }) }) @@ -163,7 +167,8 @@ describe('config', () => { msg: DEFAULT_MSG, src: DEFAULT_SRC, token, - repo: `https://${token}@github.com/${repoName}.git` + repo: `https://${token}@github.com/${repoName}.git`, + pullTagsBranch: DEFAULT_PULL_TAGS_BRANCH }) }) @@ -234,7 +239,8 @@ describe('config', () => { msg: DEFAULT_MSG, src: 'dist/web', token: 'secure', - repo: `https://secure@github-enterprise-repo-url.com/foo/bar.git` + repo: `https://secure@github-enterprise-repo-url.com/foo/bar.git`, + pullTagsBranch: DEFAULT_PULL_TAGS_BRANCH }) }) }) diff --git a/src/test/ts/index.ts b/src/test/ts/index.ts index 3fada68..1c1e36a 100644 --- a/src/test/ts/index.ts +++ b/src/test/ts/index.ts @@ -8,6 +8,7 @@ import { DEFAULT_MSG, DEFAULT_BRANCH, DEFAULT_ENTERPRISE, + DEFAULT_PULL_TAGS_BRANCH, PLUGIN_PATH } from '../../main/ts' import { getUrlFromPackage } from '../../main/ts/config' @@ -82,7 +83,8 @@ describe('index', () => { src: DEFAULT_SRC, enterprise: DEFAULT_ENTERPRISE, repo: getRepo(pluginConfig, context), - token + token, + pullTagsBranch: DEFAULT_PULL_TAGS_BRANCH }) expect(result).toBeUndefined() }) @@ -171,7 +173,7 @@ describe('index', () => { it('performs commit to docs branch via gh-pages util', async () => { const { publish: ghpagesPublish } = require('gh-pages') const { publish } = require('../../main/ts') - const { getRepo } = require('../../main/ts/config') + const { getRepo, resolveConfig } = require('../../main/ts/config') const { OK } = require('../../main/ts/ghpages') const pluginConfig = { foo: 'bar', @@ -213,7 +215,8 @@ describe('index', () => { 'pull', '--tags', '--force', - expectedOpts.repo + expectedOpts.repo, + resolveConfig(pluginConfig, context).pullTagsBranch ], execaOpts )