-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pack): impl usePackageJsonPlugin plugin
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { existsSync, writeFileSync } from 'fs-extra' | ||
import { resolve } from 'path' | ||
import { Plugin, OnDone, HookOptions } from '../interfaces' | ||
import { mark } from '../debug' | ||
|
||
type Overrides = Record<string, any> | ||
|
||
export class PackageJsonPlugin implements Plugin { | ||
name = 'PackageJsonPlugin' | ||
|
||
constructor(private overrides: Overrides) { | ||
mark('PackageJsonPlugin::constructor') | ||
} | ||
|
||
async onFinish(done: OnDone, { context, output }: HookOptions) { | ||
mark('PackageJsonPlugin::onFinish(start)') | ||
if (!existsSync(resolve(context, 'package.json'))) { | ||
throw new Error('Cannot find package.json.') | ||
} | ||
const pkg = require(resolve(context, 'package.json')) | ||
Object.assign(pkg, this.overrides) | ||
writeFileSync(resolve(output, 'package.json'), JSON.stringify(pkg, null, 2)) | ||
mark('PackageJsonPlugin::onFinish(finish)') | ||
done() | ||
} | ||
} | ||
|
||
export function usePackageJsonPlugin(overrides: Overrides): PackageJsonPlugin { | ||
return new PackageJsonPlugin(overrides) | ||
} |