@sapphire/pieces 4.2.3-pr-379.9f487c4.0 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +2 -1
  3. package/dist/cjs/index.d.cts +1 -1
  4. package/dist/cjs/lib/errors/LoaderError.cjs +1 -4
  5. package/dist/cjs/lib/errors/LoaderError.cjs.map +1 -1
  6. package/dist/cjs/lib/errors/MissingExportsError.cjs +1 -4
  7. package/dist/cjs/lib/errors/MissingExportsError.cjs.map +1 -1
  8. package/dist/cjs/lib/internal/Path.cjs +1 -2
  9. package/dist/cjs/lib/internal/Path.cjs.map +1 -1
  10. package/dist/cjs/lib/internal/RootScan.cjs +6 -12
  11. package/dist/cjs/lib/internal/RootScan.cjs.map +1 -1
  12. package/dist/cjs/lib/shared/Container.cjs.map +1 -1
  13. package/dist/cjs/lib/strategies/LoaderStrategy.cjs +11 -24
  14. package/dist/cjs/lib/strategies/LoaderStrategy.cjs.map +1 -1
  15. package/dist/cjs/lib/strategies/Shared.cjs +1 -2
  16. package/dist/cjs/lib/strategies/Shared.cjs.map +1 -1
  17. package/dist/cjs/lib/strategies/env.cjs +50 -0
  18. package/dist/cjs/lib/strategies/env.cjs.map +1 -0
  19. package/dist/cjs/lib/structures/AliasPiece.cjs +1 -4
  20. package/dist/cjs/lib/structures/AliasPiece.cjs.map +1 -1
  21. package/dist/cjs/lib/structures/AliasStore.cjs +2 -6
  22. package/dist/cjs/lib/structures/AliasStore.cjs.map +1 -1
  23. package/dist/cjs/lib/structures/Piece.cjs +1 -4
  24. package/dist/cjs/lib/structures/Piece.cjs.map +1 -1
  25. package/dist/cjs/lib/structures/PieceLocation.cjs +1 -4
  26. package/dist/cjs/lib/structures/PieceLocation.cjs.map +1 -1
  27. package/dist/cjs/lib/structures/Store.cjs +14 -32
  28. package/dist/cjs/lib/structures/Store.cjs.map +1 -1
  29. package/dist/cjs/lib/structures/StoreRegistry.cjs +6 -13
  30. package/dist/cjs/lib/structures/StoreRegistry.cjs.map +1 -1
  31. package/dist/esm/chunk-KFLDEQ5J.mjs +21 -0
  32. package/dist/esm/index.d.mts +1 -1
  33. package/dist/esm/index.mjs +1 -1
  34. package/dist/esm/lib/errors/LoaderError.mjs +1 -1
  35. package/dist/esm/lib/errors/MissingExportsError.mjs +1 -1
  36. package/dist/esm/lib/internal/Path.mjs +2 -3
  37. package/dist/esm/lib/internal/Path.mjs.map +1 -1
  38. package/dist/esm/lib/internal/RootScan.mjs +7 -13
  39. package/dist/esm/lib/internal/RootScan.mjs.map +1 -1
  40. package/dist/esm/lib/internal/constants.mjs +1 -1
  41. package/dist/esm/lib/shared/Container.mjs +1 -1
  42. package/dist/esm/lib/shared/Container.mjs.map +1 -1
  43. package/dist/esm/lib/strategies/LoaderStrategy.mjs +10 -19
  44. package/dist/esm/lib/strategies/LoaderStrategy.mjs.map +1 -1
  45. package/dist/esm/lib/strategies/Shared.mjs +2 -3
  46. package/dist/esm/lib/strategies/Shared.mjs.map +1 -1
  47. package/dist/esm/lib/strategies/env.mjs +47 -0
  48. package/dist/esm/lib/strategies/env.mjs.map +1 -0
  49. package/dist/esm/lib/structures/AliasPiece.mjs +1 -1
  50. package/dist/esm/lib/structures/AliasStore.mjs +2 -3
  51. package/dist/esm/lib/structures/AliasStore.mjs.map +1 -1
  52. package/dist/esm/lib/structures/Piece.mjs +1 -1
  53. package/dist/esm/lib/structures/PieceLocation.mjs +1 -1
  54. package/dist/esm/lib/structures/Store.mjs +7 -11
  55. package/dist/esm/lib/structures/Store.mjs.map +1 -1
  56. package/dist/esm/lib/structures/StoreRegistry.mjs +1 -1
  57. package/package.json +23 -23
  58. package/dist/esm/chunk-LIQV5WSN.mjs +0 -36
  59. /package/dist/esm/{chunk-LIQV5WSN.mjs.map → chunk-KFLDEQ5J.mjs.map} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/lib/strategies/env.ts"],"names":[],"mappings":";;;;;AAAA,SAAS,iBAAiB;AAE1B,SAAS,iBAAiB,MAAc;AACvC,SACC,QAAQ,SAAS,KAAK,CAAC,QAAQ,IAAI,SAAS,IAAI,CAAC;AAAA,EACjD,QAAQ,KAAK,KAAK,CAAC,QAAQ,IAAI,SAAS,IAAI,CAAC;AAE/C;AALS;AAOT,SAAS,oBAAoB,MAAc;AAC1C,SAAO,sBAAsB,WAAY,QAAQ,iBAA8B,SAAS,IAAI;AAC7F;AAFS;AAIT,SAAS,iBAAiB,MAAc,OAAgB;AACvD,SAAO,QAAQ,QAAQ,IAAI,IAAI,MAAM,QAAQ,CAAC,UAAU,QAAQ,IAAI,IAAI,CAAC;AAC1E;AAFS;AAqBF,IAAM;AAAA;AAAA;AAAA,EAGZ,QAAQ,IAAI,YAAY,MAAM;AAAA;AAAA,EAG9B,SAAS,QAAQ;AAAA;AAAA;AAAA,EAIjB,OAAO,IAAI,2BAA2B,KAAK;AAAA,EAE3C,iBAAiB,aAAa;AAAA,EAE9B,CAAC,UAAU,QAAQ,IAAI,WAAW;AAAA;AAAA,EAGlC,iBAAiB,YAAY;AAAA;AAAA,EAG7B,iBAAiB,UAAU,MAAM,KACjC,iBAAiB,kBAAkB;AAAA;AAAA,EAGnC,iBAAiB,gBAAgB;AAAA;AAAA,EAGjC,oBAAoB,eAAe;AAAA,EACnC,oBAAoB,oBAAoB,KACxC,iBAAiB,eAAe;AAAA;AAAA,EAGhC,oBAAoB,KAAK;AAAA;AAAA,EAGzB,oBAAoB,kBAAkB;AAAA;AAAA,EAGtC,oBAAoB,KAAK;AAAA","sourcesContent":["import { isNullish } from '@sapphire/utilities';\n\nfunction checkProcessArgv(name: string) {\n\treturn (\n\t\tprocess.execArgv.some((arg) => arg.includes(name)) || //\n\t\tprocess.argv.some((arg) => arg.includes(name))\n\t);\n}\n\nfunction checkPreloadModules(name: string) {\n\treturn '_preload_modules' in process && (process._preload_modules as string[]).includes(name);\n}\n\nfunction checkEnvVariable(name: string, value?: string) {\n\treturn value ? process.env[name] === value : !isNullish(process.env[name]);\n}\n\n/**\n * Whether or not the current environment can load TypeScript files. These\n * conditions are based on the most common tools and runtimes that support\n * loading TypeScript files directly.\n *\n * - {@linkplain https://www.npmjs.com/package/ts-node | `ts-node`}\n * - {@linkplain https://www.npmjs.com/package/ts-node-dev | `ts-node-dev`}\n * - {@linkplain https://www.npmjs.com/package/@babel/node | `@babel/node`}\n * - {@linkplain https://www.npmjs.com/package/vitest | `vitest`}\n * - {@linkplain https://www.npmjs.com/package/jest | `jest`}\n * - {@linkplain https://www.npmjs.com/package/@swc/cli | `swc`}\n * - {@linkplain https://www.npmjs.com/package/tsm | `tsm`}\n * - {@linkplain https://www.npmjs.com/package/esbuild | `esbuild`}\n * - {@linkplain https://www.npmjs.com/package/tsx | `tsx`}\n * - {@linkplain https://deno.com | `Deno`}\n * - {@linkplain https://bun.sh | `Bun`}\n */\nexport const CanLoadTypeScriptFiles: boolean =\n\t//\n\t// Deno\n\tReflect.has(globalThis, 'Deno') ||\n\t//\n\t// Bun\n\t'bun' in process.versions ||\n\t//\n\t// ts-node\n\t// > --require ts-node/register\n\tSymbol.for('ts-node.register.instance') in process ||\n\t// > --loader ts-node/esm\n\tcheckProcessArgv('ts-node/esm') ||\n\t// > ts-node-dev\n\t!isNullish(process.env.TS_NODE_DEV) ||\n\t//\n\t// @babel/node\n\tcheckProcessArgv('babel-node') ||\n\t//\n\t// vitest\n\tcheckEnvVariable('VITEST', 'true') ||\n\tcheckEnvVariable('VITEST_WORKER_ID') ||\n\t//\n\t// jest\n\tcheckEnvVariable('JEST_WORKER_ID') ||\n\t//\n\t// swc\n\tcheckPreloadModules('@swc/register') || //\n\tcheckPreloadModules('@swc-node/register') ||\n\tcheckProcessArgv('.bin/swc-node') ||\n\t//\n\t// tsm\n\tcheckPreloadModules('tsm') ||\n\t//\n\t// esbuild\n\tcheckPreloadModules('esbuild-register') ||\n\t//\n\t// tsx\n\tcheckPreloadModules('tsx');\n"]}
@@ -1,4 +1,4 @@
1
- import { __name, __publicField } from '../../chunk-LIQV5WSN.mjs';
1
+ import { __name, __publicField } from '../../chunk-KFLDEQ5J.mjs';
2
2
  import { Piece } from './Piece.mjs';
3
3
 
4
4
  var _AliasPiece = class _AliasPiece extends Piece {
@@ -1,4 +1,4 @@
1
- import { __name, __publicField } from '../../chunk-LIQV5WSN.mjs';
1
+ import { __name, __publicField } from '../../chunk-KFLDEQ5J.mjs';
2
2
  import { Collection } from '@discordjs/collection';
3
3
  import { Store } from './Store.mjs';
4
4
 
@@ -33,8 +33,7 @@ var _AliasStore = class _AliasStore extends Store {
33
33
  const piece = this.resolve(name);
34
34
  for (const alias of piece.aliases) {
35
35
  const aliasPiece = this.aliases.get(alias);
36
- if (aliasPiece === piece)
37
- this.aliases.delete(alias);
36
+ if (aliasPiece === piece) this.aliases.delete(alias);
38
37
  }
39
38
  return super.unload(piece);
40
39
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/structures/AliasStore.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,kBAAkB;AAE3B,SAAS,aAAa;AAMf,IAAM,cAAN,MAAM,oBAAgG,MAAoB;AAAA,EAA1H;AAAA;AAIN;AAAA;AAAA;AAAA,wBAAgB,WAAU,IAAI,WAAsB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMpC,IAAI,KAA4B;AAC/C,WAAO,MAAM,IAAI,GAAG,KAAK,KAAK,QAAQ,IAAI,GAAG;AAAA,EAC9C;AAAA;AAAA;AAAA;AAAA;AAAA,EAMgB,IAAI,KAAsB;AACzC,WAAO,MAAM,IAAI,GAAG,KAAK,KAAK,QAAQ,IAAI,GAAG;AAAA,EAC9C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOgB,OAAO,MAA8B;AACpD,UAAM,QAAQ,KAAK,QAAQ,IAAI;AAG/B,eAAW,SAAS,MAAM,SAAS;AAElC,YAAM,aAAa,KAAK,QAAQ,IAAI,KAAK;AACzC,UAAI,eAAe;AAAO,aAAK,QAAQ,OAAO,KAAK;AAAA,IACpD;AAEA,WAAO,MAAM,OAAO,KAAK;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAsB,OAAO,OAAU;AACtC,eAAW,OAAO,MAAM,SAAS;AAChC,WAAK,QAAQ,IAAI,KAAK,KAAK;AAAA,IAC5B;AAEA,WAAO,MAAM,OAAO,KAAK;AAAA,EAC1B;AACD;AApDiI;AAA1H,IAAM,aAAN","sourcesContent":["import { Collection } from '@discordjs/collection';\nimport type { AliasPiece } from './AliasPiece';\nimport { Store } from './Store';\nimport type { StoreRegistryKey } from './StoreRegistry';\n\n/**\n * The store class which contains {@link AliasPiece}s.\n */\nexport class AliasStore<T extends AliasPiece, StoreName extends StoreRegistryKey = StoreRegistryKey> extends Store<T, StoreName> {\n\t/**\n\t * The aliases referencing to pieces.\n\t */\n\tpublic readonly aliases = new Collection<string, T>();\n\n\t/**\n\t * Looks up the name by the store, falling back to an alias lookup.\n\t * @param key The key to look for.\n\t */\n\tpublic override get(key: string): T | undefined {\n\t\treturn super.get(key) ?? this.aliases.get(key);\n\t}\n\n\t/**\n\t * Checks whether a key is in the store, or is an alias\n\t * @param key The key to check\n\t */\n\tpublic override has(key: string): boolean {\n\t\treturn super.has(key) || this.aliases.has(key);\n\t}\n\n\t/**\n\t * Unloads a piece given its instance or its name, and removes all the aliases.\n\t * @param name The name of the file to load.\n\t * @return Returns the piece that was unloaded.\n\t */\n\tpublic override unload(name: string | T): Promise<T> {\n\t\tconst piece = this.resolve(name);\n\n\t\t// Unload all aliases for the given piece:\n\t\tfor (const alias of piece.aliases) {\n\t\t\t// We don't want to delete aliases that were overriden by another piece:\n\t\t\tconst aliasPiece = this.aliases.get(alias);\n\t\t\tif (aliasPiece === piece) this.aliases.delete(alias);\n\t\t}\n\n\t\treturn super.unload(piece);\n\t}\n\n\t/**\n\t * Inserts a piece into the store, and adds all the aliases.\n\t * @param piece The piece to be inserted into the store.\n\t * @return The inserted piece.\n\t */\n\tpublic override async insert(piece: T) {\n\t\tfor (const key of piece.aliases) {\n\t\t\tthis.aliases.set(key, piece);\n\t\t}\n\n\t\treturn super.insert(piece);\n\t}\n}\n"]}
1
+ {"version":3,"sources":["../../../../src/lib/structures/AliasStore.ts"],"names":[],"mappings":";;;;;;AAAA,SAAS,kBAAkB;AAE3B,SAAS,aAAa;AAMf,IAAM,cAAN,MAAM,oBAAgG,MAAoB;AAAA,EAA1H;AAAA;AAIN;AAAA;AAAA;AAAA,wBAAgB,WAAU,IAAI,WAAsB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMpC,IAAI,KAA4B;AAC/C,WAAO,MAAM,IAAI,GAAG,KAAK,KAAK,QAAQ,IAAI,GAAG;AAAA,EAC9C;AAAA;AAAA;AAAA;AAAA;AAAA,EAMgB,IAAI,KAAsB;AACzC,WAAO,MAAM,IAAI,GAAG,KAAK,KAAK,QAAQ,IAAI,GAAG;AAAA,EAC9C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOgB,OAAO,MAA8B;AACpD,UAAM,QAAQ,KAAK,QAAQ,IAAI;AAG/B,eAAW,SAAS,MAAM,SAAS;AAElC,YAAM,aAAa,KAAK,QAAQ,IAAI,KAAK;AACzC,UAAI,eAAe,MAAO,MAAK,QAAQ,OAAO,KAAK;AAAA,IACpD;AAEA,WAAO,MAAM,OAAO,KAAK;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAsB,OAAO,OAAU;AACtC,eAAW,OAAO,MAAM,SAAS;AAChC,WAAK,QAAQ,IAAI,KAAK,KAAK;AAAA,IAC5B;AAEA,WAAO,MAAM,OAAO,KAAK;AAAA,EAC1B;AACD;AApDiI;AAA1H,IAAM,aAAN","sourcesContent":["import { Collection } from '@discordjs/collection';\nimport type { AliasPiece } from './AliasPiece';\nimport { Store } from './Store';\nimport type { StoreRegistryKey } from './StoreRegistry';\n\n/**\n * The store class which contains {@link AliasPiece}s.\n */\nexport class AliasStore<T extends AliasPiece, StoreName extends StoreRegistryKey = StoreRegistryKey> extends Store<T, StoreName> {\n\t/**\n\t * The aliases referencing to pieces.\n\t */\n\tpublic readonly aliases = new Collection<string, T>();\n\n\t/**\n\t * Looks up the name by the store, falling back to an alias lookup.\n\t * @param key The key to look for.\n\t */\n\tpublic override get(key: string): T | undefined {\n\t\treturn super.get(key) ?? this.aliases.get(key);\n\t}\n\n\t/**\n\t * Checks whether a key is in the store, or is an alias\n\t * @param key The key to check\n\t */\n\tpublic override has(key: string): boolean {\n\t\treturn super.has(key) || this.aliases.has(key);\n\t}\n\n\t/**\n\t * Unloads a piece given its instance or its name, and removes all the aliases.\n\t * @param name The name of the file to load.\n\t * @return Returns the piece that was unloaded.\n\t */\n\tpublic override unload(name: string | T): Promise<T> {\n\t\tconst piece = this.resolve(name);\n\n\t\t// Unload all aliases for the given piece:\n\t\tfor (const alias of piece.aliases) {\n\t\t\t// We don't want to delete aliases that were overriden by another piece:\n\t\t\tconst aliasPiece = this.aliases.get(alias);\n\t\t\tif (aliasPiece === piece) this.aliases.delete(alias);\n\t\t}\n\n\t\treturn super.unload(piece);\n\t}\n\n\t/**\n\t * Inserts a piece into the store, and adds all the aliases.\n\t * @param piece The piece to be inserted into the store.\n\t * @return The inserted piece.\n\t */\n\tpublic override async insert(piece: T) {\n\t\tfor (const key of piece.aliases) {\n\t\t\tthis.aliases.set(key, piece);\n\t\t}\n\n\t\treturn super.insert(piece);\n\t}\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { __name, __publicField } from '../../chunk-LIQV5WSN.mjs';
1
+ import { __name, __publicField } from '../../chunk-KFLDEQ5J.mjs';
2
2
  import { container } from '../shared/Container.mjs';
3
3
  import { PieceLocation } from './PieceLocation.mjs';
4
4
 
@@ -1,4 +1,4 @@
1
- import { __name, __publicField } from '../../chunk-LIQV5WSN.mjs';
1
+ import { __name, __publicField } from '../../chunk-KFLDEQ5J.mjs';
2
2
  import { relative, sep, basename } from 'path';
3
3
  import { VirtualPath } from '../internal/constants.mjs';
4
4
 
@@ -1,4 +1,4 @@
1
- import { __name, __publicField, __privateAdd, __privateSet, __privateGet } from '../../chunk-LIQV5WSN.mjs';
1
+ import { __name, __publicField, __privateAdd, __privateSet, __privateGet } from '../../chunk-KFLDEQ5J.mjs';
2
2
  import { Collection } from '@discordjs/collection';
3
3
  import { isClass, classExtends } from '@sapphire/utilities';
4
4
  import { join } from 'path';
@@ -10,8 +10,8 @@ import { LoaderStrategy } from '../strategies/LoaderStrategy.mjs';
10
10
  import { StoreRegistry } from './StoreRegistry.mjs';
11
11
 
12
12
  var defaultStrategy = new LoaderStrategy();
13
- var _a, _calledLoadAll, _walk;
14
- var _Store = class _Store extends Collection {
13
+ var _a, _b, _calledLoadAll, _walk;
14
+ var _Store = class _Store extends (_b = Collection, _a = ManuallyRegisteredPiecesSymbol, _b) {
15
15
  /**
16
16
  * @param constructor The piece constructor this store loads.
17
17
  * @param options The options for the store.
@@ -33,7 +33,7 @@ var _Store = class _Store extends Collection {
33
33
  /**
34
34
  * The walk function for the store.
35
35
  */
36
- __privateAdd(this, _walk, void 0);
36
+ __privateAdd(this, _walk);
37
37
  this.Constructor = constructor;
38
38
  this.name = options.name;
39
39
  this.paths = new Set(options.paths ?? []);
@@ -203,12 +203,10 @@ var _Store = class _Store extends Collection {
203
203
  resolve(name) {
204
204
  if (typeof name === "string") {
205
205
  const result = this.get(name);
206
- if (typeof result === "undefined")
207
- throw new LoaderError(LoaderErrorType.UnloadedPiece, `The piece '${name}' does not exist.`);
206
+ if (typeof result === "undefined") throw new LoaderError(LoaderErrorType.UnloadedPiece, `The piece '${name}' does not exist.`);
208
207
  return result;
209
208
  }
210
- if (name instanceof this.Constructor)
211
- return name;
209
+ if (name instanceof this.Constructor) return name;
212
210
  throw new LoaderError(LoaderErrorType.IncorrectType, `The piece '${name.name}' is not an instance of '${this.Constructor.name}'.`);
213
211
  }
214
212
  /**
@@ -217,8 +215,7 @@ var _Store = class _Store extends Collection {
217
215
  * @return The inserted piece.
218
216
  */
219
217
  async insert(piece) {
220
- if (!piece.enabled)
221
- return piece;
218
+ if (!piece.enabled) return piece;
222
219
  this.strategy.onLoad(this, piece);
223
220
  await piece.onLoad();
224
221
  _Store.logger?.(`[STORE => ${this.name}] [INSERT] Loaded new piece '${piece.name}'.`);
@@ -279,7 +276,6 @@ var _Store = class _Store extends Collection {
279
276
  }
280
277
  }
281
278
  };
282
- _a = ManuallyRegisteredPiecesSymbol;
283
279
  _calledLoadAll = new WeakMap();
284
280
  _walk = new WeakMap();
285
281
  __name(_Store, "Store");
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/structures/Store.ts"],"names":["Store"],"mappings":";;;;;;;;;AAAA,SAAS,kBAAkB;AAC3B,SAAS,cAAc,eAA2D;AAClF,SAAS,YAAY;AACrB,SAAS,aAAa,uBAAuB;AAC7C,SAAS,mBAA8B;AACvC,SAAS,gCAAgC,mBAAmB;AAC5D,SAAS,iBAAiC;AAE1C,SAAS,sBAAsB;AAE/B,SAAS,qBAAuE;AAuChF,IAAM,kBAAkB,IAAI,eAAe;AAjD3C;AAsDO,IAAM,SAAN,MAAM,eAAsF,WAAsB;AAAA;AAAA;AAAA;AAAA;AAAA,EAyBjH,YAAY,aAAqC,SAAqC;AAC5F,UAAM;AAzBP,wBAAgB;AAChB,wBAAgB;AAChB,wBAAgB;AAChB,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAkB,IAAkC,oBAAI,IAAqD;AAK7G;AAAA;AAAA;AAAA,uCAAiB;AAKjB;AAAA;AAAA;AAAA;AAQC,SAAK,cAAc;AACnB,SAAK,OAAO,QAAQ;AACpB,SAAK,QAAQ,IAAI,IAAI,QAAQ,SAAS,CAAC,CAAC;AACxC,SAAK,WAAW,QAAQ,YAAY,OAAM;AAE1C,uBAAK,OACJ,OAAO,KAAK,SAAS,SAAS,aAC3B,KAAK,SAAS,KAAK,KAAK,KAAK,QAAQ,IACrC,gBAAgB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BAAiB,QAAQ,KAAK,SAAS,KAAK,MAAM,YAAY,GAAG;AAChE,gBAAM,KAAK,UAAU,MAAM,YAAY;AAAA,QACxC;AAAA,MACD,SAAS,OAAO;AACf,aAAK,SAAS,QAAQ,OAAgB,KAAK,IAAI;AAAA,MAChD;AAAA,IACD;AAAA,EACD;AAYD;AA/TmB;AAKlB;AAKA;AAnBwH;AAAA;AAAA;AAAA;AAAA;AAkUxH,cAlUY,QAkUE,mBAAwC;AAAA;AAAA;AAAA;AAKtD,cAvUY,QAuUE,UAA6B;AAvUrC,IAAM,QAAN;AAAA,CAmVA,CAAUA,WAAV;AACC,EAAMA,OAAA,WAAW;AAAA,GADR","sourcesContent":["import { Collection } from '@discordjs/collection';\nimport { classExtends, isClass, type AbstractConstructor, type Constructor } from '@sapphire/utilities';\nimport { join } from 'path';\nimport { LoaderError, LoaderErrorType } from '../errors/LoaderError';\nimport { resolvePath, type Path } from '../internal/Path';\nimport { ManuallyRegisteredPiecesSymbol, VirtualPath } from '../internal/constants';\nimport { container, type Container } from '../shared/Container';\nimport type { HydratedModuleData, ILoaderResultEntry, ILoaderStrategy, ModuleData } from '../strategies/ILoaderStrategy';\nimport { LoaderStrategy } from '../strategies/LoaderStrategy';\nimport type { Piece } from './Piece';\nimport { StoreRegistry, type StoreRegistryEntries, type StoreRegistryKey } from './StoreRegistry';\n\n/**\n * The options for the store, this features both hooks (changes the behaviour) and handlers (similar to event listeners).\n */\nexport interface StoreOptions<T extends Piece, StoreName extends StoreRegistryKey = StoreRegistryKey> {\n\t/**\n\t * The name for this store.\n\t */\n\treadonly name: StoreName;\n\n\t/**\n\t * The paths to load pieces from, should be absolute.\n\t * @default []\n\t */\n\treadonly paths?: readonly string[];\n\n\t/**\n\t * The strategy to be used for the loader.\n\t * @default Store.defaultStrategy\n\t */\n\treadonly strategy?: ILoaderStrategy<T>;\n}\n\n/**\n * An interface representing a logger function.\n */\nexport interface StoreLogger {\n\t/**\n\t * @param value The string to print. All strings will be formatted with the format `[STORE => ${name}] [${type}] ${content}`,\n\t * where the content may have identifiers (values or names of methods) surrounded by `'`. For example:\n\t *\n\t * - `[STORE => commands] [LOAD] Skipped piece '/home/user/bot/src/commands/foo.js' as 'LoaderStrategy#filter' returned 'null'.`\n\t * - `[STORE => commands] [INSERT] Unloaded new piece 'foo' due to 'enabled' being 'false'.`\n\t * - `[STORE => commands] [UNLOAD] Unloaded piece 'foo'.`\n\t */\n\t(value: string): void;\n}\n\nconst defaultStrategy = new LoaderStrategy();\n\n/**\n * The store class which contains {@link Piece}s.\n */\nexport class Store<T extends Piece, StoreName extends StoreRegistryKey = StoreRegistryKey> extends Collection<string, T> {\n\tpublic readonly Constructor: AbstractConstructor<T>;\n\tpublic readonly name: StoreName;\n\tpublic readonly paths: Set<string>;\n\tpublic readonly strategy: ILoaderStrategy<T>;\n\n\t/**\n\t * The queue of manually registered pieces to load.\n\t */\n\tprivate readonly [ManuallyRegisteredPiecesSymbol] = new Map<string, StoreManuallyRegisteredPiece<StoreName>>();\n\n\t/**\n\t * Whether or not the store has called `loadAll` at least once.\n\t */\n\t#calledLoadAll = false;\n\n\t/**\n\t * The walk function for the store.\n\t */\n\t#walk: LoaderStrategy<T>['walk'];\n\n\t/**\n\t * @param constructor The piece constructor this store loads.\n\t * @param options The options for the store.\n\t */\n\tpublic constructor(constructor: AbstractConstructor<T>, options: StoreOptions<T, StoreName>) {\n\t\tsuper();\n\t\tthis.Constructor = constructor;\n\t\tthis.name = options.name as StoreRegistryKey;\n\t\tthis.paths = new Set(options.paths ?? []);\n\t\tthis.strategy = options.strategy ?? Store.defaultStrategy;\n\n\t\tthis.#walk =\n\t\t\ttypeof this.strategy.walk === 'function' //\n\t\t\t\t? this.strategy.walk.bind(this.strategy)\n\t\t\t\t: defaultStrategy.walk.bind(defaultStrategy);\n\t}\n\n\t/**\n\t * A reference to the {@link Container} object for ease of use.\n\t * @see container\n\t */\n\tpublic get container(): Container {\n\t\treturn container;\n\t}\n\n\t/**\n\t * Registers a directory into the store.\n\t * @param path The path to be added.\n\t * @example\n\t * ```typescript\n\t * store\n\t * .registerPath(resolve('commands'))\n\t * .registerPath(resolve('third-party', 'commands'));\n\t * ```\n\t */\n\tpublic registerPath(path: Path): this {\n\t\tconst root = resolvePath(path);\n\n\t\tthis.paths.add(root);\n\t\tStore.logger?.(`[STORE => ${this.name}] [REGISTER] Registered path '${root}'.`);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Adds a piece into the store's list of manually registered pieces. If {@linkcode Store.loadAll()} was called, the\n\t * piece will be loaded immediately, otherwise it will be queued until {@linkcode Store.loadAll()} is called.\n\t *\n\t * All manually registered pieces will be kept even after they are loaded to ensure they can be loaded again if\n\t * {@linkcode Store.loadAll()} is called again.\n\t *\n\t * @remarks\n\t *\n\t * - Pieces loaded this way will have their {@linkcode Piece.Context.root root} and\n\t * {@linkcode Piece.Context.path path} set to {@linkcode VirtualPath}, and as such, cannot be reloaded.\n\t * - This method is useful in environments where file system access is limited or unavailable, such as when using\n\t * {@link https://en.wikipedia.org/wiki/Serverless_computing Serverless Computing}.\n\t * - This method will always throw a {@link TypeError} if `entry.piece` is not a class.\n\t * - This method will always throw a {@linkcode LoaderError} if the piece does not extend the\n\t * {@linkcode Store#Constructor store's piece constructor}.\n\t * - This operation is atomic, if any of the above errors are thrown, the piece will not be loaded.\n\t *\n\t * @seealso {@linkcode StoreRegistry.loadPiece()}\n\t * @since 3.8.0\n\t * @param entry The entry to load.\n\t * @example\n\t * ```typescript\n\t * import { container } from '@sapphire/pieces';\n\t *\n\t * class PingCommand extends Command {\n\t * // ...\n\t * }\n\t *\n\t * container.stores.get('commands').loadPiece({\n\t * name: 'ping',\n\t * piece: PingCommand\n\t * });\n\t * ```\n\t */\n\tpublic async loadPiece(entry: StoreManuallyRegisteredPiece<StoreName>) {\n\t\tif (!isClass(entry.piece)) {\n\t\t\tthrow new TypeError(`The piece ${entry.name} is not a Class. ${String(entry.piece)}`);\n\t\t}\n\n\t\t// If the piece does not extend the store's Piece class, throw an error:\n\t\tif (!classExtends(entry.piece, this.Constructor as Constructor<T>)) {\n\t\t\tthrow new LoaderError(LoaderErrorType.IncorrectType, `The piece ${entry.name} does not extend ${this.name}`);\n\t\t}\n\n\t\tthis[ManuallyRegisteredPiecesSymbol].set(entry.name, entry);\n\t\tif (this.#calledLoadAll) {\n\t\t\tconst piece = this.construct(entry.piece as unknown as Constructor<T>, {\n\t\t\t\tname: entry.name,\n\t\t\t\troot: VirtualPath,\n\t\t\t\tpath: VirtualPath,\n\t\t\t\textension: VirtualPath\n\t\t\t});\n\t\t\tawait this.insert(piece);\n\t\t}\n\t}\n\n\t/**\n\t * Loads one or more pieces from a path.\n\t * @param root The root directory the file is from.\n\t * @param path The path of the file to load, relative to the `root`.\n\t * @return All the loaded pieces.\n\t */\n\tpublic async load(root: string, path: string): Promise<T[]> {\n\t\tif (root === VirtualPath) {\n\t\t\tthrow new LoaderError(LoaderErrorType.VirtualPiece, `Cannot load a virtual file.`);\n\t\t}\n\n\t\tconst full = join(root, path);\n\t\tconst data = this.strategy.filter(full);\n\t\tif (data === null) {\n\t\t\tStore.logger?.(`[STORE => ${this.name}] [LOAD] Skipped piece '${full}' as 'LoaderStrategy#filter' returned 'null'.`);\n\t\t\treturn [];\n\t\t}\n\n\t\tconst promises: Promise<T>[] = [];\n\t\tconst finishedData = this.hydrateModuleData(root, data);\n\t\tfor await (const Ctor of this.strategy.load(this, finishedData)) {\n\t\t\tpromises.push(this.insert(this.construct(Ctor, finishedData)));\n\t\t}\n\n\t\treturn Promise.all(promises);\n\t}\n\n\t/**\n\t * Unloads a piece given its instance or its name.\n\t * @param name The name of the file to load.\n\t * @return Returns the piece that was unloaded.\n\t */\n\tpublic async unload(name: string | T): Promise<T> {\n\t\tconst piece = this.resolve(name);\n\n\t\t// Unload piece:\n\t\tthis.strategy.onUnload(this, piece);\n\t\tawait piece.onUnload();\n\t\tStore.logger?.(`[STORE => ${this.name}] [UNLOAD] Unloaded piece '${piece.name}'.`);\n\n\t\t// Remove from cache and return it:\n\t\tthis.delete(piece.name);\n\t\tStore.logger?.(`[STORE => ${this.name}] [UNLOAD] Removed piece '${piece.name}'.`);\n\t\treturn piece;\n\t}\n\n\t/**\n\t * Unloads all pieces from the store.\n\t */\n\tpublic async unloadAll(): Promise<T[]> {\n\t\tconst promises: Promise<T>[] = [];\n\t\tfor (const piece of this.values()) {\n\t\t\tpromises.push(this.unload(piece));\n\t\t}\n\n\t\tconst results = await Promise.all(promises);\n\n\t\tthis.strategy.onUnloadAll(this);\n\t\tStore.logger?.(`[STORE => ${this.name}] [UNLOAD-ALL] Removed all pieces.`);\n\t\treturn results;\n\t}\n\n\t/**\n\t * Loads all pieces from all directories specified by {@link paths}.\n\t */\n\tpublic async loadAll(): Promise<void> {\n\t\tthis.#calledLoadAll = true;\n\n\t\tconst pieces: T[] = [];\n\t\tfor (const entry of this[ManuallyRegisteredPiecesSymbol].values()) {\n\t\t\tconst piece = this.construct(entry.piece as unknown as Constructor<T>, {\n\t\t\t\tname: entry.name,\n\t\t\t\troot: VirtualPath,\n\t\t\t\tpath: VirtualPath,\n\t\t\t\textension: VirtualPath\n\t\t\t});\n\t\t\tpieces.push(piece);\n\t\t}\n\n\t\tfor (const path of this.paths) {\n\t\t\tfor await (const piece of this.loadPath(path)) {\n\t\t\t\tpieces.push(piece);\n\t\t\t}\n\t\t}\n\n\t\tStore.logger?.(`[STORE => ${this.name}] [LOAD-ALL] Found '${pieces.length}' pieces.`);\n\n\t\t// Clear the store before inserting the new pieces:\n\t\tawait this.unloadAll();\n\t\tStore.logger?.(`[STORE => ${this.name}] [LOAD-ALL] Cleared all pieces.`);\n\n\t\t// Load each piece:\n\t\tfor (const piece of pieces) {\n\t\t\tawait this.insert(piece);\n\t\t}\n\n\t\t// Call onLoadAll:\n\t\tthis.strategy.onLoadAll(this);\n\t\tStore.logger?.(`[STORE => ${this.name}] [LOAD-ALL] Successfully loaded '${this.size}' pieces.`);\n\t}\n\n\t/**\n\t * Resolves a piece by its name or its instance.\n\t * @param name The name of the piece or the instance itself.\n\t * @return The resolved piece.\n\t */\n\tpublic resolve(name: string | T): T {\n\t\tif (typeof name === 'string') {\n\t\t\tconst result = this.get(name);\n\t\t\tif (typeof result === 'undefined') throw new LoaderError(LoaderErrorType.UnloadedPiece, `The piece '${name}' does not exist.`);\n\t\t\treturn result;\n\t\t}\n\n\t\tif (name instanceof this.Constructor) return name;\n\t\tthrow new LoaderError(LoaderErrorType.IncorrectType, `The piece '${name.name}' is not an instance of '${this.Constructor.name}'.`);\n\t}\n\n\t/**\n\t * Inserts a piece into the store.\n\t * @param piece The piece to be inserted into the store.\n\t * @return The inserted piece.\n\t */\n\tpublic async insert(piece: T): Promise<T> {\n\t\tif (!piece.enabled) return piece;\n\n\t\t// Load piece:\n\t\tthis.strategy.onLoad(this, piece);\n\t\tawait piece.onLoad();\n\t\tStore.logger?.(`[STORE => ${this.name}] [INSERT] Loaded new piece '${piece.name}'.`);\n\n\t\t// If the onLoad disabled the piece, call unload and return it:\n\t\tif (!piece.enabled) {\n\t\t\t// Unload piece:\n\t\t\tthis.strategy.onUnload(this, piece);\n\t\t\tawait piece.onUnload();\n\t\t\tStore.logger?.(`[STORE => ${this.name}] [INSERT] Unloaded new piece '${piece.name}' due to 'enabled' being 'false'.`);\n\n\t\t\treturn piece;\n\t\t}\n\n\t\t// Unload existing piece, if any:\n\t\tconst previous = super.get(piece.name);\n\t\tif (previous) {\n\t\t\tawait this.unload(previous);\n\t\t\tStore.logger?.(`[STORE => ${this.name}] [INSERT] Unloaded existing piece '${piece.name}' due to conflicting 'name'.`);\n\t\t}\n\n\t\t// Set the new piece and return it:\n\t\tthis.set(piece.name, piece);\n\t\tStore.logger?.(`[STORE => ${this.name}] [INSERT] Inserted new piece '${piece.name}'.`);\n\t\treturn piece;\n\t}\n\n\t/**\n\t * Constructs a {@link Piece} instance.\n\t * @param Ctor The {@link Piece}'s constructor used to build the instance.\n\t * @param data The module's information\n\t * @return An instance of the constructed piece.\n\t */\n\tpublic construct(Ctor: ILoaderResultEntry<T>, data: HydratedModuleData): T {\n\t\treturn new Ctor({ store: this, root: data.root, path: data.path, name: data.name }, { name: data.name, enabled: true });\n\t}\n\n\t/**\n\t * Adds the final module data properties.\n\t * @param root The root directory to add.\n\t * @param data The module data returned from {@link ILoaderStrategy.filter}.\n\t * @returns The finished module data.\n\t */\n\tprivate hydrateModuleData(root: string, data: ModuleData): HydratedModuleData {\n\t\treturn { root, ...data };\n\t}\n\n\t/**\n\t * Loads a directory into the store.\n\t * @param root The directory to load the pieces from.\n\t * @return An async iterator that yields the pieces to be loaded into the store.\n\t */\n\tprivate async *loadPath(root: string): AsyncIterableIterator<T> {\n\t\tStore.logger?.(`[STORE => ${this.name}] [WALK] Loading all pieces from '${root}'.`);\n\t\tfor await (const child of this.#walk(this, root, Store.logger)) {\n\t\t\tconst data = this.strategy.filter(child);\n\t\t\tif (data === null) {\n\t\t\t\tStore.logger?.(`[STORE => ${this.name}] [LOAD] Skipped piece '${child}' as 'LoaderStrategy#filter' returned 'null'.`);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst finishedData = this.hydrateModuleData(root, data);\n\t\t\t\tfor await (const Ctor of this.strategy.load(this, finishedData)) {\n\t\t\t\t\tyield this.construct(Ctor, finishedData);\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tthis.strategy.onError(error as Error, data.path);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * The default strategy, defaults to {@link LoaderStrategy}, which is constructed on demand when a store is constructed,\n\t * when none was set beforehand.\n\t */\n\tpublic static defaultStrategy: ILoaderStrategy<any> = defaultStrategy;\n\n\t/**\n\t * The default logger, defaults to `null`.\n\t */\n\tpublic static logger: StoreLogger | null = null;\n}\n\n/**\n * An entry for a manually registered piece using {@linkcode Store.loadPiece()}.\n * @since 3.8.0\n */\nexport interface StoreManuallyRegisteredPiece<StoreName extends StoreRegistryKey> {\n\tname: string;\n\tpiece: StoreRegistryEntries[StoreName] extends Store<infer Piece> ? Constructor<Piece> : never;\n}\n\nexport namespace Store {\n\texport const Registry = StoreRegistry;\n\texport type Options<T extends Piece> = StoreOptions<T>;\n\texport type Logger = StoreLogger;\n\texport type RegistryEntries = StoreRegistryEntries;\n}\n"]}
1
+ {"version":3,"sources":["../../../../src/lib/structures/Store.ts"],"names":["Store"],"mappings":";;;;;;;;;AAAA,SAAS,kBAAkB;AAC3B,SAAS,cAAc,eAA2D;AAClF,SAAS,YAAY;AACrB,SAAS,aAAa,uBAAuB;AAC7C,SAAS,mBAA8B;AACvC,SAAS,gCAAgC,mBAAmB;AAC5D,SAAS,iBAAiC;AAE1C,SAAS,sBAAsB;AAE/B,SAAS,qBAAuE;AAuChF,IAAM,kBAAkB,IAAI,eAAe;AAjD3C;AAsDO,IAAM,SAAN,MAAM,gBAAsF,iBAShF,qCATgF,IAAsB;AAAA;AAAA;AAAA;AAAA;AAAA,EAyBjH,YAAY,aAAqC,SAAqC;AAC5F,UAAM;AAzBP,wBAAgB;AAChB,wBAAgB;AAChB,wBAAgB;AAChB,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAkB,IAAkC,oBAAI,IAAqD;AAK7G;AAAA;AAAA;AAAA,uCAAiB;AAKjB;AAAA;AAAA;AAAA;AAQC,SAAK,cAAc;AACnB,SAAK,OAAO,QAAQ;AACpB,SAAK,QAAQ,IAAI,IAAI,QAAQ,SAAS,CAAC,CAAC;AACxC,SAAK,WAAW,QAAQ,YAAY,OAAM;AAE1C,uBAAK,OACJ,OAAO,KAAK,SAAS,SAAS,aAC3B,KAAK,SAAS,KAAK,KAAK,KAAK,QAAQ,IACrC,gBAAgB,KAAK,KAAK,eAAe;AAAA,EAC9C;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAW,YAAuB;AACjC,WAAO;AAAA,EACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYO,aAAa,MAAkB;AACrC,UAAM,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YD;AA1TC;AAKA;AAnBwH;AAAA;AAAA;AAAA;AAAA;AAkUxH,cAlUY,QAkUE,mBAAwC;AAAA;AAAA;AAAA;AAKtD,cAvUY,QAuUE,UAA6B;AAvUrC,IAAM,QAAN;AAAA,CAmVA,CAAUA,WAAV;AACC,EAAMA,OAAA,WAAW;AAAA,GADR","sourcesContent":["import { Collection } from '@discordjs/collection';\nimport { classExtends, isClass, type AbstractConstructor, type Constructor } from '@sapphire/utilities';\nimport { join } from 'path';\nimport { LoaderError, LoaderErrorType } from '../errors/LoaderError';\nimport { resolvePath, type Path } from '../internal/Path';\nimport { ManuallyRegisteredPiecesSymbol, VirtualPath } from '../internal/constants';\nimport { container, type Container } from '../shared/Container';\nimport type { HydratedModuleData, ILoaderResultEntry, ILoaderStrategy, ModuleData } from '../strategies/ILoaderStrategy';\nimport { LoaderStrategy } from '../strategies/LoaderStrategy';\nimport type { Piece } from './Piece';\nimport { StoreRegistry, type StoreRegistryEntries, type StoreRegistryKey } from './StoreRegistry';\n\n/**\n * The options for the store, this features both hooks (changes the behaviour) and handlers (similar to event listeners).\n */\nexport interface StoreOptions<T extends Piece, StoreName extends StoreRegistryKey = StoreRegistryKey> {\n\t/**\n\t * The name for this store.\n\t */\n\treadonly name: StoreName;\n\n\t/**\n\t * The paths to load pieces from, should be absolute.\n\t * @default []\n\t */\n\treadonly paths?: readonly string[];\n\n\t/**\n\t * The strategy to be used for the loader.\n\t * @default Store.defaultStrategy\n\t */\n\treadonly strategy?: ILoaderStrategy<T>;\n}\n\n/**\n * An interface representing a logger function.\n */\nexport interface StoreLogger {\n\t/**\n\t * @param value The string to print. All strings will be formatted with the format `[STORE => ${name}] [${type}] ${content}`,\n\t * where the content may have identifiers (values or names of methods) surrounded by `'`. For example:\n\t *\n\t * - `[STORE => commands] [LOAD] Skipped piece '/home/user/bot/src/commands/foo.js' as 'LoaderStrategy#filter' returned 'null'.`\n\t * - `[STORE => commands] [INSERT] Unloaded new piece 'foo' due to 'enabled' being 'false'.`\n\t * - `[STORE => commands] [UNLOAD] Unloaded piece 'foo'.`\n\t */\n\t(value: string): void;\n}\n\nconst defaultStrategy = new LoaderStrategy();\n\n/**\n * The store class which contains {@link Piece}s.\n */\nexport class Store<T extends Piece, StoreName extends StoreRegistryKey = StoreRegistryKey> extends Collection<string, T> {\n\tpublic readonly Constructor: AbstractConstructor<T>;\n\tpublic readonly name: StoreName;\n\tpublic readonly paths: Set<string>;\n\tpublic readonly strategy: ILoaderStrategy<T>;\n\n\t/**\n\t * The queue of manually registered pieces to load.\n\t */\n\tprivate readonly [ManuallyRegisteredPiecesSymbol] = new Map<string, StoreManuallyRegisteredPiece<StoreName>>();\n\n\t/**\n\t * Whether or not the store has called `loadAll` at least once.\n\t */\n\t#calledLoadAll = false;\n\n\t/**\n\t * The walk function for the store.\n\t */\n\t#walk: LoaderStrategy<T>['walk'];\n\n\t/**\n\t * @param constructor The piece constructor this store loads.\n\t * @param options The options for the store.\n\t */\n\tpublic constructor(constructor: AbstractConstructor<T>, options: StoreOptions<T, StoreName>) {\n\t\tsuper();\n\t\tthis.Constructor = constructor;\n\t\tthis.name = options.name as StoreRegistryKey;\n\t\tthis.paths = new Set(options.paths ?? []);\n\t\tthis.strategy = options.strategy ?? Store.defaultStrategy;\n\n\t\tthis.#walk =\n\t\t\ttypeof this.strategy.walk === 'function' //\n\t\t\t\t? this.strategy.walk.bind(this.strategy)\n\t\t\t\t: defaultStrategy.walk.bind(defaultStrategy);\n\t}\n\n\t/**\n\t * A reference to the {@link Container} object for ease of use.\n\t * @see container\n\t */\n\tpublic get container(): Container {\n\t\treturn container;\n\t}\n\n\t/**\n\t * Registers a directory into the store.\n\t * @param path The path to be added.\n\t * @example\n\t * ```typescript\n\t * store\n\t * .registerPath(resolve('commands'))\n\t * .registerPath(resolve('third-party', 'commands'));\n\t * ```\n\t */\n\tpublic registerPath(path: Path): this {\n\t\tconst root = resolvePath(path);\n\n\t\tthis.paths.add(root);\n\t\tStore.logger?.(`[STORE => ${this.name}] [REGISTER] Registered path '${root}'.`);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Adds a piece into the store's list of manually registered pieces. If {@linkcode Store.loadAll()} was called, the\n\t * piece will be loaded immediately, otherwise it will be queued until {@linkcode Store.loadAll()} is called.\n\t *\n\t * All manually registered pieces will be kept even after they are loaded to ensure they can be loaded again if\n\t * {@linkcode Store.loadAll()} is called again.\n\t *\n\t * @remarks\n\t *\n\t * - Pieces loaded this way will have their {@linkcode Piece.Context.root root} and\n\t * {@linkcode Piece.Context.path path} set to {@linkcode VirtualPath}, and as such, cannot be reloaded.\n\t * - This method is useful in environments where file system access is limited or unavailable, such as when using\n\t * {@link https://en.wikipedia.org/wiki/Serverless_computing Serverless Computing}.\n\t * - This method will always throw a {@link TypeError} if `entry.piece` is not a class.\n\t * - This method will always throw a {@linkcode LoaderError} if the piece does not extend the\n\t * {@linkcode Store#Constructor store's piece constructor}.\n\t * - This operation is atomic, if any of the above errors are thrown, the piece will not be loaded.\n\t *\n\t * @seealso {@linkcode StoreRegistry.loadPiece()}\n\t * @since 3.8.0\n\t * @param entry The entry to load.\n\t * @example\n\t * ```typescript\n\t * import { container } from '@sapphire/pieces';\n\t *\n\t * class PingCommand extends Command {\n\t * // ...\n\t * }\n\t *\n\t * container.stores.get('commands').loadPiece({\n\t * name: 'ping',\n\t * piece: PingCommand\n\t * });\n\t * ```\n\t */\n\tpublic async loadPiece(entry: StoreManuallyRegisteredPiece<StoreName>) {\n\t\tif (!isClass(entry.piece)) {\n\t\t\tthrow new TypeError(`The piece ${entry.name} is not a Class. ${String(entry.piece)}`);\n\t\t}\n\n\t\t// If the piece does not extend the store's Piece class, throw an error:\n\t\tif (!classExtends(entry.piece, this.Constructor as Constructor<T>)) {\n\t\t\tthrow new LoaderError(LoaderErrorType.IncorrectType, `The piece ${entry.name} does not extend ${this.name}`);\n\t\t}\n\n\t\tthis[ManuallyRegisteredPiecesSymbol].set(entry.name, entry);\n\t\tif (this.#calledLoadAll) {\n\t\t\tconst piece = this.construct(entry.piece as unknown as Constructor<T>, {\n\t\t\t\tname: entry.name,\n\t\t\t\troot: VirtualPath,\n\t\t\t\tpath: VirtualPath,\n\t\t\t\textension: VirtualPath\n\t\t\t});\n\t\t\tawait this.insert(piece);\n\t\t}\n\t}\n\n\t/**\n\t * Loads one or more pieces from a path.\n\t * @param root The root directory the file is from.\n\t * @param path The path of the file to load, relative to the `root`.\n\t * @return All the loaded pieces.\n\t */\n\tpublic async load(root: string, path: string): Promise<T[]> {\n\t\tif (root === VirtualPath) {\n\t\t\tthrow new LoaderError(LoaderErrorType.VirtualPiece, `Cannot load a virtual file.`);\n\t\t}\n\n\t\tconst full = join(root, path);\n\t\tconst data = this.strategy.filter(full);\n\t\tif (data === null) {\n\t\t\tStore.logger?.(`[STORE => ${this.name}] [LOAD] Skipped piece '${full}' as 'LoaderStrategy#filter' returned 'null'.`);\n\t\t\treturn [];\n\t\t}\n\n\t\tconst promises: Promise<T>[] = [];\n\t\tconst finishedData = this.hydrateModuleData(root, data);\n\t\tfor await (const Ctor of this.strategy.load(this, finishedData)) {\n\t\t\tpromises.push(this.insert(this.construct(Ctor, finishedData)));\n\t\t}\n\n\t\treturn Promise.all(promises);\n\t}\n\n\t/**\n\t * Unloads a piece given its instance or its name.\n\t * @param name The name of the file to load.\n\t * @return Returns the piece that was unloaded.\n\t */\n\tpublic async unload(name: string | T): Promise<T> {\n\t\tconst piece = this.resolve(name);\n\n\t\t// Unload piece:\n\t\tthis.strategy.onUnload(this, piece);\n\t\tawait piece.onUnload();\n\t\tStore.logger?.(`[STORE => ${this.name}] [UNLOAD] Unloaded piece '${piece.name}'.`);\n\n\t\t// Remove from cache and return it:\n\t\tthis.delete(piece.name);\n\t\tStore.logger?.(`[STORE => ${this.name}] [UNLOAD] Removed piece '${piece.name}'.`);\n\t\treturn piece;\n\t}\n\n\t/**\n\t * Unloads all pieces from the store.\n\t */\n\tpublic async unloadAll(): Promise<T[]> {\n\t\tconst promises: Promise<T>[] = [];\n\t\tfor (const piece of this.values()) {\n\t\t\tpromises.push(this.unload(piece));\n\t\t}\n\n\t\tconst results = await Promise.all(promises);\n\n\t\tthis.strategy.onUnloadAll(this);\n\t\tStore.logger?.(`[STORE => ${this.name}] [UNLOAD-ALL] Removed all pieces.`);\n\t\treturn results;\n\t}\n\n\t/**\n\t * Loads all pieces from all directories specified by {@link paths}.\n\t */\n\tpublic async loadAll(): Promise<void> {\n\t\tthis.#calledLoadAll = true;\n\n\t\tconst pieces: T[] = [];\n\t\tfor (const entry of this[ManuallyRegisteredPiecesSymbol].values()) {\n\t\t\tconst piece = this.construct(entry.piece as unknown as Constructor<T>, {\n\t\t\t\tname: entry.name,\n\t\t\t\troot: VirtualPath,\n\t\t\t\tpath: VirtualPath,\n\t\t\t\textension: VirtualPath\n\t\t\t});\n\t\t\tpieces.push(piece);\n\t\t}\n\n\t\tfor (const path of this.paths) {\n\t\t\tfor await (const piece of this.loadPath(path)) {\n\t\t\t\tpieces.push(piece);\n\t\t\t}\n\t\t}\n\n\t\tStore.logger?.(`[STORE => ${this.name}] [LOAD-ALL] Found '${pieces.length}' pieces.`);\n\n\t\t// Clear the store before inserting the new pieces:\n\t\tawait this.unloadAll();\n\t\tStore.logger?.(`[STORE => ${this.name}] [LOAD-ALL] Cleared all pieces.`);\n\n\t\t// Load each piece:\n\t\tfor (const piece of pieces) {\n\t\t\tawait this.insert(piece);\n\t\t}\n\n\t\t// Call onLoadAll:\n\t\tthis.strategy.onLoadAll(this);\n\t\tStore.logger?.(`[STORE => ${this.name}] [LOAD-ALL] Successfully loaded '${this.size}' pieces.`);\n\t}\n\n\t/**\n\t * Resolves a piece by its name or its instance.\n\t * @param name The name of the piece or the instance itself.\n\t * @return The resolved piece.\n\t */\n\tpublic resolve(name: string | T): T {\n\t\tif (typeof name === 'string') {\n\t\t\tconst result = this.get(name);\n\t\t\tif (typeof result === 'undefined') throw new LoaderError(LoaderErrorType.UnloadedPiece, `The piece '${name}' does not exist.`);\n\t\t\treturn result;\n\t\t}\n\n\t\tif (name instanceof this.Constructor) return name;\n\t\tthrow new LoaderError(LoaderErrorType.IncorrectType, `The piece '${name.name}' is not an instance of '${this.Constructor.name}'.`);\n\t}\n\n\t/**\n\t * Inserts a piece into the store.\n\t * @param piece The piece to be inserted into the store.\n\t * @return The inserted piece.\n\t */\n\tpublic async insert(piece: T): Promise<T> {\n\t\tif (!piece.enabled) return piece;\n\n\t\t// Load piece:\n\t\tthis.strategy.onLoad(this, piece);\n\t\tawait piece.onLoad();\n\t\tStore.logger?.(`[STORE => ${this.name}] [INSERT] Loaded new piece '${piece.name}'.`);\n\n\t\t// If the onLoad disabled the piece, call unload and return it:\n\t\tif (!piece.enabled) {\n\t\t\t// Unload piece:\n\t\t\tthis.strategy.onUnload(this, piece);\n\t\t\tawait piece.onUnload();\n\t\t\tStore.logger?.(`[STORE => ${this.name}] [INSERT] Unloaded new piece '${piece.name}' due to 'enabled' being 'false'.`);\n\n\t\t\treturn piece;\n\t\t}\n\n\t\t// Unload existing piece, if any:\n\t\tconst previous = super.get(piece.name);\n\t\tif (previous) {\n\t\t\tawait this.unload(previous);\n\t\t\tStore.logger?.(`[STORE => ${this.name}] [INSERT] Unloaded existing piece '${piece.name}' due to conflicting 'name'.`);\n\t\t}\n\n\t\t// Set the new piece and return it:\n\t\tthis.set(piece.name, piece);\n\t\tStore.logger?.(`[STORE => ${this.name}] [INSERT] Inserted new piece '${piece.name}'.`);\n\t\treturn piece;\n\t}\n\n\t/**\n\t * Constructs a {@link Piece} instance.\n\t * @param Ctor The {@link Piece}'s constructor used to build the instance.\n\t * @param data The module's information\n\t * @return An instance of the constructed piece.\n\t */\n\tpublic construct(Ctor: ILoaderResultEntry<T>, data: HydratedModuleData): T {\n\t\treturn new Ctor({ store: this, root: data.root, path: data.path, name: data.name }, { name: data.name, enabled: true });\n\t}\n\n\t/**\n\t * Adds the final module data properties.\n\t * @param root The root directory to add.\n\t * @param data The module data returned from {@link ILoaderStrategy.filter}.\n\t * @returns The finished module data.\n\t */\n\tprivate hydrateModuleData(root: string, data: ModuleData): HydratedModuleData {\n\t\treturn { root, ...data };\n\t}\n\n\t/**\n\t * Loads a directory into the store.\n\t * @param root The directory to load the pieces from.\n\t * @return An async iterator that yields the pieces to be loaded into the store.\n\t */\n\tprivate async *loadPath(root: string): AsyncIterableIterator<T> {\n\t\tStore.logger?.(`[STORE => ${this.name}] [WALK] Loading all pieces from '${root}'.`);\n\t\tfor await (const child of this.#walk(this, root, Store.logger)) {\n\t\t\tconst data = this.strategy.filter(child);\n\t\t\tif (data === null) {\n\t\t\t\tStore.logger?.(`[STORE => ${this.name}] [LOAD] Skipped piece '${child}' as 'LoaderStrategy#filter' returned 'null'.`);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst finishedData = this.hydrateModuleData(root, data);\n\t\t\t\tfor await (const Ctor of this.strategy.load(this, finishedData)) {\n\t\t\t\t\tyield this.construct(Ctor, finishedData);\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tthis.strategy.onError(error as Error, data.path);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * The default strategy, defaults to {@link LoaderStrategy}, which is constructed on demand when a store is constructed,\n\t * when none was set beforehand.\n\t */\n\tpublic static defaultStrategy: ILoaderStrategy<any> = defaultStrategy;\n\n\t/**\n\t * The default logger, defaults to `null`.\n\t */\n\tpublic static logger: StoreLogger | null = null;\n}\n\n/**\n * An entry for a manually registered piece using {@linkcode Store.loadPiece()}.\n * @since 3.8.0\n */\nexport interface StoreManuallyRegisteredPiece<StoreName extends StoreRegistryKey> {\n\tname: string;\n\tpiece: StoreRegistryEntries[StoreName] extends Store<infer Piece> ? Constructor<Piece> : never;\n}\n\nexport namespace Store {\n\texport const Registry = StoreRegistry;\n\texport type Options<T extends Piece> = StoreOptions<T>;\n\texport type Logger = StoreLogger;\n\texport type RegistryEntries = StoreRegistryEntries;\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { __name, __privateAdd, __privateGet } from '../../chunk-LIQV5WSN.mjs';
1
+ import { __name, __privateAdd, __privateGet } from '../../chunk-KFLDEQ5J.mjs';
2
2
  import { Collection } from '@discordjs/collection';
3
3
  import { isClass } from '@sapphire/utilities';
4
4
  import { join } from 'path';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sapphire/pieces",
3
- "version": "4.2.3-pr-379.9f487c4.0",
3
+ "version": "4.3.0",
4
4
  "description": "Sapphire's piece loader.",
5
5
  "main": "dist/cjs/index.cjs",
6
6
  "module": "dist/esm/index.mjs",
@@ -35,35 +35,35 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "@discordjs/collection": "^1.5.3",
38
- "@sapphire/utilities": "^3.15.3",
39
- "tslib": "^2.6.2"
38
+ "@sapphire/utilities": "^3.16.2",
39
+ "tslib": "^2.6.3"
40
40
  },
41
41
  "devDependencies": {
42
- "@commitlint/cli": "^18.4.4",
43
- "@commitlint/config-conventional": "^18.4.4",
44
- "@favware/cliff-jumper": "^2.2.3",
42
+ "@commitlint/cli": "^19.3.0",
43
+ "@commitlint/config-conventional": "^19.2.2",
44
+ "@favware/cliff-jumper": "^4.0.2",
45
45
  "@favware/npm-deprecate": "^1.0.7",
46
46
  "@favware/rollup-type-bundler": "^3.3.0",
47
- "@sapphire/eslint-config": "^5.0.3",
47
+ "@sapphire/eslint-config": "^5.0.5",
48
48
  "@sapphire/prettier-config": "^2.0.0",
49
- "@sapphire/ts-config": "^5.0.0",
50
- "@types/node": "^20.11.5",
51
- "@typescript-eslint/eslint-plugin": "^6.19.0",
52
- "@typescript-eslint/parser": "^6.19.0",
49
+ "@sapphire/ts-config": "^5.0.1",
50
+ "@types/node": "^20.14.9",
51
+ "@typescript-eslint/eslint-plugin": "^7.13.1",
52
+ "@typescript-eslint/parser": "^7.13.1",
53
53
  "concurrently": "^8.2.2",
54
54
  "cz-conventional-changelog": "^3.3.0",
55
- "esbuild-plugin-file-path-extensions": "^2.0.0",
56
- "eslint": "^8.56.0",
55
+ "esbuild-plugin-file-path-extensions": "^2.1.2",
56
+ "eslint": "^8.57.0",
57
57
  "eslint-config-prettier": "^9.1.0",
58
58
  "eslint-plugin-prettier": "^5.1.3",
59
- "lint-staged": "^15.2.0",
60
- "prettier": "^3.2.4",
61
- "rimraf": "^5.0.5",
62
- "tsup": "^8.0.1",
63
- "typedoc": "^0.25.7",
64
- "typedoc-json-parser": "^9.0.1",
65
- "typescript": "^5.3.3",
66
- "vitest": "^1.2.1"
59
+ "lint-staged": "^15.2.7",
60
+ "prettier": "^3.3.2",
61
+ "rimraf": "^5.0.7",
62
+ "tsup": "^8.1.0",
63
+ "typedoc": "^0.26.1",
64
+ "typedoc-json-parser": "^10.0.0",
65
+ "typescript": "^5.5.2",
66
+ "vitest": "^1.6.0"
67
67
  },
68
68
  "repository": {
69
69
  "type": "git",
@@ -107,10 +107,10 @@
107
107
  "access": "public"
108
108
  },
109
109
  "resolutions": {
110
- "acorn": "^8.11.3",
110
+ "acorn": "^8.12.0",
111
111
  "ansi-regex": "^5.0.1",
112
112
  "minimist": "^1.2.8"
113
113
  },
114
114
  "prettier": "@sapphire/prettier-config",
115
- "packageManager": "yarn@4.0.2"
115
+ "packageManager": "yarn@4.3.1"
116
116
  }
@@ -1,36 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
4
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
5
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
6
- }) : x)(function(x) {
7
- if (typeof require !== "undefined")
8
- return require.apply(this, arguments);
9
- throw Error('Dynamic require of "' + x + '" is not supported');
10
- });
11
- var __publicField = (obj, key, value) => {
12
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
13
- return value;
14
- };
15
- var __accessCheck = (obj, member, msg) => {
16
- if (!member.has(obj))
17
- throw TypeError("Cannot " + msg);
18
- };
19
- var __privateGet = (obj, member, getter) => {
20
- __accessCheck(obj, member, "read from private field");
21
- return getter ? getter.call(obj) : member.get(obj);
22
- };
23
- var __privateAdd = (obj, member, value) => {
24
- if (member.has(obj))
25
- throw TypeError("Cannot add the same private member more than once");
26
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
27
- };
28
- var __privateSet = (obj, member, value, setter) => {
29
- __accessCheck(obj, member, "write to private field");
30
- setter ? setter.call(obj, value) : member.set(obj, value);
31
- return value;
32
- };
33
-
34
- export { __name, __privateAdd, __privateGet, __privateSet, __publicField, __require };
35
- //# sourceMappingURL=out.js.map
36
- //# sourceMappingURL=chunk-LIQV5WSN.mjs.map