Skip to content

Commit

Permalink
fix: sync remote tags before git push
Browse files Browse the repository at this point in the history
closes #48
  • Loading branch information
antongolub committed Aug 1, 2019
1 parent ab31cc3 commit 8e1ecac
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"dependencies": {
"aggregate-error": "^3.0.0",
"dot": "^1.1.2",
"execa": "^2.0.3",
"gh-pages": "^2.0.1",
"lodash": "^4.17.15",
"read-pkg": "^5.2.0",
Expand Down
35 changes: 31 additions & 4 deletions src/main/ts/ghpages.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/** @module semantic-release-gh-pages-plugin */

import { publish as ghpagePublish } from 'gh-pages'
import { ILogger, TAnyMap } from './interface'
import execa from 'execa'
import { IPushOpts, TAnyMap } from './interface'

/**
* @private
Expand All @@ -11,15 +12,41 @@ export const OK = { status: 'OK' }
/**
* @private
*/
export const publish = (src: string, opts: TAnyMap, logger: ILogger) => new Promise((resolve, reject) => {
ghpagePublish(src, opts, (err?: any) => {
export const pullTags = (opts: IPushOpts): Promise<any> => {
const execaOpts = {
env: opts.env,
cwd: opts.cwd
}

return execa('git', ['pull', '--tags', '--force'], execaOpts)
}

/**
* @private
*/
export const pushPages = (opts: IPushOpts) => new Promise((resolve, reject) => {
const { src, logger } = opts
const ghpagesOpts: TAnyMap = {
repo: opts.repo,
branch: opts.branch,
dest: opts.dst,
message: opts.message
}

ghpagePublish(src, ghpagesOpts, (err?: any) => {
if (err) {
logger.error('Publish docs failure', err)
reject(err)

} else {
logger.log(`Docs published successfully, branch=${opts.branch}, src=${src}, dst=${opts.dest}`)
logger.log(`Docs published successfully, branch=${ghpagesOpts.branch}, src=${src}, dst=${ghpagesOpts.dest}`)
resolve(OK)
}
})
})

/**
* @private
*/
export const publish = (opts: IPushOpts) => pullTags(opts)
.then(() => pushPages(opts))
18 changes: 10 additions & 8 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import AggregateError from 'aggregate-error'
import fs from 'fs'
import { TContext } from './interface'
import { TContext, IPushOpts } from './interface'
import { resolveConfig } from './config'
import { publish as ghpagesPublish } from './ghpages'
import { render } from './tpl'
Expand Down Expand Up @@ -37,21 +37,23 @@ export const verifyConditions = async (pluginConfig: any, context: TContext) =>

export const publish = async (pluginConfig: any, context: TContext) => {
const config = resolveConfig(pluginConfig, context, undefined, 'publish')
const { logger } = context
const { logger, env, cwd } = context
const message = render(config.msg, context, logger)
const pushOpts: IPushOpts = {
...config,
message,
logger,
env,
cwd
}

if (!isEqual(_config, config)) {
await verifyConditions(pluginConfig, context)
}

logger.log('Publishing docs via gh-pages')

return ghpagesPublish(config.src, {
repo: config.repo,
branch: config.branch,
dest: config.dst,
message
}, logger)
return ghpagesPublish(pushOpts)
}

export default {
Expand Down
8 changes: 8 additions & 0 deletions src/main/ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type TAnyMap = {

export type TContext = Context & {
env: TAnyMap,
cwd: string,
options: TAnyMap & {
publish?: Array<any>,
verifyConditions?: Array<any>
Expand All @@ -24,6 +25,13 @@ export interface IGhpagesPluginConfig {
enterprise?: boolean
}

export interface IPushOpts extends IGhpagesPluginConfig {
message: string
logger: ILogger,
env: TAnyMap,
cwd: string
}

export interface ILogger {
log: (message: string, ...vars: any[]) => void,
error: (message: string, ...vars: any[]) => void,
Expand Down
7 changes: 5 additions & 2 deletions src/test/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AggregateError from 'aggregate-error'
import fs from 'fs'
import path from 'path'
import { TAnyMap } from '../../main/ts/interface'
import { TAnyMap, TContext } from '../../main/ts/interface'
import {
DEFAULT_SRC,
DEFAULT_DST,
Expand Down Expand Up @@ -149,10 +149,12 @@ describe('index', () => {
}
})
}))
jest.mock('execa', () => () => Promise.resolve())
})

afterAll(() => {
jest.unmock('gh-pages')
jest.unmock('execa')
jest.resetModules()
})

Expand All @@ -170,8 +172,9 @@ describe('index', () => {
branch: 'doc-branch',
msg: 'docs updated v{{=it.nextRelease.gitTag}}'
}
const context = {
const context: TContext = {
logger,
cwd: process.cwd(),
// nextRelease: {},
options: {
...globalConfig,
Expand Down
58 changes: 56 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ cross-spawn@^5.0.1:
shebang-command "^1.2.0"
which "^1.2.9"

cross-spawn@^6.0.0:
cross-spawn@^6.0.0, cross-spawn@^6.0.5:
version "6.0.5"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
dependencies:
Expand Down Expand Up @@ -1760,6 +1760,21 @@ execa@^1.0.0:
signal-exit "^3.0.0"
strip-eof "^1.0.0"

execa@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/execa/-/execa-2.0.3.tgz#4b84301b33042cfb622771e886ed0b10e5634642"
integrity sha512-iM124nlyGSrXmuyZF1EMe83ESY2chIYVyDRZKgmcDynid2Q2v/+GuE7gNMl6Sy9Niwf4MC0DDxagOxeMPjuLsw==
dependencies:
cross-spawn "^6.0.5"
get-stream "^5.0.0"
is-stream "^2.0.0"
merge-stream "^2.0.0"
npm-run-path "^3.0.0"
onetime "^5.1.0"
p-finally "^2.0.0"
signal-exit "^3.0.2"
strip-final-newline "^2.0.0"

exit@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/exit/-/exit-0.1.2.tgz#0632638f8d877cc82107d30a0fff1a17cba1cd0c"
Expand Down Expand Up @@ -2666,6 +2681,11 @@ is-stream@^1.0.0, is-stream@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"

is-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-2.0.0.tgz#bde9c32680d6fae04129d6ac9d921ce7815f78e3"
integrity sha512-XCoy+WlUr7d1+Z8GgSuXmpuUFC9fOhRXglJMx+dwLKTkL44Cjd4W1Z5P+BQZpr+cR93aGP4S/s7Ftw6Nd/kiEw==

is-subset@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/is-subset/-/is-subset-0.1.1.tgz#8a59117d932de1de00f245fcdd39ce43f1e939a6"
Expand Down Expand Up @@ -3673,6 +3693,11 @@ merge-stream@^1.0.1:
dependencies:
readable-stream "^2.0.1"

merge-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
integrity sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==

merge2@^1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.2.3.tgz#7ee99dbd69bb6481689253f018488a1b902b0ed5"
Expand Down Expand Up @@ -3720,7 +3745,7 @@ mimic-fn@^1.0.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.2.0.tgz#820c86a39334640e99516928bd03fca88057d022"

mimic-fn@^2.0.0:
mimic-fn@^2.0.0, mimic-fn@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b"

Expand Down Expand Up @@ -4056,6 +4081,13 @@ npm-run-path@^2.0.0:
dependencies:
path-key "^2.0.0"

npm-run-path@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/npm-run-path/-/npm-run-path-3.1.0.tgz#7f91be317f6a466efed3c9f2980ad8a4ee8b0fa5"
integrity sha512-Dbl4A/VfiVGLgQv29URL9xshU8XDY1GeLy+fsaZ1AA8JDSfjvr5P5+pzRbWqRSBxk6/DW7MIh8lTM/PaGnP2kg==
dependencies:
path-key "^3.0.0"

npm-user-validate@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/npm-user-validate/-/npm-user-validate-1.0.0.tgz#8ceca0f5cea04d4e93519ef72d0557a75122e951"
Expand Down Expand Up @@ -4241,6 +4273,13 @@ once@^1.3.0, once@^1.3.1, once@^1.4.0, once@~1.4.0:
dependencies:
wrappy "1"

onetime@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/onetime/-/onetime-5.1.0.tgz#fff0f3c91617fe62bb50189636e99ac8a6df7be5"
integrity sha512-5NcSkPHhwTVFIQN+TUqXoS5+dlElHXdpAWu9I0HP20YOtIi+aZ0Ct82jdlILDxjLEAWwvm+qj1m6aEtsDVmm6Q==
dependencies:
mimic-fn "^2.1.0"

opener@^1.5.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/opener/-/opener-1.5.1.tgz#6d2f0e77f1a0af0032aca716c2c1fbb8e7e8abed"
Expand Down Expand Up @@ -4321,6 +4360,11 @@ p-finally@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae"

p-finally@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-2.0.1.tgz#bd6fcaa9c559a096b680806f4d657b3f0f240561"
integrity sha512-vpm09aKwq6H9phqRQzecoDpD8TmVyGw70qmWlyq5onxY7tqyTTFVvxMykxQSQKILBSFlbXpypIw2T1Ml7+DDtw==

p-is-promise@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/p-is-promise/-/p-is-promise-2.1.0.tgz#918cebaea248a62cf7ffab8e3bca8c5f882fc42e"
Expand Down Expand Up @@ -4487,6 +4531,11 @@ path-key@^2.0.0, path-key@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40"

path-key@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/path-key/-/path-key-3.1.0.tgz#99a10d870a803bdd5ee6f0470e58dfcd2f9a54d3"
integrity sha512-8cChqz0RP6SHJkMt48FW0A7+qUOn+OsnOsVtzI59tZ8m+5bCSk7hzwET0pulwOM2YMn9J1efb07KB9l9f30SGg==

path-parse@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c"
Expand Down Expand Up @@ -5493,6 +5542,11 @@ strip-eof@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/strip-eof/-/strip-eof-1.0.0.tgz#bb43ff5598a6eb05d89b59fcd129c983313606bf"

strip-final-newline@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/strip-final-newline/-/strip-final-newline-2.0.0.tgz#89b852fb2fcbe936f6f4b3187afb0a12c1ab58ad"
integrity sha512-BrpvfNAE3dcvq7ll3xVumzjKjZQ5tI1sEUIKr3Uoks0XUl45St3FlatVqef9prk4jRDzhW6WZg+3bk93y6pLjA==

strip-indent@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/strip-indent/-/strip-indent-2.0.0.tgz#5ef8db295d01e6ed6cbf7aab96998d7822527b68"
Expand Down

0 comments on commit 8e1ecac

Please sign in to comment.