├── .gitignore ├── ChangeLog ├── LICENSE ├── README.md ├── lib └── require.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | 2015.04.01, v1.0.4 2 | 3 | fix: 4 | - (require) exports -> module.exports 5 | 6 | 7 | 2015.03.01, v1.0.3 8 | 9 | fix: 10 | - (require) require: exports is undefined 11 | 12 | feature: 13 | - (package) v1.0.2 14 | - (require) require: rm begin, end, add args 15 | 16 | 17 | 2015.03.01, v1.0.2 18 | 19 | feature: 20 | - (require) require: rm begin, end, add args 21 | 22 | 23 | 2015.02.26, v1.0.1 24 | 25 | feature: 26 | - (require) read files as utf-8 27 | 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2015 coderaiser 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Adobe ExtendScript Require 2 | 3 | Function `require` compatible with [node.js](http://nodejs.org "Node.js"). 4 | Could be used with [es-node](https://github.com/coderaiser/es-node "AE Node"). 5 | 6 | ## Why? 7 | 8 | In Adobe ExtendScript `#include` used for loading dependencies. 9 | It puts all modules to global scope. With `require` it could be avoided. 10 | 11 | ## Example 12 | 13 | ```js 14 | #include 'lib/require.js'; 15 | 16 | var dir = File($.fileName).path + '/'; 17 | require.dir(dir); 18 | 19 | var fs = require('./lib/fs'); 20 | var data = fs.readFileSync('./name'); 21 | 22 | alert(data); 23 | ``` 24 | ## See Also 25 | 26 | - [require for MongoDB](https://github.com/coderaiser/mongo-require "Mongo Require") 27 | 28 | ## License 29 | 30 | MIT 31 | -------------------------------------------------------------------------------- /lib/require.js: -------------------------------------------------------------------------------- 1 | /*jshint -W079*/ 2 | var require; 3 | /*jshint +W079 */ 4 | 5 | (function() { 6 | 'use strict'; 7 | 8 | var Modules = {}, 9 | Dir = ''; 10 | 11 | /*global File */ 12 | 13 | /*jshint -W020 */ 14 | require = function(name) { 15 | var code, 16 | module = {}, 17 | file = openFile(Dir + name), 18 | filename = file.fullName, 19 | dirname = getDir(filename); 20 | 21 | if (Modules[filename]) 22 | module.exports = Modules[filename]; 23 | else 24 | code = readFile(file, function(error, code) { 25 | var fn; 26 | 27 | if (error) { 28 | throwError(error, file.name); 29 | } else { 30 | error = tryCatch(function() { 31 | var args = [ 32 | 'exports', 33 | 'require', 34 | 'module', 35 | '__filename', 36 | '__dirname']; 37 | 38 | fn = Function(args, code); 39 | 40 | fn(module.exports, require, module, filename, dirname); 41 | }); 42 | 43 | if (error) { 44 | if (/.js$/.test(error.message)) 45 | name = ''; 46 | else 47 | name = file.name; 48 | 49 | throwError(error.message, name); 50 | } 51 | 52 | Modules[filename] = module.exports; 53 | } 54 | }); 55 | 56 | return module.exports; 57 | }; 58 | /*jshint +W020 */ 59 | 60 | require.dir = function(dir) { 61 | Dir = dir; 62 | }; 63 | 64 | function getDir(path) { 65 | var index = path.lastIndexOf('/'), 66 | dir = path.slice(0, index) || '/'; 67 | 68 | return dir; 69 | } 70 | 71 | function openFile(name) { 72 | var file = tryOpen(name, ['.js', '.jsx']); 73 | return file; 74 | } 75 | 76 | function readFile(file, callback) { 77 | var data = file.read(data); 78 | 79 | if (!file.error) 80 | file.close(); 81 | 82 | callback(file.error, data, file); 83 | } 84 | 85 | function tryOpen(name, exts) { 86 | var file; 87 | 88 | some(exts, function(ext) { 89 | var is; 90 | 91 | file = new File(name + ext); 92 | 93 | file.encoding = 'UTF-8'; 94 | 95 | is = file.open('r:'); 96 | 97 | return is; 98 | }); 99 | 100 | if (file.error) { 101 | throwError(file.error, file.name); 102 | } 103 | 104 | return file; 105 | } 106 | 107 | function throwError(error, fileName) { 108 | if (fileName) 109 | fileName = ' in ' + fileName; 110 | 111 | throw Error(error + fileName); 112 | } 113 | 114 | function tryCatch(fn) { 115 | var error; 116 | 117 | try { 118 | fn(); 119 | } catch(err) { 120 | error = err; 121 | } 122 | 123 | return error; 124 | } 125 | 126 | function some(array, fn) { 127 | var i, is, 128 | n = array.length; 129 | 130 | for (i = 0; i < n; i++) { 131 | is = fn(array[i], i, n); 132 | 133 | if (is) 134 | break; 135 | } 136 | } 137 | 138 | })(); 139 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "es require", 3 | "version": "1.0.4", 4 | "description": "require for Adobe ExtendScript", 5 | "private": true, 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/coderaiser/es-require.git" 12 | }, 13 | "author": "coderaiser (http://coderaiser.github.io/)", 14 | "license": "MIT", 15 | "bugs": { 16 | "url": "https://github.com/coderaiser/es-require/issues" 17 | }, 18 | "homepage": "https://github.com/coderaiser/es-require" 19 | } 20 | --------------------------------------------------------------------------------