18 lines
10 KiB
JavaScript
18 lines
10 KiB
JavaScript
/* eslint-disable */
|
|
//prettier-ignore
|
|
module.exports = {
|
|
name: "@yarnpkg/plugin-conditions",
|
|
factory: function (require) {
|
|
var plugin=(()=>{var ne=Object.create,w=Object.defineProperty,oe=Object.defineProperties,ie=Object.getOwnPropertyDescriptor,re=Object.getOwnPropertyDescriptors,se=Object.getOwnPropertyNames,I=Object.getOwnPropertySymbols,ae=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty,ce=Object.prototype.propertyIsEnumerable;var N=(t,e,n)=>e in t?w(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,j=(t,e)=>{for(var n in e||(e={}))W.call(e,n)&&N(t,n,e[n]);if(I)for(var n of I(e))ce.call(e,n)&&N(t,n,e[n]);return t},V=(t,e)=>oe(t,re(e)),le=t=>w(t,"__esModule",{value:!0});var g=t=>{if(typeof require!="undefined")return require(t);throw new Error('Dynamic require of "'+t+'" is not supported')};var pe=(t,e)=>{for(var n in e)w(t,n,{get:e[n],enumerable:!0})},de=(t,e,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of se(e))!W.call(t,o)&&o!=="default"&&w(t,o,{get:()=>e[o],enumerable:!(n=ie(e,o))||n.enumerable});return t},m=t=>de(le(w(t!=null?ne(ae(t)):{},"default",t&&t.__esModule&&"default"in t?{get:()=>t.default,enumerable:!0}:{value:t,enumerable:!0})),t);var ke={};pe(ke,{default:()=>he});var Q=m(g("@yarnpkg/core"));var y=m(g("@yarnpkg/core"));function z(t){let e="condition:";if(!t.startsWith(e,0))throw new Error(`Expected 'condition:' at index 0 (${t})`);let n=e.length;c();let o=d(/[\w-]+/y);if(!o)throw new Error(`Expected an identifier at index ${n} (${t})`);c(),a("?"),c();let i=null;t[n]==="("?(i=f().trim()||null,c()):t[n]!==":"&&(i=u(":").trimRight()||null),a(":"),c();let r=null;n<t.length&&(t[n]==="("?(r=f().trim()||null,c()):t[n]!==":"&&(r=u("#").trimRight()||null));let s=null;if(n<t.length&&t[n]==="#"&&(n++,s=d(/\w+/y),c()),n!==t.length)throw new Error(`Unexpected '${t[n]}' at index ${n} (${t})`);return{test:o,consequent:i,alternate:r,hash:s};function a(l){if(t[n]!==l)throw new Error(`Expected '${l}' at index ${n} (${t})`);n++}function c(){d(/\s*/y)}function d(l){l.lastIndex=n;let p=l.exec(t);return p?(n+=p[0].length,p[0]):null}function u(l){let p=n;return n=t.indexOf(l,p),n===-1&&(n=t.length),t.slice(p,n)}function f(){a("(");let l=1,p="";for(;l;){if(n===t.length)throw new Error(`Expected ')' at index ${n} (${t})`);let D=t[n];D==="("&&l++,D===")"&&l--,(D!==")"||l>0)&&(p+=D),n++}return p}}var R=["dependencies","devDependencies","peerDependencies"],A=6;function k(t){return t.startsWith("condition:")}function Y(t){try{return z(t)}catch(e){try{let{test:n,consequent:o,alternate:i}=y.structUtils.parseRange(t).params;return{test:n,consequent:o||null,alternate:i||null}}catch{throw e}}}function x(t){return Y(t.range)}function $(t){return Y(t.reference)}function ue({test:t,consequent:e,alternate:n,hash:o}){return`condition:${t}?${e||""}:${n||""}#${o||""}`}function B(t,{test:e,consequent:n,alternate:o,hash:i}){return y.structUtils.makeLocator(t,ue({test:e,consequent:n,alternate:o,hash:i}))}function v(t,e,n,o,i){let r=y.structUtils.makeIdent(e.scope,`${e.name}-${n}-${i}`),s=t.configuration.get("defaultProtocol")+`${y.structUtils.stringifyIdent(e)}@${o}`;return y.structUtils.makeDescriptor(r,s)}function E(t,e,n,o){return y.hashUtils.makeHash(String(A),t,e||"-",n||"-",o?"1":"0").slice(0,6)}var b=m(g("@yarnpkg/core")),M={conditions:{description:"",type:b.SettingsType.MAP,valueDefinition:{description:"",type:b.SettingsType.SHAPE,properties:{source:{description:"",type:b.SettingsType.STRING,default:"env"},default:{description:"",type:b.SettingsType.BOOLEAN,default:!1}}}}};function q(t,e){if(!t.configuration.get("conditions").has(e))throw new Error(`Unknown condition: ${e}. You must add it to your .yarnrc.yml file.`)}function L(t,e){return q(t,e),t.configuration.get("conditions").get(e).get("default")}function O(t,e){var r;q(t,e);let n=t.configuration.get("conditions").get(e),o=n.get("source"),i=n.get("default");if(o!=="env")throw new Error("The only supported configuration source is 'env'");return(r=fe(process.env[e]))!=null?r:i}function fe(t){return t&&t!=="false"&&t!=="0"}var F=class{supportsDescriptor(e){return k(e.range)}supportsLocator(e){return k(e.reference)}shouldPersistResolution(){return!1}bindDescriptor(e){return e}getResolutionDependencies(e,n){let{test:o,consequent:i,alternate:r}=x(e);return[i&&v(n.project,e,o,i,!0),r&&v(n.project,e,o,r,!1)].filter(Boolean)}async getCandidates(e,n,o){let{test:i,consequent:r,alternate:s}=x(e),a=E(i,r,s,L(o.project,i));return[B(e,{test:i,consequent:r,alternate:s,hash:a})]}async getSatisfying(){return null}async resolve(e,n){let{test:o,consequent:i,alternate:r}=$(e),s=E(o,i,r,L(n.project,o)),a=i&&v(n.project,e,o,i,!0),c=r&&v(n.project,e,o,r,!1);return V(j({},e),{version:`0.0.0-condition-${s}`,languageName:n.project.configuration.get("defaultLanguageName"),linkType:Q.LinkType.HARD,dependencies:new Map([i&&[a.identHash,a],r&&[c.identHash,c]].filter(Boolean)),peerDependencies:new Map,dependenciesMeta:new Map,peerDependenciesMeta:new Map,bin:null})}};var H=m(g("@yarnpkg/core"));var _=m(g("@yarnpkg/core")),P=m(g("@yarnpkg/fslib")),J=m(g("@yarnpkg/libzip")),K=15805116e5;async function G(t,e,n,o){let[i,r]=await Promise.all([P.xfs.mktempPromise(),(0,J.getLibzipPromise)()]),s=P.ppath.join(i,"condition.zip"),a=_.structUtils.getIdentVendorPath(t),c=new P.ZipFS(s,{libzip:r,create:!0,level:e.configuration.get("compressionLevel")});return await c.mkdirpPromise(a),await Promise.all([c.writeJsonPromise(P.ppath.join(a,"package.json"),n),c.writeFilePromise(P.ppath.join(a,"index.js"),o)]),await Promise.all(c.getAllFiles().map(d=>c.utimesPromise(d,K,K))),c}var S=class{supports(e){return k(e.reference)}getLocalPath(){return null}async fetch(e,n){let o=n.checksums.get(e.locatorHash)||null,[i,r,s]=await n.cache.fetchPackageFromCache(e,o,{onHit:()=>n.report.reportCacheHit(e),onMiss:()=>n.report.reportCacheMiss(e,`${H.structUtils.prettyLocator(n.project.configuration,e)} can't be found in the cache and will be fetched from the disk`),loader:()=>this.generateConditionPackage(e,n),skipIntegrityCheck:n.skipIntegrityCheck});return{packageFs:i,releaseFs:r,prefixPath:H.structUtils.getIdentVendorPath(e),localPath:this.getLocalPath(),checksum:s}}async generateConditionPackage(e,n){let{test:o,consequent:i,alternate:r}=$(e),s=L(n.project,o),a=E(o,i,r,s),c=(f,l)=>{if(f==null)return{dependency:null,require:"null"};let p=v(n.project,e,o,f,l),D=H.structUtils.stringifyIdent(p);return{dependency:{[D]:p.range},require:`require(${JSON.stringify(D)})`}},d=c(i,!0),u=c(r,!1);return G(e,n.project,{version:`0.0.0-condition-${a}`,dependencies:j(j({},d.dependency),u.dependency)},`// env vars from the cli are always strings, so !!ENV_VAR returns true for "false"
|
|
function bool(value) {
|
|
if (value == null) return ${s};
|
|
return value && value !== "false" && value !== "0";
|
|
}
|
|
module.exports = bool(process.env[${JSON.stringify(o)}])
|
|
? ${d.require}
|
|
: ${u.require};
|
|
`)}};var T=m(g("@yarnpkg/core"));var ge=Function.call.bind(Object.prototype.hasOwnProperty);async function Z(t,e){var i;let{project:n}=t,o=!1;for(let r of R){let s=t.manifest.getForScope(r).values();for(let a of s){if(!k(a.range))continue;let{test:c,consequent:d,alternate:u}=x(a),f=O(n,c)?d:u,l=T.structUtils.stringifyIdent(a),p=r==="dependencies"&&!e.dependencies[l]&&((i=e.optionalDependencies)==null?void 0:i[l])?"optionalDependencies":r;f?(e[p][l]=f,t.manifest.raw[p][l]=f,t.manifest[r].set(a.identHash,T.structUtils.makeDescriptor(a,f))):(delete e[p][l],delete t.manifest.raw[p][l],t.manifest[r].delete(a.identHash)),o=!0}}if(ge(e,"conditions")){o=!0;let r=e.conditions;for(let[s,[a,c]]of Object.entries(r)){let d=O(n,s)?a:c;if(d)for(let[u,f]of Object.entries(d))f===null?delete e[u]:e[u]=f}delete e.conditions}o&&await t.project.configuration.triggerHook(r=>r.beforeWorkspacePacking,t,e)}var h=m(g("@yarnpkg/core")),X=m(g("@yarnpkg/cli")),C=m(g("clipanion")),ee=m(g("typanion"));var me=Function.call.bind(Object.prototype.hasOwnProperty),te=(t,e,...n)=>me(t,e)&&(n.length===0||te(t[e],...n)),U=class extends X.BaseCommand{constructor(){super(...arguments);this.condition=C.Option.String({required:!0});this.true=C.Option.Boolean("--true",!1);this.false=C.Option.Boolean("--false",!1)}async execute(){let{project:e,workspace:n,cache:o,configuration:i}=await this.getRoot();q(e,this.condition);let r=this.false?!1:this.true?!0:O(e,this.condition);for(let a of this.nestedWorkspaces(n,e))this.materializeCondition(r,a);let s=await h.StreamReport.start({configuration:i,stdout:this.context.stdout,includeLogs:!0},async a=>{await e.resolveEverything({cache:o,report:a})});if(s.hasErrors())return s.exitCode();await e.persist()}*nestedWorkspaces(e,n){yield e;for(let o of e.workspacesCwds){let i=n.workspacesByCwd.get(o);i&&(yield*this.nestedWorkspaces(i,n))}}materializeCondition(e,n){for(let i of R){let r=n.manifest.getForScope(i).values();for(let s of r){if(!k(s.range))continue;let{test:a,consequent:c,alternate:d}=x(s);if(a!==this.condition)continue;let u=e?c:d;u?n.manifest[i].set(s.identHash,h.structUtils.makeDescriptor(s,u)):n.manifest[i].delete(s.identHash)}}let o=n.manifest.raw;if(te(o,"conditions",this.condition)){let[i,r]=o.conditions[this.condition],s=e?i:r;if(s)for(let[a,c]of Object.entries(s))c===null?delete o[a]:o[a]=c;Object.keys(o.conditions).length===1?delete o.conditions:delete o.conditions[this.condition]}}async getRoot(){let e=await h.Configuration.find(this.context.cwd,this.context.plugins),[{project:n,workspace:o},i]=await Promise.all([h.Project.find(e,this.context.cwd),h.Cache.find(e,{immutable:!0})]);return{configuration:e,project:n,workspace:o,cache:i}}};U.paths=[["condition","materialize"]],U.usage=C.Command.Usage({description:"Evaluate and replace a condition in package.json files",details:"\n This command will replace all the occurrences of `<condition>` in the current workspace and in nested workspaces.\n\n The value of the condition (`true` or `false`) is based on the following sources, in descending priority order:\n\n - the `--true` or `--false` option;\n - the `<condition>` environment variable;\n - the default value specified in the Yarn configuration;\n - `false` by default.\n "}),U.schema=[ee.hasMutuallyExclusiveKeys(["true","false"])];var he={configuration:M,commands:[U],fetchers:[S],resolvers:[F],hooks:{beforeWorkspacePacking:Z}};return ke;})();
|
|
return plugin;
|
|
}
|
|
};
|