@symbo.ls/utils 3.14.276 → 3.14.446
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 +6 -0
- package/dist/cjs/sharedLibraries.js +1 -1
- package/dist/esm/sharedLibraries.js +1 -1
- package/package.json +1 -1
- package/sharedLibraries.js +131 -32
package/CHANGELOG.md
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var u=Object.defineProperty;var
|
|
1
|
+
"use strict";var u=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var U=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var g=(n,t)=>{for(var e in t)u(n,e,{get:t[e],enumerable:!0})},E=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of U(t))!j.call(n,i)&&i!==e&&u(n,i,{get:()=>t[i],enumerable:!(r=R(t,i))||r.enumerable});return n};var S=n=>E(u({},"__esModule",{value:!0}),n);var P={};g(P,{deepDefaults:()=>f,fetchLibraryData:()=>k,isWrappedLibrary:()=>d,mergeSharedLibraries:()=>F,normalizeIgnoreList:()=>w,normalizeLibraryKey:()=>c,resolveSharedLibraries:()=>K,sharedLibrary:()=>x});module.exports=S(P);var s=require("./types.js");const m="system",_="https://smbls-kv.nika-980.workers.dev",I="https://api.symbols.app";function c(n){const t=String(n||"").trim();if(!t)return{owner:m,key:"",full:""};let e=null,r=t;if(t.includes("/")){const o=t.indexOf("/");e=t.slice(0,o),r=t.slice(o+1)}r=r.replace(/\.symbo\.ls$/iu,""),e||(e=m);const i=r?`${e}/${r}`:"";return{owner:e,key:r,full:i}}const f=(n,t,e,r)=>{for(const i in t){const o=r?`${r}/${i}`:i;e&&e.has(o)||(i in n?(0,s.isObject)(n[i])&&(0,s.isObject)(t[i])&&f(n[i],t[i],e,o):n[i]=t[i])}},O=new Set,B=n=>n.replace(/\.js$/iu,""),w=n=>{const t=new Set;if(!Array.isArray(n))return t;for(const e of n){if(!(0,s.isString)(e))continue;const r=e.split("/").map(i=>B(i.trim())).filter(Boolean).join("/");r&&t.add(r)}return t},x=(n,t={})=>({library:n,ignoreList:Array.isArray(t.ignoreList)?t.ignoreList:[]}),d=n=>(0,s.isObject)(n)&&Array.isArray(n.ignoreList)&&("library"in n||"key"in n||"name"in n),C=n=>n.library!==void 0?n.library:n.key??n.name,T=(n,t,e)=>{for(const r in t)if(Object.prototype.hasOwnProperty.call(t,r)&&!e.has(r))if((0,s.isObject)(t[r])&&(0,s.isObject)(n[r]))if(r==="designSystem")f(n[r],t[r],e,r);else for(const i in t[r])e.has(`${r}/${i}`)||i in n[r]||(n[r][i]=t[r][i]);else r in n||(n[r]=t[r])},F=(n,t)=>{if(!(!t||!t.length))for(let e=0;e<t.length;e++){let r=t[e],i=O;d(r)&&(i=w(r.ignoreList),r=r.library),(0,s.isObject)(r)&&T(n,r,i)}};async function k(n,t={}){if((t.provider||"kv")==="api")return h(n,t);const r=await G(n,t);return r||h(n,t)}async function G(n,t={}){const e=t.kvBaseUrl||_,r=t.env||"production",i=`${e}/kv/${encodeURIComponent(n)}?env=${r}`;try{const o=await fetch(i,{method:"GET"});return o.ok&&(await o.json())?.value||null}catch{return null}}async function h(n,t={}){const e=t.apiBaseUrl||I,{key:r}=c(n);try{const i=`${e}/core/projects/libraries/available?search=${encodeURIComponent(r)}&limit=10`,o=await fetch(i,{method:"GET"});if(!o.ok)return null;const a=await o.json(),l=(a?.items||a?.data||(Array.isArray(a)?a:[])).find($=>{const{key:A}=c($?.key);return A.toLowerCase()===r.toLowerCase()});if(!l?.id&&!l?._id)return null;const L=l.id||l._id,v=`${e}/core/projects/${encodeURIComponent(L)}/data?branch=main`,y=await fetch(v,{method:"GET"});if(!y.ok)return null;const p=await y.json();return p?.data||p||null}catch{return null}}async function b(n,t={}){if((0,s.isObject)(n))return n;if((0,s.isString)(n)){const{full:e}=c(n);if(!e)return null;try{const r=await k(e,t);return r||console.warn(`[smbls] Shared library "${e}" not found`),r}catch(r){return console.warn(`[smbls] Failed to fetch shared library "${e}":`,r.message),null}}return null}async function K(n,t={}){return!n||!n.length?[]:(await Promise.all(n.map(async r=>{if(d(r)){const i=await b(C(r),t);return i?{library:i,ignoreList:r.ignoreList}:null}return b(r,t)}))).filter(Boolean)}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{isObject as
|
|
1
|
+
import{isObject as s,isString as d}from"./types.js";const y="system",$="https://smbls-kv.nika-980.workers.dev",A="https://api.symbols.app";function c(r){const t=String(r||"").trim();if(!t)return{owner:y,key:"",full:""};let e=null,n=t;if(t.includes("/")){const o=t.indexOf("/");e=t.slice(0,o),n=t.slice(o+1)}n=n.replace(/\.symbo\.ls$/iu,""),e||(e=y);const i=n?`${e}/${n}`:"";return{owner:e,key:n,full:i}}const p=(r,t,e,n)=>{for(const i in t){const o=n?`${n}/${i}`:i;e&&e.has(o)||(i in r?s(r[i])&&s(t[i])&&p(r[i],t[i],e,o):r[i]=t[i])}},R=new Set,U=r=>r.replace(/\.js$/iu,""),j=r=>{const t=new Set;if(!Array.isArray(r))return t;for(const e of r){if(!d(e))continue;const n=e.split("/").map(i=>U(i.trim())).filter(Boolean).join("/");n&&t.add(n)}return t},B=(r,t={})=>({library:r,ignoreList:Array.isArray(t.ignoreList)?t.ignoreList:[]}),m=r=>s(r)&&Array.isArray(r.ignoreList)&&("library"in r||"key"in r||"name"in r),g=r=>r.library!==void 0?r.library:r.key??r.name,E=(r,t,e)=>{for(const n in t)if(Object.prototype.hasOwnProperty.call(t,n)&&!e.has(n))if(s(t[n])&&s(r[n]))if(n==="designSystem")p(r[n],t[n],e,n);else for(const i in t[n])e.has(`${n}/${i}`)||i in r[n]||(r[n][i]=t[n][i]);else n in r||(r[n]=t[n])},x=(r,t)=>{if(!(!t||!t.length))for(let e=0;e<t.length;e++){let n=t[e],i=R;m(n)&&(i=j(n.ignoreList),n=n.library),s(n)&&E(r,n,i)}};async function S(r,t={}){if((t.provider||"kv")==="api")return h(r,t);const n=await _(r,t);return n||h(r,t)}async function _(r,t={}){const e=t.kvBaseUrl||$,n=t.env||"production",i=`${e}/kv/${encodeURIComponent(r)}?env=${n}`;try{const o=await fetch(i,{method:"GET"});return o.ok&&(await o.json())?.value||null}catch{return null}}async function h(r,t={}){const e=t.apiBaseUrl||A,{key:n}=c(r);try{const i=`${e}/core/projects/libraries/available?search=${encodeURIComponent(n)}&limit=10`,o=await fetch(i,{method:"GET"});if(!o.ok)return null;const a=await o.json(),l=(a?.items||a?.data||(Array.isArray(a)?a:[])).find(L=>{const{key:v}=c(L?.key);return v.toLowerCase()===n.toLowerCase()});if(!l?.id&&!l?._id)return null;const w=l.id||l._id,k=`${e}/core/projects/${encodeURIComponent(w)}/data?branch=main`,u=await fetch(k,{method:"GET"});if(!u.ok)return null;const f=await u.json();return f?.data||f||null}catch{return null}}async function b(r,t={}){if(s(r))return r;if(d(r)){const{full:e}=c(r);if(!e)return null;try{const n=await S(e,t);return n||console.warn(`[smbls] Shared library "${e}" not found`),n}catch(n){return console.warn(`[smbls] Failed to fetch shared library "${e}":`,n.message),null}}return null}async function C(r,t={}){return!r||!r.length?[]:(await Promise.all(r.map(async n=>{if(m(n)){const i=await b(g(n),t);return i?{library:i,ignoreList:n.ignoreList}:null}return b(n,t)}))).filter(Boolean)}export{p as deepDefaults,S as fetchLibraryData,m as isWrappedLibrary,x as mergeSharedLibraries,j as normalizeIgnoreList,c as normalizeLibraryKey,C as resolveSharedLibraries,B as sharedLibrary};
|
package/package.json
CHANGED
package/sharedLibraries.js
CHANGED
|
@@ -42,13 +42,101 @@ export function normalizeLibraryKey (raw) {
|
|
|
42
42
|
/**
|
|
43
43
|
* Recursively set defaults: copy keys from source into target
|
|
44
44
|
* only where target is missing them. Non-destructive.
|
|
45
|
+
*
|
|
46
|
+
* @param {object} target
|
|
47
|
+
* @param {object} source
|
|
48
|
+
* @param {Set<string>} [ignore] - normalized '/'-joined paths to skip
|
|
49
|
+
* @param {string} [prefix] - current path prefix used to match `ignore`
|
|
45
50
|
*/
|
|
46
|
-
export const deepDefaults = (target, source) => {
|
|
51
|
+
export const deepDefaults = (target, source, ignore, prefix) => {
|
|
47
52
|
for (const k in source) {
|
|
53
|
+
const path = prefix ? `${prefix}/${k}` : k
|
|
54
|
+
if (ignore && ignore.has(path)) continue
|
|
48
55
|
if (!(k in target)) {
|
|
49
56
|
target[k] = source[k]
|
|
50
57
|
} else if (isObject(target[k]) && isObject(source[k])) {
|
|
51
|
-
deepDefaults(target[k], source[k])
|
|
58
|
+
deepDefaults(target[k], source[k], ignore, path)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const EMPTY_IGNORE = new Set()
|
|
64
|
+
|
|
65
|
+
const stripJsExt = (segment) => segment.replace(/\.js$/iu, '')
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Normalize an `ignoreList` into a Set of '/'-joined path strings.
|
|
69
|
+
*
|
|
70
|
+
* Each entry names something to skip while merging a shared library — a
|
|
71
|
+
* top-level context key ('designSystem', 'state'), a file-ish top-level key
|
|
72
|
+
* ('app.js' → 'app'), or a nested path ('components/Header', 'designSystem/
|
|
73
|
+
* icons'). A trailing `.js` on any path segment is stripped, so file names and
|
|
74
|
+
* context keys are interchangeable.
|
|
75
|
+
*
|
|
76
|
+
* ['designSystem', 'app.js', 'components/Header']
|
|
77
|
+
* → Set { 'designSystem', 'app', 'components/Header' }
|
|
78
|
+
*/
|
|
79
|
+
export const normalizeIgnoreList = (ignoreList) => {
|
|
80
|
+
const set = new Set()
|
|
81
|
+
if (!Array.isArray(ignoreList)) return set
|
|
82
|
+
for (const entry of ignoreList) {
|
|
83
|
+
if (!isString(entry)) continue
|
|
84
|
+
const path = entry
|
|
85
|
+
.split('/')
|
|
86
|
+
.map((seg) => stripJsExt(seg.trim()))
|
|
87
|
+
.filter(Boolean)
|
|
88
|
+
.join('/')
|
|
89
|
+
if (path) set.add(path)
|
|
90
|
+
}
|
|
91
|
+
return set
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Wrap a shared library with merge options. Pass the result anywhere a
|
|
96
|
+
* shared-library entry is accepted (the `sharedLibraries` array); it is
|
|
97
|
+
* unwrapped during merge and its options honored.
|
|
98
|
+
*
|
|
99
|
+
* sharedLibrary(bigbrother, { ignoreList: ['designSystem', 'app.js'] })
|
|
100
|
+
*
|
|
101
|
+
* @param {object|string} library - a library context object or identifier
|
|
102
|
+
* @param {object} [options]
|
|
103
|
+
* @param {string[]} [options.ignoreList] - paths to skip while merging
|
|
104
|
+
*/
|
|
105
|
+
export const sharedLibrary = (library, options = {}) => ({
|
|
106
|
+
library,
|
|
107
|
+
ignoreList: Array.isArray(options.ignoreList) ? options.ignoreList : []
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
// A wrapped entry carries an `ignoreList` array alongside the library it
|
|
111
|
+
// applies to. The library is named by `library` (a context object or an
|
|
112
|
+
// identifier string) OR, for symbols.json-shaped configs, by `key`/`name`.
|
|
113
|
+
// The `ignoreList` array is the discriminator — a bare library context never
|
|
114
|
+
// has one — so this never misfires on a real context.
|
|
115
|
+
export const isWrappedLibrary = (entry) =>
|
|
116
|
+
isObject(entry) &&
|
|
117
|
+
Array.isArray(entry.ignoreList) &&
|
|
118
|
+
('library' in entry || 'key' in entry || 'name' in entry)
|
|
119
|
+
|
|
120
|
+
// The library a wrapped entry points at: an explicit `library`, else the
|
|
121
|
+
// symbols.json identifier fields `key`/`name`.
|
|
122
|
+
const wrappedLibraryRef = (entry) =>
|
|
123
|
+
entry.library !== undefined ? entry.library : (entry.key ?? entry.name)
|
|
124
|
+
|
|
125
|
+
const mergeOneLibrary = (context, sharedLib, ignore) => {
|
|
126
|
+
for (const key in sharedLib) {
|
|
127
|
+
if (!Object.prototype.hasOwnProperty.call(sharedLib, key)) continue
|
|
128
|
+
if (ignore.has(key)) continue // top-level skip: 'designSystem', 'app', 'state'
|
|
129
|
+
if (isObject(sharedLib[key]) && isObject(context[key])) {
|
|
130
|
+
if (key === 'designSystem') {
|
|
131
|
+
deepDefaults(context[key], sharedLib[key], ignore, key)
|
|
132
|
+
} else {
|
|
133
|
+
for (const k in sharedLib[key]) {
|
|
134
|
+
if (ignore.has(`${key}/${k}`)) continue // nested skip: 'components/Header'
|
|
135
|
+
if (!(k in context[key])) context[key][k] = sharedLib[key][k]
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
} else if (!(key in context)) {
|
|
139
|
+
context[key] = sharedLib[key]
|
|
52
140
|
}
|
|
53
141
|
}
|
|
54
142
|
}
|
|
@@ -58,26 +146,22 @@ export const deepDefaults = (target, source) => {
|
|
|
58
146
|
* - designSystem: deep defaults (non-destructive recursive)
|
|
59
147
|
* - other object keys (components, pages, etc.): shallow add missing
|
|
60
148
|
* - new top-level keys: copy from library
|
|
149
|
+
*
|
|
150
|
+
* Entries may be a bare library context, or a wrapped `{ library, ignoreList }`
|
|
151
|
+
* (see `sharedLibrary`). `ignoreList` names paths to skip while merging that
|
|
152
|
+
* library — e.g. `['designSystem']` imports a library WITHOUT its design system.
|
|
61
153
|
*/
|
|
62
154
|
export const mergeSharedLibraries = (context, libraries) => {
|
|
63
155
|
if (!libraries || !libraries.length) return
|
|
64
156
|
for (let i = 0; i < libraries.length; i++) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (key === 'designSystem') {
|
|
71
|
-
deepDefaults(context[key], sharedLib[key])
|
|
72
|
-
} else {
|
|
73
|
-
for (const k in sharedLib[key]) {
|
|
74
|
-
if (!(k in context[key])) context[key][k] = sharedLib[key][k]
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
} else if (!(key in context)) {
|
|
78
|
-
context[key] = sharedLib[key]
|
|
79
|
-
}
|
|
157
|
+
let sharedLib = libraries[i]
|
|
158
|
+
let ignore = EMPTY_IGNORE
|
|
159
|
+
if (isWrappedLibrary(sharedLib)) {
|
|
160
|
+
ignore = normalizeIgnoreList(sharedLib.ignoreList)
|
|
161
|
+
sharedLib = sharedLib.library
|
|
80
162
|
}
|
|
163
|
+
if (!isObject(sharedLib)) continue
|
|
164
|
+
mergeOneLibrary(context, sharedLib, ignore)
|
|
81
165
|
}
|
|
82
166
|
}
|
|
83
167
|
|
|
@@ -149,11 +233,36 @@ async function fetchLibraryFromApi (identifier, opts = {}) {
|
|
|
149
233
|
}
|
|
150
234
|
}
|
|
151
235
|
|
|
236
|
+
// Resolve a single entry: a context object passes through; an identifier
|
|
237
|
+
// string is fetched into a library object.
|
|
238
|
+
async function resolveOneLibrary (entry, opts = {}) {
|
|
239
|
+
if (isObject(entry)) return entry
|
|
240
|
+
if (isString(entry)) {
|
|
241
|
+
const { full } = normalizeLibraryKey(entry)
|
|
242
|
+
if (!full) return null
|
|
243
|
+
try {
|
|
244
|
+
const data = await fetchLibraryData(full, opts)
|
|
245
|
+
if (!data) {
|
|
246
|
+
console.warn(`[smbls] Shared library "${full}" not found`)
|
|
247
|
+
}
|
|
248
|
+
return data
|
|
249
|
+
} catch (e) {
|
|
250
|
+
console.warn(`[smbls] Failed to fetch shared library "${full}":`, e.message)
|
|
251
|
+
return null
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return null
|
|
255
|
+
}
|
|
256
|
+
|
|
152
257
|
/**
|
|
153
258
|
* Resolve an array that may contain strings and/or objects.
|
|
154
259
|
* Strings are treated as library identifiers and fetched.
|
|
155
260
|
* Objects pass through unchanged.
|
|
156
261
|
*
|
|
262
|
+
* Wrapped entries (`{ library, ignoreList }` from `sharedLibrary`) are
|
|
263
|
+
* preserved: the inner `library` is resolved (so it may itself be an
|
|
264
|
+
* identifier string) while `ignoreList` is carried through to the merge step.
|
|
265
|
+
*
|
|
157
266
|
* @param {Array<string|object>} libraries - Mixed array
|
|
158
267
|
* @param {object} [opts] - Options passed to fetchLibraryData
|
|
159
268
|
* @returns {Promise<object[]>} Array of resolved library objects
|
|
@@ -162,22 +271,12 @@ export async function resolveSharedLibraries (libraries, opts = {}) {
|
|
|
162
271
|
if (!libraries || !libraries.length) return []
|
|
163
272
|
const resolved = await Promise.all(
|
|
164
273
|
libraries.map(async (entry) => {
|
|
165
|
-
if (
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
try {
|
|
170
|
-
const data = await fetchLibraryData(full, opts)
|
|
171
|
-
if (!data) {
|
|
172
|
-
console.warn(`[smbls] Shared library "${full}" not found`)
|
|
173
|
-
}
|
|
174
|
-
return data
|
|
175
|
-
} catch (e) {
|
|
176
|
-
console.warn(`[smbls] Failed to fetch shared library "${full}":`, e.message)
|
|
177
|
-
return null
|
|
178
|
-
}
|
|
274
|
+
if (isWrappedLibrary(entry)) {
|
|
275
|
+
const library = await resolveOneLibrary(wrappedLibraryRef(entry), opts)
|
|
276
|
+
if (!library) return null
|
|
277
|
+
return { library, ignoreList: entry.ignoreList }
|
|
179
278
|
}
|
|
180
|
-
return
|
|
279
|
+
return resolveOneLibrary(entry, opts)
|
|
181
280
|
})
|
|
182
281
|
)
|
|
183
282
|
return resolved.filter(Boolean)
|