微生成器中 JSON API 封装,提供常见的 JSON 文件操作方法。
import { JsonAPI } from '@modern-js/codesmith-api-json';
export default async (context: GeneratorContext, generator: GeneratorCore) => {
const jsonAPI = new JsonAPI(generator);
await jsonAPI.update(context.materials.default.get('package.json'), {
query: {},
update: {
$set: {
'dependencies.@modern-js/plugin-bff': `^2.0.0`,
},
},
});
};
获取 JSON 文件内容。
参数:
FsResource
,文件资源,通过 context.materials.default.get(<filename>)
获取。合并对象至 JSON 文件。
参数:
FsResource
通过 context.materials.default.get(<filename>)
获取。Record<string, any>
需合并对象。更新对象字段至 JSON 文件。
参数:
FsResource
通过 context.materials.default.get(<filename>)
获取。{ query: Record<string, any>; update: Record<string, any> }
更新操作,详细使用姿势查看 declaration-update。