@dropins/build-tools
Advanced tools
Comparing version 0.1.0-alpha3 to 0.1.0-alpha4
@@ -5,4 +5,3 @@ export type FilePath = { | ||
}; | ||
export declare function getFilePath(file: string): Promise<FilePath | null>; | ||
export declare function getFragmentsFilePath(nodeModuleName: string): Promise<FilePath | null>; | ||
//# sourceMappingURL=get-paths.d.ts.map |
@@ -1,7 +0,7 @@ | ||
import c from"fs/promises";import{parseModule as j}from"esprima";import E from"escodegen";import{parse as x,print as S}from"graphql";import d from"path";function C(n,t){const[e]=t.definitions,o=e.name.value,s=n.definitions.find(r=>{var l,u;return r.kind===e.kind&&r.name.value===o&&r.operation===e.operation&&((l=r.typeCondition)==null?void 0:l.name.value)===((u=e.typeCondition)==null?void 0:u.name.value)});if(!s)return[n,{success:[],errors:[[o]]}];const a=h(s,e).map(r=>[o,...r]);return[n,{success:a,errors:[]}]}function h(n,t,e=[]){const o=[];for(const s of t.selectionSet.selections){const i=k(n,s),a=[...e,s.name.value];D(i,s),i!=null&&i.selectionSet&&s.selectionSet?o.push(...h(i,s,a)):M(n,s,i,a,o)}return o}function k(n,t){return n.selectionSet.selections.find(e=>{var o;return((o=e.name)==null?void 0:o.value)===t.name.value})}function D(n,t){const e=(n==null?void 0:n.arguments)??[],o=t.arguments;for(const s of o){const i=e.find(a=>a.name.value===s.name.value);i?i.value=s.value:e.push(s)}}function M(n,t,e,o,s){t.selectionSet&&o.push(t.selectionSet.selections[0].name.value),s.push(o),e||n.selectionSet.selections.push(t)}async function V(n){try{await c.access(n);const t=n.replace(/\.\w+$/,".original$&");try{await c.access(t)}catch{await c.copyFile(n,t)}return{src:d.resolve(t),dist:d.resolve(n)}}catch{return null}}async function G(n){const t=d.join(process.cwd(),"node_modules",n,"fragments.js");return await V(t)}function v(n,t){return n.body.filter(e=>e.type==="ExportNamedDeclaration").flatMap(e=>e.specifiers).find(e=>e.exported.name===t)}function w(n,t){return n.body.filter(e=>e.type==="VariableDeclaration").flatMap(e=>e.declarations).find(e=>e.id.name===t)}function L(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 Q(n,t){const e=v(n,t);if(!e)return null;const o=w(n,e.local.name),s=L(o);return{exported:e.exported.name,local:e.local.name,content:s[0],expressions:s[1]}}function A(n,t,e){const o=v(n,t),{local:s}=o,i=w(n,s.name);return i.init={type:"Literal",value:e,raw:`\`${e}\``},E.generate(n,{verbatim:"raw",format:{indent:{style:""},json:!0}}).replace(/\$\{\s(.*)\s\}/g,"${$1}")}async function _(n=[]){console.log("🔎 Checking GraphQl operation files...");let t=new Map;for(const e of n){const{npm:o,operations:s=[]}=e,i=await G(o);if(!i){console.warn("\x1B[31m%s\x1B[0m",`⛔️ Cannot access fragments.js file in "${o}".`,"\x1B[0m");continue}await c.copyFile(i.src,i.dist),console.log(` | ||
import u from"fs/promises";import{parseModule as b}from"esprima";import j from"escodegen";import{parse as v,print as k}from"graphql";import g from"path";function I(t,n){const[e,...s]=n.definitions,o=e.name.value;if(s.length>0)return[t,{success:[],errors:[[o]]}];const r=t.definitions.find(c=>{var m,l;return c.kind===e.kind&&c.name.value===o&&c.operation===e.operation&&((m=c.typeCondition)==null?void 0:m.name.value)===((l=e.typeCondition)==null?void 0:l.name.value)});if(!r)return[t,{success:[],errors:[[o]]}];const a=x(r,e).map(c=>[o,...c]);return[t,{success:a,errors:[]}]}function y(t,n){return t.selectionSet.selections.find(e=>{var s,o;return((s=e.name)==null?void 0:s.value)===((o=n.name)==null?void 0:o.value)})}function x(t,n,e=[]){const s=[];return n.selectionSet.selections.forEach(o=>{o.kind==="Field"&&P(t,o,e,s),o.kind==="InlineFragment"&&D(t,o,e,s)}),s}function P(t,n,e,s){const o=y(t,n),r=[...e,n.name.value];L(o,n),o!=null&&o.selectionSet&&n.selectionSet?s.push(...x(o,n,r)):O(t,n,o,r,s)}function D(t,n,e,s){var a;const o=(a=n.typeCondition)==null?void 0:a.name.value,r=[...e,o],i=M(t,o);i?V(i,n,r,s):G(t,n,r,s)}function M(t,n){return t.selectionSet.selections.find(e=>{var s;return e.kind==="InlineFragment"&&((s=e.typeCondition)==null?void 0:s.name.value)===n})}function V(t,n,e,s){n.selectionSet.selections.forEach(o=>{var i;const r=y(t,o);if(r)x(r,o,e);else{t.selectionSet.selections.push(o);const a=`${(i=n.typeCondition)==null?void 0:i.name.value}(${o.name.value})`;e[e.length-1]=a,s.push([...e])}})}function G(t,n,e,s){t.selectionSet.selections.push(n),n.selectionSet.selections.forEach(o=>{var i;const r=`${(i=n.typeCondition)==null?void 0:i.name.value}(${o.name.value})`;e[e.length-1]=r,s.push([...e])})}function L(t,n){const e=(t==null?void 0:t.arguments)??[],s=n.arguments;for(const o of s){const r=e.find(i=>i.name.value===o.name.value);r?r.value=o.value:e.push(o)}}function O(t,n,e,s,o){n.selectionSet&&s.push(n.selectionSet.selections[0].name.value),o.push(s),e||t.selectionSet.selections.push(n)}async function Q(t){try{await u.access(t);const n=t.replace(/\.\w+$/,".original$&");try{await u.access(n)}catch{await u.copyFile(t,n)}return{src:g.resolve(n),dist:g.resolve(t)}}catch{return null}}async function A(t){const n=g.join(process.cwd(),"node_modules",t,"fragments.js");return Q(n)}function $(t,n){return t.body.filter(e=>e.type==="ExportNamedDeclaration").flatMap(e=>e.specifiers).find(e=>e.exported.name===n)}function w(t,n){return t.body.filter(e=>e.type==="VariableDeclaration").flatMap(e=>e.declarations).find(e=>e.id.name===n)}function N(t){const n=t.init.quasis.map(s=>s.value.raw).join("").trim(),e=t.init.expressions.map(s=>`\${${s.name}}`).join(` | ||
`).trim();return[n,e]}function q(t,n){const e=$(t,n);if(!e)return null;const s=w(t,e.local.name),o=N(s);return{exported:e.exported.name,local:e.local.name,content:o[0],expressions:o[1]}}function U(t,n,e){const s=$(t,n),{local:o}=s,r=w(t,o.name);return r.init={type:"Literal",value:e,raw:`\`${e}\``},j.generate(t,{verbatim:"raw",format:{indent:{style:""},json:!0}}).replace(/\$\{\s(.*)\s\}/g,"${$1}")}async function K(t=[]){console.log("🔎 Checking GraphQl operation files...");let n=new Map;for(const s of t){const{npm:o,operations:r=[]}=s,i=await A(o);if(!i){console.warn("\x1B[31m%s\x1B[0m",`⛔️ Cannot access fragments.js file in "${o}".`,"\x1B[0m");continue}await u.copyFile(i.src,i.dist),console.log(` | ||
🔄 File "${i.dist}" restored. | ||
`);for(const a of s){const r=t.get(i.dist)??await c.readFile(i.src,"utf-8"),l=j(r),u=x(a),p=u.definitions[0].name.value,f=Q(l,p);if(!f){console.warn("\x1B[31m%s\x1B[0m",`⛔️ Could not find export "${p}" in "${i.dist}".`,"\x1B[0m");continue}const y=x(f.content),[$,{success:B,errors:F}]=C(y,u);F.forEach(m=>{console.warn("\x1B[31m%s\x1B[0m",`𐄂 ${m.join(".")}`,"\x1B[0m")}),B.forEach(m=>{console.log("\x1B[32m%s\x1B[0m",`✔ ${m.join(".")}`,"\x1B[0m")});let g=S($);f.expressions&&(g+=` | ||
`+f.expressions);const b=A(l,p,g);t.set(i.dist,b)}}for(const e of t.keys()){const o=t.get(e);await c.writeFile(e,o),console.log(` | ||
💾 File "${e}" updated. | ||
`)}}export{_ as default,_ as overrideGQLOperations}; | ||
`);for(const a of r){const c=n.get(i.dist)??await u.readFile(i.src,"utf-8"),m=b(c),l=v(a),p=l.definitions[0].name.value;if(l.definitions.length>1){console.warn("\x1B[31m%s\x1B[0m",`⛔️ Operation "${p}" in ${i.dist} has multiple definitions. Skipping...`,"\x1B[0m");continue}const f=q(m,p);if(!f){console.warn("\x1B[31m%s\x1B[0m",`⛔️ Could not find export "${p}" in "${i.dist}".`,"\x1B[0m");continue}const F=v(f.content),[B,{success:S,errors:C}]=I(F,l);C.forEach(d=>{console.warn("\x1B[31m%s\x1B[0m",`𐄂 ${d.join(".")}`,"\x1B[0m")}),S.forEach(d=>{console.log("\x1B[32m%s\x1B[0m",`✔ ${d.join(".")}`,"\x1B[0m")});let h=k(B);f.expressions&&(h+=` | ||
`+f.expressions);const E=U(m,p,h);n.set(i.dist,E)}}const e=Array.from(n.keys()).map(s=>{const o=n.get(s);return u.writeFile(s,o).then(()=>{console.log(` | ||
💾 File "${s}" updated. | ||
`)})});await Promise.all(e)}export{K as default,K as overrideGQLOperations}; |
@@ -1,1 +0,1 @@ | ||
{"name":"@dropins/build-tools","version":"0.1.0-alpha3","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-alpha4","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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
7683
50