@awsesh/core 1.0.2 → 1.0.4
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.
- package/filesystem.d.ts +6 -0
- package/index.js +71 -39
- package/package.json +1 -1
package/filesystem.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function readJsonFile<T>(filePath: string): Promise<T>;
|
|
2
|
+
export declare function writeJsonFile(filePath: string, value: unknown): Promise<void>;
|
|
3
|
+
export declare function fileExists(filePath: string): Promise<boolean>;
|
|
4
|
+
export declare function listJsonFiles(dir: string, options?: {
|
|
5
|
+
recursive?: boolean;
|
|
6
|
+
}): Promise<string[]>;
|
package/index.js
CHANGED
|
@@ -20058,52 +20058,89 @@ var Credentials;
|
|
|
20058
20058
|
Credentials.listProfiles = listProfiles;
|
|
20059
20059
|
})(Credentials ||= {});
|
|
20060
20060
|
// src/sessions.ts
|
|
20061
|
+
import path3 from "node:path";
|
|
20062
|
+
import fs3 from "node:fs/promises";
|
|
20063
|
+
|
|
20064
|
+
// src/filesystem.ts
|
|
20061
20065
|
import path2 from "node:path";
|
|
20062
20066
|
import fs2 from "node:fs/promises";
|
|
20067
|
+
async function readJsonFile(filePath) {
|
|
20068
|
+
const content = await fs2.readFile(filePath, "utf-8");
|
|
20069
|
+
return JSON.parse(content);
|
|
20070
|
+
}
|
|
20071
|
+
async function writeJsonFile(filePath, value) {
|
|
20072
|
+
await fs2.writeFile(filePath, JSON.stringify(value, null, 2));
|
|
20073
|
+
}
|
|
20074
|
+
async function fileExists(filePath) {
|
|
20075
|
+
try {
|
|
20076
|
+
await fs2.access(filePath);
|
|
20077
|
+
return true;
|
|
20078
|
+
} catch {
|
|
20079
|
+
return false;
|
|
20080
|
+
}
|
|
20081
|
+
}
|
|
20082
|
+
async function listJsonFiles(dir, options = {}) {
|
|
20083
|
+
const recursive = options.recursive ?? false;
|
|
20084
|
+
const entries = await fs2.readdir(dir, { withFileTypes: true });
|
|
20085
|
+
const files = [];
|
|
20086
|
+
for (const entry of entries) {
|
|
20087
|
+
const entryPath = path2.join(dir, entry.name);
|
|
20088
|
+
if (entry.isFile() && entry.name.endsWith(".json")) {
|
|
20089
|
+
files.push(entry.name);
|
|
20090
|
+
continue;
|
|
20091
|
+
}
|
|
20092
|
+
if (recursive && entry.isDirectory()) {
|
|
20093
|
+
const nestedFiles = await listJsonFiles(entryPath, { recursive: true });
|
|
20094
|
+
for (const nestedFile of nestedFiles) {
|
|
20095
|
+
files.push(path2.posix.join(entry.name, nestedFile));
|
|
20096
|
+
}
|
|
20097
|
+
}
|
|
20098
|
+
}
|
|
20099
|
+
return files;
|
|
20100
|
+
}
|
|
20101
|
+
|
|
20102
|
+
// src/sessions.ts
|
|
20063
20103
|
var Sessions;
|
|
20064
20104
|
((Sessions) => {
|
|
20065
20105
|
function create(options) {
|
|
20066
20106
|
const { dir } = options;
|
|
20067
20107
|
async function ensureDir() {
|
|
20068
|
-
await
|
|
20108
|
+
await fs3.mkdir(dir, { recursive: true }).catch(() => {});
|
|
20069
20109
|
}
|
|
20070
20110
|
return {
|
|
20071
20111
|
async list() {
|
|
20072
20112
|
try {
|
|
20073
20113
|
await ensureDir();
|
|
20074
|
-
const
|
|
20075
|
-
const
|
|
20076
|
-
const sessions = await Promise.all(files.map((file) => Bun.file(path2.join(dir, file)).json()));
|
|
20114
|
+
const files = await listJsonFiles(dir);
|
|
20115
|
+
const sessions = await Promise.all(files.map((file) => readJsonFile(path3.join(dir, file))));
|
|
20077
20116
|
return sessions;
|
|
20078
20117
|
} catch {
|
|
20079
20118
|
return [];
|
|
20080
20119
|
}
|
|
20081
20120
|
},
|
|
20082
20121
|
async load(name) {
|
|
20083
|
-
const target =
|
|
20084
|
-
|
|
20085
|
-
if (!await file.exists())
|
|
20122
|
+
const target = path3.join(dir, `${name}.json`);
|
|
20123
|
+
if (!await fileExists(target))
|
|
20086
20124
|
return;
|
|
20087
|
-
return
|
|
20125
|
+
return readJsonFile(target);
|
|
20088
20126
|
},
|
|
20089
20127
|
async save(session) {
|
|
20090
20128
|
await ensureDir();
|
|
20091
|
-
const target =
|
|
20092
|
-
await
|
|
20129
|
+
const target = path3.join(dir, `${session.name}.json`);
|
|
20130
|
+
await writeJsonFile(target, session);
|
|
20093
20131
|
},
|
|
20094
20132
|
async remove(name) {
|
|
20095
|
-
const target =
|
|
20096
|
-
await
|
|
20133
|
+
const target = path3.join(dir, `${name}.json`);
|
|
20134
|
+
await fs3.unlink(target).catch(() => {});
|
|
20097
20135
|
},
|
|
20098
20136
|
async exists(name) {
|
|
20099
|
-
const target =
|
|
20100
|
-
return
|
|
20137
|
+
const target = path3.join(dir, `${name}.json`);
|
|
20138
|
+
return fileExists(target);
|
|
20101
20139
|
},
|
|
20102
20140
|
async count() {
|
|
20103
20141
|
try {
|
|
20104
20142
|
await ensureDir();
|
|
20105
|
-
const
|
|
20106
|
-
const files = await Array.fromAsync(glob.scan({ cwd: dir, onlyFiles: true }));
|
|
20143
|
+
const files = await listJsonFiles(dir);
|
|
20107
20144
|
return files.length;
|
|
20108
20145
|
} catch {
|
|
20109
20146
|
return 0;
|
|
@@ -20114,50 +20151,46 @@ var Sessions;
|
|
|
20114
20151
|
Sessions.create = create;
|
|
20115
20152
|
})(Sessions ||= {});
|
|
20116
20153
|
// src/storage.ts
|
|
20117
|
-
import
|
|
20118
|
-
import
|
|
20154
|
+
import path4 from "node:path";
|
|
20155
|
+
import fs4 from "node:fs/promises";
|
|
20119
20156
|
var Storage;
|
|
20120
20157
|
((Storage) => {
|
|
20121
20158
|
function create(options) {
|
|
20122
20159
|
const { dir } = options;
|
|
20123
20160
|
async function ensureDir(dirPath) {
|
|
20124
|
-
await
|
|
20161
|
+
await fs4.mkdir(dirPath, { recursive: true }).catch(() => {});
|
|
20125
20162
|
}
|
|
20126
20163
|
return {
|
|
20127
20164
|
async read(key) {
|
|
20128
|
-
const target =
|
|
20165
|
+
const target = path4.join(dir, `${key}.json`);
|
|
20129
20166
|
try {
|
|
20130
|
-
|
|
20131
|
-
if (!await file.exists())
|
|
20132
|
-
return;
|
|
20133
|
-
return file.json();
|
|
20167
|
+
return await readJsonFile(target);
|
|
20134
20168
|
} catch {
|
|
20135
20169
|
return;
|
|
20136
20170
|
}
|
|
20137
20171
|
},
|
|
20138
20172
|
async write(key, value) {
|
|
20139
|
-
const target =
|
|
20140
|
-
await ensureDir(
|
|
20141
|
-
await
|
|
20173
|
+
const target = path4.join(dir, `${key}.json`);
|
|
20174
|
+
await ensureDir(path4.dirname(target));
|
|
20175
|
+
await writeJsonFile(target, value);
|
|
20142
20176
|
},
|
|
20143
20177
|
async update(key, fn) {
|
|
20144
|
-
const target =
|
|
20178
|
+
const target = path4.join(dir, `${key}.json`);
|
|
20145
20179
|
let content;
|
|
20146
20180
|
try {
|
|
20147
|
-
|
|
20148
|
-
content = await file.json();
|
|
20181
|
+
content = await readJsonFile(target);
|
|
20149
20182
|
} catch {
|
|
20150
20183
|
content = {};
|
|
20151
20184
|
}
|
|
20152
20185
|
const updated = fn(content);
|
|
20153
|
-
await ensureDir(
|
|
20154
|
-
await
|
|
20186
|
+
await ensureDir(path4.dirname(target));
|
|
20187
|
+
await writeJsonFile(target, updated);
|
|
20155
20188
|
return updated;
|
|
20156
20189
|
},
|
|
20157
20190
|
async remove(key) {
|
|
20158
|
-
const target =
|
|
20191
|
+
const target = path4.join(dir, `${key}.json`);
|
|
20159
20192
|
try {
|
|
20160
|
-
await
|
|
20193
|
+
await fs4.unlink(target);
|
|
20161
20194
|
} catch (error) {
|
|
20162
20195
|
const err = error;
|
|
20163
20196
|
if (err.code !== "ENOENT")
|
|
@@ -20165,10 +20198,9 @@ var Storage;
|
|
|
20165
20198
|
}
|
|
20166
20199
|
},
|
|
20167
20200
|
async list(prefix) {
|
|
20168
|
-
const targetDir =
|
|
20201
|
+
const targetDir = path4.join(dir, prefix);
|
|
20169
20202
|
try {
|
|
20170
|
-
const
|
|
20171
|
-
const results = await Array.fromAsync(glob.scan({ cwd: targetDir, onlyFiles: true }));
|
|
20203
|
+
const results = await listJsonFiles(targetDir, { recursive: true });
|
|
20172
20204
|
return results.map((x) => x.slice(0, -5)).sort();
|
|
20173
20205
|
} catch (error) {
|
|
20174
20206
|
const err = error;
|
|
@@ -20178,9 +20210,9 @@ var Storage;
|
|
|
20178
20210
|
}
|
|
20179
20211
|
},
|
|
20180
20212
|
async exists(key) {
|
|
20181
|
-
const target =
|
|
20213
|
+
const target = path4.join(dir, `${key}.json`);
|
|
20182
20214
|
try {
|
|
20183
|
-
await
|
|
20215
|
+
await fs4.access(target);
|
|
20184
20216
|
return true;
|
|
20185
20217
|
} catch {
|
|
20186
20218
|
return false;
|