diff --git a/.gitignore b/.gitignore index e09a007e..8e604d0a 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ data.json # Exclude macOS Finder (System Explorer) View States .DS_Store +meta.json diff --git a/esbuild.config.mjs b/esbuild.config.mjs index ac5250b0..2d727575 100644 --- a/esbuild.config.mjs +++ b/esbuild.config.mjs @@ -3,6 +3,7 @@ import process from "process"; import builtins from "builtin-modules"; import esbuildSvelte from "esbuild-svelte"; import sveltePreprocess from "svelte-preprocess"; +import fs from 'node:fs'; const banner = `/* THIS IS A GENERATED/BUNDLED FILE BY ESBUILD @@ -13,44 +14,46 @@ if you want to view the source, please visit the github repository of this plugi const prod = process.argv[2] === "production"; const context = await esbuild.context({ - banner: { - js: banner, - }, - entryPoints: ["src/main.ts"], - bundle: true, - plugins: [ - esbuildSvelte({ - compilerOptions: { css: "injected" }, - preprocess: sveltePreprocess(), - }), - ], - external: [ - "obsidian", - "electron", - "@codemirror/autocomplete", - "@codemirror/collab", - "@codemirror/commands", - "@codemirror/language", - "@codemirror/lint", - "@codemirror/search", - "@codemirror/state", - "@codemirror/view", - "@lezer/common", - "@lezer/highlight", - "@lezer/lr", - ...builtins, - ], - format: "cjs", - target: "es2018", - logLevel: "info", - sourcemap: prod ? false : "inline", - treeShaking: true, - outfile: "main.js", + banner: { + js: banner, + }, + entryPoints: ["src/main.ts"], + bundle: true, + metafile: true, + plugins: [ + esbuildSvelte({ + compilerOptions: { css: "injected" }, + preprocess: sveltePreprocess(), + }), + ], + external: [ + "obsidian", + "electron", + "@codemirror/autocomplete", + "@codemirror/collab", + "@codemirror/commands", + "@codemirror/language", + "@codemirror/lint", + "@codemirror/search", + "@codemirror/state", + "@codemirror/view", + "@lezer/common", + "@lezer/highlight", + "@lezer/lr", + ...builtins, + ], + format: "cjs", + target: "es2018", + logLevel: "info", + sourcemap: prod ? false : "inline", + treeShaking: true, + outfile: "main.js", }); if (prod) { - await context.rebuild(); - process.exit(0); + const result = await context.rebuild(); + fs.writeFileSync('meta.json', JSON.stringify(result.metafile)) + process.exit(0); } else { - await context.watch(); + await context.watch(); } diff --git a/src/core/objectSelect.ts b/src/core/objectSelect.ts index 6b007ea4..49470f77 100644 --- a/src/core/objectSelect.ts +++ b/src/core/objectSelect.ts @@ -1,21 +1,28 @@ import { E, parse, pipe } from "@std"; import * as O from "fp-ts/Option"; import * as NEA from "fp-ts/NonEmptyArray"; -import { filterWithIndex } from "fp-ts/lib/Record"; +import { filterWithIndex } from "fp-ts/Record"; import { object, optional, array, string, coerce } from "valibot"; +import { NonEmptyArray } from "fp-ts/NonEmptyArray"; const KeysSchema = array(coerce(string(), String)) const PickOmitSchema = object({ - pick: optional(KeysSchema), - omit: optional(KeysSchema), + pick: optional(KeysSchema), + omit: optional(KeysSchema), }); function picKeys(obj: Record) { - return (keys: string[]) => - pipe(obj, - filterWithIndex((k) => keys.includes(k)) - ); + return (keys: NonEmptyArray) => + pipe(obj, + filterWithIndex((k) => keys.includes(k)) + ); +} +function omitKeys(obj: Record) { + return (keys: NonEmptyArray) => + pipe(obj, + filterWithIndex((k) => !keys.includes(k)) + ); } /** @@ -26,24 +33,24 @@ function picKeys(obj: Record) { * @param opts the options for picking/omitting based on key names */ export function objectSelect(obj: Record, opts: unknown): Record { - return pipe( - parse(PickOmitSchema, opts, { abortEarly: true }), - E.map((opts) => { - const picked = pipe( - O.fromNullable(opts.pick), - O.flatMap(NEA.fromArray), - O.map(picKeys(obj)), - O.getOrElse(() => obj) - ); - return pipe( - O.fromNullable(opts.omit), - O.map((omit) => - filterWithIndex((k) => !omit.includes(k))(picked)), - O.getOrElse(() => picked) - ) - } - ), - E.getOrElse(() => obj) - ) + return pipe( + parse(PickOmitSchema, opts, { abortEarly: true }), + E.map((opts) => { + const picked = pipe( + O.fromNullable(opts.pick), + O.flatMap(NEA.fromArray), + O.map(picKeys(obj)), + O.getOrElse(() => obj) + ); + return pipe( + O.fromNullable(opts.omit), + O.flatMap(NEA.fromArray), + O.map(omitKeys(picked)), + O.getOrElse(() => picked) + ) + } + ), + E.getOrElse(() => obj) + ) }