From 1b9e2cf503b5918e073141901fa293529aebfdbc Mon Sep 17 00:00:00 2001 From: BluJ Date: Tue, 25 Apr 2023 14:36:43 -0600 Subject: [PATCH] chore: update the version and add the setupInit section --- lib/config/setupConfig.ts | 6 ++- lib/index.ts | 1 + lib/inits/index.ts | 3 ++ lib/{ => inits}/migrations/Migration.ts | 6 +-- lib/inits/migrations/setupMigrations.ts | 69 +++++++++++++++++++++++++ lib/inits/setupInit.ts | 24 +++++++++ lib/inits/setupInstall.ts | 24 +++++++++ lib/inits/setupUninstall.ts | 24 +++++++++ lib/migrations/setupMigrations.ts | 50 ------------------ package-lock.json | 4 +- package.json | 2 +- 11 files changed, 155 insertions(+), 58 deletions(-) create mode 100644 lib/inits/index.ts rename lib/{ => inits}/migrations/Migration.ts (81%) create mode 100644 lib/inits/migrations/setupMigrations.ts create mode 100644 lib/inits/setupInit.ts create mode 100644 lib/inits/setupInstall.ts create mode 100644 lib/inits/setupUninstall.ts delete mode 100644 lib/migrations/setupMigrations.ts diff --git a/lib/config/setupConfig.ts b/lib/config/setupConfig.ts index 5fa841b..3497f55 100644 --- a/lib/config/setupConfig.ts +++ b/lib/config/setupConfig.ts @@ -17,7 +17,7 @@ export type Save = (options: { export type Read = (options: { effects: Effects; utils: Utils; -}) => Promise>; +}) => Promise>; /** * 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>( getConfig: (async ({ effects, config }) => { return { spec: spec.build(), - config: nullIfEmpty(await read({ effects, utils: utils(effects) })), + config: nullIfEmpty( + (await read({ effects, utils: utils(effects) })) || null, + ), }; }) as ExpectedExports.getConfig, }; diff --git a/lib/index.ts b/lib/index.ts index 0c9fcdf..c01ca20 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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"; diff --git a/lib/inits/index.ts b/lib/inits/index.ts new file mode 100644 index 0000000..eac14a9 --- /dev/null +++ b/lib/inits/index.ts @@ -0,0 +1,3 @@ +export { setupInit } from "./setupInit"; +export { setupUninstall } from "./setupUninstall"; +export { setupInstall } from "./setupInstall"; diff --git a/lib/migrations/Migration.ts b/lib/inits/migrations/Migration.ts similarity index 81% rename from lib/migrations/Migration.ts rename to lib/inits/migrations/Migration.ts index d17a893..6f84ece 100644 --- a/lib/migrations/Migration.ts +++ b/lib/inits/migrations/Migration.ts @@ -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 { constructor( diff --git a/lib/inits/migrations/setupMigrations.ts b/lib/inits/migrations/setupMigrations.ts new file mode 100644 index 0000000..2913f93 --- /dev/null +++ b/lib/inits/migrations/setupMigrations.ts @@ -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>, + ) {} + private sortedMigrations = once(() => { + const migrationsAsVersions = (this.migrations as Array>).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>>( + manifest: GenericManifest, + ...migrations: EnsureUniqueId + ) { + return new Migrations(manifest, migrations as Array>); + } + async init({ + effects, + previousVersion, + }: Parameters[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[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>>( + manifest: GenericManifest, + ...migrations: EnsureUniqueId +) { + return Migrations.of(manifest, ...migrations); +} + +// prettier-ignore +export type EnsureUniqueId = + B extends [] ? A : + B extends [Migration, ...infer Rest] ? ( + id extends ids ? "One of the ids are not unique"[] : + EnsureUniqueId + ) : "There exists a migration that is not a Migration"[] diff --git a/lib/inits/setupInit.ts b/lib/inits/setupInit.ts new file mode 100644 index 0000000..5b1ccd3 --- /dev/null +++ b/lib/inits/setupInit.ts @@ -0,0 +1,24 @@ +import { ExpectedExports } from "../types"; +import { Migrations } from "./migrations/setupMigrations"; +import { Install } from "./setupInstall"; +import { Uninstall } from "./setupUninstall"; + +export function setupInit( + migrations: Migrations, + install: Install, + uninstall: Uninstall, +): { + 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); + }, + }; +} diff --git a/lib/inits/setupInstall.ts b/lib/inits/setupInstall.ts new file mode 100644 index 0000000..021f34e --- /dev/null +++ b/lib/inits/setupInstall.ts @@ -0,0 +1,24 @@ +import { Effects, ExpectedExports } from "../types"; +import { Utils, utils } from "../util"; + +export type InstallFn = (opts: { + effects: Effects; + utils: Utils; +}) => Promise; +export class Install { + private constructor(readonly fn: InstallFn) {} + static of(fn: InstallFn) { + return new Install(fn); + } + + async init({ + effects, + previousVersion, + }: Parameters[0]) { + if (!previousVersion) await this.fn({ effects, utils: utils(effects) }); + } +} + +export function setupInstall(fn: InstallFn) { + return Install.of(fn); +} diff --git a/lib/inits/setupUninstall.ts b/lib/inits/setupUninstall.ts new file mode 100644 index 0000000..ae99f35 --- /dev/null +++ b/lib/inits/setupUninstall.ts @@ -0,0 +1,24 @@ +import { Effects, ExpectedExports } from "../types"; +import { Utils, utils } from "../util"; + +export type UninstallFn = (opts: { + effects: Effects; + utils: Utils; +}) => Promise; +export class Uninstall { + private constructor(readonly fn: UninstallFn) {} + static of(fn: UninstallFn) { + return new Uninstall(fn); + } + + async uninit({ + effects, + nextVersion, + }: Parameters[0]) { + if (!nextVersion) await this.fn({ effects, utils: utils(effects) }); + } +} + +export function setupUninstall(fn: UninstallFn) { + return Uninstall.of(fn); +} diff --git a/lib/migrations/setupMigrations.ts b/lib/migrations/setupMigrations.ts deleted file mode 100644 index 8f4f542..0000000 --- a/lib/migrations/setupMigrations.ts +++ /dev/null @@ -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>>( - manifest: GenericManifest, - ...migrations: EnsureUniqueId -) { - const sortedMigrations = once(() => { - const migrationsAsVersions = (migrations as Array>).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 = - B extends [] ? A : - B extends [Migration, ...infer Rest] ? ( - id extends ids ? "One of the ids are not unique"[] : - EnsureUniqueId - ) : "There exists a migration that is not a Migration"[] diff --git a/package-lock.json b/package-lock.json index b539d7c..eb0b1c6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "start-sdk", - "version": "0.4.0-lib0.charlie38", + "version": "0.4.0-lib0.charlie39", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "start-sdk", - "version": "0.4.0-lib0.charlie38", + "version": "0.4.0-lib0.charlie39", "license": "MIT", "dependencies": { "@iarna/toml": "^2.2.5", diff --git a/package.json b/package.json index 4cb4940..4b9d7dd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "start-sdk", - "version": "0.4.0-lib0.charlie38", + "version": "0.4.0-lib0.charlie39", "description": "For making the patterns that are wanted in making services for the startOS.", "main": "./lib/index.js", "types": "./lib/index.d.ts",