Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update release and conditions plugins
- Loading branch information
1 parent
d881968
commit 77546b5
Showing
2 changed files
with
69 additions
and
20 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
/* eslint-disable */ | ||
//prettier-ignore | ||
module.exports = { | ||
name: "@yarnpkg/plugin-conditions", | ||
factory: function (require) { | ||
var plugin;plugin=(()=>{"use strict";var e={990:(e,t,n)=>{n.r(t),n.d(t,{default:()=>x});const r=require("@yarnpkg/core");const o=["dependencies","devDependencies","peerDependencies"];function i(e){return e.startsWith("condition:")}function s(e){try{return function(e){if(!e.startsWith("condition:",0))throw new Error(`Expected 'condition:' at index 0 (${e})`);let t="condition:".length;a();const n=c(/[\w-]+/y);if(!n)throw new Error(`Expected an identifier at index ${t} (${e})`);a(),s("?"),a();let r=null;"("===e[t]?(r=u().trim()||null,a()):":"!==e[t]&&(r=l(":").trimRight()||null),s(":"),a();let o=null;t<e.length&&("("===e[t]?(o=u().trim()||null,a()):":"!==e[t]&&(o=l("#").trimRight()||null));let i=null;if(t<e.length&&"#"===e[t]&&(t++,i=c(/\w+/y),a()),t!==e.length)throw new Error(`Unexpected '${e[t]}' at index ${t} (${e})`);return{test:n,consequent:r,alternate:o,hash:i};function s(n){if(e[t]!==n)throw new Error(`Expected '${n}' at index ${t} (${e})`);t++}function a(){c(/\s*/y)}function c(n){n.lastIndex=t;const r=n.exec(e);return r?(t+=r[0].length,r[0]):null}function l(n){const r=t;return t=e.indexOf(n,r),-1===t&&(t=e.length),e.slice(r,t)}function u(){s("(");let n=1,r="";for(;n;){if(t===e.length)throw new Error(`Expected ')' at index ${t} (${e})`);const o=e[t];"("===o&&n++,")"===o&&n--,(")"!==o||n>0)&&(r+=o),t++}return r}}(e)}catch(t){try{const{test:t,consequent:n,alternate:o}=r.structUtils.parseRange(e).params;return{test:t,consequent:n||null,alternate:o||null}}catch(e){throw t}}}function a(e){return s(e.range)}function c(e){return s(e.reference)}function l({test:e,consequent:t,alternate:n,hash:r}){return`condition:${e}?${t||""}:${n||""}#${r||""}`}function u(e,{test:t,consequent:n,alternate:o,hash:i}){return r.structUtils.makeLocator(e,l({test:t,consequent:n,alternate:o,hash:i}))}function d(e,t,n,o,i){const s=r.structUtils.makeIdent(t.scope,`${t.name}-${n}-${i}`),a=e.configuration.get("defaultProtocol")+`${r.structUtils.stringifyIdent(t)}@${o}`;return r.structUtils.makeDescriptor(s,a)}function p(e,t,n,o){return r.hashUtils.makeHash(String(6),e,t||"-",n||"-",o?"1":"0").slice(0,6)}const f={conditions:{description:"",type:r.SettingsType.MAP,valueDefinition:{description:"",type:r.SettingsType.SHAPE,properties:{source:{description:"",type:r.SettingsType.STRING,default:"env"},default:{description:"",type:r.SettingsType.BOOLEAN,default:!1}}}}};function h(e,t){if(!e.configuration.get("conditions").has(t))throw new Error(`Unknown condition: ${t}. You must add it to your .yarnrc.yml file.`)}function g(e,t){return h(e,t),e.configuration.get("conditions").get(t).get("default")}function m(e,t){var n;h(e,t);const r=e.configuration.get("conditions").get(t),o=r.get("source"),i=r.get("default");if("env"!==o)throw new Error("The only supported configuration source is 'env'");return null!==(n=(s=process.env[t])&&"false"!==s&&"0"!==s)&&void 0!==n?n:i;var s}const y=require("@yarnpkg/fslib"),w=require("@yarnpkg/libzip");const v=require("@yarnpkg/cli"),k=require("clipanion");var P=function(e,t,n,r){var o,i=arguments.length,s=i<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,n,r);else for(var a=e.length-1;a>=0;a--)(o=e[a])&&(s=(i<3?o(s):i>3?o(t,n,s):o(t,n))||s);return i>3&&s&&Object.defineProperty(t,n,s),s};class j extends v.BaseCommand{async execute(){if(this.false&&this.true)throw new Error("You can either specify --true or --false");const{project:e,workspace:t,cache:n,configuration:o}=await this.getRoot();h(e,this.condition);const i=!this.false&&(!!this.true||m(e,this.condition));for(const n of this.nestedWorkspaces(t,e))this.materializeCondition(i,n);const s=await r.StreamReport.start({configuration:o,stdout:this.context.stdout,includeLogs:!0},async t=>{await e.resolveEverything({cache:n,report:t})});if(s.hasErrors())return s.exitCode();await e.persist()}*nestedWorkspaces(e,t){yield e;for(const n of e.workspacesCwds){const e=t.workspacesByCwd.get(n);e&&(yield*this.nestedWorkspaces(e,t))}}materializeCondition(e,t){for(const n of o){const o=t.manifest.getForScope(n).values();for(const s of o){if(!i(s.range))continue;const{test:o,consequent:c,alternate:l}=a(s);if(o!==this.condition)continue;const u=e?c:l;u?t.manifest[n].set(s.identHash,r.structUtils.makeDescriptor(s,u)):t.manifest[n].delete(s.identHash)}}}async getRoot(){const e=await r.Configuration.find(this.context.cwd,this.context.plugins),[{project:t,workspace:n},o]=await Promise.all([r.Project.find(e,this.context.cwd),r.Cache.find(e,{immutable:!0})]);return{configuration:e,project:t,workspace:n,cache:o}}}j.usage=k.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 "}),P([k.Command.String({required:!0})],j.prototype,"condition",void 0),P([k.Command.Boolean("--true")],j.prototype,"true",void 0),P([k.Command.Boolean("--false")],j.prototype,"false",void 0),P([k.Command.Path("condition","materialize")],j.prototype,"execute",null);const x={configuration:f,commands:[j],fetchers:[class{supports(e){return i(e.reference)}getLocalPath(){return null}async fetch(e,t){const n=t.checksums.get(e.locatorHash)||null,[o,i,s]=await t.cache.fetchPackageFromCache(e,n,{onHit:()=>t.report.reportCacheHit(e),onMiss:()=>t.report.reportCacheMiss(e,r.structUtils.prettyLocator(t.project.configuration,e)+" can't be found in the cache and will be fetched from the disk"),loader:()=>this.generateConditionPackage(e,t),skipIntegrityCheck:t.skipIntegrityCheck});return{packageFs:o,releaseFs:i,prefixPath:r.structUtils.getIdentVendorPath(e),localPath:this.getLocalPath(),checksum:s}}async generateConditionPackage(e,t){const{test:n,consequent:o,alternate:i}=c(e),s=g(t.project,n),a=p(n,o,i,s),l=(o,i)=>{if(null==o)return{dependency:null,require:"null"};const s=d(t.project,e,n,o,i),a=r.structUtils.stringifyIdent(s);return{dependency:{[a]:s.range},require:`require(${JSON.stringify(a)})`}},u=l(o,!0),f=l(i,!1);return async function(e,t,n,o){const[i,s]=await Promise.all([y.xfs.mktempPromise(),(0,w.getLibzipPromise)()]),a=y.ppath.join(i,"condition.zip"),c=r.structUtils.getIdentVendorPath(e),l=new y.ZipFS(a,{libzip:s,create:!0,level:t.configuration.get("compressionLevel")});return await l.mkdirpPromise(c),await Promise.all([l.writeJsonPromise(y.ppath.join(c,"package.json"),n),l.writeFilePromise(y.ppath.join(c,"index.js"),o)]),await Promise.all(l.getAllFiles().map(e=>l.utimesPromise(e,15805116e5,15805116e5))),l}(e,t.project,{version:"0.0.0-condition-"+a,dependencies:{...u.dependency,...f.dependency}},`// env vars from the cli are always strings, so !!ENV_VAR returns true for "false"\nfunction bool(value) {\n if (value == null) return ${s};\n return value && value !== "false" && value !== "0";\n}\nmodule.exports = bool(process.env[${JSON.stringify(n)}])\n ? ${u.require}\n : ${f.require};\n`)}}],resolvers:[class{supportsDescriptor(e){return i(e.range)}supportsLocator(e){return i(e.reference)}shouldPersistResolution(){return!1}bindDescriptor(e){return e}getResolutionDependencies(e,t){const{test:n,consequent:r,alternate:o}=a(e);return[r&&d(t.project,e,n,r,!0),o&&d(t.project,e,n,o,!1)].filter(Boolean)}async getCandidates(e,t,n){const{test:r,consequent:o,alternate:i}=a(e);return[u(e,{test:r,consequent:o,alternate:i,hash:p(r,o,i,g(n.project,r))})]}async getSatisfying(){return null}async resolve(e,t){const{test:n,consequent:o,alternate:i}=c(e),s=p(n,o,i,g(t.project,n)),a=o&&d(t.project,e,n,o,!0),l=i&&d(t.project,e,n,i,!1);return{...e,version:"0.0.0-condition-"+s,languageName:t.project.configuration.get("defaultLanguageName"),linkType:r.LinkType.HARD,dependencies:new Map([o&&[a.identHash,a],i&&[l.identHash,l]].filter(Boolean)),peerDependencies:new Map,dependenciesMeta:new Map,peerDependenciesMeta:new Map,bin:null}}}],hooks:{beforeWorkspacePacking:async function(e,t){var n;const{project:s}=e;let c=!1;for(const l of o){const o=e.manifest.getForScope(l).values();for(const u of o){if(!i(u.range))continue;const{test:o,consequent:d,alternate:p}=a(u),f=m(s,o)?d:p,h=r.structUtils.stringifyIdent(u),g="dependencies"===l&&!t.dependencies[h]&&(null===(n=t.optionalDependencies)||void 0===n?void 0:n[h])?"optionalDependencies":l;f?(t[g][h]=f,e.manifest.raw[g][h]=f,e.manifest[l].set(u.identHash,r.structUtils.makeDescriptor(u,f))):(delete t[g][h],delete e.manifest.raw[g][h],e.manifest[l].delete(u.identHash)),c=!0}}c&&await e.project.configuration.triggerHook(e=>e.beforeWorkspacePacking,e,t)}}}}},t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={exports:{}};return e[r](o,o.exports,n),o.exports}return n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n(990)})(); | ||
var plugin=(()=>{var te=Object.create,U=Object.defineProperty,ne=Object.defineProperties,oe=Object.getOwnPropertyDescriptor,ie=Object.getOwnPropertyDescriptors,re=Object.getOwnPropertyNames,I=Object.getOwnPropertySymbols,se=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty,ae=Object.prototype.propertyIsEnumerable;var N=(t,e,n)=>e in t?U(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,E=(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))ae.call(e,n)&&N(t,n,e[n]);return t},V=(t,e)=>ne(t,ie(e)),ce=t=>U(t,"__esModule",{value:!0});var u=t=>{if(typeof require!="undefined")return require(t);throw new Error('Dynamic require of "'+t+'" is not supported')};var le=(t,e)=>{for(var n in e)U(t,n,{get:e[n],enumerable:!0})},pe=(t,e,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of re(e))!W.call(t,o)&&o!=="default"&&U(t,o,{get:()=>e[o],enumerable:!(n=oe(e,o))||n.enumerable});return t},f=t=>pe(ce(U(t!=null?te(se(t)):{},"default",t&&t.__esModule&&"default"in t?{get:()=>t.default,enumerable:!0}:{value:t,enumerable:!0})),t);var me={};le(me,{default:()=>fe});var Q=f(u("@yarnpkg/core"));var k=f(u("@yarnpkg/core"));function M(t){let e="condition:";if(!t.startsWith(e,0))throw new Error(`Expected 'condition:' at index 0 (${t})`);let n=e.length;a();let o=d(/[\w-]+/y);if(!o)throw new Error(`Expected an identifier at index ${n} (${t})`);a(),s("?"),a();let i=null;t[n]==="("?(i=m().trim()||null,a()):t[n]!==":"&&(i=P(":").trimRight()||null),s(":"),a();let r=null;n<t.length&&(t[n]==="("?(r=m().trim()||null,a()):t[n]!==":"&&(r=P("#").trimRight()||null));let c=null;if(n<t.length&&t[n]==="#"&&(n++,c=d(/\w+/y),a()),n!==t.length)throw new Error(`Unexpected '${t[n]}' at index ${n} (${t})`);return{test:o,consequent:i,alternate:r,hash:c};function s(l){if(t[n]!==l)throw new Error(`Expected '${l}' at index ${n} (${t})`);n++}function a(){d(/\s*/y)}function d(l){l.lastIndex=n;let p=l.exec(t);return p?(n+=p[0].length,p[0]):null}function P(l){let p=n;return n=t.indexOf(l,p),n===-1&&(n=t.length),t.slice(p,n)}function m(){s("(");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 $=["dependencies","devDependencies","peerDependencies"],z=6;function h(t){return t.startsWith("condition:")}function A(t){try{return M(t)}catch(e){try{let{test:n,consequent:o,alternate:i}=k.structUtils.parseRange(t).params;return{test:n,consequent:o||null,alternate:i||null}}catch{throw e}}}function x(t){return A(t.range)}function b(t){return A(t.reference)}function de({test:t,consequent:e,alternate:n,hash:o}){return`condition:${t}?${e||""}:${n||""}#${o||""}`}function Y(t,{test:e,consequent:n,alternate:o,hash:i}){return k.structUtils.makeLocator(t,de({test:e,consequent:n,alternate:o,hash:i}))}function v(t,e,n,o,i){let r=k.structUtils.makeIdent(e.scope,`${e.name}-${n}-${i}`),c=t.configuration.get("defaultProtocol")+`${k.structUtils.stringifyIdent(e)}@${o}`;return k.structUtils.makeDescriptor(r,c)}function j(t,e,n,o){return k.hashUtils.makeHash(String(z),t,e||"-",n||"-",o?"1":"0").slice(0,6)}var L=f(u("@yarnpkg/core")),B={conditions:{description:"",type:L.SettingsType.MAP,valueDefinition:{description:"",type:L.SettingsType.SHAPE,properties:{source:{description:"",type:L.SettingsType.STRING,default:"env"},default:{description:"",type:L.SettingsType.BOOLEAN,default:!1}}}}};function H(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 R(t,e){return H(t,e),t.configuration.get("conditions").get(e).get("default")}function q(t,e){var r;H(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=ue(process.env[e]))!=null?r:i}function ue(t){return t&&t!=="false"&&t!=="0"}var O=class{supportsDescriptor(e){return h(e.range)}supportsLocator(e){return h(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:c}=x(e),s=j(i,r,c,R(o.project,i));return[Y(e,{test:i,consequent:r,alternate:c,hash:s})]}async getSatisfying(){return null}async resolve(e,n){let{test:o,consequent:i,alternate:r}=b(e),c=j(o,i,r,R(n.project,o)),s=i&&v(n.project,e,o,i,!0),a=r&&v(n.project,e,o,r,!1);return V(E({},e),{version:`0.0.0-condition-${c}`,languageName:n.project.configuration.get("defaultLanguageName"),linkType:Q.LinkType.HARD,dependencies:new Map([i&&[s.identHash,s],r&&[a.identHash,a]].filter(Boolean)),peerDependencies:new Map,dependenciesMeta:new Map,peerDependenciesMeta:new Map,bin:null})}};var S=f(u("@yarnpkg/core"));var _=f(u("@yarnpkg/core")),y=f(u("@yarnpkg/fslib")),J=f(u("@yarnpkg/libzip")),K=15805116e5;async function G(t,e,n,o){let[i,r]=await Promise.all([y.xfs.mktempPromise(),(0,J.getLibzipPromise)()]),c=y.ppath.join(i,"condition.zip"),s=_.structUtils.getIdentVendorPath(t),a=new y.ZipFS(c,{libzip:r,create:!0,level:e.configuration.get("compressionLevel")});return await a.mkdirpPromise(s),await Promise.all([a.writeJsonPromise(y.ppath.join(s,"package.json"),n),a.writeFilePromise(y.ppath.join(s,"index.js"),o)]),await Promise.all(a.getAllFiles().map(d=>a.utimesPromise(d,K,K))),a}var F=class{supports(e){return h(e.reference)}getLocalPath(){return null}async fetch(e,n){let o=n.checksums.get(e.locatorHash)||null,[i,r,c]=await n.cache.fetchPackageFromCache(e,o,{onHit:()=>n.report.reportCacheHit(e),onMiss:()=>n.report.reportCacheMiss(e,`${S.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:S.structUtils.getIdentVendorPath(e),localPath:this.getLocalPath(),checksum:c}}async generateConditionPackage(e,n){let{test:o,consequent:i,alternate:r}=b(e),c=R(n.project,o),s=j(o,i,r,c),a=(m,l)=>{if(m==null)return{dependency:null,require:"null"};let p=v(n.project,e,o,m,l),D=S.structUtils.stringifyIdent(p);return{dependency:{[D]:p.range},require:`require(${JSON.stringify(D)})`}},d=a(i,!0),P=a(r,!1);return G(e,n.project,{version:`0.0.0-condition-${s}`,dependencies:E(E({},d.dependency),P.dependency)},`// env vars from the cli are always strings, so !!ENV_VAR returns true for "false" | ||
function bool(value) { | ||
if (value == null) return ${c}; | ||
return value && value !== "false" && value !== "0"; | ||
} | ||
module.exports = bool(process.env[${JSON.stringify(o)}]) | ||
? ${d.require} | ||
: ${P.require}; | ||
`)}};var T=f(u("@yarnpkg/core"));async function Z(t,e){var i;let{project:n}=t,o=!1;for(let r of $){let c=t.manifest.getForScope(r).values();for(let s of c){if(!h(s.range))continue;let{test:a,consequent:d,alternate:P}=x(s),m=q(n,a)?d:P,l=T.structUtils.stringifyIdent(s),p=r==="dependencies"&&!e.dependencies[l]&&((i=e.optionalDependencies)==null?void 0:i[l])?"optionalDependencies":r;m?(e[p][l]=m,t.manifest.raw[p][l]=m,t.manifest[r].set(s.identHash,T.structUtils.makeDescriptor(s,m))):(delete e[p][l],delete t.manifest.raw[p][l],t.manifest[r].delete(s.identHash)),o=!0}}o&&await t.project.configuration.triggerHook(r=>r.beforeWorkspacePacking,t,e)}var g=f(u("@yarnpkg/core")),X=f(u("@yarnpkg/cli")),w=f(u("clipanion")),ee=f(u("typanion"));var C=class extends X.BaseCommand{constructor(){super(...arguments);this.condition=w.Option.String({required:!0});this.true=w.Option.Boolean("--true",!1);this.false=w.Option.Boolean("--false",!1)}async execute(){let{project:e,workspace:n,cache:o,configuration:i}=await this.getRoot();H(e,this.condition);let r=this.false?!1:this.true?!0:q(e,this.condition);for(let s of this.nestedWorkspaces(n,e))this.materializeCondition(r,s);let c=await g.StreamReport.start({configuration:i,stdout:this.context.stdout,includeLogs:!0},async s=>{await e.resolveEverything({cache:o,report:s})});if(c.hasErrors())return c.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 o of $){let i=n.manifest.getForScope(o).values();for(let r of i){if(!h(r.range))continue;let{test:c,consequent:s,alternate:a}=x(r);if(c!==this.condition)continue;let d=e?s:a;d?n.manifest[o].set(r.identHash,g.structUtils.makeDescriptor(r,d)):n.manifest[o].delete(r.identHash)}}}async getRoot(){let e=await g.Configuration.find(this.context.cwd,this.context.plugins),[{project:n,workspace:o},i]=await Promise.all([g.Project.find(e,this.context.cwd),g.Cache.find(e,{immutable:!0})]);return{configuration:e,project:n,workspace:o,cache:i}}};C.paths=[["condition","materialize"]],C.usage=w.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 "}),C.schema=[ee.hasMutuallyExclusiveKeys(["true","false"])];var fe={configuration:B,commands:[C],fetchers:[F],resolvers:[O],hooks:{beforeWorkspacePacking:Z}};return me;})(); | ||
return plugin; | ||
} | ||
}; | ||
}; |