store-fn 1.0.0 → 1.0.1
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/dist/cli.js +9 -14
- package/dist/index.d.ts +4 -42
- package/dist/index.js +585 -3
- package/dist/index.js.map +1 -1
- package/package.json +13 -4
- package/debug.ts +0 -23
- package/vite-env.d.ts +0 -10
package/dist/cli.js
CHANGED
|
@@ -1,21 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import s from"picocolors";import{resolve as f}from"path";function p(e){return e&&e.split(/[\s-_]+/).map((r,n)=>(r=r.toLowerCase(),n>0?r.charAt(0).toUpperCase()+r.slice(1):r)).join("")}function h(e){let r=JSON.stringify(e,null,2).replace(/"(\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ)"/g,n=>`new Date(${n})`);return`export const ${p(e.name)}Product = ${r} as const satisfies Product`}async function g(e,r){if(typeof window<"u")throw new Error("writeProductsToFile is not supported in the browser");let n=`
|
|
2
|
+
import s from"picocolors";import{resolve as f,join as P}from"path";import v from"dotenv";import w from"dotenv-expand";import A from"fs";function g(e){return e&&e.split(/[\s-_]+/).map((r,o)=>(r=r.toLowerCase(),o>0?r.charAt(0).toUpperCase()+r.slice(1):r)).join("")}function y(e){let r=JSON.stringify(e,null,2).replace(/"(\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ)"/g,o=>`new Date(${o})`);return`export const ${g(e.name)}Product = ${r} as const satisfies Product`}async function h(e,r){if(typeof window<"u")throw new Error("writeProductsToFile is not supported in the browser");let o=`
|
|
4
3
|
import type { Product } from "@polar-sh/sdk/models/components/product.js"
|
|
5
4
|
|
|
6
|
-
${e.map(
|
|
5
|
+
${e.map(y).join(`
|
|
7
6
|
|
|
8
7
|
`)}
|
|
9
|
-
`,t=await import("fs/promises").then(i=>i.writeFile),a=await import("prettier").then(i=>i.default).catch(()=>null);if(a){let i=await import("path").then(
|
|
8
|
+
`,t=await import("fs/promises").then(i=>i.writeFile),a=await import("prettier").then(i=>i.default).catch(()=>null);if(a){let i=await import("path").then(n=>n.default),u=await import("fs").then(n=>n.existsSync),l=i.dirname(i.resolve(import.meta.url)),c=["prettier.config.js","prettier.config.ts"].map(n=>i.resolve(l,n)).find(n=>u(n)),d=c?await import(c).then(n=>n.default).catch(()=>null):null;o=await a.format(o.trim(),{parser:"typescript",...d})}await t(r,o)}async function I(){let e=process.argv.slice(2),r=e[0];r||(console.error(s.red("Error: No command provided")),console.log(`
|
|
10
9
|
Usage: store-fn <command>`),console.log(`
|
|
11
10
|
Commands:`),console.log(" push Sync products to Polar store"),console.log(`
|
|
12
|
-
Example:`),console.log(" store-fn push -i store.config.ts -o store/products.ts"),console.log(" store-fn push # Uses defaults: -i store.config.ts -o store/products.ts"),process.exit(1)),r==="push"?await
|
|
13
|
-
Available commands:`),console.log(" push Sync products to Polar store"),process.exit(1))}async function
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
`));let c=await d.push();if(!c||!c.updatedProducts)throw new Error("Push function did not return products. Expected { updatedProducts: Product[] }");let u=c.updatedProducts;if(!Array.isArray(u)||u.length===0){console.log(s.yellow("No products to write"));return}console.log(s.blue(`
|
|
19
|
-
Writing ${u.length} product(s) to: ${a}`)),await g(u,a),console.log(s.green(`
|
|
20
|
-
\u2713 Successfully wrote products to ${a}`))}catch(t){console.error(s.red(`
|
|
21
|
-
Error: ${t.message}`)),t.stack&&process.env.DEBUG&&console.error(t.stack),process.exit(1)}}y().catch(e=>{console.error(s.red(`Fatal error: ${e.message}`)),e.stack&&console.error(e.stack),process.exit(1)});
|
|
11
|
+
Example:`),console.log(" store-fn push -i store.config.ts -o store/products.ts"),console.log(" store-fn push # Uses defaults: -i store.config.ts -o store/products.ts"),process.exit(1)),x(),r==="push"?await C(e.slice(1)):(console.error(s.red(`Error: Unknown command "${r}"`)),console.log(`
|
|
12
|
+
Available commands:`),console.log(" push Sync products to Polar store"),process.exit(1))}async function C(e){let r,o;for(let t=0;t<e.length;t++)e[t]==="-i"&&t+1<e.length?(r=e[t+1],t++):e[t]==="-o"&&t+1<e.length&&(o=e[t+1],t++);r=r||"store.config.ts",o=o||"store/products.ts";try{let t=f(process.cwd(),r),a=f(process.cwd(),o);console.log(s.gray(`Loading store definition from: ${t}`));let i=t.endsWith(".ts")||t.endsWith(".tsx"),u;i&&await import("tsx/esm");let l=f(t),p=l.startsWith("file://")?l:`file://${l}`;try{u=await import(p)}catch(m){throw m.code==="ERR_MODULE_NOT_FOUND"||m.code==="ENOENT"?new Error(`Store file not found: ${t}
|
|
13
|
+
Make sure the file exists and the path is correct.`):m.message?.includes("Cannot find module")?new Error(`Cannot load store file: ${t}
|
|
14
|
+
Make sure the file exists and the path is correct.`):m}let c=u.default;if(!c)throw new Error(`Store file must have a default export. Found exports: ${Object.keys(u).join(", ")}`);if(typeof c.push!="function")throw new Error("Store default export must be the return value of createStoreFn (an object with a push function)");console.log(s.gray(`Syncing products to Polar store...
|
|
15
|
+
`));let d=await c.push();if(!d||!d.updatedProducts)throw new Error("Push function did not return products. Expected { updatedProducts: Product[] }");let n=d.updatedProducts;if(!Array.isArray(n)||n.length===0){console.log(s.yellow("No products to write"));return}console.log(s.gray(`Writing ${n.length} product(s) to: ${a}`)),await h(n,a),console.log(s.green(`\u2713 Successfully wrote products to ${a}`))}catch(t){console.error(s.red(`
|
|
16
|
+
Error: ${t.message}`)),t.stack&&process.env.DEBUG&&console.error(t.stack),process.exit(1)}}function x(){let e=[".env",".env.local",`.env.${process.env.NODE_ENV??"development"}`,`.env.${process.env.NODE_ENV??"development"}.local`];for(let r of e){let o=P(process.cwd(),r);if(!A.existsSync(o))continue;let t=v.config({path:o,override:!1});w.expand(t)}}I().catch(e=>{console.error(s.red(`Fatal error: ${e.message}`)),e.stack&&console.error(e.stack),process.exit(1)});
|
package/dist/index.d.ts
CHANGED
|
@@ -10,19 +10,11 @@ import { Polar } from '@polar-sh/sdk';
|
|
|
10
10
|
import { ProductCreate } from '@polar-sh/sdk/models/components/productcreate.js';
|
|
11
11
|
import { Product } from '@polar-sh/sdk/models/components/product.js';
|
|
12
12
|
|
|
13
|
-
declare global {
|
|
14
|
-
var polarClient: Polar;
|
|
15
|
-
var polarOrganizationId: string;
|
|
16
|
-
}
|
|
17
13
|
type ProductDefinition<TMeta> = ProductCreate & {
|
|
18
14
|
key: string;
|
|
15
|
+
virtual?: boolean;
|
|
19
16
|
metadata?: TMeta;
|
|
20
|
-
}
|
|
21
|
-
virtual?: false | undefined;
|
|
22
|
-
} | {
|
|
23
|
-
virtual: true;
|
|
24
|
-
id: string;
|
|
25
|
-
});
|
|
17
|
+
};
|
|
26
18
|
|
|
27
19
|
declare function writeProductsToFile(products: Product[], path: string): Promise<void>;
|
|
28
20
|
|
|
@@ -45,36 +37,7 @@ declare function createStoreFn(options: CreateStoreFnOptions): {
|
|
|
45
37
|
trialIntervalCount?: number | null | undefined;
|
|
46
38
|
recurringInterval: _polar_sh_sdk_models_components_subscriptionrecurringinterval_js.SubscriptionRecurringInterval;
|
|
47
39
|
recurringIntervalCount?: number | undefined;
|
|
48
|
-
virtual?:
|
|
49
|
-
} | {
|
|
50
|
-
metadata: TMeta & {
|
|
51
|
-
key: string;
|
|
52
|
-
};
|
|
53
|
-
name: string;
|
|
54
|
-
description?: string | null | undefined;
|
|
55
|
-
prices: Array<_polar_sh_sdk_models_components_productpricecustomcreate_js.ProductPriceCustomCreate | _polar_sh_sdk_models_components_productpricefixedcreate_js.ProductPriceFixedCreate | _polar_sh_sdk_models_components_productpricefreecreate_js.ProductPriceFreeCreate | _polar_sh_sdk_models_components_productpricemeteredunitcreate_js.ProductPriceMeteredUnitCreate | _polar_sh_sdk_models_components_productpriceseatbasedcreate_js.ProductPriceSeatBasedCreate>;
|
|
56
|
-
medias?: Array<string> | null | undefined;
|
|
57
|
-
attachedCustomFields?: Array<_polar_sh_sdk_models_components_attachedcustomfieldcreate_js.AttachedCustomFieldCreate> | undefined;
|
|
58
|
-
organizationId?: string | null | undefined;
|
|
59
|
-
trialInterval?: _polar_sh_sdk_models_components_trialinterval_js.TrialInterval | null | undefined;
|
|
60
|
-
trialIntervalCount?: number | null | undefined;
|
|
61
|
-
recurringInterval: _polar_sh_sdk_models_components_subscriptionrecurringinterval_js.SubscriptionRecurringInterval;
|
|
62
|
-
recurringIntervalCount?: number | undefined;
|
|
63
|
-
virtual: true;
|
|
64
|
-
id: string;
|
|
65
|
-
} | {
|
|
66
|
-
metadata: TMeta & {
|
|
67
|
-
key: string;
|
|
68
|
-
};
|
|
69
|
-
name: string;
|
|
70
|
-
description?: string | null | undefined;
|
|
71
|
-
prices: Array<_polar_sh_sdk_models_components_productpricecustomcreate_js.ProductPriceCustomCreate | _polar_sh_sdk_models_components_productpricefixedcreate_js.ProductPriceFixedCreate | _polar_sh_sdk_models_components_productpricefreecreate_js.ProductPriceFreeCreate | _polar_sh_sdk_models_components_productpricemeteredunitcreate_js.ProductPriceMeteredUnitCreate | _polar_sh_sdk_models_components_productpriceseatbasedcreate_js.ProductPriceSeatBasedCreate>;
|
|
72
|
-
medias?: Array<string> | null | undefined;
|
|
73
|
-
attachedCustomFields?: Array<_polar_sh_sdk_models_components_attachedcustomfieldcreate_js.AttachedCustomFieldCreate> | undefined;
|
|
74
|
-
organizationId?: string | null | undefined;
|
|
75
|
-
recurringInterval?: any | null | undefined;
|
|
76
|
-
recurringIntervalCount?: any | null | undefined;
|
|
77
|
-
virtual?: false | undefined;
|
|
40
|
+
virtual?: boolean;
|
|
78
41
|
} | {
|
|
79
42
|
metadata: TMeta & {
|
|
80
43
|
key: string;
|
|
@@ -87,8 +50,7 @@ declare function createStoreFn(options: CreateStoreFnOptions): {
|
|
|
87
50
|
organizationId?: string | null | undefined;
|
|
88
51
|
recurringInterval?: any | null | undefined;
|
|
89
52
|
recurringIntervalCount?: any | null | undefined;
|
|
90
|
-
virtual
|
|
91
|
-
id: string;
|
|
53
|
+
virtual?: boolean;
|
|
92
54
|
};
|
|
93
55
|
push: () => Promise<{
|
|
94
56
|
updatedProducts: Product[];
|