@dropins/build-tools
Advanced tools
Comparing version
@@ -1,6 +0,7 @@ | ||
import p from"fs/promises";import{parseModule as w}from"esprima";import B from"escodegen";import{parse as f,print as E}from"graphql/language/index.mjs";import g from"path";function S(n){return f(n).definitions[0].name.value}function b(n,t){const e=f(n),o=new Set,s=new Set;return{operation:t.reduce((c,u)=>{var d,x;const[l]=e.definitions,[r]=f(u).definitions,i=r.name.value;return l.kind!==r.kind||l.name.value!==i||l.operation!==r.operation||((d=l.typeCondition)==null?void 0:d.name.value)!==((x=r.typeCondition)==null?void 0:x.name.value)?(s.add([i]),n):(v(l,r).forEach($=>{o.add([i,...$])}),E(e))},n),success:[...o],failure:[...s]}}function v(n,t,e=[]){const o=[];for(const s of t.selectionSet.selections){const a=n.selectionSet.selections.find(r=>{var i;return((i=r.name)==null?void 0:i.value)===s.name.value}),c=[...e,s.name.value],u=(a==null?void 0:a.arguments)??[],l=s.arguments;for(const r of l){const i=u.find(m=>m.name.value===r.name.value);i?i.value=r.value:u.push(r)}if(a!=null&&a.selectionSet&&s.selectionSet)o.push(...v(a,s,c));else{if(s.selectionSet&&c.push(s.selectionSet.selections[0].name.value),o.push(c),a)continue;n.selectionSet.selections.push(s)}}return o}async function j(n){try{await p.access(n);const t=n.replace(/\.\w+$/,".original$&");try{await p.access(t)}catch{await p.copyFile(n,t)}return{src:g.resolve(t),dist:g.resolve(n)}}catch{return null}}async function F(n){const t=g.join(process.cwd(),"node_modules",n,"fragments.js");return await j(t)}function y(n,t){return n.body.filter(e=>e.type==="ExportNamedDeclaration").flatMap(e=>e.specifiers).find(e=>e.exported.name===t)}function h(n,t){return n.body.filter(e=>e.type==="VariableDeclaration").flatMap(e=>e.declarations).find(e=>e.id.name===t)}function M(n){const t=n.init.quasis.map(o=>o.value.raw).join("").trim(),e=n.init.expressions.map(o=>`\${${o.name}}`).join(` | ||
`).trim();return[t,e]}function A(n,t){const e=w(n),o=y(e,t);if(!o)return null;const s=h(e,o.local.name),a=M(s);return{exported:o.exported.name,local:o.local.name,content:a[0],expressions:a[1]}}function D(n,t,e){const o=w(n),s=y(o,t),{local:a}=s,c=h(o,a.name);return c.init={type:"Literal",value:e,raw:`\`${e}\``},B.generate(o,{verbatim:"raw",format:{indent:{style:""},json:!0}}).replace(/\$\{\s(.*)\s\}/g,"${$1}")}async function C(n=[]){console.group("🔄 Updating GraphQl operation files...",` | ||
`);let t=new Map;for(const e of n){const{npm:o,operations:s=[]}=e;for(const a of s){const c=await F(o);if(!c){console.warn("\x1B[31m%s\x1B[0m",`⛔️ GraphQl file in "${o}" not found.`,"\x1B[0m");continue}await p.copyFile(c.src,c.dist);const u=t.get(c.dist)||await p.readFile(c.src,"utf-8"),l=S(a),r=A(u,l);if(!r){console.warn("\x1B[31m%s\x1B[0m",`⛔️ Operation "${l}" not found.`,"\x1B[0m");continue}const i=b(r.content,[a]);i.failure.forEach(d=>{console.warn("\x1B[31m%s\x1B[0m",`𐄂 ${d.join(".")}`,"\x1B[0m")}),i.success.forEach(d=>{console.log("\x1B[32m%s\x1B[0m",`✔ ${d.join(".")}`,"\x1B[0m")}),r.expressions&&(i.operation+=` | ||
`+r.expressions);const m=D(u,l,i.operation);t.set(c.dist,m)}}console.groupEnd(),console.log(` | ||
`),await Promise.all(Array.from(t.keys()).map(async e=>{const o=t.get(e);await p.writeFile(e,o),console.log(`💾 File "${e}" updated.`)})),console.log(` | ||
`)}export{C as default,C as overrideGQLOperations}; | ||
import p from"fs/promises";import{parseModule as w}from"esprima";import B from"escodegen";import{parse as f,print as S}from"graphql/language/index.mjs";import x from"path";function b(t){return f(t).definitions[0].name.value}function E(t,n){const e=f(t),o=new Set,a=new Set;return{operation:n.reduce((l,u)=>{var d,g;const[c]=e.definitions,[r]=f(u).definitions,i=r.name.value;return c.kind!==r.kind||c.name.value!==i||c.operation!==r.operation||((d=c.typeCondition)==null?void 0:d.name.value)!==((g=r.typeCondition)==null?void 0:g.name.value)?(a.add([i]),t):(v(c,r).forEach($=>{o.add([i,...$])}),S(e))},t),success:[...o],failure:[...a]}}function v(t,n,e=[]){const o=[];for(const a of n.selectionSet.selections){const s=t.selectionSet.selections.find(r=>{var i;return((i=r.name)==null?void 0:i.value)===a.name.value}),l=[...e,a.name.value],u=(s==null?void 0:s.arguments)??[],c=a.arguments;for(const r of c){const i=u.find(m=>m.name.value===r.name.value);i?i.value=r.value:u.push(r)}if(s!=null&&s.selectionSet&&a.selectionSet)o.push(...v(s,a,l));else{if(a.selectionSet&&l.push(a.selectionSet.selections[0].name.value),o.push(l),s)continue;t.selectionSet.selections.push(a)}}return o}async function j(t){try{await p.access(t);const n=t.replace(/\.\w+$/,".original$&");try{await p.access(n)}catch{await p.copyFile(t,n)}return{src:x.resolve(n),dist:x.resolve(t)}}catch{return null}}async function F(t){const n=x.join(process.cwd(),"node_modules",t,"fragments.js");return await j(n)}function y(t,n){return t.body.filter(e=>e.type==="ExportNamedDeclaration").flatMap(e=>e.specifiers).find(e=>e.exported.name===n)}function h(t,n){return t.body.filter(e=>e.type==="VariableDeclaration").flatMap(e=>e.declarations).find(e=>e.id.name===n)}function M(t){const n=t.init.quasis.map(o=>o.value.raw).join("").trim(),e=t.init.expressions.map(o=>`\${${o.name}}`).join(` | ||
`).trim();return[n,e]}function k(t,n){const e=w(t),o=y(e,n);if(!o)return null;const a=h(e,o.local.name),s=M(a);return{exported:o.exported.name,local:o.local.name,content:s[0],expressions:s[1]}}function A(t,n,e){const o=w(t),a=y(o,n),{local:s}=a,l=h(o,s.name);return l.init={type:"Literal",value:e,raw:`\`${e}\``},B.generate(o,{verbatim:"raw",format:{indent:{style:""},json:!0}}).replace(/\$\{\s(.*)\s\}/g,"${$1}")}async function V(t=[]){console.log("🔎 Checking GraphQl operation files...");let n=new Map;for(const e of t){const{npm:o,operations:a=[]}=e,s=await F(o);if(!s){console.warn("\x1B[31m%s\x1B[0m",`⛔️ GraphQl file in "${o}" not found.`,"\x1B[0m");continue}await p.copyFile(s.src,s.dist),console.log(` | ||
🔄 File "${s.dist}" restored. | ||
`);for(const l of a){const u=n.get(s.dist)||await p.readFile(s.src,"utf-8"),c=b(l),r=k(u,c);if(!r){console.warn("\x1B[31m%s\x1B[0m",`⛔️ Operation "${c}" not found.`,"\x1B[0m");continue}const i=E(r.content,[l]);i.failure.forEach(d=>{console.warn("\x1B[31m%s\x1B[0m",`𐄂 ${d.join(".")}`,"\x1B[0m")}),i.success.forEach(d=>{console.log("\x1B[32m%s\x1B[0m",`✔ ${d.join(".")}`,"\x1B[0m")}),r.expressions&&(i.operation+=` | ||
`+r.expressions);const m=A(u,c,i.operation);n.set(s.dist,m)}}await Promise.all(Array.from(n.keys()).map(async e=>{const o=n.get(e);await p.writeFile(e,o),console.log(` | ||
💾 File "${e}" updated. | ||
`)}))}export{V as default,V as overrideGQLOperations}; |
@@ -1,1 +0,1 @@ | ||
{"name":"@dropins/build-tools","version":"0.1.0-alpha1","description":"A package that provides build tools for Dropins","license":"MIT","type":"module","engines":{"node":">=16"},"scripts":{"lint":"eslint","test":"jest","test:ci":"jest --config jest.config.js --passWithNoTests --coverage","build":"vite build","postbuild":"cp ./package.json ./dist"},"dependencies":{"escodegen":"^2.1.0","esprima":"^4.0.1","graphql":"^16.9.0"},"devDependencies":{"@types/escodegen":"^0.0.10","@types/esprima":"^4.0.6","typescript":"^4.7.3","vite":"^5.2.10","vite-plugin-dts":"^3.9.1"}} | ||
{"name":"@dropins/build-tools","version":"0.1.0-alpha2","description":"A package that provides build tools for Dropins","license":"MIT","type":"module","engines":{"node":">=16"},"scripts":{"lint":"eslint","test":"jest","test:ci":"jest --config jest.config.js --passWithNoTests --coverage","build":"vite build","postbuild":"cp ./package.json ./dist"},"dependencies":{"escodegen":"^2.1.0","esprima":"^4.0.1","graphql":"^16.9.0"},"devDependencies":{"@types/escodegen":"^0.0.10","@types/esprima":"^4.0.6","typescript":"^4.7.3","vite":"^5.2.10","vite-plugin-dts":"^3.9.1"}} |
Sorry, the diff of this file is not supported yet
50
2.04%6611
-0.21%