Skip to content

Commit

Permalink
BREAKING CHANGE: use lerna CLI for all commands, fixes #28
Browse files Browse the repository at this point in the history
- keep `run` as optional, it will display an error when user forgot to install it
- add more unit tests with helper command-runner that was missing before
- fixes issue #28
  • Loading branch information
ghiscoding committed Mar 12, 2022
1 parent bbe55bf commit a780191
Show file tree
Hide file tree
Showing 67 changed files with 2,060 additions and 996 deletions.
45 changes: 17 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Lerna-Lite 🐉
### Previously known as "ws-conventional-version-roller"

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)
Expand All @@ -18,13 +17,13 @@
## Lerna-Lite
A super light fork of Lerna, only 3 commands were extracted from the original [Lerna](https://github.com/lerna/lerna) (as shown below).

**Note:** Since the [Run](https://github.com/ghiscoding/lerna-lite/tree/main/packages/run) command is totally optional, we created separate CLIs to handle each commands, see below.
**Note:** Since the **[Run](https://github.com/ghiscoding/lerna-lite/tree/main/packages/run) command** is totally **optional**, we created separate CLIs to handle each commands, see below.

| Command | CLI | Description | Included |
| Command | Install | Description | Included |
|---------|-------------|-------------| ---------|
| [version](https://github.com/ghiscoding/lerna-lite/tree/main/packages/version) | [ws-roller](https://github.com/ghiscoding/lerna-lite/tree/main/packages/cli#installation) 📰 | create new version for each workspace package | Yes |
| [publish](https://github.com/ghiscoding/lerna-lite/tree/main/packages/publish) | [ws-roller](https://github.com/ghiscoding/lerna-lite/tree/main/packages/cli#installation) 📰 | publish each workspace package | Yes |
| [run](https://github.com/ghiscoding/lerna-lite/tree/main/packages/run) | [ws-runner](https://github.com/ghiscoding/lerna-lite/tree/main/packages/run#installation) 🏃 | run npm script in each workspace package | Optional |
| 📰 [version](https://github.com/ghiscoding/lerna-lite/tree/main/packages/version) | `npm i @lerna-lite/cli` | create new version for each workspace package | Yes |
| 📰 [publish](https://github.com/ghiscoding/lerna-lite/tree/main/packages/publish) | `npm i @lerna-lite/cli` | publish each workspace package | Yes |
| 🏃 [run](https://github.com/ghiscoding/lerna-lite/tree/main/packages/run) | `npm i @lerna-lite/run` | run npm script in each workspace package | Optional |

## Why create this lib/fork?
Mainly for the following reasons:
Expand All @@ -33,15 +32,16 @@ Mainly for the following reasons:
3. add some little extras

### This lib will help you with
##### [Version](https://github.com/ghiscoding/lerna-lite/tree/main/packages/version) & [Publish](https://github.com/ghiscoding/lerna-lite/tree/main/packages/publish) commands (`ws-roller`)
##### [Version](https://github.com/ghiscoding/lerna-lite/tree/main/packages/version) & [Publish](https://github.com/ghiscoding/lerna-lite/tree/main/packages/publish) commands
- Automate the rolling of new Versions (independent or fixed) for all your packages
- it will automatically add Commit & Tag your new Version in Git & create new Release in GitHub when enabled
- Automate the creation of Changelogs for all your packages by reading all [Conventional Commits](https://www.conventionalcommits.org/)
- each package will get its own changelog and a combined changelog will also be created in the root
- Automate the repository Publish of your new versions for all your packages
- it could push to NPM or any other repository platform
##### [Run](https://github.com/ghiscoding/lerna-lite/tree/main/packages/run) command (`ws-runner`)
##### [Run](https://github.com/ghiscoding/lerna-lite/tree/main/packages/run) command (optional)
- [Run](https://github.com/ghiscoding/lerna-lite/tree/main/packages/run) is an optional package that will help you run npm script in parallel and in topological order.

## Lerna-Lite Packages

| Package Name | Version | Description | Changes |
Expand Down Expand Up @@ -69,32 +69,32 @@ Using Lerna-Lite? Add a README badge to show it off: [![lerna--lite](https://img

## Installation
```bash
# publish/version commands CLI
# Lerna CLI which includes publish/version commands
npm install @lerna-lite/cli

# run command CLI
# optional run command
npm install @lerna-lite/run
```
**Note:** the `ws-roller` CLI only has 2 commands available `publish` and `version`, while `ws-runner` CLI has the `run` command.
**Note:** the `lerna` CLI only has 2 built-in commands available `publish` and `version`, while `run` command is optional and must be installed separately.

### Usage
Add the following NPM Scripts or simply run the following NodeJS command in your shell.
```js
// package.json / npm scripts
"scripts": {
"roll-version": "ws-roller version",
"roll-publish": "ws-roller publish from-package"
"roll-version": "lerna version",
"roll-publish": "lerna publish from-package"
}
```

### Configuration
This lib requires a config file in order to do its job properly. It could come from a separate config file in the root (read [`lerna.json` - Wiki](https://github.com/ghiscoding/lerna-lite/wiki/lerna.json) OR a `"lerna": {}` property directly under your `package.json`.

#### Command Options
- `ws-roller`
- `lerna`
- [version](https://github.com/ghiscoding/lerna-lite/blob/main/packages/version/README.md)
- [publish](https://github.com/ghiscoding/lerna-lite/blob/main/packages/publish/README.md) (make sure to double-check your [publishConfig](https://docs.npmjs.com/cli/v6/configuring-npm/package-json#publishconfig) access of each package before publishing)
- `ws-runner`
- `lerna` (optional command)
- [run](https://github.com/ghiscoding/lerna-lite/blob/main/packages/run/README.md)

### Migration for [Lerna](https://github.com/lerna/lerna) Users
Expand All @@ -105,23 +105,12 @@ npm uninstall lerna # OR yarn remove lerna
```
2. install Lerna-Lite CLI(s)
```sh
# ws-roller CLI (version/publish commands)
# Lerna CLI (`version`/`publish` commands)
npm install @lerna-lite/cli

# ws-runner CLI (run command)
# optionally install the `run` command
npm install @lerna-lite/run
```
3. change your npm scripts `lerna <command>` to `ws-roller` (or `ws-runner`)
```diff
- "roll-version": "lerna version",
+ "roll-version": "ws-roller version",

- "roll-publish": "lerna publish from-package"
+ "roll-publish": "ws-roller publish from-package"

- "build": "lerna run build"
+ "build": "ws-runner run build"
```

### Development / Contributions
If you wish to contribute to the project, follow these steps
Expand Down
78 changes: 0 additions & 78 deletions helpers/command-runner.ts

This file was deleted.

70 changes: 70 additions & 0 deletions helpers/command-runner/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"use strict";

const path = require("path");
const lernaCLI = require("../../packages/cli/src/lerna-cli");

module.exports = commandRunner;

/**
* A higher-order function to help with passing _actual_ yargs-parsed argv
* into command constructors (instead of artificial direct parameters).
*
* @param {Object} commandModule The yargs command exports
* @return {Function} with partially-applied yargs config
*/
function commandRunner(commandModule) {
/* eslint-disable import/no-dynamic-require, global-require */
const cmd = commandModule.command.split(" ")[0];

// prime the pump so slow-as-molasses CI doesn't fail with delayed require()
require(path.resolve(require.main.filename, "../.."));

return (cwd) => {
// create a _new_ yargs instance every time cwd changes to avoid singleton pollution
const cli = lernaCLI([], cwd)
.exitProcess(false)
.detectLocale(false)
.showHelpOnFail(false)
.wrap(null)
.command(commandModule);

return (...args) => {
return new Promise((resolve, reject) => {
const yargsMeta = {};

const context = {
cwd,
lernaVersion: "__TEST_VERSION__",
loglevel: "silent",
progress: false,
onResolved: (result) => {
// success resolves the result, if any, returned from execute()
resolve(Object.assign({}, result, yargsMeta));
},
onRejected: (result) => {
Object.assign(result, yargsMeta);
// tests expect errors thrown to indicate failure,
// _not_ just non-zero exitCode
reject(result);
},
};

const parseFn = (yargsError, parsedArgv, yargsOutput) => {
// this is synchronous, before the async handlers resolve
Object.assign(yargsMeta, { parsedArgv, yargsOutput });
};

cli
.fail((msg, err) => {
// since yargs 10.1.0, this is the only way to catch handler rejection
// _and_ yargs validation exceptions when using async command handlers
const actual = err || new Error(msg);
// backfill exitCode for test convenience
yargsMeta.exitCode = "exitCode" in actual ? actual.exitCode : 1;
context.onRejected(actual);
})
.parse([cmd, ...args], context, parseFn);
});
};
};
}
11 changes: 11 additions & 0 deletions helpers/command-runner/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@lerna-test/command-runner",
"version": "0.0.0-test-only",
"description": "A local test helper",
"main": "index.js",
"private": true,
"license": "MIT",
"dependencies": {
"@lerna/cli": "file:../../core/cli"
}
}
4 changes: 2 additions & 2 deletions lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"version": {
"conventionalCommits": true,
"createRelease": "github",
"gitDryRun": false,
"gitDryRun": true,
"noPrivate": true,
"message": "chore(release): publish new version %v",
"changelogHeaderMessage": "### Automate your Workspace Versions, Changelogs & Publish with [Lerna-Lite](https://github.com/ghiscoding/lerna-lite) 🚀"
Expand All @@ -19,4 +19,4 @@
"runDryRun": false
}
}
}
}

0 comments on commit a780191

Please sign in to comment.