@shop-on/theme-contract 0.2.0 → 0.4.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/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/preset-materializer.d.ts +9 -0
- package/dist/preset-materializer.js +70 -0
- package/dist/store-path.d.ts +4 -0
- package/dist/store-path.js +5 -0
- package/package.json +3 -1
package/dist/index.d.ts
CHANGED
|
@@ -8,4 +8,5 @@ import { ARTICLE_ATTRIBUTE_KEYS, ArticleAttributeKey, BLOG_ATTRIBUTE_KEYS, BlogA
|
|
|
8
8
|
import { metaobjectGid, metaobjectIdFromGid } from "./metaobject-gid.js";
|
|
9
9
|
import { RICHTEXT_ALLOWED_TAGS, RichtextTag } from "./richtext.js";
|
|
10
10
|
import { isNodeIdToken, nodeGid } from "./node-id.js";
|
|
11
|
-
|
|
11
|
+
import { INSTALL_ID_RE, STORE_SLUG_RE } from "./store-path.js";
|
|
12
|
+
export { ARTICLE_ATTRIBUTE_KEYS, ArticleAttributeKey, BLOCK_PATH_SEPARATOR, BLOG_ATTRIBUTE_KEYS, BlockDefinition, BlockSchema, BlogAttributeKey, COLLECTION_ATTRIBUTE_KEYS, COLLECTION_INLINE_ATTRIBUTE_KEYS, CollectedTypes, CollectionAttributeBindingSources, CollectionAttributeKey, CollectionInlineAttributeKey, EDITOR_COMMANDS, EditorCommand, EditorKeyInput, FieldSource, INSTALL_ID_RE, InlineSegment, InlineSources, LayoutGroupDef, MAX_PALETTE_COLORS, MIN_PALETTE_COLORS, PAGE_ATTRIBUTE_KEYS, PALETTE_FALLBACK_COLOR, PALETTE_GROUP_TYPE, PALETTE_PREFIX, PALETTE_REF_PREFIX, PRODUCT_ATTRIBUTE_KEYS, PageAttributeKey, PaletteColor, ProductAttributeKey, RICHTEXT_ALLOWED_TAGS, ResolverCtx, ResolverScope, ResolverShape, ResolverSpec, RichtextTag, SKIP, STORE_SLUG_RE, SchemaSetting, SectionGroup, SectionGroupData, SectionPreset, SectionSchema, SeedMissingStaticsReport, SelectedSection, SettingBinding, SettingBindings, SettingValue, SettingsData, SettingsSchema, SettingsSchemaSection, Skip, Template, TemplateBlock, TemplateSection, TemplateShape, WholeValueBindingOpts, applyCollectionAttributeBindings, applyInlineSources, applyMetafieldBindings, applyMetaobjectFieldBindings, blockPathsEqual, buildBlockInstance, buildDisabledAncestorSet, buildParentMap, buildSectionInstance, buildStaticChildren, collectCollectionAttributeBindings, collectMetaobjectEntryIds, collectTraversedEntryIds, collectTypes, comparePaletteIds, decodeStorefrontMetafield, defaultSettings, editorCommand, encodeSourceToken, escapeInertHtmlText, hasPaletteRefs, isBlockPathPrefix, isEditorCommand, isNodeIdToken, joinBlockPath, metafieldDefinitionFitsSetting, metafieldTypeSupportsInlineToken, metafieldTypeToSettingTypes, metaobjectGid, metaobjectIdFromGid, nodeGid, normalizeBlockPath, paletteKey, paletteRef, paletteValues, parsePalette, parsePaletteRef, parseSourceDescriptor, resolveContainerAtPath, resolveField, resolvePaletteRefs, resolvePaletteValue, resolveThemeSettingsPalette, seedMissingStatics, selectRenderableChildren, selectRenderableSections, serializeSourceDescriptor, settingTypeSupportsBinding, shortcutKey, splitBlockPath, splitInlineTokens, staticBlockId, staticChildId, stripInlineTokenSentinels, stripStaticFromBlockOrder, undoHotkey, walkBlocks, walkSections };
|
package/dist/index.js
CHANGED
|
@@ -8,4 +8,5 @@ import { metaobjectGid, metaobjectIdFromGid } from "./metaobject-gid.js";
|
|
|
8
8
|
import { ARTICLE_ATTRIBUTE_KEYS, BLOG_ATTRIBUTE_KEYS, COLLECTION_ATTRIBUTE_KEYS, COLLECTION_INLINE_ATTRIBUTE_KEYS, PAGE_ATTRIBUTE_KEYS, PRODUCT_ATTRIBUTE_KEYS, applyCollectionAttributeBindings, applyInlineSources, applyMetafieldBindings, applyMetaobjectFieldBindings, collectCollectionAttributeBindings, collectMetaobjectEntryIds, collectTraversedEntryIds, decodeStorefrontMetafield, encodeSourceToken, escapeInertHtmlText, metafieldDefinitionFitsSetting, metafieldTypeSupportsInlineToken, metafieldTypeToSettingTypes, parseSourceDescriptor, serializeSourceDescriptor, settingTypeSupportsBinding, splitInlineTokens, stripInlineTokenSentinels } from "./metafield-binding.js";
|
|
9
9
|
import { RICHTEXT_ALLOWED_TAGS } from "./richtext.js";
|
|
10
10
|
import { isNodeIdToken, nodeGid } from "./node-id.js";
|
|
11
|
-
|
|
11
|
+
import { INSTALL_ID_RE, STORE_SLUG_RE } from "./store-path.js";
|
|
12
|
+
export { ARTICLE_ATTRIBUTE_KEYS, BLOCK_PATH_SEPARATOR, BLOG_ATTRIBUTE_KEYS, BlockDefinition, BlockSchema, COLLECTION_ATTRIBUTE_KEYS, COLLECTION_INLINE_ATTRIBUTE_KEYS, EDITOR_COMMANDS, INSTALL_ID_RE, MAX_PALETTE_COLORS, MIN_PALETTE_COLORS, PAGE_ATTRIBUTE_KEYS, PALETTE_FALLBACK_COLOR, PALETTE_GROUP_TYPE, PALETTE_PREFIX, PALETTE_REF_PREFIX, PRODUCT_ATTRIBUTE_KEYS, RICHTEXT_ALLOWED_TAGS, SKIP, STORE_SLUG_RE, SchemaSetting, SectionGroup, SectionPreset, SectionSchema, SettingBinding, SettingBindings, SettingsData, SettingsSchema, SettingsSchemaSection, Template, TemplateBlock, TemplateSection, applyCollectionAttributeBindings, applyInlineSources, applyMetafieldBindings, applyMetaobjectFieldBindings, blockPathsEqual, buildBlockInstance, buildDisabledAncestorSet, buildParentMap, buildSectionInstance, buildStaticChildren, collectCollectionAttributeBindings, collectMetaobjectEntryIds, collectTraversedEntryIds, collectTypes, comparePaletteIds, decodeStorefrontMetafield, defaultSettings, editorCommand, encodeSourceToken, escapeInertHtmlText, hasPaletteRefs, isBlockPathPrefix, isEditorCommand, isNodeIdToken, joinBlockPath, metafieldDefinitionFitsSetting, metafieldTypeSupportsInlineToken, metafieldTypeToSettingTypes, metaobjectGid, metaobjectIdFromGid, nodeGid, normalizeBlockPath, paletteKey, paletteRef, paletteValues, parsePalette, parsePaletteRef, parseSourceDescriptor, resolveContainerAtPath, resolveField, resolvePaletteRefs, resolvePaletteValue, resolveThemeSettingsPalette, seedMissingStatics, selectRenderableChildren, selectRenderableSections, serializeSourceDescriptor, settingTypeSupportsBinding, shortcutKey, splitBlockPath, splitInlineTokens, staticBlockId, staticChildId, stripInlineTokenSentinels, stripStaticFromBlockOrder, undoHotkey, walkBlocks, walkSections };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/preset-materializer.d.ts
|
|
2
|
+
export declare function materializePreset(opts: {
|
|
3
|
+
configDir: string;
|
|
4
|
+
presetHandle: string;
|
|
5
|
+
}): Promise<void>;
|
|
6
|
+
/** Deep-merges each of a listing's overridable dirs over `configDir`'s copy;
|
|
7
|
+
* a file only the listing has lands as is. Leaves settings-data alone. */
|
|
8
|
+
export declare function overlayPresetListing(listingDir: string, configDir: string): Promise<void>;
|
|
9
|
+
//#endregion
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { LISTING_DIRS, PRESET_HANDLE_RE, mergePresetJson, normalizePresetKey } from "./preset-listing.js";
|
|
2
|
+
import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
//#region src/preset-materializer.ts
|
|
5
|
+
function isPlainObject(value) {
|
|
6
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
7
|
+
}
|
|
8
|
+
async function readJson(filePath) {
|
|
9
|
+
try {
|
|
10
|
+
return JSON.parse(await readFile(filePath, "utf-8"));
|
|
11
|
+
} catch (err) {
|
|
12
|
+
if (err instanceof Error && err.code === "ENOENT") return null;
|
|
13
|
+
throw err;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
async function dirExists(dir) {
|
|
17
|
+
try {
|
|
18
|
+
return (await stat(dir)).isDirectory();
|
|
19
|
+
} catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
async function materializeJsonOverrides(sourceDir, targetDir) {
|
|
24
|
+
if (!await dirExists(sourceDir)) return;
|
|
25
|
+
await mkdir(targetDir, { recursive: true });
|
|
26
|
+
const entries = await readdir(sourceDir, { withFileTypes: true });
|
|
27
|
+
for (const entry of entries) {
|
|
28
|
+
const sourcePath = path.join(sourceDir, entry.name);
|
|
29
|
+
const targetPath = path.join(targetDir, entry.name);
|
|
30
|
+
if (entry.isDirectory()) {
|
|
31
|
+
await materializeJsonOverrides(sourcePath, targetPath);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (!entry.isFile() || !entry.name.endsWith(".json") || entry.name.startsWith("._")) continue;
|
|
35
|
+
const override = await readJson(sourcePath);
|
|
36
|
+
if (override === null) continue;
|
|
37
|
+
const base = await readJson(targetPath);
|
|
38
|
+
const output = base === null ? override : mergePresetJson(base, override);
|
|
39
|
+
await writeFile(targetPath, `${JSON.stringify(output, null, 2)}\n`, "utf-8");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async function flattenSettingsData(configDir, presetHandle) {
|
|
43
|
+
const settingsPath = path.join(configDir, "settings-data.json");
|
|
44
|
+
const data = await readJson(settingsPath);
|
|
45
|
+
if (!isPlainObject(data)) return;
|
|
46
|
+
const presets = data.presets;
|
|
47
|
+
if (!isPlainObject(presets)) return;
|
|
48
|
+
const currentPreset = isPlainObject(data.current) ? data.current.preset : null;
|
|
49
|
+
const keys = Object.keys(presets);
|
|
50
|
+
const selectedKey = keys.find((key) => key === presetHandle) ?? keys.find((key) => normalizePresetKey(key) === presetHandle) ?? (typeof currentPreset === "string" && normalizePresetKey(currentPreset) === presetHandle ? currentPreset : null);
|
|
51
|
+
if (!selectedKey) throw new Error(`settings-data.json does not contain selected preset "${presetHandle}" in presets wrapper`);
|
|
52
|
+
const selected = presets[selectedKey];
|
|
53
|
+
if (!isPlainObject(selected) || !isPlainObject(selected.settings)) throw new Error(`settings-data.json preset "${selectedKey}" is missing a settings object`);
|
|
54
|
+
await writeFile(settingsPath, `${JSON.stringify(selected.settings, null, 2)}\n`, "utf-8");
|
|
55
|
+
}
|
|
56
|
+
async function materializePreset(opts) {
|
|
57
|
+
const presetHandle = opts.presetHandle.trim();
|
|
58
|
+
if (!PRESET_HANDLE_RE.test(presetHandle)) throw new Error(`invalid preset handle "${opts.presetHandle}"`);
|
|
59
|
+
const listingDir = path.join(opts.configDir, "listings", presetHandle);
|
|
60
|
+
if (!await dirExists(listingDir)) throw new Error(`selected preset "${presetHandle}" not found at config/listings/${presetHandle}`);
|
|
61
|
+
await flattenSettingsData(opts.configDir, presetHandle);
|
|
62
|
+
await overlayPresetListing(listingDir, opts.configDir);
|
|
63
|
+
}
|
|
64
|
+
/** Deep-merges each of a listing's overridable dirs over `configDir`'s copy;
|
|
65
|
+
* a file only the listing has lands as is. Leaves settings-data alone. */
|
|
66
|
+
async function overlayPresetListing(listingDir, configDir) {
|
|
67
|
+
for (const dir of LISTING_DIRS) await materializeJsonOverrides(path.join(listingDir, dir), path.join(configDir, dir));
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
export { materializePreset, overlayPresetListing };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shop-on/theme-contract",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"license": "SEE LICENSE IN dist/LICENSE.md",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -10,12 +10,14 @@
|
|
|
10
10
|
"exports": {
|
|
11
11
|
".": "./dist/index.js",
|
|
12
12
|
"./preset-listing": "./dist/preset-listing.js",
|
|
13
|
+
"./preset-materializer": "./dist/preset-materializer.js",
|
|
13
14
|
"./package.json": "./package.json"
|
|
14
15
|
},
|
|
15
16
|
"dependencies": {
|
|
16
17
|
"zod": "4.6.7"
|
|
17
18
|
},
|
|
18
19
|
"devDependencies": {
|
|
20
|
+
"@types/node": "26.6.5",
|
|
19
21
|
"typescript": "7.0.2",
|
|
20
22
|
"vitest": "5.0.5"
|
|
21
23
|
},
|