Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support multiple patterns #281

Merged
merged 2 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

gh-pages publishing plugin for [semantic-release](https://github.com/semantic-release/semantic-release)

| Step | Description |
|--------------------|-------------|
| Step | Description |
|--------------------|------------------------------------------------------------------------------------------------|
| `verifyConditions` | Verify the presence of the `GH_TOKEN` set via [environment variables](#environment-variables). |
| `publish` | Pushes commit to the [documentation branch](#options) |
| `publish` | Pushes commit to the [documentation branch](#options) |

## Install

```bash
# yarn
yarn add @qiwi/semantic-release-gh-pages-plugin --dev
Expand All @@ -21,7 +22,10 @@ npm i @qiwi/semantic-release-gh-pages-plugin -D
```

## Usage
Describe plugin configuration in [package.json / .releaserc.js](https://github.com/semantic-release/semantic-release/blob/master/docs/01-usage/plugins.md#plugins-configuration-options)

Describe plugin configuration
in [package.json / .releaserc.js](https://github.com/semantic-release/semantic-release/blob/master/docs/01-usage/plugins.md#plugins-configuration-options)

```json
{
"release": {
Expand All @@ -46,7 +50,9 @@ Describe plugin configuration in [package.json / .releaserc.js](https://github.c
}
}
```

or even shorter if default settings are used:

```json
{
"release": {
Expand All @@ -64,6 +70,7 @@ or even shorter if default settings are used:
```

## Configuration

### Environment variables

| Variable | Description |
Expand All @@ -84,7 +91,8 @@ or even shorter if default settings are used:
| `pullTagsBranch` | Target branch for tags fetching hook. If '' empty string, skips this action | `globalConfig.branch` \|\| `master` |
| `dotfiles` | gh-pages [dotfiles](https://github.com/tschaub/gh-pages#optionsdotfiles) option | `false` |
| `add` | gh-pages [add](https://github.com/tschaub/gh-pages#optionsadd) option | `false` |
| `pattern` | gh-pages [src](https://github.com/tschaub/gh-pages#optionssrc) option | `**/*` |
| `pattern` | gh-pages [src](https://github.com/tschaub/gh-pages#optionssrc) option. Use `:` to separate several values `**/*.md:**/*.png` | `**/*` |

## License

[MIT](./LICENSE)
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@
"@qiwi/substrate-types": "^2.1.0",
"aggregate-error": "^3.1.0",
"debug": "^4.4.0",
"execa": "^5.1.1",
"gh-pages": "^6.2.0",
"git-url-parse": "^16.0.0",
"lodash": "^4.17.21",
"queuefy": "^1.2.1",
"read-pkg": "^5.2.0",
"then-request": "^6.0.2",
"tslib": "^2.8.1"
"tslib": "^2.8.1",
"zurk": "^0.9.0"
},
"devDependencies": {
"@types/debug": "^4.1.12",
Expand Down
3 changes: 2 additions & 1 deletion src/main/ts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const resolveConfig = async (pluginConfig: TAnyMap, context: TContext, pa
msg = DEFAULT_MSG,
src = DEFAULT_SRC,
dst = DEFAULT_DST,
pattern = DEFAULT_PATTERN,
pattern: _pattern = DEFAULT_PATTERN,
add,
dotfiles
} = opts
Expand All @@ -144,6 +144,7 @@ export const resolveConfig = async (pluginConfig: TAnyMap, context: TContext, pa
const docsBranch = branches?.find(([from]: string[]) => from === ciBranch)?.[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)
Expand Down
20 changes: 7 additions & 13 deletions src/main/ts/ghpages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @module semantic-release-gh-pages-plugin */

import execa from 'execa'
import { $ } from 'zurk'
import { clean, publish as ghpagePublish, PublishOptions } from 'gh-pages'
import { queuefy } from 'queuefy'

Expand All @@ -21,19 +21,13 @@ export const pullTags = (opts: IPushOpts): Promise<any> => {

const repo = '' + opts.repo
const pullTagsBranch = '' + opts.pullTagsBranch
const execaOpts = {
env: opts.env,
cwd: opts.cwd
}

return execa('git', [
'pull',
'--tags',
'--force',
repo,
pullTagsBranch
], execaOpts)
.catch(console.log)
return $({
env: opts.env,
cwd: opts.cwd,
shell: false
})`git pull --tags --force ${repo} ${pullTagsBranch}`
.catch(console.error)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/** @module semantic-release-gh-pages-plugin */

import AggregateError from 'aggregate-error'
import fs from 'node:fs'
import { isEqual } from 'lodash'
import path from 'node:path'

import { resolveConfig } from './config'
import { publish as ghpagesPublish } from './ghpages'
import { IPushOpts,TContext } from './interface'
import { render } from './tpl'
import { isDirectory } from './util'

export * from './defaults'

Expand All @@ -35,7 +35,7 @@ export const verifyConditions = async (pluginConfig: any, context: TContext) =>
throw new AggregateError(['package.json repository.url does not match github.com pattern'])
}

if (!fs.existsSync(src) || !fs.lstatSync(src).isDirectory()) {
if (!isDirectory(src)) {
logger.error('Resolved docs src path=', path.resolve(src))
throw new AggregateError(['docs source directory does not exist'])
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/ts/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ICallable } from '@qiwi/substrate-types'
import fs from 'node:fs'

export const catchToSmth = (fn: ICallable, smth?: any) => {
return (...args: any[]) => {
Expand All @@ -12,3 +13,5 @@ export const catchToSmth = (fn: ICallable, smth?: any) => {
}

export const anyDefined = (...args: any[]) => args.find(item => item !== undefined)

export const isDirectory = (path: string) => fs.existsSync(path) && fs.lstatSync(path).isDirectory()
31 changes: 31 additions & 0 deletions src/test/ts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,37 @@ describe('config', () => {
})
})

it('supports multiple pattern definition', async () => {
const step = 'publish'
const token = 'token'
const pluginConfig = {
pattern: '**/*.md:**/*.html'
}
const context = {
logger,
branch,
options: globalConfig,
cwd,
env: { GITHUB_TOKEN: token }
}
const config = await resolveConfig(pluginConfig, context, undefined, step)

expect(config).toEqual({
add: undefined,
ciBranch: 'master',
docsBranch: DEFAULT_BRANCH,
dotfiles: undefined,
dst: DEFAULT_DST,
enterprise: DEFAULT_ENTERPRISE,
msg: DEFAULT_MSG,
src: DEFAULT_SRC,
token,
repo: repositoryUrl,
pullTagsBranch: DEFAULT_PULL_TAGS_BRANCH,
pattern: ['**/*.md', '**/*.html'],
})
})

it('overrides `docBranch` with `branches` value if defined', async () => {
const step = 'publish'
const path = PLUGIN_PATH
Expand Down
34 changes: 13 additions & 21 deletions src/test/ts/ghpages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { ICallable } from '@qiwi/substrate-types'
import { IPushOpts, TAnyMap } from '../../main/ts/interface'

describe('ghpages', () => {
const fakeExeca = jest.fn(() => Promise.resolve())
const _$ = jest.fn(() => Promise.resolve())
const __$ = jest.fn(() => _$)
let pullTags: any

beforeAll(() => {
jest.resetModules()
jest.mock('execa', () => fakeExeca)
jest.mock('zurk', () => ({$: __$}))
jest.mock('gh-pages', () => ({
clean: () => { /* noop */ },
publish: jest.fn((_src: string, _opts: TAnyMap, cb: ICallable) => cb())
Expand All @@ -17,11 +18,11 @@ describe('ghpages', () => {
pullTags = require('../../main/ts/ghpages').pullTags
})

afterEach(fakeExeca.mockClear)
afterEach(_$.mockClear)

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

Expand All @@ -45,31 +46,22 @@ describe('ghpages', () => {
pullTagsBranch: ''
}
expect(await pullTags(opts)).toBeUndefined()
expect(fakeExeca).not.toHaveBeenCalled()
expect(_$).not.toHaveBeenCalled()
})

it('invokes execa with proper args', async () => {
it('invokes zurk with proper args', async () => {
const opts: IPushOpts = {
...pushOptsStub,
pullTagsBranch: 'foo'
}
const execaOpts = {
cwd: opts.cwd,
env: opts.env
}

await pullTags(opts)
expect(fakeExeca).toHaveBeenCalledWith(
'git',
[
'pull',
'--tags',
'--force',
opts.repo,
opts.pullTagsBranch
],
execaOpts
)
expect(_$).toHaveBeenCalledWith(["git pull --tags --force ", " ", ""], "repo", "foo")
expect(__$).toHaveBeenCalledWith({
cwd: opts.cwd,
env: opts.env,
shell: false
})
})
})
})
38 changes: 17 additions & 21 deletions src/test/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ describe('index', () => {
})

describe('publish', () => {
const fakeExeca = jest.fn(() => Promise.resolve())
const _$ = jest.fn(() => Promise.resolve())
const __$ = jest.fn(() => _$)
const fakeZurk = {$: __$}

beforeAll(() => {
jest.resetModules()
Expand All @@ -165,14 +167,17 @@ describe('index', () => {
}
})
}))
jest.mock('execa', () => fakeExeca)
jest.mock('zurk', () => fakeZurk)
})

afterEach(fakeExeca.mockClear)
afterEach(() => {
__$.mockClear()
_$.mockClear()
})

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

Expand Down Expand Up @@ -208,27 +213,18 @@ describe('index', () => {
dest: 'root',
src: DEFAULT_PATTERN,
}
const execaOpts = {
cwd,
env: {
GITHUB_TOKEN: 'token'
}
}

const res = await publish(pluginConfig, context)
const resolvedConfig = await resolveConfig(pluginConfig, context)

expect(fakeExeca).toHaveBeenCalledWith(
'git',
[
'pull',
'--tags',
'--force',
expectedOpts.repo,
resolvedConfig.pullTagsBranch
],
execaOpts
)
expect(_$).toHaveBeenCalledWith(["git pull --tags --force ", " ", ""], expectedOpts.repo, resolvedConfig.pullTagsBranch)
expect(__$).toHaveBeenCalledWith({
cwd,
env: {
GITHUB_TOKEN: 'token'
},
shell: false
})

expect(log).toHaveBeenCalledWith('Publishing docs via gh-pages')
expect(log).toHaveBeenCalledWith(`Docs published successfully, branch=doc-branch, src=docs, pattern=${DEFAULT_PATTERN}, dst=root`)
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2029,7 +2029,7 @@ esutils@^2.0.2:
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==

execa@^5.0.0, execa@^5.1.1:
execa@^5.0.0:
version "5.1.1"
resolved "https://registry.yarnpkg.com/execa/-/execa-5.1.1.tgz#f80ad9cbf4298f7bd1d4c9555c21e93741c411dd"
integrity sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg==
Expand Down Expand Up @@ -5066,3 +5066,8 @@ yocto-queue@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.0.0.tgz#7f816433fb2cbc511ec8bf7d263c3b58a1a3c251"
integrity sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==

zurk@^0.9.0:
version "0.9.0"
resolved "https://registry.yarnpkg.com/zurk/-/zurk-0.9.0.tgz#9dbea11ef84a871cfb542ddded8c7d42c8feeaf5"
integrity sha512-MfREZRQx5VySt3VtNrfP+UTALaTTIBquhSuJS/RYAs59YkwAicx6QrXyMMpvJu0wQdfWiJJAhAeOw4+OJLZ8ig==
Loading