@mycatalog/cli 0.1.1 → 0.1.3
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/README.md +42 -2
- package/dist/bin.js +1 -1
- package/dist/commands/theme/index.js +37 -8
- package/dist/core/validate.d.ts +2 -0
- package/dist/core/validate.js +179 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,15 +4,55 @@
|
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
|
+
Requires Node.js 22 or later (`node --version`).
|
|
8
|
+
|
|
7
9
|
```sh
|
|
8
|
-
npm install
|
|
9
|
-
mycatalog
|
|
10
|
+
npm install -g @mycatalog/cli # -g is required: it puts the `mycatalog` command on your PATH
|
|
11
|
+
mycatalog --version
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
> **Do not** use `npm i @mycatalog/cli` (without `-g`) to get the command. That only adds the package to the current
|
|
15
|
+
> folder's `node_modules`, and your shell will say `command not found: mycatalog`.
|
|
16
|
+
|
|
17
|
+
No install at all? Run any command with `npx`:
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npx @mycatalog/cli login
|
|
21
|
+
npx @mycatalog/cli theme dev
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### `command not found: mycatalog`
|
|
25
|
+
|
|
26
|
+
1. Install globally: `npm install -g @mycatalog/cli`.
|
|
27
|
+
2. Still missing? Your npm global folder is not on PATH. Run `npm config get prefix`; its `bin` folder (for example
|
|
28
|
+
`/usr/local/bin` or `~/.npm-global/bin`) must be in your PATH. Add it to `~/.zshrc`, then open a new terminal:
|
|
29
|
+
`export PATH="$(npm config get prefix)/bin:$PATH"`.
|
|
30
|
+
3. `EACCES` permission error on install: do not use `sudo`. Use a Node version manager (nvm, fnm) or set
|
|
31
|
+
`npm config set prefix ~/.npm-global` and add `~/.npm-global/bin` to PATH.
|
|
32
|
+
|
|
33
|
+
## Quick start
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
mycatalog login # shows a code; approve it in your browser
|
|
37
|
+
mycatalog theme list # your themes and version ids
|
|
38
|
+
mycatalog theme init my-theme # or cd into an existing theme folder
|
|
39
|
+
cd my-theme # set themeId, versionId and storeId in mycatalog.json
|
|
40
|
+
mycatalog theme check # same checks as the server
|
|
41
|
+
mycatalog theme dev # upload, print a preview link, re-upload on save
|
|
10
42
|
```
|
|
11
43
|
|
|
12
44
|
Login uses the OAuth device flow. It displays a code and verification page, then asks before opening the browser. Use
|
|
13
45
|
`--open` to open the page immediately. Local development may use `--api-url http://localhost:3000
|
|
14
46
|
--insecure-localhost`; plain HTTP is limited to loopback hosts.
|
|
15
47
|
|
|
48
|
+
## Update
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
npm install -g @mycatalog/cli@latest
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The CLI tells you about a new version at most once a day; it never updates itself.
|
|
55
|
+
|
|
16
56
|
## Commands
|
|
17
57
|
|
|
18
58
|
- `mycatalog login [--api-url URL] [--insecure-localhost] [--open]` — authenticate with the device flow.
|
package/dist/bin.js
CHANGED
|
@@ -5,7 +5,7 @@ import { Api } from "./core/api.js";
|
|
|
5
5
|
import * as theme from "./commands/theme/index.js";
|
|
6
6
|
import { print, redact } from "./core/output.js";
|
|
7
7
|
import { checkForUpdate } from "./core/update.js";
|
|
8
|
-
const VERSION = "0.1.
|
|
8
|
+
const VERSION = "0.1.3";
|
|
9
9
|
const HELP = `mycatalog <command>
|
|
10
10
|
|
|
11
11
|
Commands:
|
|
@@ -7,6 +7,7 @@ import { Api } from "../../core/api.js";
|
|
|
7
7
|
import { loadConfig } from "../../core/config.js";
|
|
8
8
|
import { assertSafeTarget, DIRS, ignored, safeRelative, validateUpload } from "../../core/paths.js";
|
|
9
9
|
import { clean, print } from "../../core/output.js";
|
|
10
|
+
import { validateLocales, validateSectionSchema } from "../../core/validate.js";
|
|
10
11
|
const API = "/api/developer/themes";
|
|
11
12
|
const FORBIDDEN = /<(?:script|form|input)\b|\bon\w+\s*=|\bstyle\s*=/i;
|
|
12
13
|
async function projectFiles(root = process.cwd()) {
|
|
@@ -20,7 +21,7 @@ async function projectFiles(root = process.cwd()) {
|
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
const files = [];
|
|
23
|
-
for (const directory of
|
|
24
|
+
for (const directory of ["sections"]) { // snippets, locales and assets are optional on the server
|
|
24
25
|
for (const entry of await readdir(join(root, directory), { withFileTypes: true }).catch(() => [])) {
|
|
25
26
|
const path = `${directory}/${entry.name}`;
|
|
26
27
|
if (!entry.isFile() || ignored(path, patterns)) {
|
|
@@ -59,7 +60,7 @@ export async function init(directory = ".") {
|
|
|
59
60
|
/** Check required folders, JSON, forbidden markup, animation prefixes, and byte limits locally. */
|
|
60
61
|
export async function check() {
|
|
61
62
|
const errors = [];
|
|
62
|
-
for (const directory of
|
|
63
|
+
for (const directory of ["sections"]) { // snippets, locales and assets are optional on the server
|
|
63
64
|
try {
|
|
64
65
|
if (!(await stat(directory)).isDirectory()) {
|
|
65
66
|
errors.push(`Missing folder ${directory}/`);
|
|
@@ -86,6 +87,29 @@ export async function check() {
|
|
|
86
87
|
errors.push(`${file.path}: @keyframes names must start with theme-`);
|
|
87
88
|
}
|
|
88
89
|
}
|
|
90
|
+
const files = await projectFiles();
|
|
91
|
+
for (const file of files.filter((candidate) => candidate.path.startsWith("sections/") && candidate.path.endsWith(".liquid"))) {
|
|
92
|
+
try {
|
|
93
|
+
const { schema } = sectionPayload(file, files);
|
|
94
|
+
const error = validateSectionSchema(schema);
|
|
95
|
+
if (error)
|
|
96
|
+
errors.push(`${file.path}: ${error}`);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
errors.push(`${file.path}: ${error instanceof Error ? error.message : "invalid schema"}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const localeFiles = files.filter((file) => file.path.startsWith("locales/"));
|
|
103
|
+
if (localeFiles.length) {
|
|
104
|
+
try {
|
|
105
|
+
const error = validateLocales(localeBundle(localeFiles));
|
|
106
|
+
if (error)
|
|
107
|
+
errors.push(`locales/: ${error}`);
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
errors.push(`locales/: ${error instanceof Error ? error.message : "invalid locale bundle"}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
89
113
|
if (errors.length) {
|
|
90
114
|
throw new Error(errors.join("\n"));
|
|
91
115
|
}
|
|
@@ -177,6 +201,16 @@ async function pushSection(api, base, file, files) {
|
|
|
177
201
|
if (!name.endsWith(".liquid")) {
|
|
178
202
|
return;
|
|
179
203
|
}
|
|
204
|
+
const { template, schema } = sectionPayload(file, files);
|
|
205
|
+
const cssFile = files.find((candidate) => candidate.path === `sections/${name.replace(/\.liquid$/, ".css")}`);
|
|
206
|
+
await api.request(`${base}/sections/${encodeURIComponent(name.replace(/\.liquid$/, ""))}`, {
|
|
207
|
+
method: "PUT",
|
|
208
|
+
headers: { "content-type": "application/json" },
|
|
209
|
+
body: JSON.stringify({ template, schema, css: cssFile?.bytes.toString("utf8") ?? null }),
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
function sectionPayload(file, files) {
|
|
213
|
+
const name = file.path.slice("sections/".length);
|
|
180
214
|
const source = file.bytes.toString("utf8");
|
|
181
215
|
const schemaMatch = /{%[-]?\s*schema\s*[-]?%}([\s\S]*?){%[-]?\s*endschema\s*[-]?%}/.exec(source);
|
|
182
216
|
let template = source;
|
|
@@ -192,12 +226,7 @@ async function pushSection(api, base, file, files) {
|
|
|
192
226
|
}
|
|
193
227
|
schema = JSON.parse(sidecar.bytes.toString("utf8"));
|
|
194
228
|
}
|
|
195
|
-
|
|
196
|
-
await api.request(`${base}/sections/${encodeURIComponent(name.replace(/\.liquid$/, ""))}`, {
|
|
197
|
-
method: "PUT",
|
|
198
|
-
headers: { "content-type": "application/json" },
|
|
199
|
-
body: JSON.stringify({ template, schema, css: cssFile?.bytes.toString("utf8") ?? null }),
|
|
200
|
-
});
|
|
229
|
+
return { template, schema };
|
|
201
230
|
}
|
|
202
231
|
/** List themes returned by the developer API. */
|
|
203
232
|
export async function list() {
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
const MAX_SETTINGS = 40;
|
|
2
|
+
const MAX_SCHEMA_BYTES = 32 * 1024;
|
|
3
|
+
const ID_RE = /^[a-z][a-z0-9_]{0,39}$/;
|
|
4
|
+
const REF_RE = /^[A-Za-z0-9_-]{1,40}$/;
|
|
5
|
+
const METAFIELD_RE = /^[a-z0-9_]{1,40}(\.[a-z0-9_]{1,40})?$/;
|
|
6
|
+
const COMMON_KEYS = ["id", "type", "label", "default"];
|
|
7
|
+
const EXTRA_KEYS = {
|
|
8
|
+
text: ["max"], textarea: ["max"], number: ["min", "max"], checkbox: [], select: ["options"], color: [], image: [],
|
|
9
|
+
collection: [], product: [], category: [], link: [], product_list: ["max"], metafield_key: ["max"],
|
|
10
|
+
};
|
|
11
|
+
const isObj = (value) => !!value && typeof value === "object" && !Array.isArray(value);
|
|
12
|
+
const finite = (value) => typeof value === "number" && Number.isFinite(value);
|
|
13
|
+
const strLen = (value, min, max) => typeof value === "string" && value.length >= min && value.length <= max;
|
|
14
|
+
function checkField(raw, path, seen) {
|
|
15
|
+
if (!isObj(raw))
|
|
16
|
+
return `${path}: must be an object`;
|
|
17
|
+
const type = raw.type;
|
|
18
|
+
if (typeof type !== "string" || !Object.hasOwn(EXTRA_KEYS, type))
|
|
19
|
+
return `${path}.type: unknown type`;
|
|
20
|
+
const allowed = [...COMMON_KEYS, ...EXTRA_KEYS[type]];
|
|
21
|
+
for (const key of Object.keys(raw))
|
|
22
|
+
if (!allowed.includes(key))
|
|
23
|
+
return `${path}.${key}: unknown key for type ${type}`;
|
|
24
|
+
if (typeof raw.id !== "string" || !ID_RE.test(raw.id))
|
|
25
|
+
return `${path}.id: must match ${ID_RE}`;
|
|
26
|
+
if (seen.has(raw.id))
|
|
27
|
+
return `${path}.id: duplicate id \"${raw.id}\"`;
|
|
28
|
+
seen.add(raw.id);
|
|
29
|
+
if (!strLen(raw.label, 1, 80))
|
|
30
|
+
return `${path}.label: 1-80 characters required`;
|
|
31
|
+
const cap = type === "text" ? 500 : type === "textarea" ? 5000 : type === "product_list" ? 24 : type === "metafield_key" ? 8 : 0;
|
|
32
|
+
if (raw.max !== undefined && type !== "number" && !(Number.isInteger(raw.max) && raw.max >= 1 && raw.max <= cap))
|
|
33
|
+
return `${path}.max: integer 1-${cap} required`;
|
|
34
|
+
if (type === "number") {
|
|
35
|
+
if (!finite(raw.min) || !finite(raw.max))
|
|
36
|
+
return `${path}: number needs finite min and max`;
|
|
37
|
+
if (raw.min > raw.max)
|
|
38
|
+
return `${path}.min: must be <= max`;
|
|
39
|
+
}
|
|
40
|
+
if (type === "select") {
|
|
41
|
+
if (!Array.isArray(raw.options) || raw.options.length < 1 || raw.options.length > 30)
|
|
42
|
+
return `${path}.options: 1-30 options required`;
|
|
43
|
+
const values = new Set();
|
|
44
|
+
for (let i = 0; i < raw.options.length; i++) {
|
|
45
|
+
const option = raw.options[i];
|
|
46
|
+
if (!isObj(option) || Object.keys(option).some((key) => key !== "value" && key !== "label"))
|
|
47
|
+
return `${path}.options[${i}]: only value and label allowed`;
|
|
48
|
+
if (!strLen(option.value, 1, 60) || !strLen(option.label, 1, 80))
|
|
49
|
+
return `${path}.options[${i}]: value (1-60) and label (1-80) required`;
|
|
50
|
+
if (values.has(option.value))
|
|
51
|
+
return `${path}.options[${i}].value: duplicate \"${option.value}\"`;
|
|
52
|
+
values.add(option.value);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const valueError = (value) => {
|
|
56
|
+
switch (type) {
|
|
57
|
+
case "text": return typeof value === "string" && value.trim().length <= (raw.max ?? 500) && !/[\u0000-\u001f\u007f]/.test(value.trim()) ? null : "Invalid input";
|
|
58
|
+
case "textarea": return typeof value === "string" && value.trim().length <= (raw.max ?? 5000) && !/[\u0000-\u0008\u000b-\u001f\u007f]/.test(value.trim()) ? null : "Invalid input";
|
|
59
|
+
case "number": return finite(value) && value >= raw.min && value <= raw.max ? null : "Too small: expected number to be >= " + raw.min;
|
|
60
|
+
case "checkbox": return typeof value === "boolean" ? null : "Invalid input";
|
|
61
|
+
case "select": return raw.options instanceof Array && raw.options.some((option) => isObj(option) && option.value === value) ? null : "Invalid option";
|
|
62
|
+
case "color": return typeof value === "string" && /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i.test(value) ? null : "Invalid string: must match pattern /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i";
|
|
63
|
+
case "image": return typeof value === "string" && (value === "" || (/^(?![a-z][a-z0-9+.-]*:|\/\/|\\)/i.test(value.trim()) && !/[?#\\\0-\x1f]/.test(value.trim()) && !value.trim().split("/").includes(".."))) ? null : "Use a store upload path";
|
|
64
|
+
case "link": return typeof value === "string" && value.trim().length <= 2048 && (value === "" || (value.startsWith("/") && !value.startsWith("//") && !/[\\\u0000-\u001f]/.test(value) || /^https:\/\//i.test(value) && !/[\\\u0000-\u001f]/.test(value))) ? null : "Use a store path or an https link";
|
|
65
|
+
case "product":
|
|
66
|
+
case "collection":
|
|
67
|
+
case "category": return typeof value === "string" && REF_RE.test(value) ? null : "Invalid id";
|
|
68
|
+
case "product_list": return Array.isArray(value) && value.length <= (raw.max ?? 24) && value.every((item) => typeof item === "string" && REF_RE.test(item)) ? null : "Invalid input";
|
|
69
|
+
case "metafield_key": return Array.isArray(value) && value.length <= (raw.max ?? 8) && value.every((item) => typeof item === "string" && METAFIELD_RE.test(item)) ? null : "Invalid input";
|
|
70
|
+
default: return "Invalid input";
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
if (raw.default !== undefined) {
|
|
74
|
+
const error = valueError(raw.default);
|
|
75
|
+
if (error)
|
|
76
|
+
return `${path}.default: ${error}`;
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
export function validateSectionSchema(schema) {
|
|
81
|
+
if (!isObj(schema))
|
|
82
|
+
return "schema: must be an object";
|
|
83
|
+
for (const key of Object.keys(schema))
|
|
84
|
+
if (!["name", "settings", "max_blocks", "blocks"].includes(key))
|
|
85
|
+
return `${key}: unknown key`;
|
|
86
|
+
if (!strLen(schema.name, 1, 80))
|
|
87
|
+
return "name: 1-80 characters required";
|
|
88
|
+
if (!Array.isArray(schema.settings) || schema.settings.length > MAX_SETTINGS)
|
|
89
|
+
return `settings: array of at most ${MAX_SETTINGS} required`;
|
|
90
|
+
const maxBlocks = schema.max_blocks === undefined ? 16 : schema.max_blocks;
|
|
91
|
+
if (!Number.isInteger(maxBlocks) || maxBlocks < 1 || maxBlocks > 50)
|
|
92
|
+
return "max_blocks: integer 1-50 required";
|
|
93
|
+
if (schema.blocks !== undefined && (!Array.isArray(schema.blocks) || schema.blocks.length > 10))
|
|
94
|
+
return "blocks: array of at most 10 types required";
|
|
95
|
+
let json;
|
|
96
|
+
try {
|
|
97
|
+
json = JSON.stringify(schema);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return "schema: not serializable";
|
|
101
|
+
}
|
|
102
|
+
if (Buffer.byteLength(json, "utf8") > MAX_SCHEMA_BYTES)
|
|
103
|
+
return "schema: larger than 32 KB";
|
|
104
|
+
const seen = new Set();
|
|
105
|
+
for (let i = 0; i < schema.settings.length; i++) {
|
|
106
|
+
const error = checkField(schema.settings[i], `settings[${i}]`, seen);
|
|
107
|
+
if (error)
|
|
108
|
+
return error;
|
|
109
|
+
}
|
|
110
|
+
const types = new Set();
|
|
111
|
+
for (let i = 0; i < (Array.isArray(schema.blocks) ? schema.blocks.length : 0); i++) {
|
|
112
|
+
const block = schema.blocks[i];
|
|
113
|
+
const path = `blocks[${i}]`;
|
|
114
|
+
if (!isObj(block))
|
|
115
|
+
return `${path}: must be an object`;
|
|
116
|
+
for (const key of Object.keys(block))
|
|
117
|
+
if (!["type", "name", "limit", "settings"].includes(key))
|
|
118
|
+
return `${path}.${key}: unknown key`;
|
|
119
|
+
if (typeof block.type !== "string" || !/^[a-z][a-z0-9_-]{0,39}$/.test(block.type))
|
|
120
|
+
return `${path}.type: invalid block type`;
|
|
121
|
+
if (types.has(block.type))
|
|
122
|
+
return `${path}.type: duplicate block type`;
|
|
123
|
+
types.add(block.type);
|
|
124
|
+
if (!strLen(block.name, 1, 80))
|
|
125
|
+
return `${path}.name: 1-80 characters required`;
|
|
126
|
+
if (!Array.isArray(block.settings) || block.settings.length > MAX_SETTINGS)
|
|
127
|
+
return `${path}.settings: array of at most ${MAX_SETTINGS} required`;
|
|
128
|
+
if (block.limit !== undefined && (!Number.isInteger(block.limit) || block.limit < 1 || block.limit > maxBlocks))
|
|
129
|
+
return `${path}.limit: integer 1-${maxBlocks} required`;
|
|
130
|
+
const ids = new Set();
|
|
131
|
+
for (let j = 0; j < block.settings.length; j++) {
|
|
132
|
+
const error = checkField(block.settings[j], `${path}.settings[${j}]`, ids);
|
|
133
|
+
if (error)
|
|
134
|
+
return error;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
const MAX_LOCALES = 10;
|
|
140
|
+
const MAX_LOCALE_BYTES = 64 * 1024;
|
|
141
|
+
const MAX_DEPTH = 5;
|
|
142
|
+
const CODE_RE = /^[a-z]{2,3}(-[A-Za-z0-9]{2,8})?$/;
|
|
143
|
+
const KEY_RE = /^[A-Za-z0-9_-]{1,64}$/;
|
|
144
|
+
function checkLocaleTree(value, depth) {
|
|
145
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
146
|
+
return "Each locale must be a JSON object";
|
|
147
|
+
if (depth > MAX_DEPTH)
|
|
148
|
+
return `Keys can nest at most ${MAX_DEPTH} levels`;
|
|
149
|
+
for (const [key, item] of Object.entries(value)) {
|
|
150
|
+
if (!KEY_RE.test(key) || key === "__proto__")
|
|
151
|
+
return `Invalid key \"${key.slice(0, 40)}\": use letters, digits, _ and -`;
|
|
152
|
+
if (typeof item === "string")
|
|
153
|
+
continue;
|
|
154
|
+
const error = checkLocaleTree(item, depth + 1);
|
|
155
|
+
if (error)
|
|
156
|
+
return error;
|
|
157
|
+
}
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
export function validateLocales(bundle) {
|
|
161
|
+
if (!isObj(bundle) || typeof bundle.default !== "string" || !bundle.locales || typeof bundle.locales !== "object" || Array.isArray(bundle.locales))
|
|
162
|
+
return 'Expected { "default": "en", "locales": { "en": { ... } } }';
|
|
163
|
+
const locales = bundle.locales;
|
|
164
|
+
const codes = Object.keys(locales);
|
|
165
|
+
if (codes.length > MAX_LOCALES)
|
|
166
|
+
return `At most ${MAX_LOCALES} locales`;
|
|
167
|
+
if (!codes.includes(bundle.default))
|
|
168
|
+
return "The default language must have a locale";
|
|
169
|
+
for (const code of codes) {
|
|
170
|
+
if (!CODE_RE.test(code))
|
|
171
|
+
return `Invalid language code \"${code.slice(0, 20)}\"`;
|
|
172
|
+
const error = checkLocaleTree(locales[code], 1);
|
|
173
|
+
if (error)
|
|
174
|
+
return `${code}: ${error}`;
|
|
175
|
+
if (JSON.stringify(locales[code]).length > MAX_LOCALE_BYTES)
|
|
176
|
+
return `${code}: locale is over 64 KB`;
|
|
177
|
+
}
|
|
178
|
+
return null;
|
|
179
|
+
}
|