Skip to content

Commit cb5657a

Browse files
committed
feat: Added verbosity level and automatic required parameter generation for inits
1 parent 90df563 commit cb5657a

File tree

8 files changed

+57
-15
lines changed

8 files changed

+57
-15
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codify-plugin-lib",
3-
"version": "1.0.166",
3+
"version": "1.0.167",
44
"description": "Library plugin library",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",
@@ -16,7 +16,7 @@
1616
"dependencies": {
1717
"ajv": "^8.12.0",
1818
"ajv-formats": "^2.1.1",
19-
"codify-schemas": "1.0.73",
19+
"codify-schemas": "1.0.74",
2020
"@npmcli/promise-spawn": "^7.0.1",
2121
"@homebridge/node-pty-prebuilt-multiarch": "^0.12.0-beta.5",
2222
"uuid": "^10.0.0",

src/messages/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { Plugin } from '../plugin/plugin.js';
2828

2929
const SupportedRequests: Record<string, { handler: (plugin: Plugin, data: any) => Promise<unknown>; requestValidator: SchemaObject; responseValidator: SchemaObject }> = {
3030
'initialize': {
31-
handler: async (plugin: Plugin) => plugin.initialize(),
31+
handler: async (plugin: Plugin, data: any) => plugin.initialize(data),
3232
requestValidator: InitializeRequestDataSchema,
3333
responseValidator: InitializeResponseDataSchema
3434
},

src/plugin/plugin.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
GetResourceInfoResponseData,
66
ImportRequestData,
77
ImportResponseData,
8+
InitializeRequestData,
89
InitializeResponseData,
910
MatchRequestData,
1011
MatchResponseData,
@@ -23,6 +24,7 @@ import { getPty } from '../pty/index.js';
2324
import { Resource } from '../resource/resource.js';
2425
import { ResourceController } from '../resource/resource-controller.js';
2526
import { ptyLocalStorage } from '../utils/pty-local-storage.js';
27+
import { VerbosityLevel } from '../utils/utils.js';
2628

2729
export class Plugin {
2830
planStorage: Map<string, Plan<any>>;
@@ -46,7 +48,11 @@ export class Plugin {
4648
return new Plugin(name, controllersMap);
4749
}
4850

49-
async initialize(): Promise<InitializeResponseData> {
51+
async initialize(data: InitializeRequestData): Promise<InitializeResponseData> {
52+
if (data.verbosityLevel) {
53+
VerbosityLevel.set(data.verbosityLevel);
54+
}
55+
5056
for (const controller of this.resourceControllers.values()) {
5157
await controller.initialize();
5258
}
@@ -116,7 +122,7 @@ export class Plugin {
116122
const result = await ptyLocalStorage.run(this.planPty, () =>
117123
this.resourceControllers
118124
.get(core.type!)
119-
?.import(core, parameters)
125+
?.import(core, parameters, data.autoSearchAll)
120126
)
121127

122128
return {

src/pty/background-pty.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as fs from 'node:fs/promises';
66
import stripAnsi from 'strip-ansi';
77

88
import { debugLog } from '../utils/debug.js';
9+
import { VerbosityLevel } from '../utils/utils.js';
910
import { IPty, SpawnError, SpawnOptions, SpawnResult } from './index.js';
1011
import { PromiseQueue } from './promise-queue.js';
1112

@@ -76,7 +77,10 @@ export class BackgroundPty implements IPty {
7677
data: strippedData,
7778
});
7879
} else {
79-
process.stdout.write(data);
80+
// Print to stdout if the verbosity level is above 0
81+
if (VerbosityLevel.get() > 0) {
82+
process.stdout.write(data);
83+
}
8084
}
8185
})
8286

src/resource/resource-controller.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ export class ResourceController<T extends StringIndexedObject> {
258258

259259
async import(
260260
core: ResourceConfig,
261-
parameters: Partial<T>
261+
parameters: Partial<T>,
262+
autoSearchAll = false,
262263
): Promise<Array<ResourceJson> | null> {
263264
if (this.settings.importAndDestroy?.preventImport) {
264265
throw new Error(`Type: ${this.typeId} cannot be imported`);
@@ -270,6 +271,18 @@ export class ResourceController<T extends StringIndexedObject> {
270271
originalDesiredConfig: structuredClone(parameters),
271272
};
272273

274+
// Auto search means that no required parameters will be provided. We will try to generate it ourselves or return an
275+
// empty array if they can't be.
276+
if (autoSearchAll && this.settings.allowMultiple) {
277+
if (this.settings.allowMultiple === true || !this.settings.allowMultiple.findAllParameters?.()) {
278+
return [];
279+
}
280+
281+
const parametersToImport = await this.settings.allowMultiple.findAllParameters?.();
282+
const results = await Promise.all(parametersToImport.map((p) => this.import(core, p)));
283+
return results.filter(Boolean).flat() as ResourceJson[];
284+
}
285+
273286
this.addDefaultValues(parameters);
274287
await this.applyTransformParameters(parameters);
275288

src/resource/resource-settings.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface ResourceSettings<T extends StringIndexedObject> {
4343
* If paramA is required, then if resource1.paramA === resource2.paramA then are the same resource.
4444
* If resource1.paramA !== resource1.paramA, then they are different.
4545
*/
46-
identifyingParameters?: string[]
46+
identifyingParameters?: string[];
4747

4848
/**
4949
* If multiple copies are allowed then a matcher must be defined to match the desired
@@ -54,7 +54,14 @@ export interface ResourceSettings<T extends StringIndexedObject> {
5454
*
5555
* @return The matched resource.
5656
*/
57-
matcher?: (desired: Partial<T>, current: Partial<T>) => boolean
57+
matcher?: (desired: Partial<T>, current: Partial<T>) => boolean;
58+
59+
/**
60+
* This method if supported by the resource returns an array of parameters that represent all of the possible
61+
* instances of a resource on the system. An example of this is for the git-repository resource, this method returns
62+
* a list of directories which are git repositories.
63+
*/
64+
findAllParameters?: () => Promise<Array<Partial<T>>>
5865
} | boolean
5966

6067
/**

src/utils/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import { ResourceConfig, StringIndexedObject } from 'codify-schemas';
22
import os from 'node:os';
33
import path from 'node:path';
44

5+
export const VerbosityLevel = new class {
6+
private level = 0;
7+
8+
get() {
9+
return this.level;
10+
}
11+
12+
set(level: number) {
13+
this.level = level;
14+
}
15+
}
16+
517
export function isDebug(): boolean {
618
return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
719
}

0 commit comments

Comments
 (0)