@ordergroove/smi-serve 1.6.2 → 1.7.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
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.7.0](https://github.com/ordergroove/plush-toys/compare/@ordergroove/smi-serve@1.6.2...@ordergroove/smi-serve@1.7.0) (2024-04-19)
7
+
8
+
9
+ ### Features
10
+
11
+ * use smi-preview data in smi-serve ([4ed09e7](https://github.com/ordergroove/plush-toys/commit/4ed09e7494cde0352151e77d168e412c9c9a538b))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [1.6.2](https://github.com/ordergroove/plush-toys/compare/@ordergroove/smi-serve@1.6.1...@ordergroove/smi-serve@1.6.2) (2024-04-18)
7
18
 
8
19
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ordergroove/smi-serve",
3
- "version": "1.6.2",
3
+ "version": "1.7.0",
4
4
  "description": "Utility to serve a SMI template locally",
5
5
  "keywords": [],
6
6
  "author": "Eugenio Lattanzio <eugenio.lattanzio@ordergroove.com>",
@@ -32,5 +32,5 @@
32
32
  "ora": "^5.4.1",
33
33
  "yargs": "^17.7.2"
34
34
  },
35
- "gitHead": "08e75f787367ce639b591f9e2f2402cfb8badfe6"
35
+ "gitHead": "af9f006002d62c2fe095b0d6b46c672bcd45c899"
36
36
  }
@@ -1,24 +1,18 @@
1
- /* eslint "no-undef": "off" */
2
- /* eslint "no-nested-ternary": "off" */
3
- const params = new URLSearchParams(document.location.hash.substring(1) || window.location.search);
1
+ // specify global variables replaced at build time so eslint doesn't error
2
+ /*global OG_ENV, OG_MERCHANT_ID, OG_HMAC_AUTH, OG_SMI_CORE_VERSION*/
3
+ const params = new URLSearchParams(window.location.search);
4
4
 
5
- const auth_merchant_id = params.get('m') || params.get('merchant_id') || (params.get('auth') || '').split('|')[3];
6
- const auth_env = params.get('env') || (params.has('prod') && 'prod') || (params.has('staging') && 'staging') || OG_ENV;
7
- const template_merchant_id = (params.get('t') || '').split(',')[0];
8
- const is_staging = auth_env.startsWith('s');
5
+ const env = params.get('env') || OG_ENV;
9
6
 
10
- export const merchant_id = params.has('auth')
11
- ? template_merchant_id || auth_merchant_id || OG_MERCHANT_ID || '0e5de2bedc5e11e3a2e4bc764e106cf4'
12
- : OG_MERCHANT_ID || '0e5de2bedc5e11e3a2e4bc764e106cf4';
7
+ export const merchant_id = params.get('merchant_id') || OG_MERCHANT_ID;
13
8
 
14
9
  export const auth_config = params.has('auth')
15
10
  ? {
16
- env: is_staging ? 'staging' : 'prod',
17
- auth: params.has('auth')
11
+ env,
12
+ auth: params.get('auth')
18
13
  }
19
14
  : OG_HMAC_AUTH
20
15
  ? { env: OG_ENV, auth: OG_HMAC_AUTH }
21
- : {
22
- auth_url: 'https://static-origin-server.ordergroove.com/0e5de2bedc5e11e3a2e4bc764e106cf4/demo/auth.json',
23
- env: 'staging'
24
- };
16
+ : null;
17
+
18
+ export const smi_core_version = OG_SMI_CORE_VERSION;
@@ -1,10 +1,36 @@
1
1
  import './styles/main.less';
2
2
  import mainTemplate from './views/main.liquid';
3
3
 
4
- import { merchant_id, auth_config } from './runtime-config';
4
+ import { merchant_id, auth_config, smi_core_version } from './runtime-config';
5
5
 
6
- const smi = window.og.smi;
6
+ // only run on initial load, not hot reload
7
+ if (!window.og?.smi) {
8
+ if (auth_config && merchant_id) {
9
+ waitForScriptToLoad(`https://static.ordergroove.com/@ordergroove/smi-core/${smi_core_version}/dist/smi.js`).then(
10
+ () => {
11
+ window.og.smi.bootstrap({ merchant_id, ...auth_config }, mainTemplate);
12
+ }
13
+ );
14
+ } else {
15
+ waitForScriptToLoad(`https://static.ordergroove.com/@ordergroove/smi-preview/latest/`).then(() => {
16
+ const smiPreview = window.og['smi-preview'];
17
+ const preview = smiPreview.createPreview(document.body, smi_core_version);
18
+ preview.template = mainTemplate;
19
+ preview.locale = 'en-US';
20
+ });
21
+ }
22
+ }
7
23
 
8
- smi.bootstrap({ merchant_id, ...auth_config }, mainTemplate);
24
+ async function waitForScriptToLoad(src) {
25
+ return new Promise((resolve, reject) => {
26
+ const script = document.createElement('script');
27
+ script.src = src;
28
+ script.addEventListener('load', () => {
29
+ resolve();
30
+ });
31
+ document.body.appendChild(script);
32
+ });
33
+ }
9
34
 
10
- export { mainTemplate, smi };
35
+ // export for the live reload script to use
36
+ export { mainTemplate };
@@ -4,10 +4,8 @@
4
4
  <meta charset="UTF-8" />
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1" />
6
6
  <link rel="stylesheet" href="/entrypoint.css" />
7
- <script src="https://static.ordergroove.com/@ordergroove/smi-core/{{ SMI_CORE_VERSION }}/dist/smi.js"></script>
8
7
  <script>
9
8
  new EventSource('/esbuild').addEventListener('change', ev => {
10
- console.log('HRM', ev.data);
11
9
  let stylesheet = document.querySelector('link');
12
10
  try {
13
11
  const { updated, added } = JSON.parse(ev.data);
@@ -20,7 +18,8 @@
20
18
  document.head.appendChild(stylesheet);
21
19
  }
22
20
  if (updated.includes('/entrypoint.js')) {
23
- import('./entrypoint.js?_=' + ts).then(({ mainTemplate, smi }) => {
21
+ import('./entrypoint.js?_=' + ts).then(({ mainTemplate }) => {
22
+ const smi = window.og.smi;
24
23
  smi.HTMLSmiElement.template = mainTemplate(smi.html);
25
24
  });
26
25
  }
package/src/serve.js CHANGED
@@ -20,6 +20,9 @@ const setupSmiDevMode = async argv => {
20
20
  hmacAuth = customer && JSON.stringify([customer.merchant_user_id, customer.ts, customer.hash].join('|'));
21
21
  }
22
22
 
23
+ const packageJson = readPackageJson();
24
+ const smiCoreVersion = packageJson[packageJsonKeys.OG_SECTION]?.[packageJsonKeys.CORE_VERSION] || 'latest';
25
+
23
26
  return {
24
27
  name: 'resolve_smi_templates',
25
28
  setup(build) {
@@ -73,7 +76,8 @@ const setupSmiDevMode = async argv => {
73
76
  define: {
74
77
  OG_MERCHANT_ID: merchant ? JSON.stringify(merchant.public_id) : "''",
75
78
  OG_HMAC_AUTH: hmacAuth,
76
- OG_ENV: JSON.stringify(argv.env)
79
+ OG_ENV: JSON.stringify(argv.env),
80
+ OG_SMI_CORE_VERSION: JSON.stringify(smiCoreVersion)
77
81
  }
78
82
  })
79
83
  ).code;
@@ -103,13 +107,8 @@ async function serve(argv) {
103
107
 
104
108
  fs.mkdirSync(path.join(getcwd(), outdir), { recursive: true });
105
109
 
106
- const packageJson = readPackageJson();
107
- const smiCoreVersion = packageJson[packageJsonKeys.OG_SECTION]?.[packageJsonKeys.CORE_VERSION] || 'latest';
108
-
109
110
  const smiIndexFile = path.join(getcwd(), outdir, 'index.html');
110
- const smiIndexSource = fs
111
- .readFileSync(`${__dirname}/partials/index.html`, 'utf8')
112
- .replace('{{ SMI_CORE_VERSION }}', smiCoreVersion);
111
+ const smiIndexSource = fs.readFileSync(`${__dirname}/partials/index.html`, 'utf8');
113
112
  fs.writeFileSync(smiIndexFile, smiIndexSource, { encoding: 'utf8' });
114
113
 
115
114
  const buildConf = {