Skip to content

Commit

Permalink
chore(type): 修复类型错误, 增加日志开关
Browse files Browse the repository at this point in the history
  • Loading branch information
czfadmin committed Sep 29, 2024
1 parent 911a994 commit e2ab0b5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-queens-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vsc-extension-helper': patch
---

修复类型错误, 增加日志开关
1 change: 1 addition & 0 deletions examples/hello-world/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import './commands/index';
// 1.
export const activate = withActivate({
extensionId: 'hello-world',
logger: true
})(async function (ctx: vscode.ExtensionContext) {
// 1.use hoc
withCommand({ name: 'foo' })(function foo() {
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const EXTENSION_ID = 'extensionId';
export const GLOBAL_CONFIG = "GLOBAL_CONFIG";
export const EXTENSION_COMMANDS = 'commands';
export const EXTENSION_CONTEXT = 'context';
export const VSC_EXTENSION_HELPER = 'vsc-extension-helper';
68 changes: 36 additions & 32 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,43 @@ import {
CommandOptions,
InternalCommandOption,
RegisterContextOption,
WithActiveOptions,
} from './types';
import {
EXTENSION_COMMANDS,
GLOBAL_CONFIG,
EXTENSION_CONTEXT,
EXTENSION_ID,
VSC_EXTENSION_HELPER,
} from './constants';

const globalContext = new Map<
string,
null | ExtensionContext | string | InternalCommandOption[]
>();
export type GlobalContextType = WithActiveOptions & {
commands: InternalCommandOption[];
context: ExtensionContext | null;
};

// 保存上下文ID
globalContext.set(EXTENSION_ID, '');
// 保存上下文
globalContext.set(EXTENSION_CONTEXT, null);
let globalContext: GlobalContextType = {
context: null,
commands: [],
extensionId: '',
};

// 保存全局命令注册信息
globalContext.set(EXTENSION_COMMANDS, [] as InternalCommandOption[]);
export function useGlobalContext(): GlobalContextType {
return globalContext;
}

/**
* @zh 获取插件id
* @en Get the plugin id
* @returns
*/
export function useExtensionId() {
const extensionId = globalContext.get(EXTENSION_ID);
if (!extensionId) {
const config = useGlobalContext();
if (!config.extensionId) {
throw new Error(
'未发现插件ID,请在在插件启动入口处,通过`withActivate`注入插件ID',
);
}
return extensionId;
return config.extensionId;
}

/**
Expand All @@ -46,7 +49,7 @@ export function useExtensionId() {
* @returns
*/
export function useExtensionContext(): ExtensionContext {
const context = globalContext.get(EXTENSION_CONTEXT);
const context = useGlobalContext().context;
if (!context) {
throw new Error(
'未发现插件上下文,请在在插件启动入口处,通过`withActivate`注入插件上下文',
Expand All @@ -68,31 +71,30 @@ export function useCommands(): [
) => void,
(command: string) => void,
] {
let cmds = globalContext.get(EXTENSION_COMMANDS) as InternalCommandOption[];

function addCommand(
name: string,
options: CommandOptions & { handler: CommandHandlerType },
) {
if (cmds.find(it => it.name === name)) {
if (globalContext.commands.find(it => it.name === name)) {
console.error(
`${VSC_EXTENSION_HELPER} | ${name}: This command has already been registered, registration failed.`,
);
return;
}

const { handler, ...rest } = options;
cmds.push({
globalContext.commands.push({
name,
options: rest,
handler: handler,
});
}
function deleteCommand(name: string) {
cmds = cmds.filter(it => it.name !== name);
globalContext.set(EXTENSION_COMMANDS, cmds);
globalContext.commands = globalContext.commands.filter(
it => it.name !== name,
);
}
return [cmds, addCommand, deleteCommand];
return [globalContext.commands, addCommand, deleteCommand];
}

/**
Expand All @@ -101,20 +103,19 @@ export function useCommands(): [
* @param options
*/
export function registerContext(options: RegisterContextOption) {
globalContext.set(EXTENSION_CONTEXT, options.context);
globalContext.set(EXTENSION_ID, options.extensionId);
globalContext.context = options.context;
globalContext.extensionId = options.extensionId;
globalContext.logger = options.logger || false;
}

/**
* @zh 开始注册使用装饰器和高阶函数注册的命令
*/
export function registerCommands() {
const extensionId = globalContext.get(EXTENSION_ID) as string;
const cmds = globalContext.get(EXTENSION_COMMANDS) as InternalCommandOption[];
const context = useExtensionContext();
cmds.forEach(it => {
globalContext.commands.forEach(it => {
let disposer: Disposable | null;
const commandName = `${extensionId}.${it.name}`;
const commandName = `${globalContext.extensionId}.${it.name}`;
if (!it.options.textEditor) {
disposer = commands.registerCommand(commandName, it.handler);
} else {
Expand All @@ -123,9 +124,10 @@ export function registerCommands() {
if (!disposer && context) {
context.subscriptions.push(disposer);
}
console.log(
`${VSC_EXTENSION_HELPER} | ${commandName}: Registration succeeded`,
);
globalContext.logger &&
console.log(
`${VSC_EXTENSION_HELPER} | ${commandName}: Registration succeeded`,
);
});
}

Expand All @@ -136,5 +138,7 @@ export function registerCommands() {
export function clearGlobalContext() {
const context = useExtensionContext();
context?.subscriptions.forEach(it => it.dispose());
globalContext.clear();
globalContext.commands.length = 0;
globalContext.context = null;
globalContext.extensionId = '';
}
6 changes: 2 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,16 @@ export interface InternalCommandOption {
export type TextEditorCommandOptions = Omit<CommandOptions, 'textEditor'>;

export interface IVoidHandler {
group: string | string[];
group?: string | string[];

(...args: any[]): void;
}

export interface WithActiveOptions {
extensionId: string;
logger?: boolean;
}

export interface RegisterContextOption extends WithActiveOptions {
context: ExtensionContext;
}



0 comments on commit e2ab0b5

Please sign in to comment.