mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-26 10:21:55 +00:00
chore: update the version and add the setupInit section
This commit is contained in:
3
lib/inits/index.ts
Normal file
3
lib/inits/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { setupInit } from "./setupInit";
|
||||
export { setupUninstall } from "./setupUninstall";
|
||||
export { setupInstall } from "./setupInstall";
|
||||
28
lib/inits/migrations/Migration.ts
Normal file
28
lib/inits/migrations/Migration.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { ManifestVersion } from "../../manifest/ManifestTypes";
|
||||
import { Effects } from "../../types";
|
||||
import { Utils } from "../../util";
|
||||
|
||||
export class Migration<Version extends ManifestVersion> {
|
||||
constructor(
|
||||
readonly options: {
|
||||
version: Version;
|
||||
up: (opts: { effects: Effects }) => Promise<void>;
|
||||
down: (opts: { effects: Effects }) => Promise<void>;
|
||||
},
|
||||
) {}
|
||||
static of<Version extends ManifestVersion>(options: {
|
||||
version: Version;
|
||||
up: (opts: { effects: Effects }) => Promise<void>;
|
||||
down: (opts: { effects: Effects }) => Promise<void>;
|
||||
}) {
|
||||
return new Migration(options);
|
||||
}
|
||||
|
||||
async up(opts: { effects: Effects }) {
|
||||
this.up(opts);
|
||||
}
|
||||
|
||||
async down(opts: { effects: Effects }) {
|
||||
this.down(opts);
|
||||
}
|
||||
}
|
||||
69
lib/inits/migrations/setupMigrations.ts
Normal file
69
lib/inits/migrations/setupMigrations.ts
Normal 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
24
lib/inits/setupInit.ts
Normal 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
24
lib/inits/setupInstall.ts
Normal 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);
|
||||
}
|
||||
24
lib/inits/setupUninstall.ts
Normal file
24
lib/inits/setupUninstall.ts
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user