|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @summary Some package manager that doesn't sucks too much. |
| 4 | + * |
| 5 | + * @author {PUKE-RIGHTS-AUTHOR} |
| 6 | + * @version {PUKE-PACKAGE-VERSION} |
| 7 | + * |
| 8 | + * @license {PUKE-RIGHTS-LICENSE}. |
| 9 | + * @copyright {PUKE-RIGHTS-COPYRIGHT} |
| 10 | + * @name {PUKE-GIT-ROOT}/jsboot/gister/packman.js{PUKE-GIT-REVISION} |
| 11 | + */ |
| 12 | + |
| 13 | + |
| 14 | +// Guarantee that require is here |
| 15 | + |
| 16 | +/* |
| 17 | +Inline module declarations |
| 18 | +
|
| 19 | +// Define sources |
| 20 | +gister.module('Bar').at('bar.js'); |
| 21 | +gister.module('Foo').at('foo.js'); |
| 22 | +
|
| 23 | +Modules can be declared inline: |
| 24 | +
|
| 25 | +gister.module('Foo', function() { |
| 26 | + export let x = 42; |
| 27 | +}); |
| 28 | +
|
| 29 | +gister.import('bar.js').as('Bar'); |
| 30 | +gister.import({'y': 'localName'}).from('Bar'); |
| 31 | +gister.import('y').from("bar.js"); |
| 32 | +
|
| 33 | +External module load |
| 34 | +
|
| 35 | +Modules can be loaded from external resources: |
| 36 | +
|
| 37 | + // Variant A: import URL syntax |
| 38 | +
|
| 39 | + // Variant B: module = syntax |
| 40 | +It is not necessary to bind a module to a local name, if the programmer simply wishes to import |
| 41 | +directly from the module: |
| 42 | +
|
| 43 | +*/ |
| 44 | + |
| 45 | + |
| 46 | +/* |
| 47 | +gister.use('Module').as('Toto').from('Thing'); |
| 48 | +gister.use('Module').as('Toto').from('Thing'); |
| 49 | +gister.add('Toto1234').as('TotoVariableName'); |
| 50 | +gister.pack('Stuffy.Thing', function(){ |
| 51 | +}); |
| 52 | +
|
| 53 | +
|
| 54 | + define = function(name, deps, callback) { |
| 55 | + require = req = function(deps, callback, relName, forceSync, alt) { |
| 56 | +
|
| 57 | +*/ |
| 58 | + |
| 59 | +/*global window*/ |
| 60 | + |
| 61 | +if (typeof jsBoot == 'undefined') |
| 62 | + var jsBoot = {}; |
| 63 | + |
| 64 | +(function(globalObject) { |
| 65 | + 'use strict'; |
| 66 | + |
| 67 | + // Adapted from JSON3 (credit Oyvind Sean Kinsey) |
| 68 | + // Detect the "define" function exposed by asynchronous module loaders |
| 69 | + // var isLoader = typeof define === 'function' && define.amd; |
| 70 | + // var internalObj = typeof exports == 'object' && exports; |
| 71 | + |
| 72 | + var toUse = []; |
| 73 | + var lastUse; |
| 74 | + var toAdd = []; |
| 75 | + var lastAdd; |
| 76 | + |
| 77 | + var flush = function() { |
| 78 | + if (lastUse) { |
| 79 | + toUse.push(lastUse); |
| 80 | + lastUse = null; |
| 81 | + } |
| 82 | + if (lastAdd) { |
| 83 | + if (!lastAdd.name) |
| 84 | + throw new Error('NEED_A_NAME', 'Trying to bind something without name'); |
| 85 | + toAdd.push(lastAdd); |
| 86 | + lastAdd = null; |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + this.use = function(a, optional) { |
| 91 | + flush(); |
| 92 | + lastUse = {module: a, name: a.split('.').pop(), optional: optional}; |
| 93 | + return this; |
| 94 | + }; |
| 95 | + |
| 96 | + this.add = function(a, optional) { |
| 97 | + if ((a === undefined) && !optional) |
| 98 | + throw new Error('UNDEFINED', 'Requesting something local that is undefined'); |
| 99 | + flush(); |
| 100 | + lastAdd = {value: a}; |
| 101 | + return this; |
| 102 | + }; |
| 103 | + |
| 104 | + this.as = function(a) { |
| 105 | + if (lastUse) |
| 106 | + lastUse.name = a; |
| 107 | + else if (lastAdd) |
| 108 | + lastAdd.name = a; |
| 109 | + flush(); |
| 110 | + }; |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + var simplePull = function(glob, name, optional) { |
| 115 | + name.split('.').forEach(function(fragment) { |
| 116 | + if (!glob || !(fragment in glob)) |
| 117 | + if (!optional) |
| 118 | + throw new Error('MISSING', 'Trying to require something that doesn\'t exist: ' + name); |
| 119 | + else |
| 120 | + return (glob = undefined); |
| 121 | + glob = glob[fragment]; |
| 122 | + }); |
| 123 | + return glob; |
| 124 | + }; |
| 125 | + |
| 126 | + var parentPull = function(glob, name) { |
| 127 | + var ret = {}; |
| 128 | + name.split('.').forEach(function(fragment) { |
| 129 | + ret.o = glob; |
| 130 | + ret.k = fragment; |
| 131 | + if (!(fragment in glob)) |
| 132 | + glob[fragment] = {}; |
| 133 | + glob = glob[fragment]; |
| 134 | + }); |
| 135 | + return ret; |
| 136 | + }; |
| 137 | + |
| 138 | + // var amdHack = {}; |
| 139 | + |
| 140 | + this.pack = function(name, factory) { |
| 141 | + // Close anything going on |
| 142 | + flush(); |
| 143 | + // Dereference requested stuff and flush it |
| 144 | + var localUse = toUse; |
| 145 | + var localAdd = toAdd; |
| 146 | + toUse = []; |
| 147 | + toAdd = []; |
| 148 | + |
| 149 | + // Add local elements to the API |
| 150 | + var api = {}; |
| 151 | + localAdd.forEach(function(item) { |
| 152 | + if (item.name in api) |
| 153 | + throw new Error('ALREADY_DEFINED', 'You are shadowing ' + api[item.name] + ' with ' + |
| 154 | + item + ' for name ' + item.name); |
| 155 | + api[item.name] = item.value; |
| 156 | + }); |
| 157 | + |
| 158 | + // If AMD pattern |
| 159 | + /* |
| 160 | + if (isLoader || internalObj) { |
| 161 | + // Get dependencies names |
| 162 | + var deps = []; |
| 163 | + var udeps = []; |
| 164 | + var localNames = []; |
| 165 | + localUse.forEach(function(item) { |
| 166 | + var k = item.module.replace(/\./g, '/').split('/'); |
| 167 | + var rest = []; |
| 168 | + while(!(k.join('/') in amdHack) && k.length){ |
| 169 | + rest.unshift(k.pop()); |
| 170 | + console.warn("******** unshifting", JSON.stringify(rest)); |
| 171 | + } |
| 172 | + if(!k.length) |
| 173 | + throw new Error('UNRESOLVED', 'Requested dep doesnt pan out'); |
| 174 | + deps.push(k.join('/')); |
| 175 | + udeps.push(rest); |
| 176 | +
|
| 177 | + localNames.push(item.name); |
| 178 | + }); |
| 179 | +
|
| 180 | + // Fuck the identifier as well AMD style |
| 181 | + name = name.replace(/\./g, '/'); |
| 182 | + // deps.unshift(name); |
| 183 | +
|
| 184 | + if (isLoader) { |
| 185 | + var module; |
| 186 | + var there = (name in amdHack); |
| 187 | + amdHack[name] = module = there ? amdHack[name] : {}; |
| 188 | + if(!there){ |
| 189 | + console.warn("defininnnnnnnnng", name); |
| 190 | + define(name, amdHack[name]); |
| 191 | + } |
| 192 | +
|
| 193 | + console.warn("getting", JSON.stringify(deps), JSON.stringify(udeps)); |
| 194 | + require(deps, function(){ |
| 195 | + console.warn("Actually defining/requiring with no name", name); |
| 196 | + var args = Array.prototype.slice.call(arguments); |
| 197 | + localNames.forEach(function(key, idx){ |
| 198 | + if (key in api) |
| 199 | + throw new Error('ALREADY_DEFINED', 'You are shadowing ' + key); |
| 200 | + api[key] = args.shift(); |
| 201 | + while(udeps[idx].length){ |
| 202 | + console.warn("----------> raising the bar"); |
| 203 | + api[key] = api[key][udeps[idx].shift()]; |
| 204 | + } |
| 205 | + if(api[key] === undefined) |
| 206 | + throw new Error('MISSING', 'Trying to require something that doesn\'t exist'); |
| 207 | + }); |
| 208 | + // udeps |
| 209 | + console.warn("getting udeps", udeps); |
| 210 | +
|
| 211 | + var sub = {}; |
| 212 | + sub = factory.apply(sub, [api]) || sub; |
| 213 | + Object.keys(sub).forEach(function(key){ |
| 214 | + module[key] = sub[key]; |
| 215 | + define(name + '/' + key, module[key]); |
| 216 | + }); |
| 217 | + }); |
| 218 | +
|
| 219 | + // require(deps, function(mod){ |
| 220 | + // var module = mod || {}; |
| 221 | + // var args = Array.prototype.slice.call(arguments, 1); |
| 222 | + // localNames.forEach(function(key){ |
| 223 | + // if (key in api) |
| 224 | + // throw new Error('ALREADY_DEFINED', 'You are shadowing ' + key); |
| 225 | + // api[key] = args.shift(); |
| 226 | + // if(api[key] === undefined) |
| 227 | + // throw new Error('MISSING', 'Trying to require something that doesn\'t exist'); |
| 228 | + // }); |
| 229 | + // console.warn("requiring", name, mod, api); |
| 230 | +
|
| 231 | + // factory.apply(module, [api]) || module; |
| 232 | + // }); |
| 233 | +
|
| 234 | + } |
| 235 | + }else{*/ |
| 236 | + localUse.forEach(function(item) { |
| 237 | + if (item.name in api) |
| 238 | + throw new Error('ALREADY_DEFINED', 'You are shadowing name ' + item.name); |
| 239 | + api[item.name] = simplePull(globalObject, item.module, item.optional); |
| 240 | + }); |
| 241 | + |
| 242 | + var r = parentPull(globalObject, name); |
| 243 | + r.o[r.k] = factory.apply(r.o[r.k], [api]) || r.o[r.k]; |
| 244 | + // } |
| 245 | + }; |
| 246 | + |
| 247 | + this.run = function(factory) { |
| 248 | + // Close anything going on |
| 249 | + flush(); |
| 250 | + // Dereference requested stuff and flush it |
| 251 | + var localUse = toUse; |
| 252 | + var localAdd = toAdd; |
| 253 | + toUse = []; |
| 254 | + toAdd = []; |
| 255 | + |
| 256 | + // Add local elements to the API |
| 257 | + var api = {}; |
| 258 | + localAdd.forEach(function(item) { |
| 259 | + if (item.name in api) |
| 260 | + throw new Error('ALREADY_DEFINED', 'You are shadowing ' + item.name); |
| 261 | + api[item.name] = item.value; |
| 262 | + }); |
| 263 | + |
| 264 | + // If AMD pattern |
| 265 | + /* if (isLoader || internalObj) { |
| 266 | + // Get dependencies names |
| 267 | + var deps = localUse.map(function(item) { |
| 268 | + return item.module.replace(/\./g, '/'); |
| 269 | + }); |
| 270 | + var localNames = localUse.map(function(item) { |
| 271 | + return item.name; |
| 272 | + }); |
| 273 | + if (isLoader) { |
| 274 | + require(deps, function(){ |
| 275 | + var args = Array.prototype.slice.call(arguments); |
| 276 | + localNames.forEach(function(key){ |
| 277 | + if (key in api) |
| 278 | + throw new Error('ALREADY_DEFINED', 'You are shadowing ' + key); |
| 279 | + api[key] = args.shift(); |
| 280 | + if(api[key] === undefined) |
| 281 | + throw new Error('MISSING', 'Trying to require something that doesn\'t exist'); |
| 282 | + }); |
| 283 | + factory.apply({}, [api]); |
| 284 | + }); |
| 285 | + } |
| 286 | + // Regular pattern |
| 287 | + } else {*/ |
| 288 | + localUse.forEach(function(item) { |
| 289 | + if (item.name in api) |
| 290 | + throw new Error('ALREADY_DEFINED', 'You are shadowing ' + item.name); |
| 291 | + api[item.name] = simplePull(globalObject, item.module, item.optional); |
| 292 | + }); |
| 293 | + |
| 294 | + factory.apply({}, [api]); |
| 295 | + // } |
| 296 | + }; |
| 297 | +}).apply(jsBoot, [window]); |
| 298 | + |
0 commit comments