@blue-labs/repository-generator 4.0.2-rc.0 → 4.1.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.
@@ -1 +0,0 @@
1
- "use strict";const T=require("path"),I=require("fs"),p=require("@blue-labs/language"),h=require("@blue-labs/repository-contract"),j=require("fast-json-patch"),C=require("js-yaml");function U(e){const s=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const t in e)if(t!=="default"){const n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(s,t,n.get?n:{enumerable:!0,get:()=>e[t]})}}return s.default=e,Object.freeze(s)}const $=U(T),N=new Set([...p.Properties.CORE_TYPES]),k=p.Properties.CORE_TYPE_NAME_TO_BLUE_ID_MAP,M="Blue Repository",d={Dev:"dev",Stable:"stable"},q=new Set([".git",".github",".husky","node_modules",".nx",".vscode",".idea"]);function L(e){const s=I.readdirSync(e,{withFileTypes:!0}),t=new Map;for(const n of s){if(!n.isDirectory()||q.has(n.name))continue;const r=T.join(e,n.name),o=_(r);if(o.length!==0)for(const i of o){const c=i.endsWith(".dev.blue")?d.Dev:d.Stable,a=F(i),u=G(a,i),f=`${n.name}/${u}`,l=t.get(f);if(l)throw l.status!==c?new Error(`Type ${f} has both stable and dev definitions (${l.filePath} and ${i}).`):new Error(`Type ${f} is defined multiple times (${l.filePath} and ${i}).`);const y=Y(a,i);t.set(f,{packageName:n.name,typeName:u,status:c,content:a,filePath:i,references:y})}}return t}function F(e){const s=I.readFileSync(e,"utf8"),t=p.yamlBlueParse(s);if(!R(t))throw new Error(`Type file ${e} must contain a YAML object.`);return t}function G(e,s){if(!R(e)||typeof e.name!="string")throw new Error(`Type file ${s} is missing required "name" field.`);return e.name}function _(e){const s=I.readdirSync(e,{withFileTypes:!0}),t=[];for(const n of s){if(n.isDirectory()){t.push(..._(T.join(e,n.name)));continue}(n.name.endsWith(".blue")||n.name.endsWith(".dev.blue"))&&t.push(T.join(e,n.name))}return t}function Y(e,s){const t=new Set,n=r=>{if(Array.isArray(r)){r.forEach(i=>n(i));return}if(!R(r))return;const o=i=>{if(typeof i=="string"&&!N.has(i)){if(!i.includes("/"))throw new Error(`Invalid type reference "${i}" in ${s}. Use <Package>/<Type> for non-primitive references.`);t.add(i)}};o(r.type),o(r.itemType),o(r.keyType),o(r.valueType),Object.values(r).forEach(i=>n(i))};return n(e),t}function R(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function J(e){const s=new Map;for(const[t,n]of e){const r=new Set;for(const o of n.references){if(!e.has(o))throw new Error(`Type ${t} references unknown alias ${o}. Ensure the target type exists.`);r.add(o)}s.set(t,r)}return s}function W(e){const s=[],t=new Map,n=(r,o)=>{const i=t.get(r);if(i==="visited")return;if(i==="visiting"){const a=[...o,r].join(" -> ");throw new Error(`Circular type dependency detected: ${a}`)}t.set(r,"visiting"),o.push(r);const c=e.get(r)||new Set;for(const a of c)n(a,o);o.pop(),t.set(r,"visited"),s.push(r)};for(const r of e.keys())t.has(r)||n(r,[]);return s}function H(e,s){const t=new Map,n=r=>{if(t.has(r))return t.get(r)??!1;const o=e.get(r)||new Set;for(const i of o){const c=s.get(i);if(c&&(c.status===d.Dev||n(i)))return t.set(r,!0),!0}return t.set(r,!1),!1};for(const[r,o]of s)if(o.status===d.Stable&&n(r))throw new Error(`Stable type ${r} depends on a dev type. Stable types may only depend on other stable types.`)}function g(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function v(e){return g(e)?Object.getPrototypeOf(e)===Object.prototype:!1}function A(e){if(Array.isArray(e))return e.map(s=>A(s));if(v(e)){const s={};for(const[t,n]of Object.entries(e))s[t]=A(n);return s}return e}function P(e){return e.map(s=>({repositoryVersionIndex:s.repositoryVersionIndex,typeBlueId:s.typeBlueId,attributesAdded:[...s.attributesAdded]}))}const z=/^\$\{([\s\S]*)\}$/;class K{process(s,t){const n=t.getValue();if(O(n)){const r=t.clone();return r.setValue(n),r.setProperties(void 0),r.setItems(void 0),r.setType(void 0),r}return s}postProcess(s,t){const n=t.getValue();if(O(n)&&s.getValue()!==n){const r=s.clone();return r.setValue(n),r}return s}}function O(e){return typeof e!="string"||!z.test(e)?!1:e.indexOf("${")===e.lastIndexOf("${")}function X(){return new p.MergingProcessors.SequentialMergingProcessor([new p.MergingProcessors.ValuePropagator,new K,new p.MergingProcessors.TypeAssigner,new p.MergingProcessors.ListProcessor,new p.MergingProcessors.DictionaryProcessor,new p.MergingProcessors.MetadataPropagator,new p.MergingProcessors.BasicTypesVerifier])}function Q(e,s){const t=new Map,n=new Map,r=new Map,o=new p.Blue,i=p.createNodeProvider(a=>Z(r,a).map(u=>o.jsonValueToNode(u))),c=new p.Blue({nodeProvider:i,mergingProcessor:X()});for(const a of e){const u=s.get(a);if(!u)continue;const f=S(A(u.content),t),l=c.jsonValueToNode(f),y=N.has(u.typeName),m=y?te(u.typeName):c.calculateBlueIdSync(l),w=c.nodeToJson(l,"official");t.set(a,m),n.set(a,w),y||r.set(m,w),c.registerBlueIds({[a]:m})}return{aliasToBlueId:t,aliasToStorageContent:n}}function Z(e,s){const t=ee(s);if(t===void 0)return[];const n=e.get(t.baseBlueId);if(n===void 0)return[];if(t.index===void 0)return Array.isArray(n)?n:[n];if(!Array.isArray(n))return t.index===0?[n]:[];const r=n[t.index];return r===void 0?[]:[r]}function ee(e){const s=e.indexOf("#");if(s===-1)return{baseBlueId:e};const t=e.slice(0,s),n=e.slice(s+1),r=Number(n);if(!(!Number.isInteger(r)||r<0))return{baseBlueId:t,index:r}}function S(e,s,t=!1,n=!1){if(Array.isArray(e))return e.map(o=>S(o,s,t,n));if(!g(e))return e;const r={};for(const[o,i]of Object.entries(e)){const c=n||o===h.OBJECT_CONTRACTS;if((o==="type"||o==="itemType"||o==="keyType"||o==="valueType")&&typeof i=="string"&&!(t&&c)){if(N.has(i)){const a=k[i];if(!a)throw new Error(`Missing primitive BlueId for ${i}.`);r[o]={blueId:a}}else{const a=i,u=s.get(a);if(!u)throw new Error(`Missing BlueId for alias ${a}.`);r[o]={blueId:u}}continue}r[o]=S(i,s,t,c)}return r}function te(e){const s=k[e];if(!s)throw new Error(`Missing primitive BlueId for ${e}.`);return s}const x=new Set(Object.values(k)),ne=new Set(["type","itemType","valueType","keyType"]),re=new Set(["name","description"]),b={Unchanged:"unchanged",NonBreaking:"non-breaking",Breaking:"breaking"};function se(e,s,t,n,r){const o=j.compare(e,s);if(o.length===0)return{status:b.Unchanged,attributesAdded:[]};const i=[];let c=!1;for(const a of o){if(ce(a,e,r)){c=!0;continue}if(a.op!=="add")return{status:b.Breaking,attributesAdded:[]};if(!ie(a))return{status:b.Breaking,attributesAdded:[]};i.push(a.path)}return i.length===0&&!c?{status:b.Breaking,attributesAdded:[]}:(i.length>0&&oe(i,t,n),{status:b.NonBreaking,attributesAdded:i})}function oe(e,s,t){for(const n of e)try{h.validateAttributesAddedPointer(n)}catch(r){throw new Error(`Invalid attribute pointer "${n}" for ${s}/${t}: ${r.message}`)}}function ie(e){if(e.op!=="add")return!1;const s=D(e.path);if(s.length===0)return!1;const t=s.at(-1);return t&&/^\d+$/.test(t)||t&&ne.has(t)||s.some(n=>h.RESERVED_ATTRIBUTES_POINTER_SEGMENTS.has(n))?!1:g(e.value)?!ae(e.value):t?re.has(t):!1}function D(e){try{return h.parsePointer(e)}catch{return[]}}function ae(e){const s=[e];for(;s.length;){const t=s.pop();if(t){if(Object.prototype.hasOwnProperty.call(t,"value")||Object.prototype.hasOwnProperty.call(t,"items"))return!0;if(Object.prototype.hasOwnProperty.call(t,h.OBJECT_SCHEMA)){const n=t[h.OBJECT_SCHEMA];if(g(n)&&n.required===!0)return!0}for(const[n,r]of Object.entries(t))n!==h.OBJECT_SCHEMA&&g(r)&&s.push(r)}}return!1}function ce(e,s,t){if(e.op!=="replace"||typeof e.path!="string")return!1;const n=D(e.path);if(n.length<2)return!1;const r=n.at(-1),o=n.at(-2);if(!(r==="blueId"&&o!==void 0&&(o==="type"||o==="itemType"||o==="keyType"||o==="valueType")))return!1;const i=ue(s,n),c=typeof i=="string"?i:g(i)&&typeof i.blueId=="string"?i.blueId:null,a=typeof e.value=="string"?e.value:g(e.value)&&typeof e.value.blueId=="string"?e.value.blueId:null;if(c&&x.has(c)||a&&x.has(a)||!c||!a)return!1;const u=t.get(c),f=t.get(a);if(!u||!f)return!1;for(const l of u)if(f.has(l))return!0;return!1}function ue(e,s){let t=e;for(const n of s)if(Array.isArray(t)){const r=Number(n);if(Number.isNaN(r)||r<0||r>=t.length)return;t=t[r]}else if(g(t))t=t[n];else return;return t}function fe(e){const s=new Map;if(!e)return s;for(const t of e.packages||[]){if(typeof t.name!="string")continue;const n=new Map;for(const r of t.types||[]){const o=r.content,i=v(o)&&typeof o.name=="string"?o.name:void 0;i&&n.set(i,r)}s.set(t.name,n)}return s}function le(e,s){for(const[t,n]of s)for(const[r,o]of n){const i=`${t}/${r}`;if(!e.has(i)&&o.status===d.Stable)throw new Error(`Stable type ${i} was removed. Breaking changes require a new logical type name.`)}}function pe({discovered:e,previousTypes:s,aliasToBlueId:t,aliasToStorageContent:n,nextRepoVersionIndex:r}){const o=new Map,i=de(s,t);for(const[c,a]of e){const u=t.get(c);if(!u)throw new Error(`Failed to compute BlueId for type ${c}.`);const f=s.get(a.packageName)?.get(a.typeName)??null;if(f&&f.status===d.Stable&&a.status===d.Dev)throw new Error(`Type ${c} was stable previously and cannot be downgraded to dev. Use a new logical name instead.`);const l=n.get(c)??a.content,y=a.status===d.Dev?ye({blueId:u,nextRepoVersionIndex:r,previousType:f}):!f||f.status===d.Dev?ge({blueId:u,nextRepoVersionIndex:r}):me({alias:c,blueId:u,nextRepoVersionIndex:r,previousType:f,currentContent:l,packageName:a.packageName,typeName:a.typeName,blueIdAliases:i});y.content=l,y.status=a.status;const m=o.get(a.packageName)??[];m.push(y),o.set(a.packageName,m)}return o}function de(e,s){const t=new Map,n=(r,o)=>{const i=t.get(r);if(i){i.add(o);return}t.set(r,new Set([o]))};for(const[r,o]of e)for(const[i,c]of o){const a=`${r}/${i}`;for(const u of c.versions??[])typeof u.typeBlueId=="string"&&n(u.typeBlueId,a)}for(const[r,o]of s)n(o,r);return t}function ye({blueId:e,nextRepoVersionIndex:s,previousType:t}){const n=t&&t.status===d.Dev?t:null,r=n?.versions?.length===1?n.versions[0]:null,o=r&&r.typeBlueId===e;return{status:d.Dev,content:{},versions:o&&n?.versions?P(n.versions):[{repositoryVersionIndex:s,typeBlueId:e,attributesAdded:[]}]}}function ge({blueId:e,nextRepoVersionIndex:s}){return{status:d.Stable,content:{},versions:[{repositoryVersionIndex:s,typeBlueId:e,attributesAdded:[]}]}}function me({alias:e,blueId:s,nextRepoVersionIndex:t,previousType:n,currentContent:r,packageName:o,typeName:i,blueIdAliases:c}){if(!v(n.content))throw new Error(`Type ${e} has non-object content; cannot compute diff for stable versioning.`);const a=se(n.content,r,o,i,c);if(a.status===b.Unchanged){const u=P(n.versions||[]),f=u.at(-1);if(f&&f.typeBlueId!==s)throw new Error(`Type ${e} content is unchanged but BlueId differs from previous metadata.`);return{status:d.Stable,content:{},versions:u}}if(a.status===b.NonBreaking)return{status:d.Stable,content:{},versions:[...P(n.versions||[]),{repositoryVersionIndex:t,typeBlueId:s,attributesAdded:a.attributesAdded}]};throw new Error(`Breaking change detected in stable type ${e}. Introduce a new type name for breaking changes.`)}function he(e){return Array.from(e.entries()).map(([s,t])=>({name:s,types:t.map(n=>({...n,versions:[...n.versions].sort((r,o)=>r.repositoryVersionIndex-o.repositoryVersionIndex)})).sort((n,r)=>{const o=v(n.content)&&typeof n.content.name=="string"?n.content.name:"",i=v(r.content)&&typeof r.content.name=="string"?r.content.name:"";return o.localeCompare(i)})})).sort((s,t)=>s.name.localeCompare(t.name))}function be(e){return p.BlueIdCalculator.INSTANCE.calculateSync(e)}function we(e,s,t){const n=s?.repositoryVersions.at(-1),r=!n||n!==t,o=s?.repositoryVersions?[...s.repositoryVersions]:[];return r&&o.push(t),{document:{name:M,packages:e,repositoryVersions:o},changed:r}}function ve(e,s,t){const n={name:M,repositoryVersions:s,packages:Ie(e,t)};h.validateNoCycles(n),h.validateStableDoesNotDependOnDev(n)}function Ie(e,s){const t={};for(const n of e){const r=Ee(n.name,s),o={},i={};for(const c of n.types){const a=c.versions?.[c.versions.length-1]?.typeBlueId;if(!a)throw new Error(`Type ${n.name} is missing a current BlueId in versions.`);const u=Te(c);o[a]={status:c.status,name:u,versions:c.versions??[]},i[a]=c.content}t[n.name]={name:n.name,aliases:r,typesMeta:o,contents:i,schemas:{}}}return t}function Ee(e,s){const t={},n=`${e}/`;return s.forEach((r,o)=>{o.startsWith(n)&&(t[o]=r)}),t}function Te(e){const s=v(e.content)&&typeof e.content.name=="string"?e.content.name:null;if(!s)throw new Error('Type content is missing required "name" field.');return s}function Be(e){if(!I.existsSync(e))return{previous:null};const s=I.readFileSync(e,"utf8"),t=p.yamlBlueParse(s);if(!t||!g(t))throw new Error("Existing BlueRepository.blue is not a valid object.");const n=t,r=n.packages,o=n.repositoryVersions,i=Array.isArray(r)&&r.every(l=>g(l)&&typeof l.name=="string"&&Array.isArray(l.types)),c=Array.isArray(o)&&o.every(l=>typeof l=="string");if(!i||!c)throw new Error("Invalid BlueRepository.blue structure: expected { packages: BluePackage[], repositoryVersions: string[] }");const a=r,u=o,f={name:typeof n.name=="string"?n.name:M,packages:a,repositoryVersions:u};return{existingYaml:s,previous:f}}function Ae(e){return C.dump(e,{lineWidth:-1})}function Pe(e){const s=$.resolve(e.repoRoot),t=$.resolve(e.blueRepositoryPath),{existingYaml:n,previous:r}=Be(t),o=L(s),i=fe(r);le(o,i);const c=J(o),a=W(c);H(c,o);const{aliasToBlueId:u,aliasToStorageContent:f}=Q(a,o),l=r?r.repositoryVersions.length:0,y=pe({discovered:o,previousTypes:i,aliasToBlueId:u,aliasToStorageContent:f,nextRepoVersionIndex:l}),m=he(y),w=be(m),{document:E,changed:B}=we(m,r,w);ve(E.packages,E.repositoryVersions,u);const V=Ae(E);if(r&&!B&&n&&n!==V)throw new Error("BlueRepository.blue content differs from regenerated output while RepoBlueId is unchanged. Please revert manual edits or rerun in write mode.");return e.verbose&&console.info(`[repository-generator] RepoBlueId: ${w} (${B?"changed":"unchanged"})`),{document:E,currentRepoBlueId:w,previousRepoBlueId:r?.repositoryVersions.at(-1),changed:B||!n,yaml:V,existingYaml:n}}exports.generateRepository=Pe;