Skip to content

Commit

Permalink
Use type for component state instead of enum
Browse files Browse the repository at this point in the history
Signed-off-by: worksofliam <[email protected]>
  • Loading branch information
worksofliam committed Oct 4, 2024
1 parent 226d0ce commit f79e370
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 24 deletions.
14 changes: 4 additions & 10 deletions src/components/component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import IBMi from "../api/IBMi";

export const enum ComponentState {
NotChecked = `Not checked`,
NotInstalled = `Not installed`,
Installed = `Installed`,
NeedUpdate = `Need update`,
Error = `Error`,
}
export type ComponentState = `NotChecked` | `NotInstalled` | `Installed` | `NeedsUpdate` | `Error`;

export type ComponentIdentification = {
name: string
Expand Down Expand Up @@ -39,7 +33,7 @@ export type IBMiComponentType<T extends IBMiComponent> = new (c: IBMi) => T;
*
*/
export abstract class IBMiComponent {
private state = ComponentState.NotChecked;
private state: ComponentState = `NotChecked`;

constructor(protected readonly connection: IBMi) {

Expand All @@ -52,14 +46,14 @@ export abstract class IBMiComponent {
async check() {
try {
this.state = await this.getRemoteState();
if (this.state !== ComponentState.Installed) {
if (this.state !== `Installed`) {
this.state = await this.update();
}
}
catch (error) {
console.log(`Error occurred while checking component ${this.toString()}`);
console.log(error);
this.state = ComponentState.Error;
this.state = `Error`;
}

return this;
Expand Down
4 changes: 2 additions & 2 deletions src/components/copyToImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export class CopyToImport extends IBMiComponent {
return { name: 'CopyToImport', version: 1 };
}

protected getRemoteState() {
return ComponentState.Installed;
protected getRemoteState(): ComponentState {
return `Installed`;
}

protected update(): ComponentState | Promise<ComponentState> {
Expand Down
10 changes: 5 additions & 5 deletions src/components/getMemberInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export class GetMemberInfo extends IBMiComponent {
}
}
if (this.installedVersion < this.currentVersion) {
return ComponentState.NeedUpdate;
return `NeedsUpdate`;
}

return ComponentState.Installed;
return `Installed`;
}

protected async update() {
protected async update(): Promise<ComponentState> {
const config = this.connection.config!;
return this.connection.withTempDirectory(async tempDir => {
const tempSourcePath = posix.join(tempDir, `getMemberInfo.sql`);
Expand All @@ -40,9 +40,9 @@ export class GetMemberInfo extends IBMiComponent {
});

if (result.code) {
return ComponentState.Error;
return `Error`;
} else {
return ComponentState.Installed;
return `Installed`;
}
});
}
Expand Down
12 changes: 6 additions & 6 deletions src/components/getNewLibl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export class GetNewLibl extends IBMiComponent {
return { name: 'GetNewLibl', version: 1 };
}

protected async getRemoteState() {
return this.connection.remoteFeatures[`GETNEWLIBL.PGM`] ? ComponentState.Installed : ComponentState.NotInstalled;
protected async getRemoteState(): Promise<ComponentState> {
return this.connection.remoteFeatures[`GETNEWLIBL.PGM`] ? `Installed` : `NotInstalled`;
}

protected update() {
protected update(): Promise<ComponentState> {
const config = this.connection.config!
const content = instance.getContent();
return this.connection.withTempDirectory(async tempDir => {
return this.connection.withTempDirectory(async (tempDir): Promise<ComponentState> => {
const tempSourcePath = posix.join(tempDir, `getnewlibl.sql`);

await content!.writeStreamfileRaw(tempSourcePath, getSource(config.tempLibrary));
Expand All @@ -25,9 +25,9 @@ export class GetNewLibl extends IBMiComponent {
});

if (!result.code) {
return ComponentState.Installed;
return `Installed`;
} else {
return ComponentState.Error;
return `Error`;
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class ComponentManager {

get<T extends IBMiComponent>(type: IBMiComponentType<T>, ignoreState?: boolean): T | undefined {
const component = this.registered.get(type);
if (component && (ignoreState || component.getState() === ComponentState.Installed)) {
if (component && (ignoreState || component.getState() === `Installed`)) {
return component as T;
}
}
Expand Down

0 comments on commit f79e370

Please sign in to comment.