@winner-fed/plugin-access
Version:
适配 access(权限)的 WinJS 插件,适用于 Vue3。
63 lines (62 loc) • 2.19 kB
JavaScript
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { Mustache } from "@winner-fed/utils";
const getCurrentDir = ()=>{
if ('undefined' != typeof __dirname) return __dirname;
return dirname(fileURLToPath(import.meta.url));
};
const ACCESS_TEMPLATES_DIR = join(getCurrentDir(), '../templates');
const DIR_NAME = 'plugin-access';
const src = (api)=>{
api.describe({
key: 'access',
config: {
schema ({ zod }) {
return zod.object({
roles: zod.object({})
}).required();
}
},
enableBy: api.EnableBy.config
});
api.onGenerateFiles(()=>{
const { roles = {} } = api.config.access || {};
const accessTpl = readFileSync(join(ACCESS_TEMPLATES_DIR, 'core.tpl'), 'utf-8');
api.writeTmpFile({
path: join(DIR_NAME, 'index.ts'),
noPluginDir: true,
content: Mustache.render(accessTpl, {
roles: JSON.stringify(roles)
}),
context: {}
});
api.writeTmpFile({
path: join(DIR_NAME, 'runtime.ts'),
noPluginDir: true,
content: readFileSync(join(ACCESS_TEMPLATES_DIR, 'runtime.tpl'), 'utf-8')
});
api.writeTmpFile({
path: join(DIR_NAME, 'createComponent.ts'),
noPluginDir: true,
content: readFileSync(join(ACCESS_TEMPLATES_DIR, 'createComponent.tpl'), 'utf-8')
});
api.writeTmpFile({
path: join(DIR_NAME, 'createDirective.ts'),
noPluginDir: true,
content: readFileSync(join(ACCESS_TEMPLATES_DIR, 'createDirective.tpl'), 'utf-8')
});
api.writeTmpFile({
path: join(DIR_NAME, 'types.d.ts'),
noPluginDir: true,
content: readFileSync(join(ACCESS_TEMPLATES_DIR, 'types.d.ts'), 'utf-8')
});
});
api.addRuntimePluginKey(()=>[
'access'
]);
api.addRuntimePlugin(()=>[
`${api.paths.absTmpPath}/${DIR_NAME}/runtime.ts`
]);
};
export { src as default };