@riffyh/execute 1.0.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.
@@ -0,0 +1 @@
1
+ export { };
package/dist/index.mjs ADDED
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env bun
2
+ import yargs from "yargs";
3
+ import { hideBin } from "yargs/helpers";
4
+ import path from "node:path";
5
+ import debug from "debug";
6
+ import task from "tasuku";
7
+ import mongoose from "mongoose";
8
+ import { GalleryModel } from "@riffyh/database";
9
+ //#region src/commands/sync.ts
10
+ const sync = async (config) => {
11
+ if (process.env.MONGODB_URI === void 0) throw new Error("mongo url not provided");
12
+ await mongoose.connect(process.env.MONGODB_URI);
13
+ if (config.store) for (const store of config.store) await storeIterator(store, config.dataSources);
14
+ await mongoose.disconnect();
15
+ };
16
+ const storeIterator = async (store, dataSources) => task(store.key, async ({ setTitle, setError }) => {
17
+ const dataSource = dataSources.find((d) => d.key === store.key);
18
+ if (!dataSource) {
19
+ setError("data source for " + store.key + " not found");
20
+ return;
21
+ }
22
+ setTitle(dataSource.name);
23
+ const existingDocs = await GalleryModel.find({ key: store.key }, "id").lean();
24
+ const existingIds = new Set(existingDocs.map((doc) => doc.id));
25
+ const missingIds = store.ids.filter((id) => !existingIds.has(id));
26
+ let successCount = store.ids.length - missingIds.length;
27
+ let failedCount = 0;
28
+ const totalCount = store.ids.length;
29
+ const updateTitle = () => setTitle(`${dataSource.name} ${successCount} / ${totalCount} (${failedCount} failed)`);
30
+ updateTitle();
31
+ for (const id of missingIds) {
32
+ try {
33
+ const gallery = await dataSource.getGallery({ id });
34
+ await GalleryModel.create(gallery);
35
+ successCount++;
36
+ } catch {
37
+ failedCount++;
38
+ }
39
+ updateTitle();
40
+ }
41
+ });
42
+ //#endregion
43
+ //#region src/index.ts
44
+ const log = debug("riffyh:execute");
45
+ const configFile = process.env.RIFFYH_CONFIG_PATH || "./riffyh.config.ts";
46
+ const configPath = path.resolve(configFile);
47
+ log(`resolving config file at ${configPath}...`);
48
+ const config = await import(configPath).then((o) => o.default);
49
+ yargs(hideBin(process.argv)).command("sync", "sync data to database", {}, () => sync(config)).demandCommand(1).parse();
50
+ //#endregion
51
+ export {};
52
+
53
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/commands/sync.ts","../src/index.ts"],"sourcesContent":["import task from \"tasuku\";\nimport mongoose from \"mongoose\";\nimport { GalleryModel } from \"@riffyh/database\";\n\nimport type { Config, Store, DataSource } from \"@riffyh/commons\";\n\nexport const sync = async (config: Config) => {\n if (process.env.MONGODB_URI === undefined) throw new Error(\"mongo url not provided\");\n\n await mongoose.connect(process.env.MONGODB_URI);\n\n if (config.store) {\n for (const store of config.store) {\n await storeIterator(store, config.dataSources);\n }\n }\n\n await mongoose.disconnect();\n};\n\nconst storeIterator = async (store: Store, dataSources: DataSource[]) =>\n task(store.key, async ({ setTitle, setError }) => {\n const dataSource = dataSources.find((d) => d.key === store.key);\n\n if (!dataSource) {\n setError(\"data source for \" + store.key + \" not found\");\n return;\n }\n\n setTitle(dataSource.name);\n\n // 1. get list of ids that already exist in mongoose db (find multiple by key)\n const existingDocs = await GalleryModel.find({ key: store.key }, \"id\").lean();\n const existingIds = new Set(existingDocs.map((doc: any) => doc.id));\n\n // 2. compute list of ids that does not yet exist in db\n const missingIds = store.ids.filter((id) => !existingIds.has(id));\n\n let successCount = store.ids.length - missingIds.length;\n let failedCount = 0;\n const totalCount = store.ids.length;\n\n const updateTitle = () =>\n setTitle(`${dataSource.name} ${successCount} / ${totalCount} (${failedCount} failed)`);\n updateTitle();\n\n // 3. from those list do a foreach await loop to get gallery data via dataSource.getGallery()\n for (const id of missingIds) {\n try {\n const gallery = await dataSource.getGallery({ id });\n\n // 4. if call is success, use mongoose to push to db. otherwise log item as failed\n await GalleryModel.create(gallery);\n\n successCount++;\n } catch {\n failedCount++;\n }\n\n // 5. setTitle to log of current progress\n updateTitle();\n }\n });\n","#!/usr/bin/env bun\n\nimport yargs from \"yargs\";\nimport { hideBin } from \"yargs/helpers\";\nimport path from \"node:path\";\nimport debug from \"debug\";\n\nimport { sync } from \"./commands/sync\";\n\nimport type { Config } from \"@riffyh/commons\";\n\nconst log = debug(\"riffyh:execute\");\n\nconst configFile = process.env.RIFFYH_CONFIG_PATH || \"./riffyh.config.ts\";\nconst configPath = path.resolve(configFile);\nlog(`resolving config file at ${configPath}...`);\nconst config: Config = await import(configPath).then((o) => o.default);\n\nyargs(hideBin(process.argv))\n .command(\"sync\", \"sync data to database\", {}, () => sync(config))\n .demandCommand(1)\n .parse();\n"],"mappings":";;;;;;;;;AAMA,MAAa,OAAO,OAAO,WAAmB;AAC5C,KAAI,QAAQ,IAAI,gBAAgB,KAAA,EAAW,OAAM,IAAI,MAAM,yBAAyB;AAEpF,OAAM,SAAS,QAAQ,QAAQ,IAAI,YAAY;AAE/C,KAAI,OAAO,MACT,MAAK,MAAM,SAAS,OAAO,MACzB,OAAM,cAAc,OAAO,OAAO,YAAY;AAIlD,OAAM,SAAS,YAAY;;AAG7B,MAAM,gBAAgB,OAAO,OAAc,gBACzC,KAAK,MAAM,KAAK,OAAO,EAAE,UAAU,eAAe;CAChD,MAAM,aAAa,YAAY,MAAM,MAAM,EAAE,QAAQ,MAAM,IAAI;AAE/D,KAAI,CAAC,YAAY;AACf,WAAS,qBAAqB,MAAM,MAAM,aAAa;AACvD;;AAGF,UAAS,WAAW,KAAK;CAGzB,MAAM,eAAe,MAAM,aAAa,KAAK,EAAE,KAAK,MAAM,KAAK,EAAE,KAAK,CAAC,MAAM;CAC7E,MAAM,cAAc,IAAI,IAAI,aAAa,KAAK,QAAa,IAAI,GAAG,CAAC;CAGnE,MAAM,aAAa,MAAM,IAAI,QAAQ,OAAO,CAAC,YAAY,IAAI,GAAG,CAAC;CAEjE,IAAI,eAAe,MAAM,IAAI,SAAS,WAAW;CACjD,IAAI,cAAc;CAClB,MAAM,aAAa,MAAM,IAAI;CAE7B,MAAM,oBACJ,SAAS,GAAG,WAAW,KAAK,GAAG,aAAa,KAAK,WAAW,IAAI,YAAY,UAAU;AACxF,cAAa;AAGb,MAAK,MAAM,MAAM,YAAY;AAC3B,MAAI;GACF,MAAM,UAAU,MAAM,WAAW,WAAW,EAAE,IAAI,CAAC;AAGnD,SAAM,aAAa,OAAO,QAAQ;AAElC;UACM;AACN;;AAIF,eAAa;;EAEf;;;ACnDJ,MAAM,MAAM,MAAM,iBAAiB;AAEnC,MAAM,aAAa,QAAQ,IAAI,sBAAsB;AACrD,MAAM,aAAa,KAAK,QAAQ,WAAW;AAC3C,IAAI,4BAA4B,WAAW,KAAK;AAChD,MAAM,SAAiB,MAAM,OAAO,YAAY,MAAM,MAAM,EAAE,QAAQ;AAEtE,MAAM,QAAQ,QAAQ,KAAK,CAAC,CACzB,QAAQ,QAAQ,yBAAyB,EAAE,QAAQ,KAAK,OAAO,CAAC,CAChE,cAAc,EAAE,CAChB,OAAO"}
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@riffyh/execute",
3
+ "version": "1.0.0",
4
+ "description": "Execuable command as an extension of Riffy H system",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/rayriffy/rayriffy-h.git"
8
+ },
9
+ "files": [
10
+ "dist/"
11
+ ],
12
+ "type": "module",
13
+ "main": "dist/index.mjs",
14
+ "types": "dist/index.d.mts",
15
+ "publishConfig": {
16
+ "access": "public",
17
+ "registry": "https://registry.npmjs.org"
18
+ },
19
+ "dependencies": {
20
+ "debug": "4.4.3",
21
+ "mongoose": "8.9.5",
22
+ "tasuku": "2.3.0",
23
+ "yargs": "18.0.0",
24
+ "@riffyh/commons": "2.1.1",
25
+ "@riffyh/database": "1.0.0"
26
+ },
27
+ "devDependencies": {
28
+ "@types/debug": "4.1.13",
29
+ "@types/yargs": "17.0.35",
30
+ "tsdown": "0.21.7"
31
+ },
32
+ "scripts": {
33
+ "dev": "tsdown --watch",
34
+ "build": "tsdown"
35
+ },
36
+ "bin": {
37
+ "execute": "dist/index.mjs"
38
+ }
39
+ }