@hubspot/cms-dev-server 0.3.0 → 0.3.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.
@@ -6,13 +6,14 @@ import { normalizePath } from 'vite';
6
6
  import createDevServerRoutes from './routes/createDevServerRoutes.js';
7
7
  import { getProjectNameFromHSConfig } from './lib/proxyHelpers.js';
8
8
  import createViteDevServer from './createViteDevServer.js';
9
+ import { debug } from './lib/debug.js';
9
10
  import { getEnv, getAccountId, loadConfig, validateConfig, } from '@hubspot/cli-lib/lib/config.js';
10
11
  const BASE_PATH = '/';
11
12
  const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
12
13
  const STORYBOOK_PORT = process.env.STORYBOOK_PORT
13
14
  ? parseInt(process.env.STORYBOOK_PORT, 10)
14
15
  : 3123;
15
- async function createDevServer(webRoot, storybook, hubspotCliConfigPathOption, hubspotCliAccountNameOption, debug) {
16
+ async function createDevServer(webRoot, storybook, hubspotCliConfigPathOption, hubspotCliAccountNameOption) {
16
17
  console.log('Warning: this is a prerelease and requires specific HubSpot account configuration to work');
17
18
  let authedPortalId = null;
18
19
  let authedEnv = null;
@@ -86,7 +87,7 @@ HubSpot CMS dev server hosting: ${expressServer.locals.projectName}/${expressSer
86
87
  },
87
88
  });
88
89
  console.log(`Starting storybook server on port ${STORYBOOK_PORT}`);
89
- if (debug) {
90
+ if (debug.enabled) {
90
91
  storybookProcess.stdout.pipe(process.stdout);
91
92
  }
92
93
  storybookProcess.stderr.pipe(process.stderr);
package/dist/lib/cli.js CHANGED
@@ -16,12 +16,10 @@ export function getDevServerCommandLineOptions(argv) {
16
16
  args.splice(accountOptionIndex, 2);
17
17
  }
18
18
  const storybook = args.findIndex(arg => arg === '--storybook') > 0;
19
- const debug = args.findIndex(arg => arg === '--debug') > 0;
20
19
  return {
21
20
  webRoot,
22
21
  hubspotCliConfigPathOption,
23
22
  hubspotCliAccountNameOption,
24
23
  storybook,
25
- debug,
26
24
  };
27
25
  }
@@ -0,0 +1,2 @@
1
+ import { default as debugFactory } from 'debug';
2
+ export const debug = debugFactory('cms-dev-server');
@@ -7,10 +7,17 @@ import moduleRouteHandler from './module.js';
7
7
  import proxyPageRouteHandler from './proxyPageRouteHandler.js';
8
8
  import proxyPageResourceRedirect from './proxyPageResourceRedirect.js';
9
9
  import homeRouteHandler from './homeRouteHandler.js';
10
+ import { debug } from '../lib/debug.js';
10
11
  import { VALID_PROXY_DOMAIN_SUFFIXES } from '../proxyPage/constants.js';
11
12
  const STORYBOOK_DOMAINS = VALID_PROXY_DOMAIN_SUFFIXES.map(domain => `storybook.${domain}`);
