chore: update the version and add the setupInit section

This commit is contained in:
BluJ
2023-04-25 14:36:43 -06:00
parent 5107d38547
commit 1b9e2cf503
11 changed files with 155 additions and 58 deletions

View File

@@ -17,7 +17,7 @@ export type Save<WD, A> = (options: {
export type Read<WD, A> = (options: {
effects: Effects;
utils: Utils<WD>;
}) => Promise<null | DeepPartial<A>>;
}) => Promise<void | DeepPartial<A>>;
/**
* We want to setup a config export with a get and set, this
* is going to be the default helper to setup config, because it will help
@@ -46,7 +46,9 @@ export function setupConfig<WD, A extends Config<InputSpec>>(
getConfig: (async ({ effects, config }) => {
return {
spec: spec.build(),
config: nullIfEmpty(await read({ effects, utils: utils<WD>(effects) })),
config: nullIfEmpty(
(await read({ effects, utils: utils<WD>(effects) })) || null,
),
};
}) as ExpectedExports.getConfig,
};

View File

@@ -15,3 +15,4 @@ export * as properties from "./properties";
export * as autoconfig from "./autoconfig";
export * as actions from "./actions";
export * as manifest from "./manifest";
export * as inits from "./inits";

3
lib/inits/index.ts Normal file
View File

@@ -0,0 +1,3 @@
export { setupInit } from "./setupInit";
export { setupUninstall } from "./setupUninstall";
export { setupInstall } from "./setupInstall";

View File

@@ -1,6 +1,6 @@
import { ManifestVersion } from "../manifest/ManifestTypes";
import { Effects } from "../types";
import { Utils } from "../util";
import { ManifestVersion } from "../../manifest/ManifestTypes";
import { Effects } from "../../types";
import { Utils } from "../../util";
export class Migration<Version extends ManifestVersion> {
constructor(

View File

@@ -0,0 +1,69 @@
import { setupActions } from "../../actions/setupActions";
import { EmVer } from "../../emverLite/mod";
import { GenericManifest } from "../../manifest/ManifestTypes";
import { ExpectedExports } from "../../types";
import { once } from "../../util/once";
import { Migration } from "./Migration";
export class Migrations {
private constructor(
readonly manifest: GenericManifest,
readonly migrations: Array<Migration<any>>,
) {}
private sortedMigrations = once(() => {
const migrationsAsVersions = (this.migrations as Array<Migration<any>>).map(
(x) => [EmVer.parse(x.options.version), x] as const,
);
migrationsAsVersions.sort((a, b) => a[0].compareForSort(b[0]));
return migrationsAsVersions;
});
private currentVersion = once(() => EmVer.parse(this.manifest.version));
static of<Migrations extends Array<Migration<any>>>(
manifest: GenericManifest,
...migrations: EnsureUniqueId<Migrations>
) {
return new Migrations(manifest, migrations as Array<Migration<any>>);
}
async init({
effects,
previousVersion,
}: Parameters<ExpectedExports.init>[0]) {
if (!!previousVersion) {
const previousVersionEmVer = EmVer.parse(previousVersion);
for (const [_, migration] of this.sortedMigrations()
.filter((x) => x[0].greaterThan(previousVersionEmVer))
.filter((x) => x[0].lessThanOrEqual(this.currentVersion()))) {
await migration.up({ effects });
}
}
}
async uninit({
effects,
nextVersion,
}: Parameters<ExpectedExports.uninit>[0]) {
if (!!nextVersion) {
const nextVersionEmVer = EmVer.parse(nextVersion);
const reversed = [...this.sortedMigrations()].reverse();
for (const [_, migration] of reversed
.filter((x) => x[0].greaterThan(nextVersionEmVer))
.filter((x) => x[0].lessThanOrEqual(this.currentVersion()))) {
await migration.down({ effects });
}
}
}
}
export function setupMigrations<Migrations extends Array<Migration<any>>>(
manifest: GenericManifest,
...migrations: EnsureUniqueId<Migrations>
) {
return Migrations.of(manifest, ...migrations);
}
// prettier-ignore
export type EnsureUniqueId<A, B = A, ids = never> =
B extends [] ? A :
B extends [Migration<infer id>, ...infer Rest] ? (
id extends ids ? "One of the ids are not unique"[] :
EnsureUniqueId<A, Rest, id | ids>
) : "There exists a migration that is not a Migration"[]

24
lib/inits/setupInit.ts Normal file
View File

@@ -0,0 +1,24 @@
import { ExpectedExports } from "../types";
import { Migrations } from "./migrations/setupMigrations";
import { Install } from "./setupInstall";
import { Uninstall } from "./setupUninstall";
export function setupInit<WrapperData>(
migrations: Migrations,
install: Install<WrapperData>,
uninstall: Uninstall<WrapperData>,
): {
init: ExpectedExports.init;
uninit: ExpectedExports.uninit;
} {
return {
init: async (opts) => {
await migrations.init(opts);
await install.init(opts);
},
uninit: async (opts) => {
await migrations.uninit(opts);
await uninstall.uninit(opts);
},
};
}

24
lib/inits/setupInstall.ts Normal file
View File

@@ -0,0 +1,24 @@
import { Effects, ExpectedExports } from "../types";
import { Utils, utils } from "../util";
export type InstallFn<WrapperData> = (opts: {
effects: Effects;
utils: Utils<WrapperData>;
}) => Promise<void>;
export class Install<WrapperData> {
private constructor(readonly fn: InstallFn<WrapperData>) {}
static of<WrapperData>(fn: InstallFn<WrapperData>) {
return new Install(fn);
}
async init({
effects,
previousVersion,
}: Parameters<ExpectedExports.init>[0]) {
if (!previousVersion) await this.fn({ effects, utils: utils(effects) });
}
}
export function setupInstall<WrapperData>(fn: InstallFn<WrapperData>) {
return Install.of(fn);
}

View File

@@ -0,0 +1,24 @@
import { Effects, ExpectedExports } from "../types";
import { Utils, utils } from "../util";
export type UninstallFn<WrapperData> = (opts: {
effects: Effects;
utils: Utils<WrapperData>;
}) => Promise<void>;
export class Uninstall<WrapperData> {
private constructor(readonly fn: UninstallFn<WrapperData>) {}
static of<WrapperData>(fn: UninstallFn<WrapperData>) {
return new Uninstall(fn);
}
async uninit({
effects,
nextVersion,
}: Parameters<ExpectedExports.uninit>[0]) {
if (!nextVersion) await this.fn({ effects, utils: utils(effects) });
}
}
export function setupUninstall<WrapperData>(fn: UninstallFn<WrapperData>) {
return Uninstall.of(fn);
}

View File

@@ -1,50 +0,0 @@
import { setupActions } from "../actions/setupActions";
import { EmVer } from "../emverLite/mod";
import { GenericManifest } from "../manifest/ManifestTypes";
import { ExpectedExports } from "../types";
import { once } from "../util/once";
import { Migration } from "./Migration";
export function setupMigrations<Migrations extends Array<Migration<any>>>(
manifest: GenericManifest,
...migrations: EnsureUniqueId<Migrations>
) {
const sortedMigrations = once(() => {
const migrationsAsVersions = (migrations as Array<Migration<any>>).map(
(x) => [EmVer.parse(x.options.version), x] as const,
);
migrationsAsVersions.sort((a, b) => a[0].compareForSort(b[0]));
return migrationsAsVersions;
});
const currentVersion = once(() => EmVer.parse(manifest.version));
const init: ExpectedExports.init = async ({ effects, previousVersion }) => {
if (!!previousVersion) {
const previousVersionEmVer = EmVer.parse(previousVersion);
for (const [_, migration] of sortedMigrations()
.filter((x) => x[0].greaterThan(previousVersionEmVer))
.filter((x) => x[0].lessThanOrEqual(currentVersion()))) {
await migration.up({ effects });
}
}
};
const uninit: ExpectedExports.uninit = async ({ effects, nextVersion }) => {
if (!!nextVersion) {
const nextVersionEmVer = EmVer.parse(nextVersion);
const reversed = [...sortedMigrations()].reverse();
for (const [_, migration] of reversed
.filter((x) => x[0].greaterThan(nextVersionEmVer))
.filter((x) => x[0].lessThanOrEqual(currentVersion()))) {
await migration.down({ effects });
}
}
};
return { init, uninit };
}
// prettier-ignore
export type EnsureUniqueId<A, B = A, ids = never> =
B extends [] ? A :
B extends [Migration<infer id>, ...infer Rest] ? (
id extends ids ? "One of the ids are not unique"[] :
EnsureUniqueId<A, Rest, id | ids>
) : "There exists a migration that is not a Migration"[]