Skip to content

Commit 942ef94

Browse files
committed
feat: Initial commit
0 parents  commit 942ef94

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+9609
-0
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/coverage/

.eslintrc.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"parserOptions": {
3+
"sourceType": "module",
4+
"ecmaVersion": 2017,
5+
"experimentalObjectRestSpread": true
6+
},
7+
"extends": [
8+
"eslint:recommended",
9+
"plugin:prettier/recommended"
10+
],
11+
"env": {
12+
"es6": true,
13+
"node": true,
14+
"jest": true
15+
},
16+
"rules": {
17+
"eqeqeq": 2,
18+
"no-alert": 2,
19+
"no-caller": 2,
20+
"no-eval": 2,
21+
"no-extend-native": 2,
22+
"no-floating-decimal": 2,
23+
"no-implicit-globals": 2,
24+
"no-labels": 2,
25+
"no-loop-func": 2,
26+
"no-new-require": 2,
27+
"no-path-concat": 2,
28+
"no-useless-rename": 2,
29+
"no-var": 2,
30+
"no-shadow": 2,
31+
"prefer-const": 2,
32+
"prefer-spread": 2,
33+
"strict": 2,
34+
"valid-jsdoc": 2,
35+
"global-require": 0,
36+
"no-console": 0,
37+
"no-unused-vars": 0
38+
}
39+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/coverage/

.prettierrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"tabWidth": 4,
3+
"useTabs": true,
4+
"printWidth": 120,
5+
"parser": "babylon",
6+
"proseWrap": "never"
7+
}

.travis.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language: node_js
2+
3+
cache: yarn
4+
5+
jobs:
6+
include:
7+
- stage: test
8+
node_js: "8"
9+
- stage: test
10+
node_js: "10"
11+
- stage: release
12+
node_js: "10"
13+
script: skip # Skip running "yarn test" again
14+
deploy:
15+
provider: script
16+
skip_cleanup: true
17+
script: yarn publish

LICENSE.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Copyright (C) 2017 by Dave Houlbrooke <dave@shax.com>
2+
Permission to use, copy, modify, and/or distribute this software
3+
for any purpose with or without fee is hereby granted.
4+
5+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
6+
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
7+
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
8+
THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
9+
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
10+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
11+
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
12+
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