12
13
  const createDevServerRoutes = async (viteServer, storybookPort) => {
13
14
  const devServerApp = Router();
15
+ // Debug incoming requests, enable with the DEBUG=cms-dev-server env variable
16
+ devServerApp.use((request, response, next) => {
17
+ const { method, url, hostname } = request;
18
+ debug(`${method}:${hostname}${url}`);
19
+ next();
20
+ });
14
21
  if (storybookPort) {
15
22
  devServerApp.use(createProxyMiddleware((__pathname, req) => STORYBOOK_DOMAINS.includes(req.hostname), {
16
23
  logLevel: 'warn',
package/dist/run.js CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
  import { getDevServerCommandLineOptions } from './lib/cli.js';
3
3
  import createDevServer from './createDevServer.js';
4
- const { webRoot, hubspotCliConfigPathOption, hubspotCliAccountNameOption, storybook, debug, } = getDevServerCommandLineOptions(process.argv);
4
+ const { webRoot, hubspotCliConfigPathOption, hubspotCliAccountNameOption, storybook, } = getDevServerCommandLineOptions(process.argv);
5
5
  if (!webRoot) {
6
6
  throw new Error(`No web root path argument specified`);
7
7
  }
8
- createDevServer(webRoot, storybook, hubspotCliConfigPathOption, hubspotCliAccountNameOption, debug);
8
+ createDevServer(webRoot, storybook, hubspotCliConfigPathOption, hubspotCliAccountNameOption);
@@ -0,0 +1,42 @@
1
+ export default function getFieldsArgTypes(fieldsJson, currentPathSegments = []) {
2
+ let resultArgTypes = {};
3
+ for (const field of fieldsJson) {
4
+ // storybook doesn't support nested controls, so emulate our nested
5
+ // structure with group1.group2.field structure
6
+ const fullFieldName = [...currentPathSegments, field.name].join('.');
7
+ // should this definition of fieldType => argType live next to fields in cms-components?
8
+ switch (field.type) {
9
+ case 'group': {
10
+ if (!field.children) {
11
+ continue;
12
+ }
13
+ const groupResult = getFieldsArgTypes(field.children, [
14
+ ...currentPathSegments,
15
+ field.name,
16
+ ]);
17
+ resultArgTypes = {
18
+ ...resultArgTypes,
19
+ ...groupResult,
20
+ };
21
+ break;
22
+ }
23
+ case 'boolean': {
24
+ resultArgTypes[fullFieldName] = {
25
+ control: 'boolean',
26
+ defaultValue: field.default,
27
+ };
28
+ break;
29
+ }
30
+ case 'text': {
31
+ resultArgTypes[fullFieldName] = {
32
+ control: 'text',
33
+ defaultValue: field.default,
34
+ };
35
+ break;
36
+ }
37
+ default:
38
+ break;
39
+ }
40
+ }
41
+ return resultArgTypes;
42
+ }
@@ -0,0 +1,67 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { describe, expect, it } from 'vitest';
3
+ import { ModuleFields, BooleanField, TextField, FieldGroup, } from '@hubspot/cms-components/fields';
4
+ import { renderFieldsToJson } from '@hubspot/cms-js-internal';
5
+ import getFieldsArgTypes from './getFieldsArgTypes.js';
6
+ describe('getFieldsArgTypes', () => {
7
+ it('creates argType for boolean field', () => {
8
+ const fields = (_jsx(ModuleFields, { children: _jsx(BooleanField, { name: "testBooleanField", label: "field", default: false }) }));
9
+ const argTypes = getFieldsArgTypes(renderFieldsToJson(fields));
10
+ expect(argTypes).toStrictEqual({
11
+ testBooleanField: {
12
+ control: 'boolean',
13
+ defaultValue: false,
14
+ },
15
+ });
16
+ });
17
+ it('creates text argType for text field', () => {
18
+ const fields = (_jsx(ModuleFields, { children: _jsx(TextField, { name: "testStringField", label: "field", default: 'test' }) }));
19
+ const argTypes = getFieldsArgTypes(renderFieldsToJson(fields));
20
+ expect(argTypes).toStrictEqual({
21
+ testStringField: {
22
+ control: 'text',
23
+ defaultValue: 'test',
24
+ },
25
+ });
26
+ });
27
+ it('prefixes fields in a group with . separated group names', () => {
28
+ const fields = (_jsx(ModuleFields, { children: _jsxs(FieldGroup, { name: "mygroup", label: "field group", children: [_jsx(TextField, { name: "testStringField", label: "field", default: 'test' }), _jsx(BooleanField, { name: "testBooleanField", label: "field", default: false })] }) }));
29
+ const argTypes = getFieldsArgTypes(renderFieldsToJson(fields));
30
+ expect(argTypes).toStrictEqual({
31
+ 'mygroup.testStringField': {
32
+ control: 'text',
33
+ defaultValue: 'test',
34
+ },
35
+ 'mygroup.testBooleanField': {
36
+ control: 'boolean',
37
+ defaultValue: false,
38
+ },
39
+ });
40
+ });
41
+ it('handles multiple nesting alongside top level values', () => {
42
+ const fields = (_jsxs(ModuleFields, { children: [_jsx(TextField, { name: "toplevel", label: "top level text field", default: "defaultvalue" }), _jsxs(FieldGroup, { name: "mygroup", label: "field group", children: [_jsx(TextField, { name: "testStringField", label: "field", default: 'test' }), _jsx(BooleanField, { name: "testBooleanField", label: "field", default: false }), _jsxs(FieldGroup, { name: "nestedgroup", label: "nested group", children: [_jsx(TextField, { name: "testStringField", label: "field", default: 'test' }), _jsx(BooleanField, { name: "testBooleanField", label: "field", default: false })] })] })] }));
43
+ const argTypes = getFieldsArgTypes(renderFieldsToJson(fields));
44
+ expect(argTypes).toStrictEqual({
45
+ toplevel: {
46
+ control: 'text',
47
+ defaultValue: 'defaultvalue',
48
+ },
49
+ 'mygroup.testStringField': {
50
+ control: 'text',
51
+ defaultValue: 'test',
52
+ },
53
+ 'mygroup.testBooleanField': {
54
+ control: 'boolean',
55
+ defaultValue: false,
56
+ },
57
+ 'mygroup.nestedgroup.testStringField': {
58
+ control: 'text',
59
+ defaultValue: 'test',
60
+ },
61
+ 'mygroup.nestedgroup.testBooleanField': {
62
+ control: 'boolean',
63
+ defaultValue: false,
64
+ },
65
+ });
66
+ });
67
+ });
@@ -0,0 +1,47 @@
1
+ import { Field } from '@hubspot/cms-components/fields';
2
+
3
+ export default function getFieldsArgTypes(
4
+ fieldsJson: Field[],
5
+ currentPathSegments = []
6
+ ) {
7
+ let resultArgTypes = {};
8
+ for (const field of fieldsJson) {
9
+ // storybook doesn't support nested controls, so emulate our nested
10
+ // structure with group1.group2.field structure
11
+ const fullFieldName = [...currentPathSegments, field.name].join('.');
12
+ // should this definition of fieldType => argType live next to fields in cms-components?
13
+ switch (field.type) {
14
+ case 'group': {
15
+ if (!field.children) {
16
+ continue;
17
+ }
18
+ const groupResult = getFieldsArgTypes(field.children, [
19
+ ...currentPathSegments,
20
+ field.name,
21
+ ]);
22
+ resultArgTypes = {
23
+ ...resultArgTypes,
24
+ ...groupResult,
25
+ };
26
+ break;
27
+ }
28
+ case 'boolean': {
29
+ resultArgTypes[fullFieldName] = {
30
+ control: 'boolean',
31
+ defaultValue: field.default,
32
+ };
33
+ break;
34
+ }
35
+ case 'text': {
36
+ resultArgTypes[fullFieldName] = {
37
+ control: 'text',
38
+ defaultValue: field.default,
39
+ };
40
+ break;
41
+ }
42
+ default:
43
+ break;
44
+ }
45
+ }
46
+ return resultArgTypes;
47
+ }
@@ -0,0 +1,22 @@
1
+ // default values come through via `defaultValue` in the story's argTypes, we
2
+ // need to re-nest them to match the field structure
3
+ export default function getNestedFieldObjectForArgs(_args) {
4
+ const result = {};
5
+ for (const argPath in _args) {
6
+ if (Object.prototype.hasOwnProperty.call(_args, argPath)) {
7
+ const argPathSegments = argPath.split('.');
8
+ const nestedSegments = argPathSegments.slice(0, -1);
9
+ const fieldName = argPathSegments[argPathSegments.length - 1];
10
+ const fieldValue = _args[argPath];
11
+ let currentNest = result;
12
+ for (const segment of nestedSegments) {
13
+ if (!Object.prototype.hasOwnProperty.call(currentNest, segment)) {
14
+ currentNest[segment] = {};
15
+ }
16
+ currentNest = currentNest[segment];
17
+ }
18
+ currentNest[fieldName] = fieldValue;
19
+ }
20
+ }
21
+ return result;
22
+ }
@@ -0,0 +1,40 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import getNestedFieldObjectForArgs from './getNestedFieldObjectForArgs.js';
3
+ describe('getNestedFieldObjectForArgs', () => {
4
+ it('leaves top level args as is', () => {
5
+ const argMap = {
6
+ testField: 'testValue',
7
+ testField2: 'testValue',
8
+ };
9
+ const result = getNestedFieldObjectForArgs(argMap);
10
+ expect(result).toStrictEqual(argMap);
11
+ });
12
+ it('turns a . separated arg name into a nested field', () => {
13
+ const argMap = {
14
+ 'group.testField': 'testValue',
15
+ topLevelField: 'testValue',
16
+ };
17
+ const result = getNestedFieldObjectForArgs(argMap);
18
+ expect(result).toStrictEqual({
19
+ group: {
20
+ testField: 'testValue',
21
+ },
22
+ topLevelField: 'testValue',
23
+ });
24
+ });
25
+ it('keeps nested fields with the same group prefix nested in the same object', () => {
26
+ const argMap = {
27
+ 'group.testField': 'testValue',
28
+ 'group.secondField': 'testValue',
29
+ topLevelField: 'testValue',
30
+ };
31
+ const result = getNestedFieldObjectForArgs(argMap);
32
+ expect(result).toStrictEqual({
33
+ group: {
34
+ testField: 'testValue',
35
+ secondField: 'testValue',
36
+ },
37
+ topLevelField: 'testValue',
38
+ });
39
+ });
40
+ });
@@ -35,8 +35,20 @@ function getFieldsArgTypes(fieldsJson, currentPathSegments = []) {
35
35
  };
36
36
  break;
37
37
  }
38
- default:
38
+ case "number": {
39
+ resultArgTypes[fullFieldName] = {
40
+ control: "number",
41
+ defaultValue: field.default
42
+ };
39
43
  break;
44
+ }
45
+ default: {
46
+ resultArgTypes[fullFieldName] = {
47
+ control: "object",
48
+ defaultValue: field.default
49
+ };
50
+ break;
51
+ }
40
52
  }
41
53
  }
42
54
  return resultArgTypes;
@@ -37,8 +37,20 @@ function getFieldsArgTypes(fieldsJson, currentPathSegments = []) {
37
37
  };
38
38
  break;
39
39
  }
40
- default:
40
+ case 'number': {
41
+ resultArgTypes[fullFieldName] = {
42
+ control: 'number',
43
+ defaultValue: field.default,
44
+ };
41
45
  break;
46
+ }
47
+ default: {
48
+ resultArgTypes[fullFieldName] = {
49
+ control: 'object',
50
+ defaultValue: field.default,
51
+ };
52
+ break;
53
+ }
42
54
  }
