UNPKG

atma-io

Version:
1,619 lines (1,574 loc) 109 kB
// source ./RootModule.js (function(){ var _src_Directory = {}; var _src_Env = {}; var _src_ExportsGlob = {}; var _src_ExportsSetts = {}; var _src_File = {}; var _src_FileFactory = {}; var _src_FileHookRegistration = {}; var _src_FileHooks = {}; var _src_Watcher = {}; var _src_global = {}; var _src_middleware_json = {}; var _src_transport_custom = {}; var _src_transport_dir_transport = {}; var _src_transport_file_transport = {}; var _src_transport_filesystem_fs_dir = {}; var _src_transport_filesystem_fs_file = {}; var _src_transport_filesystem_transport = {}; var _src_util_Await = {}; var _src_util_arr = {}; var _src_util_cli = {}; var _src_util_filesystem_util = {}; var _src_util_glob = {}; var _src_util_logger = {}; var _src_util_obj = {}; var _src_util_path = {}; var _src_util_rgx = {}; var _src_util_stack = {}; var _src_util_uri = {}; // source ./ModuleSimplified.js var _src_global; (function () { var exports = {}; var module = { exports: exports }; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var g = global; exports.global = g; var logger = g.logger || require('atma-logger'); exports.logger = logger; var io = {}; exports.io = io; ; function isObject(x) { return x != null && typeof x === 'object' && x.constructor === Object; } if (isObject(_src_global) && isObject(module.exports)) { Object.assign(_src_global, module.exports); return; } _src_global = module.exports; }()); // end:source ./ModuleSimplified.js // source ./ModuleSimplified.js var _src_Env; (function () { var exports = {}; var module = { exports: exports }; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var atma_utils_1 = require("atma-utils"); var global_1 = _src_global; var os = require("os"); var mainFile = new atma_utils_1.class_Uri(normalizePath(process.mainModule.filename)); var platform = process.platform; var cwd = toDir(process.cwd()); exports.Env = { settings: {}, cwd: cwd, applicationDir: new atma_utils_1.class_Uri(mainFile.toDir()), currentDir: new atma_utils_1.class_Uri(cwd), tmpDir: new atma_utils_1.class_Uri("file:///" + os.tmpdir + "/"), newLine: os.EOL, getTmpPath: function (filename) { return exports.Env .tmpDir .combine(Date.now() + "-" + ((Math.random() * 10000) | 0) + "-" + filename) .toString(); }, get appdataDir() { var path; switch (platform) { case 'win32': case 'win64': path = process.env.APPDATA || process.env.HOME; break; case 'darwin': path = process.env.HOME; break; default: path = process.env.HOME; break; } if (path == null) { global_1.logger.error('<io.env> Unknown AppData Dir'); Object.defineProperty(this, 'appdataDir', { value: this.applicationDir }); return this.applicationDir; } path = new atma_utils_1.class_Uri(toDir(path)); if (platform === 'darwin') path = path.combine('Library/Application Support/'); path = path.combine('.' + mainFile.file + '/'); Object.defineProperty(this, 'appdataDir', { value: path }); return path; } }; function toDir(path) { return atma_utils_1.class_Uri.combine(normalizePath(path), '/'); } function normalizePath(path) { return path.replace(/\\/g, '/'); } ; function isObject(x) { return x != null && typeof x === 'object' && x.constructor === Object; } if (isObject(_src_Env) && isObject(module.exports)) { Object.assign(_src_Env, module.exports); return; } _src_Env = module.exports; }()); // end:source ./ModuleSimplified.js // source ./ModuleSimplified.js var _src_Watcher; (function () { var exports = {}; var module = { exports: exports }; "use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var __fs = require("fs"); var global_1 = _src_global; var atma_utils_1 = require("atma-utils"); var event_CHANGE = 'change'; var WATCHERS = {}; exports.Watcher = { watch: function (path, callback) { if (WATCHERS[path]) { WATCHERS[path].on(event_CHANGE, callback); return; } if (__fs.existsSync(path) === false) { global_1.logger.error('<watcher> File not exists', path); return; } WATCHERS[path] = new FileWatcher(path); WATCHERS[path].on(event_CHANGE, callback); }, unwatch: function (path, callback) { var watcher = WATCHERS[path]; if (watcher == null) { global_1.logger.warn('<watcher> No exists', path); return; } if (callback != null) { watcher.off(event_CHANGE, callback); if (watcher._listeners.length !== 0) { return; } } watcher.close(); delete WATCHERS[path]; } }; var FileWatcher = /** @class */ (function (_super) { __extends(FileWatcher, _super); function FileWatcher(path) { var _this = _super.call(this) || this; _this.changed = _this.changed.bind(_this); _this.reportChange = _this.reportChange.bind(_this); _this.path = path; _this.fswatcher = __fs.watch(path, _this.changed); return _this; } FileWatcher.prototype.changed = function () { if (this.timeout) { clearTimeout(this.timeout); } this.timeout = setTimeout(this.reportChange, 100); }; FileWatcher.prototype.reportChange = function () { this.trigger(event_CHANGE, this.path); }; FileWatcher.prototype.close = function () { this.fswatcher.close(); this.off(event_CHANGE); }; return FileWatcher; }(atma_utils_1.class_EventEmitter)); ; ; function isObject(x) { return x != null && typeof x === 'object' && x.constructor === Object; } if (isObject(_src_Watcher) && isObject(module.exports)) { Object.assign(_src_Watcher, module.exports); return; } _src_Watcher = module.exports; }()); // end:source ./ModuleSimplified.js // source ./ModuleSimplified.js var _src_util_glob; (function () { var exports = {}; var module = { exports: exports }; "use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var global_1 = _src_global; function glob_getCalculatedPath(path, glob) { var star = glob.indexOf('*'), slash = glob.lastIndexOf('/', star), strict = slash === -1 ? null : glob.substring(0, slash); if (!slash) return path; var index = path.toLowerCase().indexOf(strict.toLowerCase()); if (index === -1) { global_1.logger.warn('[substring not found]', path, strict); return path; } return path.substring(index + strict.length); } exports.glob_getCalculatedPath = glob_getCalculatedPath; ; function glob_matchPath(pattern /*String*/, path /*String*/) { if (path[0] === '/') path = path.substring(1); if (pattern[0] === '/') pattern = pattern.substring(1); return glob_toRegExp(pattern).test(path); } exports.glob_matchPath = glob_matchPath; ; function glob_parsePatterns(mix, out) { if (mix == null) return null; if (out == null) out = []; if (Array.isArray(mix)) { mix.forEach(function (x) { glob_parsePatterns(x, out); }); return out; } if (mix instanceof RegExp) { out.push(mix); return out; } if (typeof mix === 'string') { var pattern = mix; if (pattern[0] === '/') { pattern = pattern.substring(1); } var _a = glob_parseDirs(pattern), depth = _a[0], rootCount = _a[1], root = _a[2]; var regexp = glob_toRegExp(pattern); regexp.depth = depth; regexp.rootCount = rootCount; regexp.root = root; out.push(regexp); return out; } global_1.logger.error('<glob> Unsupported pattern', mix); return out; } exports.glob_parsePatterns = glob_parsePatterns; ; function glob_parseDirs(pattern) { if (pattern[0] === '/') pattern = pattern.substring(1); var depth = 0, dirs = pattern.split('/'); depth = pattern.indexOf('**') !== -1 ? Infinity : dirs.length; // remove file dirs.pop(); for (var i = 0; i < dirs.length; i++) { if (dirs[i].indexOf('*') === -1) continue; dirs.splice(i); } return [depth, dirs.length, dirs.join('/').toLowerCase()]; } exports.glob_parseDirs = glob_parseDirs; ; function glob_toRegExp(glob) { var specialChars = "\\^$*+?.()|{}[]", stream = '', i = -1, length = glob.length; glob = glob.replace(/(\*\*\/){2,}/g, '**/'); while (++i < length) { var c = glob[i]; switch (c) { case '?': stream += '.'; break; case '*': if (glob[i + 1] === '*') { if (i === 0 && /[\\\/]/.test(glob[i + 2])) { stream += '.+'; i += 2; } stream += '.+'; i++; break; } stream += '[^/]+'; break; case '{': var close = glob.indexOf('}', i); if (~close) { stream += '(' + glob.substring(i + 1, close).replace(/,/g, '|') + ')'; i = close; break; } stream += c; break; case '[': var close = glob.indexOf(']', i); if (~close) { stream = glob.substring(i, close); i = close; break; } stream += c; break; default: if (~specialChars.indexOf(c)) { stream += '\\'; } stream += c; break; } } stream = '^' + stream + '$'; return new GlobRegExp(stream, 'i'); } exports.glob_toRegExp = glob_toRegExp; ; /** * [as dir] '/dev/*.js' -> '/dev/' */ function glob_getStrictPath(path) { var index = path.indexOf('*'); if (index === -1) { global_1.logger.error('glob.js [path is not a glob pattern]', path); return null; } return path.substring(0, path.lastIndexOf('/', index) + 1); } exports.glob_getStrictPath = glob_getStrictPath; ; /** * 'c:/dev/*.js' -> '*.js' */ function glob_getRelativePath(path) { var index = path.indexOf('*'); if (index === -1) { global_1.logger.error('glob.js [path is not a glob pattern]', path); return null; } return path.substring(path.lastIndexOf('/', index) + 1); } exports.glob_getRelativePath = glob_getRelativePath; ; var GlobRegExp = /** @class */ (function (_super) { __extends(GlobRegExp, _super); function GlobRegExp() { return _super !== null && _super.apply(this, arguments) || this; } return GlobRegExp; }(RegExp)); exports.GlobRegExp = GlobRegExp; ; function isObject(x) { return x != null && typeof x === 'object' && x.constructor === Object; } if (isObject(_src_util_glob) && isObject(module.exports)) { Object.assign(_src_util_glob, module.exports); return; } _src_util_glob = module.exports; }()); // end:source ./ModuleSimplified.js // source ./ModuleSimplified.js var _src_util_path; (function () { var exports = {}; var module = { exports: exports }; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var global_1 = _src_global; var atma_utils_1 = require("atma-utils"); function path_getProtocol(path) { var i = path.indexOf(':'); if (i === -1 || path[i + 1] !== '/' || path[i + 2] !== '/') { return null; } return path.substring(0, i); } exports.path_getProtocol = path_getProtocol; function path_getUri(path, base) { if (typeof path !== 'string') { path = path.toString(); } path = path_normalize(path); if (path[0] === '/') { path = path.substring(1); } var uri = new atma_utils_1.class_Uri(path); if (uri.isRelative() === false) { return uri; } if (base) { return new atma_utils_1.class_Uri(base).combine(uri); } if (global_1.io.env) { return global_1.io.env.currentDir.combine(uri); } return new atma_utils_1.class_Uri('file://' + process.cwd() + '/') .combine(uri); } exports.path_getUri = path_getUri; function path_combine(_1, _2) { if (!_1) return _2; if (!_2) return _1; if (_2[0] === '/') _2 = _2.substring(1); if (_1[_1.length - 1] === '/') return _1 + _2; return _1 + '/' + _2; } exports.path_combine = path_combine; function path_getDir(url) { if (!url) return '/'; var index = url.lastIndexOf('/'); return index === -1 ? '' : url.substring(index + 1, -index); } exports.path_getDir = path_getDir; function path_isSubDir(basepath, path) { var basedir = path_getDir(basepath), dir = path_getDir(path); return dir .toLowerCase() .indexOf(basedir.toLowerCase()) === 0; } exports.path_isSubDir = path_isSubDir; function path_resolveUri(url, parentLocation, base) { if (url[0] === '/') { parentLocation = base; url = url.substring(1); } var uri = new atma_utils_1.class_Uri(url); return uri.isRelative() ? (new atma_utils_1.class_Uri(parentLocation)).combine(uri) : uri; } exports.path_resolveUri = path_resolveUri; function path_resolveAppUri(url, parentPath) { if (url[0] === '/') return url; if (url.substring(0, 2) === './') url = url.substring(2); if (!parentPath || url.substring(0, 4) === 'file') return '/'; var index = parentPath.lastIndexOf('/'); return (index === -1 ? '/' : (parentPath.substring(index + 1, -index))) + url; } exports.path_resolveAppUri = path_resolveAppUri; function path_ensureTrailingSlash(path) { if (path[path.length - 1] === '/') return path; return path + '/'; } exports.path_ensureTrailingSlash = path_ensureTrailingSlash; ; function path_normalize(str) { str = str .replace(/\\/g, '/') .replace(/^\.\//, ''); var double = /\/{2,}/g; var protocolMatched = false; do { var match = double.exec(str); if (match == null) { break; } if (match.index === 0) { continue; } if (str[match.index - 1] === ':') { if (protocolMatched === false) { protocolMatched = true; continue; } // otherwise remove extra slashes e.g. file://c://foo/bar.jpg } str = str.substring(0, match.index) + '/' + str.substring(match.index + match[0].length); } while (true); return str; } ; function isObject(x) { return x != null && typeof x === 'object' && x.constructor === Object; } if (isObject(_src_util_path) && isObject(module.exports)) { Object.assign(_src_util_path, module.exports); return; } _src_util_path = module.exports; }()); // end:source ./ModuleSimplified.js // source ./ModuleSimplified.js var _src_transport_custom; (function () { var exports = {}; var module = { exports: exports }; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Repository = {}; var CustomTransport = /** @class */ (function () { function CustomTransport() { } CustomTransport.register = function (protocol, transport) { exports.Repository[protocol] = transport; }; CustomTransport.get = function (protocol) { return exports.Repository[protocol]; }; CustomTransport.all = function () { return exports.Repository; }; CustomTransport.set = function (repository) { for (var key in repository) { exports.Repository[key] = repository[key]; } }; return CustomTransport; }()); exports.CustomTransport = CustomTransport; ; function isObject(x) { return x != null && typeof x === 'object' && x.constructor === Object; } if (isObject(_src_transport_custom) && isObject(module.exports)) { Object.assign(_src_transport_custom, module.exports); return; } _src_transport_custom = module.exports; }()); // end:source ./ModuleSimplified.js // source ./ModuleSimplified.js var _src_util_obj; (function () { var exports = {}; var module = { exports: exports }; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function obj_extend(target, source) { if (target == null) target = {}; if (source == null) return target; for (var key in source) { target[key] = source[key]; } return target; } exports.obj_extend = obj_extend; ; function isObject(x) { return x != null && typeof x === 'object' && x.constructor === Object; } if (isObject(_src_util_obj) && isObject(module.exports)) { Object.assign(_src_util_obj, module.exports); return; } _src_util_obj = module.exports; }()); // end:source ./ModuleSimplified.js // source ./ModuleSimplified.js var _src_util_filesystem_util; (function () { var exports = {}; var module = { exports: exports }; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var __fs = require("fs"); function fs_isDirectory(path) { try { return __fs .statSync(path) .isDirectory(); } catch (e) { return false; } } exports.fs_isDirectory = fs_isDirectory; function fs_getStat(path) { try { return __fs.statSync(path); } catch (e) { return null; } } exports.fs_getStat = fs_getStat; ; function isObject(x) { return x != null && typeof x === 'object' && x.constructor === Object; } if (isObject(_src_util_filesystem_util) && isObject(module.exports)) { Object.assign(_src_util_filesystem_util, module.exports); return; } _src_util_filesystem_util = module.exports; }()); // end:source ./ModuleSimplified.js // source ./ModuleSimplified.js var _src_transport_filesystem_fs_dir; (function () { var exports = {}; var module = { exports: exports }; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var global_1 = _src_global; var __fs = require("fs"); var obj_1 = _src_util_obj; var filesystem_util_1 = _src_util_filesystem_util; var path_1 = _src_util_path; exports.DirectoryFsTransport = { ensure: function (path) { return dir_ensure(path); }, ensureAsync: function (path, cb) { return dir_ensureAsync(path, cb); }, ceateSymlink: function (source, target) { dir_symlink(source, target); }, exists: function (path) { return dir_exists(path); }, existsAsync: function (path, cb) { dir_existsAsync(path, cb); }, readFiles: function (path, patterns, excludes, data) { return dir_files(path, patterns, excludes, data); }, readFilesAsync: function (path, patternsOrCb, excludesOrCb, dataOrCb, Cb) { dir_filesAsync(path, patternsOrCb, excludesOrCb, dataOrCb, Cb); }, remove: function (path) { return dir_remove(path); }, removeAsync: function (path, cb) { dir_removeAsync(path, cb); }, rename: function (oldPath, newPath) { __fs.renameSync(oldPath, newPath); }, renameAsync: function (oldPath, newPath, cb) { __fs.rename(oldPath, newPath, cb); } }; function dir_ensure(path) { if (path[path.length - 1] === '/') path = path.substring(0, path.length - 1); if (__fs.existsSync(path) === false) { var sub = path.substring(0, path.lastIndexOf('/')), error; if (isRoot(sub) === false) error = dir_ensure(sub); if (error) return error.toString(); try { __fs.mkdirSync(path); } catch (e) { return e.toString(); } } if (!filesystem_util_1.fs_isDirectory(path)) return 'Target exists, but is not a directory:' + path; } function dir_ensureAsync(path, cb) { path = path.replace(/\/*$/, ''); dir_existsAsync(path, function (error, exists) { if (exists) return cb(); var sub = path.substring(0, path.lastIndexOf('/')); if (isRoot(sub) === false) { dir_ensureAsync(sub, mkdir); } else { mkdir(); } }); function mkdir() { __fs.mkdir(path, function (error) { if (error) { if (error.code === 'EEXIST') error = null; else if (error.errno === 47 || error.errno === -4707) error = null; } cb(error); }); } } function dir_exists(path) { return filesystem_util_1.fs_isDirectory(path); } function dir_existsAsync(path, cb) { __fs.stat(path, function (error, stat) { cb(error, stat && stat.isDirectory()); }); } function dir_files(path, patterns, excludes, data) { return dir_walk(path, '', obj_1.obj_extend(data, { depth: 0, maxdepth: rgxs_getDepth(patterns), patterns: patterns, excludes: excludes })); } function dir_filesAsync(path) { var args = []; /* [?patterns, ?excludes, ?data], cb */ for (var _i = 1 /* [?patterns, ?excludes, ?data], cb */; _i < arguments.length /* [?patterns, ?excludes, ?data], cb */; _i++ /* [?patterns, ?excludes, ?data], cb */) { args[_i - 1] = arguments[_i]; /* [?patterns, ?excludes, ?data], cb */ } var cb = args.pop(); while (cb == null && args.length > 0) { cb = args.pop(); } var patterns = args.shift(), excludes = args.shift(), data = args.shift(); dir_walkAsync(path, '', 0, obj_1.obj_extend(data, { maxdepth: rgxs_getDepth(patterns), patterns: patterns, excludes: excludes }), [], cb); } function dir_symlink(source, target) { try { __fs.symlinkSync(source, target, 'junction'); } catch (error) { global_1.logger.error('symlink: bold<%s>', error); } } function dir_remove(path) { if (dir_exists(path) === false) return true; try { dir_removeRecursive(path); return true; } catch (err) { return false; } } function dir_removeAsync(path, cb) { dir_removeRecursiveAsync(path, cb); } //> private function dir_removeRecursive(path) { var subentries = __fs.readdirSync(path), imax = subentries.length, i = -1, filename, entry, stats; while (++i < imax) { filename = subentries[i]; if ('.' === filename || '..' === filename) continue; entry = path + '/' + filename; stats = __fs.lstatSync(entry); if (stats.isDirectory()) { dir_removeRecursive(entry); continue; } __fs.unlinkSync(entry); } __fs.rmdirSync(path); } function dir_removeRecursiveAsync(path, cb) { __fs.readdir(path, function (error, files) { if (error) { cb(error); return; } var imax = files.length, i = -1; if (imax === 0) { onSubCompleted(); return; } var next = cb_listeners(imax, onSubCompleted), fsname; while (++i < imax) { fsname = files[i]; if ('.' === fsname || '..' === fsname) { next(); continue; } processSubEntry(path_1.path_combine(path, fsname), next); } }); function processSubEntry(path, cb) { __fs.lstat(path, function (error, stat) { if (error) { cb(error); return; } if (stat.isDirectory()) { dir_removeRecursiveAsync(path, cb); return; } __fs.unlink(path, cb); }); } function onSubCompleted() { __fs.rmdir(path, cb); } } function dir_walk(dir, root, data) { var results = [], files; try { files = __fs.readdirSync(dir); } catch (error) { console.error('<dir walk>', error); return results; } if (root == null) root = ''; if (data == null) { data = { depth: 0, maxdepth: Infinity, directories: false, symlinks: false }; } var currentDepth = data.depth, patterns = data.patterns, excludes = data.excludes; data.depth++; for (var i = 0, x, imax = files.length; i < imax; i++) { x = files[i]; var stats = lstat_(path_1.path_combine(dir, x)), path = path_1.path_combine(root, x), match = true; if (stats == null) continue; if (stats.isDirectory()) { if (stats.isSymbolicLink()) continue; if (data.directories) { results.push(path_1.path_combine(dir, x) + '/'); } if (data.depth >= data.maxdepth) continue; var dirroot = path_1.path_combine(root, x); if (patterns) { var dirCanBeMatched = false; for (var j = 0, jmax = patterns.length; j < jmax; j++) { var patternRootCount = patterns[j].rootCount - currentDepth, patternRoot = patterns[j].root; if (!patternRootCount || currentDepth > patternRootCount) { dirCanBeMatched = true; break; } if (patternRoot.indexOf(dirroot) === 0) { dirCanBeMatched = true; break; } global_1.logger(90).warn('<glob> not matched %s | %s', dirroot, patternRoot); } if (dirCanBeMatched === false) continue; } global_1.logger(90).warn('<glob> match sub-', dirroot); results = results.concat(dir_walk(path_1.path_combine(dir, x), dirroot, data)); continue; } if (patterns) { match = false; for (var j = 0, jmax = patterns.length; j < jmax; j++) { if (patterns[j].test(path)) { match = true; break; } } } if (match && excludes) { for (var j = 0, jmax = excludes.length; j < jmax; j++) { if (excludes[j].test(path)) { match = false; break; } } } if (match) results.push(path); } data.depth = currentDepth; return results; } /* * - dir: String directory to get the filelist from * - root: String current subdirectory * - depth: Number current subdirectory depth * - data: Object { maxdepth, patterns, excludes, directories } * - result: Array current filelist */ function dir_walkAsync(dir, root, depth, data, results, cb) { var currentDepth = depth, maxdepth = data.maxdepth, patterns = data.patterns, excludes = data.excludes; depth++; __fs.readdir(dir, function (error, files) { if (error) return cb(error, results); if (files.length === 0) return cb(null, results); var i = -1, imax = files.length; var listener = listeners(imax); while (++i < imax) { process(files[i], listener); } }); function listeners(count) { var err; return function (error) { err = err || error; if (--count === 0) cb(err, results); }; } function process(fsname, cb) { var path = path_1.path_combine(dir, fsname); __fs.lstat(path, function (error, stat) { if (error) return cb(); if (stat.isDirectory()) return processDirectory(fsname, stat, cb); processFile(fsname, results); cb(); }); } function processDirectory(name, stat, cb) { if (stat.isSymbolicLink()) return cb(); if (data.directories) results.push(path_1.path_combine(root, name) + '/'); if (depth >= maxdepth) return cb(); var dirroot = path_1.path_combine(root, name); if (patterns) { var i = -1, imax = patterns.length; while (++i < imax) { var patternRootCount = patterns[i].rootCount - currentDepth, patternRoot = patterns[i].root; if (!patternRootCount || currentDepth > patternRootCount) break; if (patternRoot.indexOf(dirroot) === 0) break; } if (i >= imax) { // directory can not be matched return cb(); } } dir_walkAsync(path_1.path_combine(dir, name), dirroot, depth, data, results, cb); } function processFile(fsname, results) { var path = path_1.path_combine(root, fsname); if (patterns && matchPath(path, patterns) === false) return; if (excludes && matchPath(path, excludes) === true) return; results.push(path); } } //< walkAsync function isRoot(path) { if (path === '' || path === '/') return true; return /^[A-Z]:\/?$/i.test(path); } function matchPath(path, rgxs) { var i = -1, imax = rgxs.length; while (++i < imax) { if (rgxs[i].test(path)) return true; } return false; } function rgxs_getDepth(rgxs) { if (rgxs == null) return Infinity; var maxdepth = 0, imax = rgxs.length, i = -1; while (++i < imax) { if (maxdepth < rgxs[i].depth) maxdepth = rgxs[i].depth; } return maxdepth || Infinity; } function cb_listeners(count, cb) { var err; return function (error) { err = err || error; if (--count < 1) cb(err); }; } function lstat_(path) { try { return __fs.lstatSync(path); } catch (e) { return null; } } ; function isObject(x) { return x != null && typeof x === 'object' && x.constructor === Object; } if (isObject(_src_transport_filesystem_fs_dir) && isObject(module.exports)) { Object.assign(_src_transport_filesystem_fs_dir, module.exports); return; } _src_transport_filesystem_fs_dir = module.exports; }()); // end:source ./ModuleSimplified.js // source ./ModuleSimplified.js var _src_util_logger; (function () { var exports = {}; var module = { exports: exports }; "use strict"; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); var global_1 = _src_global; function log_error() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } log.apply(void 0, __spreadArrays([NAME.red], args)); } exports.log_error = log_error; ; function log_info() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } log.apply(void 0, __spreadArrays([NAME.cyan], args)); } exports.log_info = log_info; ; //= private var NAME = '[atma-io]'; function log(title) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } args.unshift(title); global_1.logger.log.apply(global_1.logger, args); } ; function isObject(x) { return x != null && typeof x === 'object' && x.constructor === Object; } if (isObject(_src_util_logger) && isObject(module.exports)) { Object.assign(_src_util_logger, module.exports); return; } _src_util_logger = module.exports; }()); // end:source ./ModuleSimplified.js // source ./ModuleSimplified.js var _src_transport_filesystem_fs_file; (function () { var exports = {}; var module = { exports: exports }; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var __fs = require("fs"); var fs_dir_1 = _src_transport_filesystem_fs_dir; var logger_1 = _src_util_logger; var global_1 = _src_global; var path_1 = _src_util_path; exports.FileFsTransport = { save: function (path, content, options) { var error = fs_dir_1.DirectoryFsTransport.ensure(path_1.path_getDir(path)); if (error) { logger_1.log_error('file_save', path); return; } try { __fs.writeFileSync(path, content, options); } catch (error) { logger_1.log_error('file_save', error.toString()); } }, saveAsync: function (path, content, options, cb) { fs_dir_1.DirectoryFsTransport.ensureAsync(path_1.path_getDir(path), function (error) { if (error) return cb(error); __fs.writeFile(path, content, options || writeOpts, cb); }); }, copy: function (from, to) { if (__fs.existsSync(from) === false) { logger_1.log_error('file_copy 404', from); return; } var error = fs_dir_1.DirectoryFsTransport.ensure(path_1.path_getDir(to)); if (error) { logger_1.log_error('file_copy Target error', to); return; } try { copySync(from, to); } catch (error) { logger_1.log_error('file_copy', error.toString()); } }, copyAsync: function (from, to, cb) { exports.FileFsTransport.existsAsync(from, prepairFn); function prepairFn(error, exists) { if (exists !== true) return cb({ code: 404, message: from + " not exists." }); fs_dir_1.DirectoryFsTransport.ensureAsync(path_1.path_getDir(to), copyFn); } function copyFn(error) { if (error) { cb(error); return; } var readstream = __fs .createReadStream(from) .on('error', function (err) { global_1.logger.log('readstream error', from, err); cb && cb(err); cb = null; }); var writestream = __fs .createWriteStream(to) .on('error', function (err) { global_1.logger.log('writestream error', to, err); cb && cb(err); cb = null; }) .on('close', function () { cb && cb(null); cb = null; }); readstream.pipe(writestream); } }, exists: function (path) { return __fs.existsSync(path) && __fs.statSync(path).isFile(); }, existsAsync: function (path, cb) { __fs.stat(path, function (error, stat) { var _a, _b; if (Errno.isNotFound(error)) { cb(null, false); return; } var exists = (_b = (_a = stat) === null || _a === void 0 ? void 0 : _a.isFile(), (_b !== null && _b !== void 0 ? _b : false)); cb(error, exists); }); }, read: function (path, encoding) { try { return __fs.readFileSync(path, encoding); } catch (error) { logger_1.log_error('file_read', error.toString()); } return ''; }, readAsync: function (path, encoding, cb) { __fs.readFile(path, { encoding: encoding }, cb); }, remove: function (path) { if (exports.FileFsTransport.exists(path) === false) { return true; } try { __fs.unlinkSync(path); } catch (error) { logger_1.log_error('file_remove', error.toString()); return false; } return true; }, removeAsync: function (path, cb) { __fs.unlink(path, function (error) { if (Errno.isNotFound(error)) { error = null; } cb(error); }); }, rename: function (path, filename) { if (exports.FileFsTransport.exists(path) === false) { logger_1.log_error('file_rename 404', path); return false; } try { __fs.renameSync(path, getDir(path) + filename); } catch (error) { logger_1.log_error('file_rename', error.toString()); return false; } return true; }, renameAsync: function (path, filename, cb) { __fs.rename(path, getDir(path) + filename, function (error) { cb(error, error == null); }); } }; //= private var writeOpts = { encoding: 'utf8' }; function getDir(path) { return path.substring(0, path.lastIndexOf('/') + 1); } function copySync(from, to) { var BUF_LENGTH = 64 * 1024, buff = new Buffer(BUF_LENGTH), bytesRead = 1, fdr = __fs.openSync(from, 'r'), fdw = __fs.openSync(to, 'w'), pos = 0; while (bytesRead > 0) { bytesRead = __fs.readSync(fdr, buff, 0, BUF_LENGTH, pos); __fs.writeSync(fdw, buff, 0, bytesRead); pos += bytesRead; } __fs.closeSync(fdr); return __fs.closeSync(fdw); } var Errno; (function (Errno) { function isNotFound(error) { if (error == null) { return false; } return error.errno === 34 || error.errno === -4058 || error.code === 'ENOENT'; } Errno.isNotFound = isNotFound; function isExists(error) { if (error == null) { return false; } return error.errno === -4075 || error.code === 'EEXIST'; } Errno.isExists = isExists; })(Errno = exports.Errno || (exports.Errno = {})); ; function isObject(x) { return x != null && typeof x === 'object' && x.constructor === Object; } if (isObject(_src_transport_filesystem_fs_file) && isObject(module.exports)) { Object.assign(_src_transport_filesystem_fs_file, module.exports); return; } _src_transport_filesystem_fs_file = module.exports; }()); // end:source ./ModuleSimplified.js // source ./ModuleSimplified.js var _src_transport_filesystem_transport; (function () { var exports = {}; var module = { exports: exports }; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var fs_file_1 = _src_transport_filesystem_fs_file; var fs_dir_1 = _src_transport_filesystem_fs_dir; exports.FsTransport = { File: fs_file_1.FileFsTransport, Directory: fs_dir_1.DirectoryFsTransport }; ; function isObject(x) { return x != null && typeof x === 'object' && x.constructor === Object; } if (isObject(_src_transport_filesystem_transport) && isObject(module.exports)) { Object.assign(_src_transport_filesystem_transport, module.exports); return; } _src_transport_filesystem_transport = module.exports; }()); // end:source ./ModuleSimplified.js // source ./ModuleSimplified.js var _src_transport_dir_transport; (function () { var exports = {}; var module = { exports: exports }; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var path_1 = _src_util_path; var custom_1 = _src_transport_custom; var transport_1 = _src_transport_filesystem_transport; function dir_ensure(path) { var transport = getDirectoryTransportForPath(path); return transport.ensure(path); } exports.dir_ensure = dir_ensure; function dir_ensureAsync(path, cb) { var transport = getDirectoryTransportForPath(path); transport.ensureAsync(path, cb); } exports.dir_ensureAsync = dir_ensureAsync; function dir_exists(path) { var transport = getDirectoryTransportForPath(path); return transport.exists(path); } exports.dir_exists = dir_exists; function dir_existsAsync(path, cb) { var transport = getDirectoryTransportForPath(path); transport.existsAsync(path, cb); } exports.dir_existsAsync = dir_existsAsync; function dir_files(path, patterns, excludes, data) { var transport = getDirectoryTransportForPath(path); return transport.readFiles(path, patterns, excludes, data); } exports.dir_files = dir_files; function dir_filesAsync(path, patternsOrCb, excludesOrCb, dataOrCb, Cb) { var transport = getDirectoryTransportForPath(path); return transport.readFilesAsync(path, patternsOrCb, excludesOrCb, dataOrCb, Cb); } exports.dir_filesAsync = dir_filesAsync; function dir_symlink(source, target) { var transport = getDirectoryTransportForPath(source); transport.ceateSymlink(source, target); } exports.dir_symlink = dir_symlink; function dir_remove(path) { var transport = getDirectoryTransportForPath(path); return transport.remove(path); } exports.dir_remove = dir_remove; function dir_removeAsync(path, cb) { var transport = getDirectoryTransportForPath(path); return transport.removeAsync(path, cb); } exports.dir_removeAsync = dir_removeAsync; function dir_rename(oldPath, newPath) { var transport = getDirectoryTransportForPath(oldPath); return transport.rename(oldPath, newPath); } exports.dir_rename = dir_rename; function dir_renameAsync(oldPath, newPath, cb) { var transport = getDirectoryTransportForPath(oldPath); return transport.renameAsync(oldPath, newPath, cb); } exports.dir_renameAsync = dir_renameAsync; //> private function getDirectoryTransportForPath(path) { var protocol = path_1.path_getProtocol(path); if (protocol == null || protocol === 'file') { return transport_1.FsTransport.Directory; } var transport = custom_1.CustomTransport.get(protocol); if (transport == null) { throw new Error("Transport '" + protocol + "' is not supported or not installed"); } return transport.Directory; } ; function isObject(x) { return x != null && typeof x === 'object' && x.constructor === Object; } if (isObject(_src_transport_dir_transport) && isObject(module.exports)) { Object.assign(_src_transport_dir_transport, module.exports); return; } _src_transport_dir_transport = module.exports; }()); // end:source ./ModuleSimplified.js // source ./ModuleSimplified.js var _src_transport_file_transport; (function () { var exports = {}; var module = { exports: exports }; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var custom_1 = _src_transport_custom; var transport_1 = _src_transport_filesystem_transport; var path_1 = _src_util_path; function file_save(path, content, options) { var transport = getFileTransportForPath(path); transport.save(path, content, options); } exports.file_save = file_save; ; function file_saveAsync(path, content, options, cb) { var transport = getFileTransportForPath(path); transport.saveAsync(path, content, options, cb); } exports.file_saveAsync = file_saveAsync; ; function file_copy(from, to) { var fromTransport = getFileTransportForPath(from); var toTransport = getFileTransportForPath(to); if (fromTransport === toTransport) { fromTransport.copy(from, to); return; } var data = fromTransport.read(from); toTransport.save(to, data); } exports.file_copy = file_copy; ; function file_copyAsync(from, to, cb) { var fromTransport = getFileTransportForPath(from); var toTransport = getFileTransportForPath(to); if (fromTransport === toTransport) { fromTransport.copyAsync(from, to, cb); return; } fromTransport.readAsync(from, null, function (err, data) { if (err) { cb(err); return; } toTransport.saveAsync(to, data, null, cb); }); } exports.file_copyAsync = file_copyAsync; ; function file_exists(path) { var transport = getFileTransportForPath(path); return transport.exists(path); } exports.file_exists = file_exists; ; function file_existsAsync(path, cb) { var transport = getFileTransportForPath(path); return transport.existsAsync(path, cb); } exports.file_existsAsync = file_existsAsync; ; function file_read(path, encoding) { var transport = getFileTransportForPath(path); return transport.read(path, encoding); } exports.file_read = file_read; ; function file_readAsync(path, encoding, cb) { var transport = getFileTransportForPath(path); transport.readAsync(path, encoding, cb); } exports.file_readAsync = file_readAsync; ; function file_remove(path) { var transport = getFileTransportForPath(path); return transport.remove(path); } exports.file_remove = file_remove; ; function file_removeAsync(path, cb) { var transport = getFileTransportForPath(path); transport.removeAsync(path, cb); } exports.file_removeAsync = file_removeAsync; ; function file_rename(path, filename) { var transport = getFileTransportForPath(path); return transport.rename(path, filename); } exports.file_rename = file_rename; ; function file_renameAsync(path, filename, cb) { var transport = getFileTransportForPath(path); transport.renameAsync(path, filename, cb); } exports.file_renameAsync = file_renameAsync; ; function getFileTransportForPath(path) { var protocol = path_1.path_getProtocol(path); if (protocol == null || protocol === 'file') { return transport_1.FsTransport.File; } var transport = custom_1.CustomTransport.get(protocol); if (transport == null) { throw new Error("Transport '" + protocol + "' is not supported or not installed for path '" + path + "'"); } return transport.File; } ; function isObject(x) { return x != null && typeof x === 'object' && x.constructor === Object; } if (isObject(_src_transport_file_transport) && isObject(module.exports)) { Object.assign(_src_transport_file_transport, module.exports); return; } _src_transport_file_transport = module.exports; }()); // end:source ./ModuleSimplified.js // source ./ModuleSimplified.js var _src_FileFactory; (function () { var exports = {}; var module = { exports: exports }; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var atma_utils_1 = require("atma-utils"); var FileFactory = /** @class */ (function () { function FileFactory() { this.handlers = []; } FileFactory.prototype.registerHandler = function (regexp, handler) { normalizeHandler(handler); this.handlers.push({ handler: handler, regexp: regexp }); }; FileFactory.prototype.unregisterHandler = function (regexp, handler) { var str = regexp.toString(), imax = this.handlers.length, i = -1, x; while (++i < imax) { x = this.handlers[i]; if (x.regexp.toString() !== str) continue; if (handler === void 0) { this.handlers.splice(i, 1); i--; imax--; continue; } if (handler === x) { this.handlers.splice(i, 1); return; } } }; FileFactory.prototype.resolveHandler