Import upstream version 0.3.0+git20180318.b22bc1a, md5 f746a7c29a68a3af77ab44a37a8dd582
Debian Janitor
4 years ago
0 | node_modules/⏎ |
0 | language: node_js | |
1 | node_js: | |
2 | - "node" | |
3 | - "6" | |
4 | - "4" | |
5 | - "0.12" | |
6 | - "0.10" | |
7 | ||
8 | # master + PRs | |
9 | branches: | |
10 | only: | |
11 | - master |
0 | 0 | The MIT License (MIT) |
1 | 1 | |
2 | Copyright (c) 2016 Nicholas McCready | |
2 | Copyright (c) 2016 Antti Pihlaja | |
3 | 3 | |
4 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | 5 | of this software and associated documentation files (the "Software"), to deal |
0 | 0 | # lazy-debug |
1 | 1 | |
2 | [![npm lazy-debug](https://nodei.co/npm/lazy-debug.png?compact=true)](https://www.npmjs.com/package/lazy-debug) | |
2 | [![Travis Build Status](https://travis-ci.org/apihlaja/lazy-debug.svg?branch=master)](https://travis-ci.org/apihlaja/lazy-debug) [![Appveyor Build status](https://ci.appveyor.com/api/projects/status/yihkfbm7e89h1ngf/branch/master?svg=true)](https://ci.appveyor.com/project/apihlaja/lazy-debug/branch/master) | |
3 | 3 | |
4 | Node.js module which generates app & module names for [visionmedia´s debug](https://github.com/visionmedia/debug) using `__filename` and package.json. | |
4 | **__UNMAINTAINED__**: this was developed for webpack v1, not tested since then (v4 is out now). | |
5 | 5 | |
6 | Basic usage example: | |
6 | **lazy-debug** is a helper for [visionmedia´s debug](https://github.com/visionmedia/debug). It generates module identifiers based on `__filename`, so your debug identifiers reflect the file structure automatically. | |
7 | 7 | |
8 | ```javascript | |
9 | var debug = require('lazy-debug-legacy').get(__filename); | |
10 | ``` | |
8 | [![console log](https://raw.githubusercontent.com/apihlaja/lazy-debug/master/examples/express/output.png?raw=true)](https://github.com/apihlaja/lazy-debug/tree/master/examples/express) | |
11 | 9 | |
12 | 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: | |
10 | Supports | |
13 | 11 | |
14 | ```javascript | |
15 | var lazyDebug = require('lazy-debug-legacy'); | |
16 | lazyDebug.configure({filter: function (pathArray) { | |
17 | if ( pathArray[0] === 'src' ) { | |
18 | pathArray.shift(); | |
19 | } | |
20 | return pathArray; | |
21 | }}); | |
22 | ||
23 | // now, when called in packageRoot/src/module1/index.js | |
24 | var debug = require('lazy-debug-legacy').get(__filename); | |
25 | // debug name will be projectName:module1 | |
26 | ``` | |
27 | ||
12 | * Node.js v0.10 and later | |
13 | * Windows and Linux | |
14 | * Browserify | |
15 | * Webpack | |
28 | 16 | |
29 | 17 | ## Install |
30 | 18 | |
31 | 19 | `npm install --save debug lazy-debug` |
32 | 20 | |
21 | The module works out of the box in Node.js and Browserify environments. | |
22 | ||
23 | ||
24 | ### Webpack | |
25 | ||
26 | Webpack needs [configuration](https://webpack.github.io/docs/configuration.html#node), include these in your `webpack.config.js`: | |
27 | ||
28 | ```javascript | |
29 | config.node = { | |
30 | __filename: true, // populates filenames relative to your app root | |
31 | fs: "empty" // include empty "mock" for fs module | |
32 | } | |
33 | ``` | |
34 | ||
35 | ## Usage | |
36 | ||
37 | Just require it and provide filename: | |
38 | ||
39 | ```javascript | |
40 | var debug = require('lazy-debug')(__filename) | |
41 | debug('ready for logging..') | |
42 | ``` | |
43 | Debugger identifier will be relative to module root, ie. something like `dir:file`. File extensions are omitted and file name is omitted if it´s `index`. | |
44 | ||
45 | ||
46 | ### Suffix | |
47 | ||
48 | Add suffix by providing `submoduleName`: | |
49 | ||
50 | ```javascript | |
51 | var debug = require('lazy-debug')(__filename, {submoduleName: 'sub'}) | |
52 | var debug = require('lazy-debug')(__filename, 'sub') | |
53 | ``` | |
54 | Debugger identifier will be `dir:file:sub`. | |
55 | ||
56 | ||
57 | ### Prefix | |
58 | ||
59 | Add prefix by providing `packageName`: | |
60 | ||
61 | ```javascript | |
62 | var debug = require('lazy-debug')(__filename, {packageName: 'my-lib'}) | |
63 | var debug = require('lazy-debug')(__filename, {packageName: true}) | |
64 | ``` | |
65 | ||
66 | Debugger identifier will be `my-lib:dir:file`. If package name is boolean `true`, name from `package.json` is used. That works only in Node.js environment. | |
67 | ||
68 | ||
33 | 69 | ## Tests |
34 | 70 | |
35 | 71 | `npm test` |
36 | 72 | |
73 | Tests node.js compability by running vanilla [Mocha](https://mochajs.org/) at first, then Browserify and Webpack builds are tested in [PhantomJS](http://phantomjs.org/) using [Karma](http://karma-runner.github.io/). | |
74 | ||
75 | ||
37 | 76 | ## License |
38 | 77 | |
39 | 78 | [The MIT License](LICENSE.md) |
0 | version: "{build}" | |
1 | ||
2 | environment: | |
3 | matrix: | |
4 | - nodejs_version: "Stable" # latest | |
5 | - nodejs_version: 6 | |
6 | - nodejs_version: 4 | |
7 | - nodejs_version: 0.12 | |
8 | - nodejs_version: 0.10 | |
9 | ||
10 | platform: | |
11 | - x64 | |
12 | ||
13 | # master + PRs | |
14 | branches: | |
15 | only: | |
16 | - master | |
17 | ||
18 | install: | |
19 | - ps: Install-Product node $env:nodejs_version | |
20 | # tools versions | |
21 | - node --version | |
22 | - npm --version | |
23 | # install modules | |
24 | - npm install | |
25 | ||
26 | test_script: | |
27 | - npm test | |
28 | ||
29 | build: off⏎ |
0 | # lazy-debug-example | |
1 | ||
2 | Slightly modified version of express-generator template to show off what this module does. | |
3 | ||
4 | ![console log](https://raw.githubusercontent.com/apihlaja/lazy-debug/master/examples/express/output.png?raw=true) |
Binary diff not shown
0 | { | |
1 | "name": "lazy-debug-example", | |
2 | "version": "0.0.0", | |
3 | "private": true, | |
4 | "scripts": { | |
5 | "start": "node ./server/index.js" | |
6 | }, | |
7 | "dependencies": { | |
8 | "body-parser": "~1.15.2", | |
9 | "cookie-parser": "~1.4.3", | |
10 | "debug": "2.2.0", | |
11 | "express": "~4.14.0", | |
12 | "hbs": "~4.0.1", | |
13 | "lazy-debug": "github:apihlaja/lazy-debug#dev", | |
14 | "serve-favicon": "~2.3.0" | |
15 | } | |
16 | } |
0 | var debug = require('lazy-debug')(__filename) | |
1 | ||
2 | debug('loading depedencies..') | |
3 | var express = require('express'); | |
4 | var path = require('path'); | |
5 | var favicon = require('serve-favicon'); | |
6 | var cookieParser = require('cookie-parser'); | |
7 | var bodyParser = require('body-parser'); | |
8 | ||
9 | debug('loading routes..') | |
10 | var index = require('./routes/index'); | |
11 | var users = require('./routes/users'); | |
12 | ||
13 | debug('configuring express app..') | |
14 | var app = express(); | |
15 | ||
16 | // view engine setup | |
17 | app.set('views', path.join(__dirname, 'views')); | |
18 | app.set('view engine', 'hbs'); | |
19 | ||
20 | // uncomment after placing your favicon in /public | |
21 | //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); | |
22 | app.use(bodyParser.json()); | |
23 | app.use(bodyParser.urlencoded({ extended: false })); | |
24 | app.use(cookieParser()); | |
25 | app.use(express.static(path.join(__dirname, 'public'))); | |
26 | ||
27 | app.use('/', index); | |
28 | app.use('/users', users); | |
29 | ||
30 | // catch 404 and forward to error handler | |
31 | app.use(function(req, res, next) { | |
32 | debug('no match for ', req.url) | |
33 | var err = new Error('Not Found'); | |
34 | err.status = 404; | |
35 | next(err); | |
36 | }); | |
37 | ||
38 | // error handler | |
39 | app.use(function(err, req, res, next) { | |
40 | debug('error handler') | |
41 | ||
42 | // set locals, only providing error in development | |
43 | res.locals.message = err.message; | |
44 | res.locals.error = req.app.get('env') === 'development' ? err : {}; | |
45 | ||
46 | // render the error page | |
47 | res.status(err.status || 500); | |
48 | res.render('error'); | |
49 | }); | |
50 | ||
51 | module.exports = app; |
0 | /** | |
1 | * Module dependencies. | |
2 | */ | |
3 | ||
4 | var app = require('./app'); | |
5 | var debug = require('lazy-debug')(__filename); | |
6 | var http = require('http'); | |
7 | ||
8 | /** | |
9 | * Get port from environment and store in Express. | |
10 | */ | |
11 | ||
12 | var port = normalizePort(process.env.PORT || '3000'); | |
13 | app.set('port', port); | |
14 | ||
15 | /** | |
16 | * Create HTTP server. | |
17 | */ | |
18 | ||
19 | var server = http.createServer(app); | |
20 | ||
21 | /** | |
22 | * Listen on provided port, on all network interfaces. | |
23 | */ | |
24 | ||
25 | server.listen(port); | |
26 | server.on('error', onError); | |
27 | server.on('listening', onListening); | |
28 | ||
29 | /** | |
30 | * Normalize a port into a number, string, or false. | |
31 | */ | |
32 | ||
33 | function normalizePort(val) { | |
34 | var port = parseInt(val, 10); | |
35 | ||
36 | if (isNaN(port)) { | |
37 | // named pipe | |
38 | return val; | |
39 | } | |
40 | ||
41 | if (port >= 0) { | |
42 | // port number | |
43 | return port; | |
44 | } | |
45 | ||
46 | return false; | |
47 | } | |
48 | ||
49 | /** | |
50 | * Event listener for HTTP server "error" event. | |
51 | */ | |
52 | ||
53 | function onError(error) { | |
54 | if (error.syscall !== 'listen') { | |
55 | throw error; | |
56 | } | |
57 | ||
58 | var bind = typeof port === 'string' | |
59 | ? 'Pipe ' + port | |
60 | : 'Port ' + port; | |
61 | ||
62 | // handle specific listen errors with friendly messages | |
63 | switch (error.code) { | |
64 | case 'EACCES': | |
65 | console.error(bind + ' requires elevated privileges'); | |
66 | process.exit(1); | |
67 | break; | |
68 | case 'EADDRINUSE': | |
69 | console.error(bind + ' is already in use'); | |
70 | process.exit(1); | |
71 | break; | |
72 | default: | |
73 | throw error; | |
74 | } | |
75 | } | |
76 | ||
77 | /** | |
78 | * Event listener for HTTP server "listening" event. | |
79 | */ | |
80 | ||
81 | function onListening() { | |
82 | var addr = server.address(); | |
83 | var bind = typeof addr === 'string' | |
84 | ? 'pipe ' + addr | |
85 | : 'port ' + addr.port; | |
86 | debug('Listening on ' + bind); | |
87 | } |
0 | body { | |
1 | padding: 50px; | |
2 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; | |
3 | } | |
4 | ||
5 | a { | |
6 | color: #00B7FF; | |
7 | } |
0 | var debug = require('lazy-debug')(__filename) | |
1 | var express = require('express'); | |
2 | var router = express.Router(); | |
3 | ||
4 | /* GET home page. */ | |
5 | router.get('/', function(req, res, next) { | |
6 | debug('rendering index') | |
7 | res.render('index', { title: 'Express' }); | |
8 | }); | |
9 | ||
10 | module.exports = router; |
0 | var debug = require('lazy-debug')(__filename) | |
1 | var express = require('express'); | |
2 | var router = express.Router(); | |
3 | ||
4 | /* GET users listing. */ | |
5 | router.get('/', function(req, res, next) { | |
6 | debug('sending users') | |
7 | res.send('respond with a resource'); | |
8 | }); | |
9 | ||
10 | module.exports = router; |
0 | <!DOCTYPE html> | |
1 | <html> | |
2 | <head> | |
3 | <title>{{title}}</title> | |
4 | <link rel='stylesheet' href='/stylesheets/style.css' /> | |
5 | </head> | |
6 | <body> | |
7 | {{{body}}} | |
8 | </body> | |
9 | </html> |
0 | 0 | { |
1 | "name": "lazy-debug-legacy", | |
2 | "version": "0.0.1", | |
3 | "description": "Generates module names for vision media's debug", | |
1 | "name": "lazy-debug", | |
2 | "version": "0.3.0", | |
3 | "description": "lazy-debug is a helper for visionmedia´s debug. It generates module identifiers based on filename, so your debug identifiers reflect the file structure automatically.", | |
4 | 4 | "main": "src/index.js", |
5 | 5 | "scripts": { |
6 | "test": "mocha", | |
7 | "print-test": "node scripts/print-test.js" | |
6 | "test": "npm run test:nodejs && npm run test:browserify && npm run test:webpack", | |
7 | "test:nodejs": "mocha", | |
8 | "test:browserify": "karma start ./test/karma.browserify.js", | |
9 | "test:webpack": "karma start ./test/karma.webpack.js" | |
8 | 10 | }, |
9 | 11 | "repository": { |
10 | 12 | "type": "git", |
11 | "url": "git+https://github.com/apihlaja/lazy-debug.git" | |
13 | "url": "https://github.com/apihlaja/lazy-debug.git" | |
12 | 14 | }, |
13 | 15 | "keywords": [ |
14 | "debug" | |
16 | "debug", | |
17 | "webpack", | |
18 | "browserify" | |
15 | 19 | ], |
16 | "author": { | |
17 | "name": "Nicholas McCready" | |
18 | }, | |
19 | "originalAuthor": { | |
20 | "name": "Antti Pihlaja" | |
21 | }, | |
20 | "author": "Antti Pihlaja", | |
22 | 21 | "license": "MIT", |
23 | 22 | "peerDependencies": { |
24 | 23 | "debug": "*" |
25 | 24 | }, |
26 | "dependencies": {}, | |
25 | "dependencies": { | |
26 | "detect-node": "2.0.3" | |
27 | }, | |
27 | 28 | "devDependencies": { |
29 | "browserify": "^13.3.0", | |
28 | 30 | "chai": "^3.5.0", |
29 | "mocha": "^2.5.3" | |
30 | }, | |
31 | "browserify": { | |
32 | "transform": [ | |
33 | [ | |
34 | "babelify", | |
35 | { | |
36 | "presets": [ | |
37 | "es2015" | |
38 | ] | |
39 | } | |
40 | ] | |
41 | ] | |
42 | }, | |
43 | "gitHead": "af1051caa0f3a201ec6cf28fe8919871233bd4b0", | |
44 | "bugs": { | |
45 | "url": "https://github.com/apihlaja/lazy-debug/issues" | |
46 | }, | |
47 | "homepage": "https://github.com/apihlaja/lazy-debug#readme", | |
48 | "_id": "lazy-debug@0.0.3", | |
49 | "_shasum": "82cc2a6f4dcf36facf0c7a7946857bff62828ac7", | |
50 | "_from": "lazy-debug@>=0.0.3 <0.0.4", | |
51 | "_npmVersion": "2.14.2", | |
52 | "_nodeVersion": "4.0.0", | |
53 | "_npmUser": { | |
54 | "name": "apihlaja", | |
55 | "email": "antti.pihlaja@live.fi" | |
56 | }, | |
57 | "dist": { | |
58 | "shasum": "82cc2a6f4dcf36facf0c7a7946857bff62828ac7", | |
59 | "tarball": "https://registry.npmjs.org/lazy-debug/-/lazy-debug-0.0.3.tgz" | |
60 | }, | |
61 | "maintainers": [ | |
62 | { | |
63 | "name": "apihlaja", | |
64 | "email": "antti.pihlaja@live.fi" | |
65 | } | |
66 | ], | |
67 | "_npmOperationalInternal": { | |
68 | "host": "packages-16-east.internal.npmjs.com", | |
69 | "tmp": "tmp/lazy-debug-0.0.3.tgz_1466088930341_0.7004884697962552" | |
70 | }, | |
71 | "directories": {}, | |
72 | "_resolved": "https://registry.npmjs.org/lazy-debug/-/lazy-debug-0.0.3.tgz" | |
31 | "karma": "^1.4.1", | |
32 | "karma-browserify": "^5.1.0", | |
33 | "karma-mocha": "^1.3.0", | |
34 | "karma-phantomjs-launcher": "^1.0.2", | |
35 | "karma-spec-reporter": "^0.0.30", | |
36 | "karma-webpack": "^2.0.2", | |
37 | "mocha": "^3.1.2", | |
38 | "webpack": "^1.14.0" | |
39 | } | |
73 | 40 | } |
0 | var path = require('path'); | |
1 | ||
2 | var functions = module.exports = { | |
3 | parseFilePath: function (file, platform, filter) { | |
4 | var delimiter = '/'; | |
5 | if (typeof platform == 'function') { | |
6 | filter = platform; | |
7 | platform = false; | |
8 | } | |
9 | ||
10 | if (!platform) platform = process.platform; | |
11 | if (!platform) platform = 'browser'; | |
12 | ||
13 | if (platform === 'win32') { | |
14 | delimiter = '\\'; | |
15 | } | |
16 | if (platform === 'browser') { | |
17 | if ( file.indexOf('\\') !== -1 ) { | |
18 | delimiter = '\\'; | |
19 | } | |
20 | if (file.indexOf('/') === 0) { | |
21 | file = file.substr(1,file.length); | |
22 | } | |
23 | } | |
24 | // should be posix.. | |
25 | var candidates = file.split(delimiter); | |
26 | var modules = []; | |
27 | for ( var i = 0; i < candidates.length; ++i ) { | |
28 | if (candidates[i] && candidates[i].length ) { | |
29 | modules.push(candidates[i]); | |
30 | } | |
31 | } | |
32 | var last = modules.length - 1; | |
33 | if ( last > 0 ) { | |
34 | var fileName = removeFileExt(modules[last]); | |
35 | if ( fileName === 'index' ) { | |
36 | modules.pop(); | |
37 | } else { | |
38 | modules[last] = fileName; | |
39 | } | |
40 | } | |
41 | if ( modules.length > 0 ) { | |
42 | if ( modules[0] === '..' ) { | |
43 | modules.shift(); | |
44 | } | |
45 | } | |
46 | if ( filter && typeof filter === 'function' ) { | |
47 | return filter(modules); | |
48 | } | |
49 | return modules; | |
50 | }, | |
51 | locatePackageJson: function(filePath, platform) { | |
52 | if (!platform) { platform = process.platform }; | |
53 | var pathParts = functions.parseFilePath(filePath, platform); | |
54 | var filedir = path.dirname(filePath); | |
55 | var testdir = filedir; | |
56 | var counter = 1; | |
57 | var result; | |
58 | while (pathParts.length > counter ) { | |
59 | try { | |
60 | var testfile = path.join(testdir, 'package.json'); | |
61 | return require.resolve(testfile); | |
62 | } catch( err ) { | |
63 | // ignore | |
64 | } | |
65 | var testdir = path.resolve(testdir, '..'); | |
66 | counter++; | |
67 | } | |
68 | return false; | |
69 | }, | |
70 | getModuleDebugId: function(filePath, options) { | |
71 | options = options || {}; | |
72 | ||
73 | if (typeof options.platform == 'function') { | |
74 | options.filter = platform; | |
75 | options.platform = false; | |
76 | } | |
77 | ||
78 | if (!options.platform) { options.platform = process.platform }; | |
79 | var packagePath = functions.locatePackageJson(filePath, options.platform); | |
80 | var relpath = (packagePath) ? | |
81 | path.relative(packagePath, filePath) : functions.findModuleRoot(filePath); | |
82 | var submodules = functions.parseFilePath(relpath, options.filter); | |
83 | ||
84 | if (options.prependPackageName){ | |
85 | var packageName = (packagePath) ? | |
86 | require(packagePath).name : functions.getPseudoName(filePath); | |
87 | return packageName + ':' + submodules.join(':'); | |
88 | } | |
89 | return submodules.join(':'); | |
90 | }, | |
91 | getPseudoName: function(filePath) { | |
92 | var search = 'node_modules'; | |
93 | var idx = filePath.lastIndexOf(search); | |
94 | if ( idx === -1 ) return 'app'; | |
95 | var moduleRoot = functions.findModuleRoot(filePath); | |
96 | if ( filePath.lastIndexOf('node_modules/') !== -1 ) | |
97 | return moduleRoot.substr(0, moduleRoot.indexOf('/')); | |
98 | else | |
99 | return moduleRoot.substr(0, moduleRoot.indexOf('\\')); | |
100 | }, | |
101 | findModuleRoot: function(filePath) { | |
102 | var search = 'node_modules'; | |
103 | var idx = filePath.lastIndexOf(search); | |
104 | if ( idx === -1 ) return filePath.substr(1); | |
105 | return filePath.substr(idx+1+search.length); | |
106 | } | |
107 | } | |
108 | ||
109 | function removeFileExt(fileName) { | |
110 | var index = fileName.lastIndexOf('.'); | |
111 | if ( index !== -1 ) | |
112 | return fileName.substr(0, index); | |
113 | else | |
114 | return fileName; | |
115 | } |
0 | 0 | var debug = require('debug'); |
1 | var getModuleDebugId = require('./functions').getModuleDebugId; | |
2 | var filter; | |
3 | 1 | |
4 | var cache = {}; | |
2 | var idCache = {}; | |
3 | var pkgNameCache = {}; | |
5 | 4 | |
6 | var api = module.exports = { | |
7 | configure: function(opts) { | |
8 | if ( !opts ) opts = {}; | |
9 | if ( opts.filter && typeof opts.filter === 'function' ) { | |
10 | filter = opts.filter; | |
11 | cache = {}; | |
5 | function get( filename, options ) { | |
6 | return debug(getModuleDebugName(filename, options)); | |
7 | } | |
8 | ||
9 | function getModuleDebugName( filename, options ) { | |
10 | ||
11 | var name = idCache[filename]; | |
12 | ||
13 | options = options || {}; | |
14 | ||
15 | if ( typeof options === 'string' ) { | |
16 | options = {submoduleName: options} | |
17 | } | |
18 | ||
19 | if ( !name ) { | |
20 | name = getModuleDebugId(filename, {platform: process.platform}); | |
21 | idCache[filename] = name; | |
22 | } | |
23 | ||
24 | if (options.submoduleName){ | |
25 | name += ':' + options.submoduleName; | |
26 | } | |
27 | ||
28 | if (options.packageName) { | |
29 | var pkgName; | |
30 | ||
31 | if (typeof options.packageName === 'string') { | |
32 | pkgName = options.packageName; | |
12 | 33 | } |
13 | }, | |
14 | get: function( filename, submoduleName ) { | |
15 | return debug(api.getModuleDebugName(filename, submoduleName)); | |
16 | }, | |
17 | getModuleDebugName: function ( filename, submoduleName ) { | |
18 | var name = cache[filename]; | |
19 | if ( !name ) { | |
20 | name = getModuleDebugId(filename, {platform: process.platform, filter:filter}); | |
21 | cache[filename] = name; | |
34 | else { | |
35 | pkgName = pkgNameCache[filename]; | |
36 | if (!pkgName) { | |
37 | pkgName = getPackageName(filename); | |
38 | pkgNameCache[filename] = pkgName; | |
39 | } | |
22 | 40 | } |
23 | if ( submoduleName ) { | |
24 | return name + ':' + submoduleName; | |
25 | } else { | |
26 | return name; | |
41 | name = pkgName + ':' + name; | |
42 | } | |
43 | ||
44 | return name; | |
45 | } | |
46 | ||
47 | // define public API | |
48 | ||
49 | get.get = get | |
50 | ||
51 | get.getModuleDebugName = getModuleDebugName | |
52 | ||
53 | module.exports = get | |
54 | ||
55 | // internals.. | |
56 | ||
57 | var path = require('path'); | |
58 | ||
59 | var fs; | |
60 | try { | |
61 | fs = require('fs'); | |
62 | } catch (e) {} | |
63 | ||
64 | ||
65 | var isNodeJs = require('detect-node'); | |
66 | var isWin32 = isNodeJs && process.platform === 'win32' | |
67 | var isWebpack = !isNodeJs && (typeof process === 'undefined' || process.platform !== 'browser') | |
68 | ||
69 | function detectSeparator(file) { | |
70 | if (isNodeJs) return path.sep; | |
71 | ||
72 | // try to guess in browser | |
73 | if (file.indexOf('\\') !== -1) { | |
74 | return '\\' | |
75 | } else { | |
76 | return '/' | |
77 | } | |
78 | } | |
79 | ||
80 | function parseFilePath(file) { | |
81 | ||
82 | var separator = detectSeparator(file) | |
83 | ||
84 | if (separator === '\\' && file.indexOf('/') === 0) { | |
85 | file = file.substr(1,file.length); | |
86 | } | |
87 | ||
88 | var candidates = file.split(separator); | |
89 | var modules = []; | |
90 | for ( var i = 0; i < candidates.length; ++i ) { | |
91 | if (candidates[i] && candidates[i].length ) { | |
92 | modules.push(candidates[i]); | |
27 | 93 | } |
28 | 94 | } |
29 | }; | |
95 | var last = modules.length - 1; | |
96 | if ( last > 0 ) { | |
97 | var fileName = removeFileExt(modules[last]); | |
98 | if ( fileName === 'index' ) { | |
99 | modules.pop(); | |
100 | } else { | |
101 | modules[last] = fileName; | |
102 | } | |
103 | } | |
104 | if ( modules.length > 0 ) { | |
105 | if ( modules[0] === '..' ) { | |
106 | modules.shift(); | |
107 | } | |
108 | } | |
109 | return modules; | |
110 | } | |
111 | ||
112 | function locatePackageJson(filePath) { | |
113 | var pathParts = parseFilePath(filePath); | |
114 | var filedir = path.dirname(filePath); | |
115 | var testdir = filedir; | |
116 | var counter = 1; | |
117 | var result; | |
118 | ||
119 | // can't find in browserify/webpack build | |
120 | if (!isNodeJs) return false; | |
121 | ||
122 | while (pathParts.length > counter ) { | |
123 | try { | |
124 | var testfile = path.join(testdir, 'package.json'); | |
125 | if (fs.existsSync(testfile)) { | |
126 | return testfile | |
127 | } | |
128 | } catch( err ) { | |
129 | // ignore | |
130 | } | |
131 | var testdir = path.resolve(testdir, '..'); | |
132 | counter++; | |
133 | } | |
134 | return false; | |
135 | } | |
136 | ||
137 | function getModuleDebugId(filePath, options) { | |
138 | options = options || {}; | |
139 | ||
140 | var packagePath = locatePackageJson(filePath); | |
141 | var relpath = (packagePath) ? | |
142 | path.relative(packagePath, filePath) : findModuleRoot(filePath); | |
143 | var submodules = parseFilePath(relpath); | |
144 | ||
145 | return submodules.join(':'); | |
146 | } | |
147 | ||
148 | function getPackageName(filePath) { | |
149 | var packagePath = locatePackageJson(filePath); | |
150 | ||
151 | if (packagePath) { | |
152 | return JSON.parse(fs.readFileSync(packagePath, {encoding: 'utf-8'})).name | |
153 | } else { | |
154 | return getPseudoName(filePath) | |
155 | } | |
156 | } | |
157 | ||
158 | function getPseudoName(filePath) { | |
159 | var search = 'node_modules'; | |
160 | var idx = filePath.lastIndexOf(search); | |
161 | ||
162 | if ( idx === -1 ) return 'app'; | |
163 | ||
164 | var moduleRoot = findModuleRoot(filePath); | |
165 | ||
166 | if ( filePath.lastIndexOf('node_modules/') !== -1 ) | |
167 | return moduleRoot.substr(0, moduleRoot.indexOf('/')); | |
168 | else | |
169 | return moduleRoot.substr(0, moduleRoot.indexOf('\\')); | |
170 | } | |
171 | ||
172 | function findModuleRoot(filePath) { | |
173 | var search = 'node_modules'; | |
174 | var idx = filePath.lastIndexOf(search); | |
175 | if ( idx === -1 ) { | |
176 | return (isWebpack) ? filePath : filePath.substr(1); | |
177 | } | |
178 | return filePath.substr(idx+1+search.length); | |
179 | } | |
180 | ||
181 | function removeFileExt(fileName) { | |
182 | var index = fileName.lastIndexOf('.'); | |
183 | if ( index !== -1 ) | |
184 | return fileName.substr(0, index); | |
185 | else | |
186 | return fileName; | |
187 | } |
0 | var expect = require('chai').expect; | |
1 | ||
2 | var isNodeJs = require('detect-node'); | |
3 | ||
4 | describe('lazy-debug', function () { | |
5 | var lazyDebug = require('../src'); | |
6 | ||
7 | it('is shortcut for #get', function () { | |
8 | expect(lazyDebug).to.equal(lazyDebug.get); | |
9 | }); | |
10 | ||
11 | describe('#get(filename, submoduleName|options)', function () { | |
12 | it('returns named debug instance', function () { | |
13 | lazyDebug.get(__filename)('this should be function'); | |
14 | }); | |
15 | }); | |
16 | ||
17 | describe('#getModuleDebugName(filename, submoduleName|options)', function () { | |
18 | ||
19 | it('gives debug name for file', function () { | |
20 | var name = lazyDebug.getModuleDebugName(__filename); | |
21 | expect(name).to.equal('test:01-lazy-debug-test'); | |
22 | }); | |
23 | ||
24 | it('adds submodule name if given', function () { | |
25 | ||
26 | expect( | |
27 | lazyDebug.getModuleDebugName(__filename, 'test2-1') | |
28 | ).to.equal('test:01-lazy-debug-test:test2-1'); | |
29 | ||
30 | expect( | |
31 | lazyDebug.getModuleDebugName(__filename, {submoduleName: 'test2-2'})) | |
32 | .to.equal('test:01-lazy-debug-test:test2-2'); | |
33 | }); | |
34 | ||
35 | it('adds real package name in node.js', function () { | |
36 | if (!isNodeJs) this.skip() | |
37 | ||
38 | expect( | |
39 | lazyDebug.getModuleDebugName(__filename, {packageName: true}) | |
40 | ).to.equal('lazy-debug:test:01-lazy-debug-test'); | |
41 | }); | |
42 | ||
43 | ||
44 | it('uses "app" as package name in browser', function () { | |
45 | if (isNodeJs) this.skip() | |
46 | ||
47 | expect( | |
48 | lazyDebug.getModuleDebugName(__filename, {packageName: true}) | |
49 | ).to.equal('app:test:01-lazy-debug-test'); | |
50 | }) | |
51 | ||
52 | it('sets package name if given', function () { | |
53 | expect( | |
54 | lazyDebug.getModuleDebugName(__filename, {packageName: 'fake'}) | |
55 | ).to.equal('fake:test:01-lazy-debug-test'); | |
56 | }) | |
57 | }); | |
58 | }); |
0 | module.exports = function(config) { | |
1 | config.set({ | |
2 | // base path that will be used to resolve all patterns (eg. files, exclude) | |
3 | basePath: '../', | |
4 | ||
5 | // frameworks to use | |
6 | // available frameworks: https://npmjs.org/browse/keyword/karma-adapter | |
7 | frameworks: ['browserify', 'mocha'], | |
8 | ||
9 | // list of files / patterns to load in the browser | |
10 | files: [ | |
11 | 'test/**/*test.js' | |
12 | ], | |
13 | ||
14 | // list of files to exclude | |
15 | exclude: [ | |
16 | ], | |
17 | ||
18 | // preprocess matching files before serving them to the browser | |
19 | // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor | |
20 | preprocessors: { | |
21 | 'test/**/*.js': [ 'browserify' ] | |
22 | }, | |
23 | ||
24 | // test results reporter to use | |
25 | // possible values: 'dots', 'progress' | |
26 | // available reporters: https://npmjs.org/browse/keyword/karma-reporter | |
27 | reporters: ['spec'], | |
28 | ||
29 | // web server port | |
30 | port: 9876, | |
31 | ||
32 | // enable / disable colors in the output (reporters and logs) | |
33 | colors: true, | |
34 | ||
35 | // level of logging | |
36 | // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG | |
37 | logLevel: config.LOG_INFO, | |
38 | ||
39 | // enable / disable watching file and executing tests whenever any file changes | |
40 | autoWatch: false, | |
41 | ||
42 | // start these browsers | |
43 | // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher | |
44 | browsers: ['PhantomJS'], | |
45 | ||
46 | // Continuous Integration mode | |
47 | // if true, Karma captures browsers, runs the tests and exits | |
48 | singleRun: true, | |
49 | ||
50 | // Concurrency level | |
51 | // how many browser should be started simultaneous | |
52 | concurrency: Infinity | |
53 | }) | |
54 | } |
0 | module.exports = function(config) { | |
1 | config.set({ | |
2 | ||
3 | // base path that will be used to resolve all patterns (eg. files, exclude) | |
4 | basePath: '../', | |
5 | ||
6 | // frameworks to use | |
7 | // available frameworks: https://npmjs.org/browse/keyword/karma-adapter | |
8 | frameworks: ['mocha'], | |
9 | ||
10 | // list of files / patterns to load in the browser | |
11 | files: [ | |
12 | 'test/**/*test.js' | |
13 | ], | |
14 | ||
15 | // list of files to exclude | |
16 | exclude: [ | |
17 | ], | |
18 | ||
19 | // preprocess matching files before serving them to the browser | |
20 | // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor | |
21 | preprocessors: { | |
22 | 'test/**/*.js': [ 'webpack' ] | |
23 | }, | |
24 | ||
25 | webpack: { | |
26 | node: { | |
27 | __filename: true, | |
28 | "fs": "empty" | |
29 | } | |
30 | }, | |
31 | ||
32 | // test results reporter to use | |
33 | // possible values: 'dots', 'progress' | |
34 | // available reporters: https://npmjs.org/browse/keyword/karma-reporter | |
35 | reporters: ['spec'], | |
36 | ||
37 | // web server port | |
38 | port: 9876, | |
39 | ||
40 | // enable / disable colors in the output (reporters and logs) | |
41 | colors: true, | |
42 | ||
43 | // level of logging | |
44 | // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG | |
45 | logLevel: config.LOG_INFO, | |
46 | ||
47 | // enable / disable watching file and executing tests whenever any file changes | |
48 | autoWatch: false, | |
49 | ||
50 | // start these browsers | |
51 | // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher | |
52 | browsers: ['PhantomJS'], | |
53 | ||
54 | // Continuous Integration mode | |
55 | // if true, Karma captures browsers, runs the tests and exits | |
56 | singleRun: true, | |
57 | ||
58 | // Concurrency level | |
59 | // how many browser should be started simultaneous | |
60 | concurrency: Infinity | |
61 | }) | |
62 | } |
0 | --reporter spec |
0 | process.env.DEBUG = '*'; | |
1 | var lazyDebug = require('../src'); | |
2 | ||
3 | lazyDebug(__filename)('it works?'); | |
4 | ||
5 | lazyDebug(__filename, 'sub')('debugger for submodule'); | |
6 | ||
7 | lazyDebug(__filename, {packageName: true})('includes package name'); | |
8 | ||
9 | lazyDebug | |
10 | (__filename, {packageName: 'fake', submoduleName: 'sub2'}) | |
11 | ('fake package name + submodule'); |