43
55
  }
44
56
  return resultArgTypes;
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hubspot/cms-dev-server",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "engines": {
@@ -21,8 +21,8 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@hubspot/cli-lib": "^3.0.12",
24
- "@hubspot/cms-components": "^0.3.0",
25
- "@hubspot/cms-js-internal": "^0.3.0",
24
+ "@hubspot/cms-components": "^0.3.1",
25
+ "@hubspot/cms-js-internal": "^0.3.1",
26
26
  "@storybook/addon-actions": "^7.0.0-beta.19",
27
27
  "@storybook/addon-essentials": "^7.0.0-beta.19",
28
28
  "@storybook/addon-interactions": "^7.0.0-beta.19",
@@ -33,6 +33,7 @@
33
33
  "@storybook/testing-library": "^0.0.13",
34
34
  "@vitejs/plugin-react": "^3.0.0",
35
35
  "ansi-to-html": "0.7.x",
36
+ "debug": "^4.3.4",
36
37
  "express": "^4.17.3",
37
38
  "http-proxy-middleware": "^2.0.6",
38
39
  "react": "^18.1.0",
@@ -84,5 +85,5 @@
84
85
  "publishConfig": {
85
86
  "access": "public"
86
87
  },
87
- "gitHead": "e995b0c502a618dd584a9ae887405aef3bc3f962"
88
+ "gitHead": "3eaa36708908e15af14d091ba1995b9bcd6caad7"
88
89
  }
@@ -0,0 +1,9 @@
1
+ import { Field } from '@hubspot/cms-components/fields';
2
+ declare type ArgTypes = {
3
+ [fieldPath: string]: {
4
+ control: string;
5
+ defaultValue: any;
6
+ };
7
+ };
8
+ export default function getFieldsArgTypes(fieldsJson: Field[], currentPathSegments?: string[]): ArgTypes;
9
+ export {};
@@ -0,0 +1,6 @@
1
+ import { Args } from './types.js';
2
+ declare type FieldValues = {
3
+ [fieldNameOrGroupName: string]: FieldValues;
4
+ };
5
+ export default function getNestedFieldObjectForArgs(_args: Args): FieldValues;
6
+ export {};
@@ -0,0 +1,3 @@
1
+ export declare type Args = {
2
+ [fieldPath: string]: any;
3
+ };