Skip to content

Commit

Permalink
feat(cli): 添加onInit/onPrepare配置
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen authored and jia000 committed Aug 18, 2022
1 parent 4e4cae4 commit 87f1bfb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
11 changes: 9 additions & 2 deletions packages/cli/src/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import path from 'path';

import fs from 'fs-extra';

import { UserConfig } from './types';
import { ModuleMainFilePath, UserConfig } from './types';
import { prepareEntryFile, resolveAppPackages } from './utils';

export default class Core {
public version = require('../package.json').version;

public options: UserConfig;

public moduleMainFilePath = {
public moduleMainFilePath: ModuleMainFilePath = {
componentMap: {},
pluginMap: {},
configMap: {},
Expand All @@ -32,9 +32,16 @@ export default class Core {

public async init() {
this.moduleMainFilePath = resolveAppPackages(this);
if (typeof this.options.onInit === 'function') {
this.moduleMainFilePath = await this.options.onInit(this);
}
}

public async prepare() {
await prepareEntryFile(this);

if (typeof this.options.onPrepare === 'function') {
this.options.onPrepare(this);
}
}
}
12 changes: 12 additions & 0 deletions packages/cli/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type Core from './Core';

export enum EntryType {
CONFIG = 'config',
VALUE = 'value',
Expand Down Expand Up @@ -36,11 +38,21 @@ export interface NpmConfig {
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 {
source: string;
temp: string;
packages: (string | Record<string, string>)[];
componentFileAffix: string;
cleanTemp: boolean;
npmConfig?: NpmConfig;
onInit?: (app: Core) => ModuleMainFilePath | Promise<ModuleMainFilePath>;
onPrepare?: (app: Core) => void;
}
4 changes: 2 additions & 2 deletions packages/cli/src/utils/resolveAppPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fs from 'fs-extra';
import * as recast from 'recast';

import type App from '../Core';
import { Entry, EntryType, NpmConfig, PackageType } from '../types';
import { Entry, EntryType, ModuleMainFilePath, NpmConfig, PackageType } from '../types';

interface TypeAssertion {
type: string;
Expand All @@ -20,7 +20,7 @@ interface ParseEntryOption {
indexPath: string;
}

export const resolveAppPackages = (app: App) => {
export const resolveAppPackages = (app: App): ModuleMainFilePath => {
const componentMap: Record<string, string> = {};
const configMap: Record<string, string> = {};
const eventMap: Record<string, string> = {};
Expand Down

0 comments on commit 87f1bfb

Please sign in to comment.