feat(cli): 添加onInit/onPrepare配置

This commit is contained in:
roymondchen 2022-08-18 19:27:19 +08:00 committed by jia000
parent 4e4cae4a77
commit 87f1bfbdd6
3 changed files with 23 additions and 4 deletions

View File

@ -2,7 +2,7 @@ import path from 'path';
import fs from 'fs-extra'; import fs from 'fs-extra';
import { UserConfig } from './types'; import { ModuleMainFilePath, UserConfig } from './types';
import { prepareEntryFile, resolveAppPackages } from './utils'; import { prepareEntryFile, resolveAppPackages } from './utils';
export default class Core { export default class Core {
@ -10,7 +10,7 @@ export default class Core {
public options: UserConfig; public options: UserConfig;
public moduleMainFilePath = { public moduleMainFilePath: ModuleMainFilePath = {
componentMap: {}, componentMap: {},
pluginMap: {}, pluginMap: {},
configMap: {}, configMap: {},
@ -32,9 +32,16 @@ export default class Core {
public async init() { public async init() {
this.moduleMainFilePath = resolveAppPackages(this); this.moduleMainFilePath = resolveAppPackages(this);
if (typeof this.options.onInit === 'function') {
this.moduleMainFilePath = await this.options.onInit(this);
}
} }
public async prepare() { public async prepare() {
await prepareEntryFile(this); await prepareEntryFile(this);
if (typeof this.options.onPrepare === 'function') {
this.options.onPrepare(this);
}
} }
} }

View File

@ -1,3 +1,5 @@
import type Core from './Core';
export enum EntryType { export enum EntryType {
CONFIG = 'config', CONFIG = 'config',
VALUE = 'value', VALUE = 'value',
@ -36,6 +38,14 @@ export interface NpmConfig {
client?: 'npm' | 'yarn' | 'pnpm'; client?: 'npm' | 'yarn' | 'pnpm';
} }
export interface ModuleMainFilePath {
componentMap: Record<string, string>;
pluginMap: Record<string, string>;
configMap: Record<string, string>;
valueMap: Record<string, string>;
eventMap: Record<string, string>;
}
export interface UserConfig { export interface UserConfig {
source: string; source: string;
temp: string; temp: string;
@ -43,4 +53,6 @@ export interface UserConfig {
componentFileAffix: string; componentFileAffix: string;
cleanTemp: boolean; cleanTemp: boolean;
npmConfig?: NpmConfig; npmConfig?: NpmConfig;
onInit?: (app: Core) => ModuleMainFilePath | Promise<ModuleMainFilePath>;
onPrepare?: (app: Core) => void;
} }

View File

@ -7,7 +7,7 @@ import fs from 'fs-extra';
import * as recast from 'recast'; import * as recast from 'recast';
import type App from '../Core'; import type App from '../Core';
import { Entry, EntryType, NpmConfig, PackageType } from '../types'; import { Entry, EntryType, ModuleMainFilePath, NpmConfig, PackageType } from '../types';
interface TypeAssertion { interface TypeAssertion {
type: string; type: string;
@ -20,7 +20,7 @@ interface ParseEntryOption {
indexPath: string; indexPath: string;
} }
export const resolveAppPackages = (app: App) => { export const resolveAppPackages = (app: App): ModuleMainFilePath => {
const componentMap: Record<string, string> = {}; const componentMap: Record<string, string> = {};
const configMap: Record<string, string> = {}; const configMap: Record<string, string> = {};
const eventMap: Record<string, string> = {}; const eventMap: Record<string, string> = {};