@sapphire/pieces
Advanced tools
Comparing version 2.1.1-next.de5de92.0 to 2.2.0-next.073222d.0
@@ -149,2 +149,7 @@ import Collection from '@discordjs/collection'; | ||
/** | ||
* Called after all pieces have been unloaded. | ||
* @param store The store that unloaded all pieces. | ||
*/ | ||
onUnloadAll(store: Store<T>): Awaited<unknown>; | ||
/** | ||
* @param error The error that was thrown. | ||
@@ -221,3 +226,3 @@ * @param path The path of the file that caused the error to be thrown. | ||
* @param path The path of the file to load. | ||
* @return An async iterator that yields each one of the loaded pieces. | ||
* @return All the loaded pieces. | ||
*/ | ||
@@ -232,2 +237,6 @@ load(path: string): Promise<T[]>; | ||
/** | ||
* Unloads all pieces from the store. | ||
*/ | ||
unloadAll(): Promise<T[]>; | ||
/** | ||
* Loads all pieces from all directories specified by {@link paths}. | ||
@@ -536,2 +545,3 @@ */ | ||
onUnload(): unknown; | ||
onUnloadAll(): unknown; | ||
onError(error: Error, path: string): void; | ||
@@ -538,0 +548,0 @@ } |
@@ -92,2 +92,5 @@ "use strict"; | ||
} | ||
onUnloadAll() { | ||
return undefined; | ||
} | ||
onError(error, path) { | ||
@@ -94,0 +97,0 @@ console.error(`Error when loading '${path}':`, error); |
@@ -10,3 +10,2 @@ "use strict"; | ||
constructor(context, options = {}) { | ||
var _a; | ||
super(context, options); | ||
@@ -22,3 +21,3 @@ /** | ||
}); | ||
this.aliases = (_a = options.aliases) !== null && _a !== void 0 ? _a : []; | ||
this.aliases = options.aliases ?? []; | ||
} | ||
@@ -25,0 +24,0 @@ /** |
@@ -28,4 +28,3 @@ "use strict"; | ||
get(key) { | ||
var _a; | ||
return (_a = super.get(key)) !== null && _a !== void 0 ? _a : this.aliases.get(key); | ||
return super.get(key) ?? this.aliases.get(key); | ||
} | ||
@@ -32,0 +31,0 @@ /** |
@@ -10,3 +10,2 @@ "use strict"; | ||
constructor(context, options = {}) { | ||
var _a, _b; | ||
/** | ||
@@ -50,4 +49,4 @@ * The store that contains the piece. | ||
this.path = context.path; | ||
this.name = (_a = options.name) !== null && _a !== void 0 ? _a : context.name; | ||
this.enabled = (_b = options.enabled) !== null && _b !== void 0 ? _b : true; | ||
this.name = options.name ?? context.name; | ||
this.enabled = options.enabled ?? true; | ||
} | ||
@@ -54,0 +53,0 @@ /** |
@@ -20,3 +20,2 @@ "use strict"; | ||
constructor(constructor, options) { | ||
var _a, _b; | ||
super(); | ||
@@ -49,4 +48,4 @@ Object.defineProperty(this, "Constructor", { | ||
this.name = options.name; | ||
this.paths = new Set((_a = options.paths) !== null && _a !== void 0 ? _a : []); | ||
this.strategy = (_b = options.strategy) !== null && _b !== void 0 ? _b : Store.defaultStrategy; | ||
this.paths = new Set(options.paths ?? []); | ||
this.strategy = options.strategy ?? Store.defaultStrategy; | ||
} | ||
@@ -71,5 +70,4 @@ /** | ||
registerPath(path) { | ||
var _a; | ||
this.paths.add(path); | ||
(_a = Store.logger) === null || _a === void 0 ? void 0 : _a.call(Store, `[STORE => ${this.name}] [REGISTER] Registered path '${path}'.`); | ||
Store.logger?.(`[STORE => ${this.name}] [REGISTER] Registered path '${path}'.`); | ||
return this; | ||
@@ -80,9 +78,8 @@ } | ||
* @param path The path of the file to load. | ||
* @return An async iterator that yields each one of the loaded pieces. | ||
* @return All the loaded pieces. | ||
*/ | ||
async load(path) { | ||
var _a; | ||
const data = this.strategy.filter(path); | ||
if (data === null) { | ||
(_a = Store.logger) === null || _a === void 0 ? void 0 : _a.call(Store, `[STORE => ${this.name}] [LOAD] Skipped piece '${path}' as 'LoaderStrategy#filter' returned 'null'.`); | ||
Store.logger?.(`[STORE => ${this.name}] [LOAD] Skipped piece '${path}' as 'LoaderStrategy#filter' returned 'null'.`); | ||
return []; | ||
@@ -102,3 +99,2 @@ } | ||
async unload(name) { | ||
var _a, _b; | ||
const piece = this.resolve(name); | ||
@@ -108,13 +104,25 @@ // Unload piece: | ||
await piece.onUnload(); | ||
(_a = Store.logger) === null || _a === void 0 ? void 0 : _a.call(Store, `[STORE => ${this.name}] [UNLOAD] Unloaded piece '${piece.name}'.`); | ||
Store.logger?.(`[STORE => ${this.name}] [UNLOAD] Unloaded piece '${piece.name}'.`); | ||
// Remove from cache and return it: | ||
this.delete(piece.name); | ||
(_b = Store.logger) === null || _b === void 0 ? void 0 : _b.call(Store, `[STORE => ${this.name}] [UNLOAD] Removed piece '${piece.name}'.`); | ||
Store.logger?.(`[STORE => ${this.name}] [UNLOAD] Removed piece '${piece.name}'.`); | ||
return piece; | ||
} | ||
/** | ||
* Unloads all pieces from the store. | ||
*/ | ||
async unloadAll() { | ||
const promises = []; | ||
for (const piece of this.values()) { | ||
promises.push(this.unload(piece)); | ||
} | ||
const results = await Promise.all(promises); | ||
this.strategy.onUnloadAll(this); | ||
Store.logger?.(`[STORE => ${this.name}] [UNLOAD-ALL] Removed all pieces.`); | ||
return results; | ||
} | ||
/** | ||
* Loads all pieces from all directories specified by {@link paths}. | ||
*/ | ||
async loadAll() { | ||
var _a, _b, _c; | ||
const pieces = []; | ||
@@ -126,6 +134,6 @@ for (const path of this.paths) { | ||
} | ||
(_a = Store.logger) === null || _a === void 0 ? void 0 : _a.call(Store, `[STORE => ${this.name}] [LOAD-ALL] Found '${pieces.length}' pieces.`); | ||
Store.logger?.(`[STORE => ${this.name}] [LOAD-ALL] Found '${pieces.length}' pieces.`); | ||
// Clear the store before inserting the new pieces: | ||
this.clear(); | ||
(_b = Store.logger) === null || _b === void 0 ? void 0 : _b.call(Store, `[STORE => ${this.name}] [LOAD-ALL] Cleared all pieces.`); | ||
await this.unloadAll(); | ||
Store.logger?.(`[STORE => ${this.name}] [LOAD-ALL] Cleared all pieces.`); | ||
// Load each piece: | ||
@@ -137,3 +145,3 @@ for (const piece of pieces) { | ||
this.strategy.onLoadAll(this); | ||
(_c = Store.logger) === null || _c === void 0 ? void 0 : _c.call(Store, `[STORE => ${this.name}] [LOAD-ALL] Successfully loaded '${this.size}' pieces.`); | ||
Store.logger?.(`[STORE => ${this.name}] [LOAD-ALL] Successfully loaded '${this.size}' pieces.`); | ||
} | ||
@@ -162,3 +170,2 @@ /** | ||
async insert(piece) { | ||
var _a, _b, _c, _d; | ||
if (!piece.enabled) | ||
@@ -169,3 +176,3 @@ return piece; | ||
await piece.onLoad(); | ||
(_a = Store.logger) === null || _a === void 0 ? void 0 : _a.call(Store, `[STORE => ${this.name}] [INSERT] Loaded new piece '${piece.name}'.`); | ||
Store.logger?.(`[STORE => ${this.name}] [INSERT] Loaded new piece '${piece.name}'.`); | ||
// If the onLoad disabled the piece, call unload and return it: | ||
@@ -176,3 +183,3 @@ if (!piece.enabled) { | ||
await piece.onUnload(); | ||
(_b = Store.logger) === null || _b === void 0 ? void 0 : _b.call(Store, `[STORE => ${this.name}] [INSERT] Unloaded new piece '${piece.name}' due to 'enabled' being 'false'.`); | ||
Store.logger?.(`[STORE => ${this.name}] [INSERT] Unloaded new piece '${piece.name}' due to 'enabled' being 'false'.`); | ||
return piece; | ||
@@ -184,7 +191,7 @@ } | ||
await this.unload(previous); | ||
(_c = Store.logger) === null || _c === void 0 ? void 0 : _c.call(Store, `[STORE => ${this.name}] [INSERT] Unloaded existing piece '${piece.name}' due to conflicting 'name'.`); | ||
Store.logger?.(`[STORE => ${this.name}] [INSERT] Unloaded existing piece '${piece.name}' due to conflicting 'name'.`); | ||
} | ||
// Set the new piece and return it: | ||
this.set(piece.name, piece); | ||
(_d = Store.logger) === null || _d === void 0 ? void 0 : _d.call(Store, `[STORE => ${this.name}] [INSERT] Inserted new piece '${piece.name}'.`); | ||
Store.logger?.(`[STORE => ${this.name}] [INSERT] Inserted new piece '${piece.name}'.`); | ||
return piece; | ||
@@ -207,8 +214,7 @@ } | ||
async *loadPath(directory) { | ||
var _a, _b; | ||
(_a = Store.logger) === null || _a === void 0 ? void 0 : _a.call(Store, `[STORE => ${this.name}] [WALK] Loading all pieces from '${directory}'.`); | ||
Store.logger?.(`[STORE => ${this.name}] [WALK] Loading all pieces from '${directory}'.`); | ||
for await (const child of this.walk(directory)) { | ||
const data = this.strategy.filter(child); | ||
if (data === null) { | ||
(_b = Store.logger) === null || _b === void 0 ? void 0 : _b.call(Store, `[STORE => ${this.name}] [LOAD] Skipped piece '${child}' as 'LoaderStrategy#filter' returned 'null'.`); | ||
Store.logger?.(`[STORE => ${this.name}] [LOAD] Skipped piece '${child}' as 'LoaderStrategy#filter' returned 'null'.`); | ||
continue; | ||
@@ -232,4 +238,3 @@ } | ||
async *walk(path) { | ||
var _a; | ||
(_a = Store.logger) === null || _a === void 0 ? void 0 : _a.call(Store, `[STORE => ${this.name}] [WALK] Loading all pieces from '${path}'.`); | ||
Store.logger?.(`[STORE => ${this.name}] [WALK] Loading all pieces from '${path}'.`); | ||
try { | ||
@@ -236,0 +241,0 @@ const dir = await fs_1.promises.opendir(path); |
{ | ||
"name": "@sapphire/pieces", | ||
"version": "2.1.1-next.de5de92.0", | ||
"version": "2.2.0-next.073222d.0", | ||
"description": "Sapphire's piece loader.", | ||
@@ -29,21 +29,22 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@discordjs/collection": "^0.1.6", | ||
"@sapphire/utilities": "^1.6.1", | ||
"tslib": "^2.3.0" | ||
"@discordjs/collection": "^0.2.1", | ||
"@sapphire/utilities": "^2.0.1", | ||
"tslib": "^2.3.1" | ||
}, | ||
"devDependencies": { | ||
"@commitlint/cli": "^12.1.4", | ||
"@commitlint/config-conventional": "^12.1.4", | ||
"@favware/rollup-type-bundler": "^1.0.2", | ||
"@sapphire/eslint-config": "^3.2.1", | ||
"@commitlint/cli": "^13.1.0", | ||
"@commitlint/config-conventional": "^13.1.0", | ||
"@favware/npm-deprecate": "^1.0.2", | ||
"@favware/rollup-type-bundler": "^1.0.3", | ||
"@sapphire/eslint-config": "^3.2.3", | ||
"@sapphire/prettier-config": "^1.1.6", | ||
"@sapphire/ts-config": "^2.3.1", | ||
"@types/node": "^16.3.3", | ||
"@sapphire/ts-config": "^3.0.0", | ||
"@types/node": "^16.6.2", | ||
"cz-conventional-changelog": "^3.3.0", | ||
"husky": "^7.0.1", | ||
"lint-staged": "^11.0.0", | ||
"lint-staged": "^11.1.2", | ||
"npm-run-all": "^4.1.5", | ||
"pretty-quick": "^3.1.1", | ||
"standard-version": "^9.3.0", | ||
"typedoc": "^0.21.4", | ||
"standard-version": "^9.3.1", | ||
"typedoc": "^0.21.5", | ||
"typedoc-plugin-nojekyll": "^1.0.1", | ||
@@ -50,0 +51,0 @@ "typescript": "^4.3.5" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1424
69817
17
+ Added@discordjs/collection@0.2.4(transitive)
+ Added@sapphire/utilities@2.0.1(transitive)
- Removed@discordjs/collection@0.1.6(transitive)
- Removed@sapphire/utilities@1.7.0(transitive)
Updated@discordjs/collection@^0.2.1
Updated@sapphire/utilities@^2.0.1
Updatedtslib@^2.3.1