diff --git a/CHANGELOG.md b/CHANGELOG.md index 877af85b..3a535e41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ keyword. `exports.run = __async__ (client, msg, [...args])`. command. ### Changed +- [[#296](https://github.com/dirigeants/komada/pull/296)] **[Update]** The JSON provider and schemaManager now uses atomics. - [[#293](https://github.com/dirigeants/komada/pull/293)] **[Performance]** Faster prefix check and resolve for prefixes stored inside an Array. - [[#262](https://github.com/dirigeants/komada/pull/262)] **[Performance && Cleanup]** Refactored several pieces. diff --git a/classes/schemaManager.js b/classes/schemaManager.js index 439989eb..7c1c864a 100644 --- a/classes/schemaManager.js +++ b/classes/schemaManager.js @@ -19,7 +19,7 @@ class SchemaManager { await fs.ensureDir(baseDir); this.filePath = resolve(baseDir, "schema.json"); const schema = await fs.readJSON(this.filePath) - .catch(() => fs.outputJSON(this.filePath, this.defaultDataSchema).then(() => this.defaultDataSchema)); + .catch(() => fs.outputJSONAtomic(this.filePath, this.defaultDataSchema).then(() => this.defaultDataSchema)); return this.validate(schema); } @@ -60,9 +60,9 @@ class SchemaManager { * @param {number} options.min The min value for the key (String.length for String, value for number). * @param {number} options.max The max value for the key (String.length for String, value for number). * @param {boolean} [force=false] Whether this change should modify all configurations or not. - * @returns {void} + * @returns {Promise} */ - add(key, options, force = false) { + async add(key, options, force = false) { if (key in this.schema) throw `The key ${key} already exists in the current schema.`; if (!options.type) throw "The option type is required."; if (!validTypes.includes(options.type)) throw `The type ${options.type} is not supported.`; @@ -79,21 +79,21 @@ class SchemaManager { if (this.settingGateway.sql) options.sql = this.settingGateway.sql.buildSingleSQLSchema(options); this.schema[key] = options; this.defaults[key] = options.default; - if (force) this.force("add", key); - return fs.outputJSON(this.filePath, this.schema); + if (force) await this.force("add", key); + return fs.outputJSONAtomic(this.filePath, this.schema); } /** * Remove a key from the schema. * @param {string} key The key to remove. * @param {boolean} [force=false] Whether this change should modify all configurations or not. - * @returns {void} + * @returns {Promise} */ - remove(key, force = false) { + async remove(key, force = false) { if (key === "prefix") throw "You can't remove the prefix key."; delete this.schema[key]; - if (force) this.force("delete", key); - return fs.outputJSON(this.filePath, this.schema); + if (force) await this.force("delete", key); + return fs.outputJSONAtomic(this.filePath, this.schema); } /** diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index c6bcdb8f..61d64f58 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "komada", - "version": "0.20.2", + "version": "0.20.3", "lockfileVersion": 1, "dependencies": { "acorn": { @@ -44,12 +44,8 @@ "ansi-regex": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", - "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" - }, - "ansi-styles": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", - "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=" + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=", + "dev": true }, "argparse": { "version": "1.0.9", @@ -112,9 +108,21 @@ "dev": true }, "chalk": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", - "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=" + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.0.1.tgz", + "integrity": "sha512-Mp+FXEI+FrwY/XYV45b2YD3E8i3HwnEAoFcM0qlZzq/RZ9RwWitt2Y/c7cqRAz70U7hfekqx6qNYthuKFO6K0g==", + "dependencies": { + "ansi-styles": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.1.0.tgz", + "integrity": "sha1-CcIC1ckX7CMYjKpcnLkXnNlUd1A=" + }, + "supports-color": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-4.1.0.tgz", + "integrity": "sha1-kswUuz2tiSjKVlbDPhmhnyCvXHo=" + } + } }, "circular-json": { "version": "0.3.1", @@ -146,6 +154,16 @@ "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=", "dev": true }, + "color-convert": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.0.tgz", + "integrity": "sha1-Gsz5fdc5uYO/mU1W/sj5WFNkG3o=" + }, + "color-name": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.2.tgz", + "integrity": "sha1-XIq3K2S9IhXWF66VWeuxSEdc+Y0=" + }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -195,7 +213,7 @@ "dev": true }, "discord.js": { - "version": "github:hydrabolt/discord.js#0a337a4646c2b906a195182bddfcdf4a774fbcbf" + "version": "github:hydrabolt/discord.js#62537b7debb004eb4bcd26a562f8204d0323439a" }, "doctrine": { "version": "2.0.0", @@ -373,9 +391,9 @@ "dev": true }, "fs-nextra": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/fs-nextra/-/fs-nextra-0.1.2.tgz", - "integrity": "sha512-v9nYPCI26zurCivzLSkSzoyMMWXfkLhHAKg1YozoZzyrJm2dqc+w+U8EM791Hyqju5CWGwwvKr5R+bFnMVaSZg==" + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/fs-nextra/-/fs-nextra-0.1.4.tgz", + "integrity": "sha512-bMqvWg+8b3mHhOZKx1UtSh0/l9KOXHqAEl/E9NgElYjmGZi2lkA6g9VZE6hDgvVBuUPp0qCBLzqd9NbgfVecgQ==" }, "fs.realpath": { "version": "1.0.0", @@ -431,10 +449,10 @@ "integrity": "sha1-hGFzP1OLCDfJNh45qauelwTcLyg=", "dev": true }, - "has-ansi": { + "has-flag": { "version": "2.0.0", - "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", - "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=" + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-2.0.0.tgz", + "integrity": "sha1-6CB68cx7MNRGzHC3NLXovhj4jVE=" }, "hosted-git-info": { "version": "2.4.2", @@ -968,7 +986,8 @@ "strip-ansi": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", - "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=" + "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", + "dev": true }, "strip-bom": { "version": "3.0.0", @@ -982,11 +1001,6 @@ "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=", "dev": true }, - "supports-color": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", - "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=" - }, "table": { "version": "3.8.3", "resolved": "https://registry.npmjs.org/table/-/table-3.8.3.tgz", diff --git a/package.json b/package.json index 7a596aed..62dcfd28 100644 --- a/package.json +++ b/package.json @@ -18,9 +18,9 @@ "test": "eslint classes commands finalizers functions inhibitors events extendables" }, "dependencies": { - "chalk": "^1.1.3", - "discord.js": "hydrabolt/discord.js", - "fs-nextra": "^0.1.2", + "chalk": "^2.0.1", + "discord.js": "github:hydrabolt/discord.js#master", + "fs-nextra": "^0.1.4", "moment": "^2.18.1", "moment-duration-format": "^1.3.0", "performance-now": "^2.1.0" diff --git a/providers/json.js b/providers/json.js index 798d501d..81b71e6f 100644 --- a/providers/json.js +++ b/providers/json.js @@ -76,7 +76,7 @@ exports.getRandom = table => this.getAll(table).then(data => data[Math.floor(Mat * @param {Object} data The object with all properties you want to insert into the document. * @returns {Promise} */ -exports.create = (table, document, data) => fs.outputJSON(resolve(baseDir, table, `${document}.json`), Object.assign(data, { id: document })); +exports.create = (table, document, data) => fs.outputJSONAtomic(resolve(baseDir, table, `${document}.json`), Object.assign(data, { id: document })); exports.set = (...args) => this.create(...args); exports.insert = (...args) => this.create(...args); @@ -88,7 +88,7 @@ exports.insert = (...args) => this.create(...args); * @returns {Promise} */ exports.update = (table, document, data) => this.get(table, document) - .then(current => fs.outputJSON(resolve(baseDir, table, `${document}.json`), Object.assign(current, data))); + .then(current => fs.outputJSONAtomic(resolve(baseDir, table, `${document}.json`), Object.assign(current, data))); /** * Replace all the data from a document. @@ -97,7 +97,7 @@ exports.update = (table, document, data) => this.get(table, document) * @param {Object} data The new data for the document. * @returns {Promise} */ -exports.replace = (table, document, data) => fs.outputJSON(resolve(baseDir, table, `${document}.json`), data); +exports.replace = (table, document, data) => fs.outputJSONAtomic(resolve(baseDir, table, `${document}.json`), data); /** * Delete a document from the table.