README.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# multi-semantic-release: hacky semantic-release for monorepos
2+
3+
[![Travis CI](https://travis-ci.org/dhoulb/blork.svg?branch=master)](https://travis-ci.org/dhoulb/blork)
4+
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat)](https://github.com/semantic-release/semantic-release)
5+
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
6+
[![Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
7+
[![npm](https://img.shields.io/npm/dm/blork.svg)](https://www.npmjs.com/package/blork)
8+
9+
Proof of concept that wraps `[semantic-release](https://github.com/semantic-release/semantic-release)` to work with monorepos.
10+
11+
This package should work well, but may not be fundamentally stable enough for important production use as it's pretty dependent on how semantic-release works (so it may break or get out-of-date in future versions of semantic-release).
12+
13+
One of the best things about semantic-release is forgetting about version numbers. In a monorepo though there's still a lot of version number management required for local deps (packages in the same monorepo referenced in `dependencies` or `devDependencies` or `peerDependencies`). However in multi-semantic-release the version numbers of local deps are written into `package.json` at release time. This means there's no need to hard-code versions any more (we recommend just using `*` asterisk instead in your repo code).
14+
15+
## Installation
16+
17+
```sh
18+
yarn add multi-semantic-release --dev
19+
```
20+
21+
## Usage
22+
23+
```sh
24+
multi-semantic-release
25+
```
26+
27+
## Configuration
28+
29+
Configuration for releases is the same as [semantic-release configuration](https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md), i.e. using a `release` key under `package.json` or in `.releaserc` file of any type e.g. `.yaml`, `.json`.
30+
31+
_But_ in multi-semantic-release this configuration can be done globally (in your top-level dir), or per-package (in that individual package's dir). If you set both then per-package settings will override global settings.
32+
33+
multi-semantic-release does not support any command line arguments (this wasn't possible without duplicating files from semantic-release, which I've tried to avoid).
34+
35+
## API
36+
37+
multi-semantic-release default exports a `multirelease()` method which takes the following arguments:
38+
39+
- `packages` An array containing string paths to `package.json` files
40+
- `options` An object containing default semantic-release configuration options
41+
42+
`multirelease()` returns an array of objects describing the result of the multirelease (corresponding to the `packages` array that is passed in).
43+
44+
```js
45+
const multirelease = require("multi-semantic-release");
46+
47+
multirelease([
48+
`${__dirname}/packages/my-pkg-1/package.json`,
49+
`${__dirname}/packages/my-pkg-2/package.json`,
50+
]);
51+
```
52+
53+
## Implementation notes (and other thoughts)
54+
55+
### Support for monorepos
56+
57+
Automatically finds packages as long as workspaces are configured as-per [Yarn workspaces](https://yarnpkg.com/lang/en/docs/workspaces/). _You don't need to use Yarn but the way they define monorepos seems intuitive, and is likely what NPM will copy when they add this functionality (as rumoured)._
58+
59+
I'm aware Lerna is the best-known tool right now, but in future it seems clear it will be replaced by functionality in Yarn and NPM directly. If you use Yarn workspaces today (January 2019), then publishing is the only remaining feature Lerna is _really_ required for (though it'd be lovely if Yarn added parallel script execution). Thus using multi-semantic-release means you can probably remove Lerna entirely from your project.
60+
61+
### Iteration vs coordination
62+
63+
Other packages that enable semantic-release for monorepos work by iterating into each package and running the `semantic-release` command. This is conceptually simple but unfortunately not viable because:
64+
65+
- If a package is published that depends on minor changes that have been made in a sibling package it could cause extremely subtle errors (the worst kind!) — if the project follows semver religiously this should never happen, but it's better to eliminate the _potential_ for errors
66+
- Dependency version numbers need to reflect the _next_ release at time of publishing, so a package needs to know the state of _all other packages_ before it can publish correctly — this central state needs to be coordinated by something
67+
68+
### Local dependencies and version numbers
69+
70+
A key requirement is handling local dep version numbers elegantly. multi-semantic-release does the following:
71+
72+
- The next version number of all packages is established first
73+
- If a release has not changed but has local deps that _have_ changed... do a `patch` bump on that package too
74+
- Before packages are released (in semantic-release's prepare step), the correct current/next version number of _all_ local dependencies is written into the `package.json` file (overwriting any existing value)
75+
- This ensures the package at the time of publishing will be atomically correct with all other packages in the monorepo.
76+
77+
The above means that, possibly, if someone upgrades dependencies and pulls down a package from NPM _during the multirelease_ (before all its deps have been published at their next versions), then their `npm install` will fail (it will work if they try again in a few minutes). On balance I thought it was more important to be atomically correct (this situation should be fairly rare assuming projects commit their lockfiles).
78+
79+
### Integration with semantic-release
80+
81+
This is the jankiest part of multi-semantic-release and most likely part to break relies. I expect this to cause maintenance issues down the line. In an ideal world semantic-release will bake-in support for monorepos (making this package unnecessary).
82+
83+
The way I ended up integrating is to create a custom "inline plugin" for semantic-release, and passing that in to `semanticRelease()` as the only plugin. This then calls any other configured plugins to retrieve and potentially modify the response.
84+
85+
The plugin starts all release at once, then pauses them (using Promises) at various points to allow other packages in the multirelease to catch up. This is mainly needed so the version number of all packages can be established _before_ any package is released. This allows us to do a `patch` bump on releases whose local deps have bumped, and to accurately write in the version of local deps in each `package.json`
86+
87+
The inline plugin does the following:
88+
89+
- **verifyConditions:** _not used_
90+
- **analyzeCommits:**
91+
- Replaces `context.commits` with a list of commits filtered to the folder only
92+
- Calls `plugins.analyzeCommits()` to get the next release type (e.g. from @semantic-release/commit-analyzer)
93+
- Waits for _all_ packages to catch up to this point.
94+
- For packages that haven't bumped, checks if it has local deps (or deps of deps) that have bumped and returns `patch` if that's true
95+
- **verifyRelease:** _not used_
96+
- **generateNotes:**
97+
- Calls `plugins.generateNotes()` to get the notes (e.g. from @semantic-release/release-notes-generator)
98+
- Appends a section listing any local deps bumps (e.g. "my-pkg-2: upgraded to 1.2.1")
99+
- **prepare:**
100+
- Writes in the correct version for local deps in `dependencies`, `devDependencies`, `peerDependencies` in `package.json`
101+
- Serialize the releases so they happen one-at-a-time (because semantic-release calls `git push` asyncronously, multiple releases at once fail because Git refs aren't locked — semantic-release should use `execa.sync()` so Git operations are atomic)
102+
- **publish:** _not used_
103+
- **success:** _not used_
104+
- **fail:** _not used_
105+
106+
### Git tags
107+
108+
Releases always use a `tagFormat` of `my-pkg-1@1.0.1` for Git tags, and always overrides any `gitTag` set in semantic-release configuration.
109+
110+
I can personally see the potential for this option in coordinating a semantic-release (e.g. so two packages with the same tag always bump and release simultaneously). Unfortunately with the points of integration available in semantic-release, it was effectively impossible when releasing to stop a second package creating a duplicate tag (causing an error).
111+
112+
To make the `tagFormat` option work as intended the following would need to happen:
113+
114+
- semantic-release needs to check if a given tag already exists at a given commit, and not create it / push it if that's true
115+
- Release notes for multiple package releases need to be merged BUT the Github release only done once (by having the notes merged at the semantic-release level but only published once, or having the Github plugin merge them)
116+
- Make it clear in documentation that the default tag `v1.0.0` will have the same effect as Lerna's fixed mode (all changed monorepo packages released at same time)

bin/cli.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env node
2+
3+
// Imports.
4+
const getWorkspacesYarn = require("../lib/getWorkspacesYarn");
5+
const multiSemanticRelease = require("../lib/multiSemanticRelease");
6+
7+
// Get directory.
8+
const cwd = process.cwd();
9+
10+
// Catch errors.
11+
try {
12+
// Get list of package.json paths according to Yarn workspaces.
13+
const paths = getWorkspacesYarn(cwd);
14+
15+
// Do multirelease (log out any errors).
16+
multiSemanticRelease(paths, {}, { cwd }).then(
17+
() => {
18+
// Success.
19+
process.exit(0);
20+
},
21+
error => {
22+
// Log out errors.
23+
console.error(`[multi-semantic-release]:`, error);
24+
process.exit(1);
25+
}
26+
);
27+
} catch (error) {
28+
// Log out errors.
29+
console.error(`[multi-semantic-release]:`, error);
30+
process.exit(1);
31+
}

lib/blork.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const { existsSync, lstatSync } = require("fs");
2+
const { checker, check, add, ValueError } = require("blork");
3+
const { Signale } = require("signale");
4+
const { Writable } = require("stream");
5+
const { WritableStreamBuffer } = require("stream-buffers");
6+
7+
// Get some checkers.
8+
const isAbsolute = checker("absolute");
9+
const isObject = checker("object");
10+
const isString = checker("string");
11+
12+
// Add a directory checker.
13+
add(
14+
"directory",
15+
v => isAbsolute(v) && existsSync(v) && lstatSync(v).isDirectory(),
16+
"directory that exists in the filesystem"
17+
);
18+
19+
// Add a writable stream checker.
20+
add(
21+
"stream",
22+
// istanbul ignore next (not important)
23+
v => v instanceof Writable || v instanceof WritableStreamBuffer,
24+
"instance of stream.Writable or WritableStreamBuffer"
25+
);
26+
27+
// Exports.
28+
module.exports = { checker, check, ValueError };

lib/cleanPath.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { normalize, isAbsolute, join } = require("path");
2+
const { check } = require("blork");
3+
4+
/**
5+
* Normalize and make a path absolute, optionally using a custom CWD.
6+
* Trims any trailing slashes from the path.
7+
*
8+
* @param {string} path The path to normalize and make absolute.
9+
* @param {string} cwd=process.cwd() The CWD to prepend to the path to make it absolute.
10+
* @returns {string} The absolute and normalized path.
11+
*
12+
* @internal
13+
*/
14+
function cleanPath(path, cwd = process.cwd()) {
15+
// Checks.
16+
check(path, "path: path");
17+
check(cwd, "cwd: absolute");
18+
19+
// Normalize, absolutify, and trim trailing slashes from the path.
20+
return normalize(isAbsolute(path) ? path : join(cwd, path)).replace(/[/\\]+$/, "");
21+
}
22+
23+
// Exports.
24+
module.exports = cleanPath;

0 commit comments

Comments
 (0)