Skip to content

Commit

Permalink
feat: initial release (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Dec 21, 2017
1 parent 5f5c5ff commit 2ac206f
Show file tree
Hide file tree
Showing 18 changed files with 793 additions and 2 deletions.
131 changes: 131 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@

# Created by https://www.gitignore.io/api/macos,windows,linux,node

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

### macOS ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env


### Windows ###
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

# End of https://www.gitignore.io/api/macos,windows,linux,node

package-lock.json
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: node_js
notifications:
email: false
node_js:
- 9
- 8

# Trigger a push build on master and greenkeeper branches + PRs build on every branches
# Avoid double build on PRs (See https://github.com/travis-ci/travis-ci/issues/1147)
branches:
only:
- master
- /^greenkeeper.*$/

# Retry install on fail to avoid failing a build on network/disk/external errors
install:
- travis_retry npm install

script:
- npm run test

after_success:
- npm run codecov
- npm run semantic-release
1 change: 1 addition & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--install.no-lockfile true
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
126 changes: 124 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,124 @@
# exec
:shell: Set of semantic-release plugins to run custom scripts
# @semantic-release/exec

Set of [semantic-release](https://github.com/semantic-release/semantic-release) plugins to execute custom shell commands.

[![Travis](https://img.shields.io/travis/semantic-release/exec.svg)](https://travis-ci.org/semantic-release/exec)
[![Codecov](https://img.shields.io/codecov/c/github/semantic-release/exec.svg)](https://codecov.io/gh/semantic-release/exec)
[![Greenkeeper badge](https://badges.greenkeeper.io/semantic-release/exec.svg)](https://greenkeeper.io/)

## verifyConditions

Execute a shell command to verify if the release should happen.

| Command property | Description |
|------------------|--------------------------------------------------------------------------|
| `exit code` | `0` if the verification is successful, or any other exit code otherwise. |
| `stdout` | Write only the reason for the verification to fail. |
| `stderr` | Can be used for logging. |

## getLastRelease

Execute a shell command to determine the last release.

| Command property | Description |
|------------------|-------------------------------------------------------------------------------------------------------------------------------|
| `exit code` | Any non `0` code is considered as an unexpected error and will stop the `semantic-release` execution with an error. |
| `stdout` | Only the `lastRelease` must be written to `stdout` as parseable JSON (for example `{"version": "x.x.x", "gitHead": "xxxx"}`). |
| `stderr` | Can be used for logging. |

## analyzeCommits

Execute a shell command to determine the type release.

| Command property | Description |
|------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `exit code` | Any non `0` code is considered as an unexpected error and will stop the `semantic-release` execution with an error. |
| `stdout` | Only the release type (`major`, `minor` or `patch` etc..) can be written to `stdout`. If no release has to be done the command must not write to `stdout`. |
| `stderr` | Can be used for logging. |

## verifyRelease

Execute a shell command to verifying a release that was determined before and is about to be published.

| Command property | Description |
|------------------|--------------------------------------------------------------------------|
| `exit code` | `0` if the verification is successful, or any other exit code otherwise. |
| `stdout` | Only the reason for the verification to fail can be written to `stdout`. |
| `stderr` | Can be used for logging. |

## generateNotes

Execute a shell command to generate the release note.

| Command property | Description |
|------------------|---------------------------------------------------------------------------------------------------------------------|
| `exit code` | Any non `0` code is considered as an unexpected error and will stop the `semantic-release` execution with an error. |
| `stdout` | Only the release note must be written to `stdout`. |
| `stderr` | Can be used for logging. |

## publish

Execute a shell command to publish the release.

| Command property | Description |
|------------------|---------------------------------------------------------------------------------------------------------------------|
| `exit code` | Any non `0` code is considered as an unexpected error and will stop the `semantic-release` execution with an error. |
| `stdout` | Can be used for logging. |
| `stderr` | Can be used for logging. |

## Configuration

### Options

| Options | Description |
|---------|------------------------------------------------|
| `cmd` | The shell command to execute. See [cmd](#cmd). |

#### `cmd`

The shell command is generated with [Lodash template](https://lodash.com/docs#template). All the objets passed to the [semantic-release plugins](https://github.com/semantic-release/semantic-release#plugins) are available as template options.

##### `cmd` examples

```json
{
"release": {
"publish": [
{
"path": "@semantic-release/exec",
"cmd": "./publish.sh ${nextRelease.version} ${options.branch} ${commits.length} ${Date.now()}",
},
"@semantic-release/npm",
"@semantic-release/github"
]
}
}
```

This will execute the shell command `./publish.sh 1.0.0 master 3 870668040000` (for the release of version `1.0.0` from branch `master` with `3` commits on `August 4th, 1997 at 2:14 AM`).

### Usage

Options can be set within the plugin definition in the `semantic-release` configuration file:

```json
{
"release": {
"verifyConditions": [
"@semantic-release/npm",
{
"path": "@semantic-release/exec",
"cmd": "./verify.sh",
}
],
"publish": [
"@semantic-release/npm",
{
"path": "@semantic-release/exec",
"cmd": "./publish.sh ${nextRelease.version}",
},
"@semantic-release/github"
]
}
}
```
58 changes: 58 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const {castArray, isPlainObject} = require('lodash');
const parseJson = require('parse-json');
const SemanticReleaseError = require('@semantic-release/error');
const execScript = require('./lib/exec-script');
const verifyConfig = require('./lib/verify-config');

const PLUGIN_TYPES = ['getLastRelease', 'analyzeCommits', 'verifyRelease', 'generateNotes', 'publish'];

async function verifyConditions(pluginConfig, params) {
for (const [option, value] of Object.entries(params.options || {})) {
if (PLUGIN_TYPES.includes(option)) {
for (const plugin of castArray(value)) {
if (
plugin === '@semantic-release/script' ||
(isPlainObject(plugin) && plugin.path === '@semantic-release/script')
) {
verifyConfig(plugin);
}
}
}
}

verifyConfig(pluginConfig);

try {
await execScript(pluginConfig, params);
} catch (err) {
throw new SemanticReleaseError(err.stdout, 'EVERIFYCONDITIONS');
}
}

async function getLastRelease(pluginConfig, params) {
const stdout = await execScript(pluginConfig, params);
return stdout ? parseJson(stdout) : undefined;
}

async function analyzeCommits(pluginConfig, params) {
const stdout = await execScript(pluginConfig, params);
return stdout.trim() ? stdout : undefined;
}

async function verifyRelease(pluginConfig, params) {
try {
await execScript(pluginConfig, params);
} catch (err) {
throw new SemanticReleaseError(err.stdout, 'EVERIFYRELEASE');
}
}

async function generateNotes(pluginConfig, params) {
return execScript(pluginConfig, params);
}

async function publish(pluginConfig, params) {
await execScript(pluginConfig, params);
}

module.exports = {verifyConditions, getLastRelease, analyzeCommits, verifyRelease, generateNotes, publish};
14 changes: 14 additions & 0 deletions lib/exec-script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const {template} = require('lodash');
const execa = require('execa');

module.exports = async ({cmd, ...config}, {logger, ...opts}) => {
const script = template(cmd)({config, ...opts});

logger.log('Call script %s', script);

const shell = execa.shell(script);
shell.stdout.pipe(process.stdout);
shell.stderr.pipe(process.stderr);

return (await shell).stdout.trim();
};
11 changes: 11 additions & 0 deletions lib/verify-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const {isString} = require('lodash');
const SemanticReleaseError = require('@semantic-release/error');

module.exports = config => {
if (!isString(config.cmd) || !config.cmd.trim()) {
throw new SemanticReleaseError(
'The script plugin must be configured with the shell command to execute in the a "cmd" option.',
'EINVALIDCMD'
);
}
};
Loading

0 comments on commit 2ac206f

Please sign in to comment.