|
| 1 | +# run-shared-scripts |
| 2 | + |
| 3 | +<p align="center"><strong>run-shared-scripts</strong></p> |
| 4 | +<p align="center">Define and run shared scripts of a monorepo using Yarn workspaces, Bolt, Lerna or pnpm</p> |
| 5 | + |
| 6 | +<p align="center"> |
| 7 | +<a href="/LICENSE"><img src="https://img.shields.io/github/license/bubkoo/run-shared-scripts?style=flat-square" alt="MIT License"></a> |
| 8 | +<a href="https://www.typescriptlang.org"><img alt="Language" src="https://img.shields.io/badge/language-TypeScript-blue.svg?style=flat-square"></a> |
| 9 | +<a href="https://github.com/bubkoo/run-shared-scripts/pulls"><img alt="PRs Welcome" src="https://img.shields.io/badge/PRs-Welcome-brightgreen.svg?style=flat-square"></a> |
| 10 | +</p> |
| 11 | + |
| 12 | +<p align="center"> |
| 13 | +<a href="https://github.com/bubkoo/run-shared-scripts/actions/workflows/release.yml"><img alt="build" src="https://img.shields.io/github/workflow/status/bubkoo/run-shared-scripts/%F0%9F%9A%80%E3%80%80Release/master?logo=github&style=flat-square"></a> |
| 14 | +<a href="https://www.npmjs.com/package/run-shared-scripts"><img alt="NPM Package" src="https://img.shields.io/npm/v/run-shared-scripts.svg?style=flat-square"></a> |
| 15 | +<a href="https://www.npmjs.com/package/run-shared-scripts"><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/run-shared-scripts?logo=npm&style=flat-square"></a> |
| 16 | +</p> |
| 17 | + |
| 18 | +## Install |
| 19 | +```shell |
| 20 | +$ npm install --save-dev run-shared-scripts |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +Using [npm-scripts](https://docs.npmjs.com/misc/scripts) is a convenient way to our CI/CD tasks, and we may have some similar `"scripts"` in a monorepo. Take the following monorepo for example. |
| 26 | + |
| 27 | +``` |
| 28 | +. |
| 29 | +├── lerna.json |
| 30 | +├── package.json |
| 31 | +└── packages |
| 32 | + ├── project-a |
| 33 | + │ ├── index.js |
| 34 | + │ ├── node_modules |
| 35 | + │ └── package.json |
| 36 | + ├── project-b |
| 37 | + │ ├── index.js |
| 38 | + │ ├── node_module |
| 39 | + │ └── package.json |
| 40 | + ... |
| 41 | +``` |
| 42 | + |
| 43 | +The `"scripts"` property defined in `./packages/project-a/package.json` and `./packages/project-b/package.json` is similar. |
| 44 | + |
| 45 | +```json |
| 46 | + "scripts": { |
| 47 | + "clean:build": "rimraf dist lib es", |
| 48 | + "clean:coverage": "rimraf ./test/coverage", |
| 49 | + "clean": "run-p clean:build clean:coverage", |
| 50 | + "build:esm": "tsc --module esnext --target es2015 --outDir ./es", |
| 51 | + "build:cjs": "tsc --module commonjs --target es5 --outDir ./lib", |
| 52 | + "build:umd": "rollup -c", |
| 53 | + "build:style": "../../scripts/build-style.js", |
| 54 | + "build": "run-p build:style build:cjs build:esm build:umd", |
| 55 | + "prebuild": "run-s lint clean", |
| 56 | + "test": "jest", |
| 57 | + "coveralls": "cat ./test/coverage/lcov.info | coveralls", |
| 58 | + "pretest": "run-p clean:coverage", |
| 59 | + "prepare": "yarn build" |
| 60 | + } |
| 61 | +``` |
| 62 | + |
| 63 | +### Basic Usage |
| 64 | + |
| 65 | +Then we can use `run-shared-scripts` to define and run these similar `"scripts"`. |
| 66 | + |
| 67 | +1. Add `rss` config in the monorepo root's `./package.json` file. |
| 68 | +```json |
| 69 | + "rss": { |
| 70 | + "clean:build": "rimraf dist lib es", |
| 71 | + "clean:coverage": "rimraf ./test/coverage", |
| 72 | + "clean": "run-p clean:build clean:coverage", |
| 73 | + "build:esm": "tsc --module esnext --target es2015 --outDir ./es", |
| 74 | + "build:cjs": "tsc --module commonjs --target es5 --outDir ./lib", |
| 75 | + "build:umd": "rollup -c", |
| 76 | + "build:style": { "file": "./scripts/build-style.js" }, |
| 77 | + "build": "run-p build:style build:cjs build:esm build:umd", |
| 78 | + "prebuild": "run-s lint clean", |
| 79 | + "test": "jest", |
| 80 | + "coveralls": "cat ./test/coverage/lcov.info | coveralls", |
| 81 | + "pretest": "run-p clean:coverage", |
| 82 | + "prepare": "yarn build" |
| 83 | + } |
| 84 | +``` |
| 85 | + |
| 86 | +**Note that** the `"build:style"` command define the task to run an executable file. The executable file path must be an absolute path or a path relative the monorepo's root directory. |
| 87 | + |
| 88 | +2. Replace with `rss` command in `./packages/project-a/package.json` and `./packages/project-b/package.json`. |
| 89 | + |
| 90 | +```json |
| 91 | + "scripts": { |
| 92 | + "clean:build": "rss", |
| 93 | + "clean:coverage": "rss", |
| 94 | + "clean": "rss", |
| 95 | + "build:esm": "rss", |
| 96 | + "build:cjs": "rss", |
| 97 | + "build:umd": "rss", |
| 98 | + "build:style": "rss", |
| 99 | + "build": "rss", |
| 100 | + "prebuild": "rss", |
| 101 | + "test": "rss", |
| 102 | + "coveralls": "rss", |
| 103 | + "pretest": "rss", |
| 104 | + "prepare": "rss" |
| 105 | + } |
| 106 | +``` |
| 107 | + |
| 108 | +### Run specified task |
| 109 | + |
| 110 | +The `rss` command run the same named(the key of `"scripts"`) task by default. We can pass a task name to specify the task to run. |
| 111 | + |
| 112 | +```json |
| 113 | + "scripts": { |
| 114 | + "clean": "rss clean:build" // will run "clean:build" task defined in the "rss" config |
| 115 | + } |
| 116 | +``` |
| 117 | + |
| 118 | +### Run with arguments |
| 119 | + |
| 120 | +Any arguments following `--` will pass to task. |
| 121 | + |
| 122 | +```json |
| 123 | + "scripts": { |
| 124 | + "test": "rss -- --watch" // run jest in watch mode |
| 125 | + } |
| 126 | +``` |
| 127 | + |
| 128 | +## Contributing |
| 129 | + |
| 130 | +Please let us know how can we help. Do check out [issues](https://github.com/bubkoo/run-shared-scripts/issues) for bug reports or suggestions first. |
| 131 | + |
| 132 | +To become a contributor, please follow our [contributing guide](/CONTRIBUTING.md). |
| 133 | + |
| 134 | +<!-- <a href="https://github.com/bubkoo/run-shared-scripts/graphs/contributors"> |
| 135 | + <img src="/CONTRIBUTORS.svg" alt="Contributors" width="740" /> |
| 136 | +</a> --> |
| 137 | + |
| 138 | + |
| 139 | +## License |
| 140 | + |
| 141 | +The scripts and documentation in this project are released under the [MIT License](LICENSE) |
0 commit comments