@ordergroove/smi-serve 1.3.0 → 1.3.2

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/CHANGELOG.md CHANGED
@@ -3,6 +3,26 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [1.3.2](https://github.com/ordergroove/plush-toys/compare/@ordergroove/smi-serve@1.3.1...@ordergroove/smi-serve@1.3.2) (2023-09-12)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **smi-serve:** Check if dir is not empty and alert soon. ([3955dad](https://github.com/ordergroove/plush-toys/commit/3955dadc59398b169d6175ef7add41334b4d895b))
12
+ * **smi-serve:** Fix promise exception impersonate. Add disclaimer ([aad5adc](https://github.com/ordergroove/plush-toys/commit/aad5adcc8a56793f46f6b42ab127acc8a867c855))
13
+
14
+
15
+
16
+
17
+
18
+ ## [1.3.1](https://github.com/ordergroove/plush-toys/compare/@ordergroove/smi-serve@1.3.0...@ordergroove/smi-serve@1.3.1) (2023-09-12)
19
+
20
+ **Note:** Version bump only for package @ordergroove/smi-serve
21
+
22
+
23
+
24
+
25
+
6
26
  # [1.3.0](https://github.com/ordergroove/plush-toys/compare/@ordergroove/smi-serve@1.2.2...@ordergroove/smi-serve@1.3.0) (2023-08-31)
7
27
 
8
28
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ordergroove/smi-serve",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "Utility to serve a SMI template locally",
5
5
  "keywords": [],
6
6
  "author": "Eugenio Lattanzio <eugenio.lattanzio@ordergroove.com>",
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "homepage": "https://github.com/ordergroove/plush-toys/tree/master/packages/smi-serve#readme",
23
23
  "dependencies": {
24
- "@ordergroove/smi-precompile": "^1.7.6",
24
+ "@ordergroove/smi-precompile": "^1.7.7",
25
25
  "adm-zip": "^0.5.10",
26
26
  "esbuild": "^0.19.2",
27
27
  "esbuild-plugin-less": "^1.2.4",
@@ -32,5 +32,5 @@
32
32
  "ora": "^5.4.1",
33
33
  "yargs": "^17.7.2"
34
34
  },
35
- "gitHead": "cf0f17b4a662797b6134a16cc36fcd63ae4acc38"
35
+ "gitHead": "f805cf1009189b3be3033361bd5796e77ea17414"
36
36
  }
package/smi-serve.js CHANGED
@@ -135,6 +135,12 @@ async function program() {
135
135
  default: '.ogrc.json',
136
136
  description: 'Ordergroove configuration file'
137
137
  })
138
+ .option('cwd', {
139
+ alias: ['w', 'working-dir'],
140
+ type: 'string',
141
+ default: '.',
142
+ description: 'Sets the current working directory'
143
+ })
138
144
  .option('port', {
139
145
  alias: 'p',
140
146
  type: 'number',
@@ -12,20 +12,26 @@ async function impersonate(args) {
12
12
 
13
13
  const impersonateInput = args.impersonate;
14
14
  const search = async input => {
15
- customersResponse = await (
16
- await fetch(
17
- `${getRC3Url(args)}csa/customer_search_req_keyword.json?${new URLSearchParams([
18
- ['merchant_id', merchant.public_id],
19
- ['q', input],
20
- ['page_limit', 10]
21
- ])}`,
22
- {
23
- headers: {
24
- authorization: `Bearer ${token}`
25
- }
15
+ az = [];
16
+ customersResponse = [];
17
+ if (!input) return [];
18
+ const fetchRes = await fetch(
19
+ `${getRC3Url(args)}csa/customer_search_req_keyword.json?${new URLSearchParams([
20
+ ['merchant_id', merchant.public_id],
21
+ ['q', input],
22
+ ['page_limit', 10]
23
+ ])}`,
24
+ {
25
+ headers: {
26
+ authorization: `Bearer ${token}`
26
27
  }
27
- )
28
- ).json();
28
+ }
29
+ );
30
+
31
+ if (fetchRes.status !== 200) return [];
32
+
33
+ customersResponse = await fetchRes.json();
34
+
29
35
  az = (customersResponse.customers || []).map(
30
36
  ({ first_name, last_name, email }) => `${first_name} ${last_name} (${email})`
31
37
  );
@@ -46,16 +52,29 @@ async function impersonate(args) {
46
52
  type: 'autocomplete',
47
53
  name: 'customer',
48
54
  message: 'Choose a customer to impersonate',
49
- source: async (answersSoFar, input) => search(input !== undefined ? input : impersonateInput)
55
+ source: (answersSoFar, input) => search(input !== undefined ? input : impersonateInput)
50
56
  }
51
57
  ])
52
58
  .then(({ customer: c }) => {
53
- console.log(c);
54
59
  const result = customersResponse.customers[az.indexOf(c)];
55
- console.log(result);
60
+
56
61
  return result;
57
62
  });
58
63
 
64
+ console.log(`\
65
+
66
+ -------------------------------------------------------------------------------
67
+ Browsing SMI for customer: ${customer.first_name} ${customer.last_name} (${customer.email})
68
+
69
+ Please note:
70
+ This tool browses the live SMI of the customer you are impersonating.
71
+ Do not make any changes to orders/subscriptions as they will actually
72
+ take effect. This tool is intended for troubleshooting and previewing
73
+ only. Please use the CSA to edit order/subscription data.
74
+
75
+ -------------------------------------------------------------------------------
76
+ `);
77
+
59
78
  return [token, merchant, { ...customer, ts: customersResponse.hmac_timestamp }];
60
79
  }
61
80
 
package/src/init.js CHANGED
@@ -11,7 +11,9 @@ const SUBSCRIPTION_MANAGEMENT_ENDPOINT = 'msi';
11
11
  const SUBSCRIPTION_SETTINGS_ENDPOINT = 'subscription-configs';
12
12
  const SMI_CORE_NAME = '@ordergroove/smi-core';
13
13
  const SMI_TEMPLATES_NAME = '@ordergroove/smi-templates';
14
+ const OG_RC_FILE = '.ogrc.json';
14
15
 
16
+ exports.OG_RC_FILE = OG_RC_FILE;
15
17
  async function getConfigs(args, endpoint, merchant, token) {
16
18
  const res = await fetch(
17
19
  `${getRC3Url(args)}configs/${endpoint}/?${new URLSearchParams([['merchant_public_id', merchant.public_id]])}`,
@@ -19,7 +21,13 @@ async function getConfigs(args, endpoint, merchant, token) {
19
21
  headers: { Authorization: `Bearer ${token}` }
20
22
  }
21
23
  );
22
- return await res.json().catch(() => ({ configs: {} }));
24
+ return await res
25
+ .json()
26
+ .then(r => {
27
+ if (typeof r !== 'object') throw new Error(r);
28
+ return r;
29
+ })
30
+ .catch(() => ({ configs: {} }));
23
31
  }
24
32
 
25
33
  async function updateJsonFile(filename, config) {
@@ -53,12 +61,22 @@ const downloadAndExtract = async url => {
53
61
  }
54
62
  };
55
63
 
64
+ async function isEmptyDir(args) {
65
+ const files = await fs.promises.readdir(args.cwd);
66
+ if (files.length === 0) return true;
67
+ if (files.length === 1 && files[0] === OG_RC_FILE) return true;
68
+ return false;
69
+ }
56
70
  class DirNotEmpty extends Error {}
57
71
  exports.DirNotEmpty = DirNotEmpty;
58
72
  /**
59
73
  * initializes a repo from sspc
60
74
  */
61
75
  async function init(args) {
76
+ if (!(await isEmptyDir(args)) && !args.overwrite) {
77
+ throw new DirNotEmpty();
78
+ }
79
+
62
80
  const [token, merchant] = await chooseMerchant(args);
63
81
 
64
82
  const { configs: msiConfigs, meta_fields, meta_fields_default } = await getConfigs(
@@ -92,9 +110,6 @@ async function init(args) {
92
110
  (msiConfigs.smi.files || []).forEach(({ content, name }) => {
93
111
  const filepath = `.${name}`;
94
112
  fs.mkdirSync(path.dirname(filepath), { recursive: true });
95
- if (fs.existsSync(filepath) && !args.overwrite) {
96
- throw new DirNotEmpty();
97
- }
98
113
  fs.writeFileSync(filepath, content, { encoding: 'utf8' });
99
114
  });
100
115
 
@@ -146,7 +161,7 @@ export const auth_config = ${JSON.stringify(auth_config)};
146
161
  fs.writeFileSync(
147
162
  '.gitignore',
148
163
  `\
149
- .ogrc.json
164
+ ${OG_RC_FILE}
150
165
  node_modules/
151
166
  `
152
167
  );