package/package.json000644 000765 000314 0000003351 12777427045013040 0ustar00000000 000000 { "name": "lazy-debug-legacy", "version": "0.0.1", "description": "Generates module names for vision media's debug", "main": "src/index.js", "scripts": { "test": "mocha", "print-test": "node scripts/print-test.js" }, "repository": { "type": "git", "url": "git+https://github.com/apihlaja/lazy-debug.git" }, "keywords": [ "debug" ], "author": { "name": "Nicholas McCready" }, "originalAuthor": { "name": "Antti Pihlaja" }, "license": "MIT", "peerDependencies": { "debug": "*" }, "dependencies": {}, "devDependencies": { "chai": "^3.5.0", "mocha": "^2.5.3" }, "browserify": { "transform": [ [ "babelify", { "presets": [ "es2015" ] } ] ] }, "gitHead": "af1051caa0f3a201ec6cf28fe8919871233bd4b0", "bugs": { "url": "https://github.com/apihlaja/lazy-debug/issues" }, "homepage": "https://github.com/apihlaja/lazy-debug#readme", "_id": "lazy-debug@0.0.3", "_shasum": "82cc2a6f4dcf36facf0c7a7946857bff62828ac7", "_from": "lazy-debug@>=0.0.3 <0.0.4", "_npmVersion": "2.14.2", "_nodeVersion": "4.0.0", "_npmUser": { "name": "apihlaja", "email": "antti.pihlaja@live.fi" }, "dist": { "shasum": "82cc2a6f4dcf36facf0c7a7946857bff62828ac7", "tarball": "https://registry.npmjs.org/lazy-debug/-/lazy-debug-0.0.3.tgz" }, "maintainers": [ { "name": "apihlaja", "email": "antti.pihlaja@live.fi" } ], "_npmOperationalInternal": { "host": "packages-16-east.internal.npmjs.com", "tmp": "tmp/lazy-debug-0.0.3.tgz_1466088930341_0.7004884697962552" }, "directories": {}, "_resolved": "https://registry.npmjs.org/lazy-debug/-/lazy-debug-0.0.3.tgz" } package/.npmignore000644 000765 000314 0000000033 12777427120012535 0ustar00000000 000000 test/ scripts/ *.json *.md package/README.md000644 000765 000314 0000002123 12777431371012022 0ustar00000000 000000 # lazy-debug [![npm lazy-debug](https://nodei.co/npm/lazy-debug.png?compact=true)](https://www.npmjs.com/package/lazy-debug) Node.js module which generates app & module names for [visionmedia´s debug](https://github.com/visionmedia/debug) using `__filename` and package.json. Basic usage example: ```javascript var debug = require('lazy-debug-legacy').get(__filename); ``` Depending on `__filename`, debug name will be something like packageName:dir:file. File extension is removed and if file name is `index`, its removed too. For futher customization, to fit better for project structure, you can provide filter function: ```javascript var lazyDebug = require('lazy-debug-legacy'); lazyDebug.configure({filter: function (pathArray) { if ( pathArray[0] === 'src' ) { pathArray.shift(); } return pathArray; }}); // now, when called in packageRoot/src/module1/index.js var debug = require('lazy-debug-legacy').get(__filename); // debug name will be projectName:module1 ``` ## Install `npm install --save debug lazy-debug` ## Tests `npm test` ## License [The MIT License](LICENSE.md) package/LICENSE.md000644 000765 000314 0000002077 12777427140012156 0ustar00000000 000000 The MIT License (MIT) Copyright (c) 2016 Nicholas McCready Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/src/functions.js000644 000765 000314 0000006547 12777430774013724 0ustar00000000 000000 var path = require('path'); var functions = module.exports = { parseFilePath: function (file, platform, filter) { var delimiter = '/'; if (typeof platform == 'function') { filter = platform; platform = false; } if (!platform) platform = process.platform; if (!platform) platform = 'browser'; if (platform === 'win32') { delimiter = '\\'; } if (platform === 'browser') { if ( file.indexOf('\\') !== -1 ) { delimiter = '\\'; } if (file.indexOf('/') === 0) { file = file.substr(1,file.length); } } // should be posix.. var candidates = file.split(delimiter); var modules = []; for ( var i = 0; i < candidates.length; ++i ) { if (candidates[i] && candidates[i].length ) { modules.push(candidates[i]); } } var last = modules.length - 1; if ( last > 0 ) { var fileName = removeFileExt(modules[last]); if ( fileName === 'index' ) { modules.pop(); } else { modules[last] = fileName; } } if ( modules.length > 0 ) { if ( modules[0] === '..' ) { modules.shift(); } } if ( filter && typeof filter === 'function' ) { return filter(modules); } return modules; }, locatePackageJson: function(filePath, platform) { if (!platform) { platform = process.platform }; var pathParts = functions.parseFilePath(filePath, platform); var filedir = path.dirname(filePath); var testdir = filedir; var counter = 1; var result; while (pathParts.length > counter ) { try { var testfile = path.join(testdir, 'package.json'); return require.resolve(testfile); } catch( err ) { // ignore } var testdir = path.resolve(testdir, '..'); counter++; } return false; }, getModuleDebugId: function(filePath, options) { options = options || {}; if (typeof options.platform == 'function') { options.filter = platform; options.platform = false; } if (!options.platform) { options.platform = process.platform }; var packagePath = functions.locatePackageJson(filePath, options.platform); var relpath = (packagePath) ? path.relative(packagePath, filePath) : functions.findModuleRoot(filePath); var submodules = functions.parseFilePath(relpath, options.filter); if (options.prependPackageName){ var packageName = (packagePath) ? require(packagePath).name : functions.getPseudoName(filePath); return packageName + ':' + submodules.join(':'); } return submodules.join(':'); }, getPseudoName: function(filePath) { var search = 'node_modules'; var idx = filePath.lastIndexOf(search); if ( idx === -1 ) return 'app'; var moduleRoot = functions.findModuleRoot(filePath); if ( filePath.lastIndexOf('node_modules/') !== -1 ) return moduleRoot.substr(0, moduleRoot.indexOf('/')); else return moduleRoot.substr(0, moduleRoot.indexOf('\\')); }, findModuleRoot: function(filePath) { var search = 'node_modules'; var idx = filePath.lastIndexOf(search); if ( idx === -1 ) return filePath.substr(1); return filePath.substr(idx+1+search.length); } } function removeFileExt(fileName) { var index = fileName.lastIndexOf('.'); if ( index !== -1 ) return fileName.substr(0, index); else return fileName; } package/src/index.js000644 000765 000314 0000001432 12777431077013004 0ustar00000000 000000 var debug = require('debug'); var getModuleDebugId = require('./functions').getModuleDebugId; var filter; var cache = {}; var api = module.exports = { configure: function(opts) { if ( !opts ) opts = {}; if ( opts.filter && typeof opts.filter === 'function' ) { filter = opts.filter; cache = {}; } }, get: function( filename, submoduleName ) { return debug(api.getModuleDebugName(filename, submoduleName)); }, getModuleDebugName: function ( filename, submoduleName ) { var name = cache[filename]; if ( !name ) { name = getModuleDebugId(filename, {platform: process.platform, filter:filter}); cache[filename] = name; } if ( submoduleName ) { return name + ':' + submoduleName; } else { return name; } } };