/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/mechanism@0.0.14/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";var fs=require("fs"),path=require("path"),util=require("util"),assert=require("assert"),events=require("events"),EventEmitter=events.EventEmitter,async=require("async"),globby=require("globby"),mkdirp=require("mkdirp"),minimatch=require("minimatch"),Orchestrator=require("orchestrator"),Cache=require("./lib/cache.js"),automatch=require("./lib/automatch.js"),unique=require("./lib/unique.js"),sfile=function(t){return t.file};function Mechanism(){this.ops=[],this.cache=new Cache,this.gens=[],this.generated=!1}function check(t,e){var n="",r="";return t.some((function(t){t.targets.some((function(t){"/"===t.slice(-1)&&(n=t),"/*"===t.slice(-2)&&(n=t),t.slice(0,-3).indexOf("*")>=0&&(r=t)}))})),n?e(new Error('To make "'+n+'" target directory, trailing it with "/**"')):r?e(new Error('Star found in target "'+r+'". Maybe swap targets and sources?')):e()}function clone(t){return{id:t.id,unresolvedSources:t.unresolvedSources,targets:t.targets,plugin:t.plugin,perform:!1,deps:[]}}function SingleRound(t,e,n){this.ops=t.map(clone),this.targetsList=[],this.targetsMap={},this.cache=e,this.options=n||{}}util.inherits(Mechanism,EventEmitter),Mechanism.prototype.generate=function(t,e){return Array.isArray(t)||(t=[t]),this.gens.push({targets:t,content:e}),this._operate(t)},Mechanism.prototype._generate=function(t){return this.generated?t():(this.generated=!0,0===this.gens.length?t():void async.map(this.gens,(function(t,e){assert(1===t.targets.length),mkdirp(path.dirname(t.targets[0]),(function(n){if(n)return e(n);fs.writeFile(t.targets[0],t.content,(function(t){return t?e(t):e()}))}))}),(function(e){return t(e)})))},Mechanism.prototype.operate=function(t,e,n){return Array.isArray(t)||(t=[t]),Array.isArray(e)||(e=[e]),this.ops.push({id:unique(),unresolvedSources:t,targets:e,plugin:n}),this._operate(e)},Mechanism.prototype._operate=function(t){return{operate:function(e,n,r){return"object"==typeof n&&(r=n,n=e,e=[]),Array.isArray(e)||(e=[e]),Array.isArray(n)||(n=[n]),e=e.concat(t),this.operate(e,n,r)}.bind(this)}},SingleRound.prototype.populateTargets=function(t){var e=this;return e.ops.some((function(t){t.targets.some((function(n){assert("string"==typeof n),e.targetsMap[n]||(e.targetsMap[n]=[],e.targetsList.push(n)),e.targetsMap[n].push(t)}))})),t()},SingleRound.prototype._globbyWithStat=function(t,e,n){async.waterfall([function(e){globby(t,{},e)},function(t,n){t=t.filter((function(t){return!1===e.some((function(e){return automatch(t,e.file)}))})),async.map(t,(function(t,e){fs.stat(t,(function(n,r){return n?e(n):e(null,{file:t,stat:r})}))}),n)},function(t,e){return e(null,t=t.filter((function(t){return assert(t.stat),!t.stat.isDirectory()})))}],n)},SingleRound.prototype.resolveSources=function(t){var e=this;async.map(e.ops,(function(t,n){var r=t.unresolvedSources,i=t.plugin.additionalSources||[],o=[];e.targetsList.some((function(n){automatch(r,n)&&(o.push({file:n}),e.targetsMap[n].some((function(e){t.deps.push(e)})))})),async.waterfall([function(t){e._globbyWithStat(r,o,t)},function(t,n){e._globbyWithStat(i,o,(function(e,r){return n(e,t,r)}))},function(n,s,a){if(o.length+n.length===0)return a(new Error('Source "'+JSON.stringify(r)+'" not found neither on disk nor in targets'));var u=e.cache.changed(r,n),c=e.cache.changed(i,s);return t.perform=u||c,t.sourcesFromTargets=r,t.sourcesFromDisk=n.map(sfile),a()}],n)}),(function(e){return t(e)}))},SingleRound.prototype.detect=function(t){for(var e,n,r,i,o=this,s=function(t){return t.perform};;){for(r=!1,e=0;e<o.ops.length;)(n=o.ops[e]).perform||n.deps.some(s)&&(n.perform=!0,r=!0),e++;if(!r)break}for(i=!1,e=0;e<o.ops.length;){if((n=o.ops[e]).perform){i=!0;break}e++}return o.cache.flush(),t(null,i)},SingleRound.prototype.handler=function(t,e){var n=this;async.waterfall([function(e){var r=t.targets,i=t.sourcesFromDisk,o={mkdirp:mkdirp,minimatch:minimatch,automatch:automatch};if(0===t.sourcesFromTargets.length)return e(null,r,i,o);var s=function(t){return i.indexOf(t)<0};n._globbyWithStat(t.sourcesFromTargets,[],(function(t,n){if(t)return e(t);n=n.map(sfile).filter(s);var a=i.concat(n);return e(null,r,a,o)}))},function(e,r,i,o){if(n.options.verbose&&r.length&&e.length){var s;1===r.length?s=path.basename(r[0]):(s=t.unresolvedSources[0],"*"!==path.basename(s).slice(0,1)&&(s=path.basename(s)));var a=path.basename(e[0]);"**"===a&&(a=e[0]);var u=new Array(40).join(" ").slice(0,-s.length);console.log(s+u+a)}t.plugin.handler({targets:e,sources:r,shares:i},o)}],e)},SingleRound.prototype.start=function(t){var e=this,n=[];e.ops.some((function(t){n.push({name:t.id,deps:t.deps.map((function(t){return t.id})),handler:function(n){if(!t.perform)return n();e.handler(t,n)},_p:t.perform})}));var r=new Orchestrator;n.some((function(t){r.add(t.name,t.deps,t.handler)})),r.start(n.map((function(t){return t.name})),t)},Mechanism.prototype.start=function(t){var e=this,n=function(t,n){t?e.emit("error",t):e.emit(n)},r=function(t,e){async.waterfall([function(e){t.populateTargets(e)},function(e){t.resolveSources(e)},function(e){t.detect(e)}],e)},i=e.cache.clone();async.waterfall([function(t){var n;n=t,async.waterfall([function(t){check(e.ops,t)},function(t){e._generate(t)}],n)},function(n){var o=new SingleRound(e.ops,i,t);r(o,n)},function(o,s){if(!o)return s();n(null,"detected"),function n(){async.waterfall([function(n){var o=new SingleRound(e.ops,i,t);r(o,n)},function(i,o){if(i)return setTimeout(n,200);var s=new SingleRound(e.ops,e.cache,t);r(s,(function(t,e){o(t,s,e)}))},function(t,e,n){t.start(n)}],s)}()}],(function(t){n(t,"finish")}))},module.exports=Mechanism;
//# sourceMappingURL=/sm/aca25353051030c67adfd7d6e9f834943e83d8aa29f4a9f15e5b7f9e54a4d2ae.map