Skip to content

Commit 69789db

Browse files
committed
feat(pack): impl usePackageJsonPlugin plugin
1 parent 2ad5610 commit 69789db

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

packages/pack/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,20 @@ type Options = {
248248
function useTypeScriptPlugin(options: Options): TypeScriptPlugin
249249
```
250250

251+
### PackageJsonPlugin
252+
253+
A plugin that copy package.json and modify content. _(Run at `onFinish` step)._
254+
255+
#### Usage
256+
257+
```js
258+
const { usePackageJsonPlugin } = require('@bem-react/pack/lib/plugins/PackageJsonPlugin')
259+
260+
usePackageJsonPlugin({
261+
scripts: {},
262+
})
263+
```
264+
251265
## 🏗 Write own plugin
252266

253267
The plugin can perform an action on one of the available hook `onBeforeRun`, `onRun` and `onAfterRun`.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { existsSync, writeFileSync } from 'fs-extra'
2+
import { resolve } from 'path'
3+
import { Plugin, OnDone, HookOptions } from '../interfaces'
4+
import { mark } from '../debug'
5+
6+
type Overrides = Record<string, any>
7+
8+
export class PackageJsonPlugin implements Plugin {
9+
name = 'PackageJsonPlugin'
10+
11+
constructor(private overrides: Overrides) {
12+
mark('PackageJsonPlugin::constructor')
13+
}
14+
15+
async onFinish(done: OnDone, { context, output }: HookOptions) {
16+
mark('PackageJsonPlugin::onFinish(start)')
17+
if (!existsSync(resolve(context, 'package.json'))) {
18+
throw new Error('Cannot find package.json.')
19+
}
20+
const pkg = require(resolve(context, 'package.json'))
21+
Object.assign(pkg, this.overrides)
22+
writeFileSync(resolve(output, 'package.json'), JSON.stringify(pkg, null, 2))
23+
mark('PackageJsonPlugin::onFinish(finish)')
24+
done()
25+
}
26+
}
27+
28+
export function usePackageJsonPlugin(overrides: Overrides): PackageJsonPlugin {
29+
return new PackageJsonPlugin(overrides)
30+
}

0 commit comments

Comments
 (0)