defaultValue = structuredClone(defaultValue)
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
node_modules
|
node_modules
|
||||||
lib
|
lib
|
||||||
examples/*.js
|
examples/*.js
|
||||||
|
data
|
||||||
@@ -5,8 +5,19 @@ const define = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
build({
|
build({
|
||||||
entryPoints: ['examples/main.ts'],
|
entryPoints: ['examples/basic.ts'],
|
||||||
outfile: 'examples/main.js',
|
outfile: 'examples/basic.js',
|
||||||
|
|
||||||
|
target: 'es6',
|
||||||
|
platform: 'node',
|
||||||
|
format: 'cjs',
|
||||||
|
|
||||||
|
define,
|
||||||
|
})
|
||||||
|
|
||||||
|
build({
|
||||||
|
entryPoints: ['examples/test.ts'],
|
||||||
|
outfile: 'examples/test.js',
|
||||||
|
|
||||||
target: 'es6',
|
target: 'es6',
|
||||||
platform: 'node',
|
platform: 'node',
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
{ "a": "aaaaa", "b": "bbbbb", "c": "ccccc" }
|
|
||||||
33
examples/test.ts
Normal file
33
examples/test.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { Guild } from 'discord.js'
|
||||||
|
import { setFlag, globalStorage, guildStorage } from '..'
|
||||||
|
import { GuildStorage } from '../lib/storageclass'
|
||||||
|
|
||||||
|
const DefaultStorage = {
|
||||||
|
pingCount: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
//change the storage directory
|
||||||
|
setFlag('dir', './data/test')
|
||||||
|
|
||||||
|
let globalStor = globalStorage()
|
||||||
|
let guildStor = guildStorage({ id: '000' } as Guild) as
|
||||||
|
| GuildStorage
|
||||||
|
| undefined
|
||||||
|
|
||||||
|
let globalJson = globalStor.getJson(DefaultStorage)
|
||||||
|
let guildJson = guildStor?.getJson(DefaultStorage)
|
||||||
|
|
||||||
|
globalJson.pingCount++
|
||||||
|
console.log(globalJson, guildJson)
|
||||||
|
|
||||||
|
if (guildJson) guildJson.pingCount++
|
||||||
|
console.log(globalJson, guildJson)
|
||||||
|
|
||||||
|
globalStor.set(globalJson)
|
||||||
|
if (guildStor && guildJson) guildStor.set(guildJson)
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
||||||
|
// setTimeout(() => main(), 1000)
|
||||||
|
// setTimeout(() => main(), 2000)
|
||||||
@@ -18,6 +18,7 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/fs-extra": "^9.0.13",
|
"@types/fs-extra": "^9.0.13",
|
||||||
|
"@types/node": "^17.0.35",
|
||||||
"discord.js": "^13.6.0",
|
"discord.js": "^13.6.0",
|
||||||
"prettier": "^2.6.2",
|
"prettier": "^2.6.2",
|
||||||
"typescript": "^4.6.3"
|
"typescript": "^4.6.3"
|
||||||
|
|||||||
12
pnpm-lock.yaml
generated
12
pnpm-lock.yaml
generated
@@ -2,6 +2,7 @@ lockfileVersion: 5.3
|
|||||||
|
|
||||||
specifiers:
|
specifiers:
|
||||||
'@types/fs-extra': ^9.0.13
|
'@types/fs-extra': ^9.0.13
|
||||||
|
'@types/node': ^17.0.35
|
||||||
discord.js: ^13.6.0
|
discord.js: ^13.6.0
|
||||||
esbuild: ^0.14.28
|
esbuild: ^0.14.28
|
||||||
fs-extra: ^10.1.0
|
fs-extra: ^10.1.0
|
||||||
@@ -14,6 +15,7 @@ dependencies:
|
|||||||
|
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@types/fs-extra': 9.0.13
|
'@types/fs-extra': 9.0.13
|
||||||
|
'@types/node': 17.0.35
|
||||||
discord.js: 13.6.0
|
discord.js: 13.6.0
|
||||||
prettier: 2.6.2
|
prettier: 2.6.2
|
||||||
typescript: 4.6.3
|
typescript: 4.6.3
|
||||||
@@ -63,7 +65,7 @@ packages:
|
|||||||
integrity: sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==,
|
integrity: sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==,
|
||||||
}
|
}
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 17.0.23
|
'@types/node': 17.0.35
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
/@types/node-fetch/2.6.1:
|
/@types/node-fetch/2.6.1:
|
||||||
@@ -72,14 +74,14 @@ packages:
|
|||||||
integrity: sha512-oMqjURCaxoSIsHSr1E47QHzbmzNR5rK8McHuNb11BOM9cHcIK3Avy0s/b2JlXHoQGTYS3NsvWzV1M0iK7l0wbA==,
|
integrity: sha512-oMqjURCaxoSIsHSr1E47QHzbmzNR5rK8McHuNb11BOM9cHcIK3Avy0s/b2JlXHoQGTYS3NsvWzV1M0iK7l0wbA==,
|
||||||
}
|
}
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 17.0.23
|
'@types/node': 17.0.35
|
||||||
form-data: 3.0.1
|
form-data: 3.0.1
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
/@types/node/17.0.23:
|
/@types/node/17.0.35:
|
||||||
resolution:
|
resolution:
|
||||||
{
|
{
|
||||||
integrity: sha512-UxDxWn7dl97rKVeVS61vErvw086aCYhDLyvRQZ5Rk65rZKepaFdm53GeqXaKBuOhED4e9uWq34IC3TdSdJJ2Gw==,
|
integrity: sha512-vu1SrqBjbbZ3J6vwY17jBs8Sr/BKA+/a/WtjRG+whKg1iuLFOosq872EXS0eXWILdO36DHQQeku/ZcL6hz2fpg==,
|
||||||
}
|
}
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
@@ -89,7 +91,7 @@ packages:
|
|||||||
integrity: sha512-6YOoWjruKj1uLf3INHH7D3qTXwFfEsg1kf3c0uDdSBJwfa/llkwIjrAGV7j7mVgGNbzTQ3HiHKKDXl6bJPD97w==,
|
integrity: sha512-6YOoWjruKj1uLf3INHH7D3qTXwFfEsg1kf3c0uDdSBJwfa/llkwIjrAGV7j7mVgGNbzTQ3HiHKKDXl6bJPD97w==,
|
||||||
}
|
}
|
||||||
dependencies:
|
dependencies:
|
||||||
'@types/node': 17.0.23
|
'@types/node': 17.0.35
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
/asynckit/0.4.0:
|
/asynckit/0.4.0:
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ var GlobalCache = ''
|
|||||||
const GuildCache = new Map<string, string>()
|
const GuildCache = new Map<string, string>()
|
||||||
|
|
||||||
export function initGlobalCache(defaultValue?: string | JSONObject) {
|
export function initGlobalCache(defaultValue?: string | JSONObject) {
|
||||||
|
defaultValue = structuredClone(defaultValue)
|
||||||
if (GlobalCache.length === 0) {
|
if (GlobalCache.length === 0) {
|
||||||
let data = readFile('global')
|
let data = readFile('global')
|
||||||
GlobalCache = defaultJsonString(data, defaultValue)
|
GlobalCache = defaultJsonString(data, defaultValue)
|
||||||
@@ -18,6 +19,7 @@ export function initGuildCache(
|
|||||||
guild: Guild,
|
guild: Guild,
|
||||||
defaultValue?: string | JSONObject
|
defaultValue?: string | JSONObject
|
||||||
) {
|
) {
|
||||||
|
defaultValue = structuredClone(defaultValue)
|
||||||
if (!GuildCache.has(guild.id)) {
|
if (!GuildCache.has(guild.id)) {
|
||||||
let data = readFile(guild)
|
let data = readFile(guild)
|
||||||
GuildCache.set(guild.id, defaultJsonString(data, defaultValue))
|
GuildCache.set(guild.id, defaultJsonString(data, defaultValue))
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ export class StorageBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getJson<T = JSONObject>(defaultValue: T): T {
|
getJson<T = JSONObject>(defaultValue: T): T {
|
||||||
|
defaultValue = structuredClone(defaultValue)
|
||||||
try {
|
try {
|
||||||
let val = JSON.parse(this.val.get())
|
let val = JSON.parse(this.val.get())
|
||||||
if (defaultValue) {
|
if (defaultValue) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ export type JSONObject<Value = JSONDataTypes, Key extends string = string> = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function defaultJson<T>(data: any, defaultValue: T): T {
|
export function defaultJson<T>(data: any, defaultValue: T): T {
|
||||||
|
defaultValue = structuredClone(defaultValue)
|
||||||
if (typeof data !== 'object') {
|
if (typeof data !== 'object') {
|
||||||
return defaultValue
|
return defaultValue
|
||||||
}
|
}
|
||||||
@@ -22,6 +23,7 @@ export function defaultJsonString(
|
|||||||
data: string,
|
data: string,
|
||||||
defaultValue: string | JSONObject = ''
|
defaultValue: string | JSONObject = ''
|
||||||
): string {
|
): string {
|
||||||
|
defaultValue = structuredClone(defaultValue)
|
||||||
if (data.length === 0) {
|
if (data.length === 0) {
|
||||||
//no existing data, give the default value instead
|
//no existing data, give the default value instead
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user