Skip to content

Commit 8c4c490

Browse files
author
tunnckoCore
committed
implement 👏
1 parent 80ec19b commit 8c4c490

11 files changed

+347
-3
lines changed

Diff for: .codeclimate.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
languages:
2+
JavaScript: true
3+
exclude_paths:
4+
- "dist/**/*"
5+
- "dist/*.js"
6+
- "dist/**/*.js"
7+
- "dist/**.js"

Diff for: .editorconfig

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# .editorconfig <https://github.com/tunnckoCore/dotfiles>
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
end_of_line = lf
8+
indent_size = 2
9+
indent_style = space
10+
11+
[*.js]
12+
insert_final_newline = true
13+
trim_trailing_whitespace = true
14+
15+
[*.php]
16+
indent_size = 4
17+
insert_final_newline = true
18+
trim_trailing_whitespace = true
19+
20+
[*.md]
21+
insert_final_newline = false
22+
trim_trailing_whitespace = false
23+
24+
[*.py]
25+
indent_size = 4
26+
insert_final_newline = true
27+
trim_trailing_whitespace = true

Diff for: .gitignore

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# .gitignore <https://github.com/tunnckoCore/dotfiles>
2+
3+
# Always-ignore dirs #
4+
# ####################
5+
_gh_pages
6+
node_modules
7+
bower_components
8+
components
9+
vendor
10+
build
11+
dest
12+
dist
13+
src
14+
lib-cov
15+
coverage
16+
nbproject
17+
cache
18+
temp
19+
tmp
20+
try-require-please
21+
22+
# Packages #
23+
# ##########
24+
*.7z
25+
*.dmg
26+
*.gz
27+
*.iso
28+
*.jar
29+
*.rar
30+
*.tar
31+
*.zip
32+
33+
# OS, Logs and databases #
34+
# #########################
35+
*.pid
36+
*.dat
37+
*.log
38+
*.sql
39+
*.sqlite
40+
*~
41+
~*
42+
43+
# Another files #
44+
# ###############
45+
Icon?
46+
.DS_Store*
47+
Thumbs.db
48+
ehthumbs.db
49+
Desktop.ini
50+
npm-debug.log
51+
.directory
52+
._*
53+
lcov.info

Diff for: .travis.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
language: "node_js"
2+
sudo: false
3+
4+
node_js:
5+
- "0.10"
6+
- "0.12"
7+
- "4"
8+
- "5"
9+
10+
notifications:
11+
email:
12+
on_success: never
13+
on_failure: never
14+
15+
before_script:
16+
- npm install standard
17+
- standard
18+
19+
script:
20+
- npm install istanbul
21+
- node --harmony node_modules/.bin/istanbul cover test.js
22+
23+
after_success:
24+
- npm install coveralls
25+
- cat coverage/lcov.info | coveralls
26+
27+
matrix:
28+
allow_failures:
29+
- node_js: "0.10"

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
3+
## 0.0.0 - 2016-02-06
4+
- Initial commit

Diff for: CONTRIBUTING.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Contributing Guidelines
2+
3+
Contributions are always welcome!
4+
5+
**Before spending lots of time on something, ask for feedback on your idea first!**
6+
7+
Please search issues and pull requests before adding something new to avoid duplicating efforts and conversations.
8+
9+
10+
## Installing
11+
12+
Fork and clone the repo, then `npm install` to install all dependencies and `npm test` to ensure all is okey before you start anything.
13+
14+
15+
## Testing
16+
17+
Tests are run with `npm test`. Please ensure all tests are passing before submitting a pull request (unless you're creating a failing test to increase test coverage or show a problem).
18+
19+
## Code Style
20+
21+
[![standard][standard-image]][standard-url]
22+
23+
This repository uses [`standard`][standard-url] to maintain code style and consistency, and to avoid style arguments. You are encouraged to install it globally. `npm test` runs `standard` so you don't have to!
24+
25+
```
26+
npm i standard -g
27+
```
28+
29+
It is intentional to don't have `standard`, `istanbul` and `coveralls` in the devDependencies. Travis will handle all that stuffs. That approach will save bandwidth also installing and development time.
30+
31+
[standard-image]: https://cdn.rawgit.com/feross/standard/master/badge.svg
32+
[standard-url]: https://github.com/feross/standard

Diff for: LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
21+
SOFTWARE.

Diff for: README.md

+74-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,74 @@
1-
# try-require-please
2-
Try to require the given module, failing loudly with default message if module does not exists.
1+
# [try-require-please][author-www-url] [![npmjs.com][npmjs-img]][npmjs-url] [![The MIT License][license-img]][license-url]
2+
3+
> Try to require the given module, failing loudly with default message if module does not exists.
4+
5+
[![code climate][codeclimate-img]][codeclimate-url] [![standard code style][standard-img]][standard-url] [![travis build status][travis-img]][travis-url] [![coverage status][coveralls-img]][coveralls-url] [![dependency status][david-img]][david-url]
6+
7+
8+
## Install
9+
```
10+
npm i try-require-please --save
11+
```
12+
13+
14+
## Usage
15+
> For more use-cases see the [tests](./test.js)
16+
17+
```js
18+
const tryRequirePlease = require('try-require-please')
19+
```
20+
21+
22+
## Contributing
23+
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/tunnckoCore/try-require-please/issues/new).
24+
But before doing anything, please read the [CONTRIBUTING.md](./CONTRIBUTING.md) guidelines.
25+
26+
27+
## [Charlike Make Reagent](http://j.mp/1stW47C) [![new message to charlike][new-message-img]][new-message-url] [![freenode #charlike][freenode-img]][freenode-url]
28+
29+
[![tunnckocore.tk][author-www-img]][author-www-url] [![keybase tunnckocore][keybase-img]][keybase-url] [![tunnckoCore npm][author-npm-img]][author-npm-url] [![tunnckoCore twitter][author-twitter-img]][author-twitter-url] [![tunnckoCore github][author-github-img]][author-github-url]
30+
31+
32+
[npmjs-url]: https://www.npmjs.com/package/try-require-please
33+
[npmjs-img]: https://img.shields.io/npm/v/try-require-please.svg?label=try-require-please
34+
35+
[license-url]: https://github.com/tunnckoCore/try-require-please/blob/master/LICENSE
36+
[license-img]: https://img.shields.io/badge/license-MIT-blue.svg
37+
38+
39+
[codeclimate-url]: https://codeclimate.com/github/tunnckoCore/try-require-please
40+
[codeclimate-img]: https://img.shields.io/codeclimate/github/tunnckoCore/try-require-please.svg
41+
42+
[travis-url]: https://travis-ci.org/tunnckoCore/try-require-please
43+
[travis-img]: https://img.shields.io/travis/tunnckoCore/try-require-please.svg
44+
45+
[coveralls-url]: https://coveralls.io/r/tunnckoCore/try-require-please
46+
[coveralls-img]: https://img.shields.io/coveralls/tunnckoCore/try-require-please.svg
47+
48+
[david-url]: https://david-dm.org/tunnckoCore/try-require-please
49+
[david-img]: https://img.shields.io/david/tunnckoCore/try-require-please.svg
50+
51+
[standard-url]: https://github.com/feross/standard
52+
[standard-img]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
53+
54+
55+
[author-www-url]: http://www.tunnckocore.tk
56+
[author-www-img]: https://img.shields.io/badge/www-tunnckocore.tk-fe7d37.svg
57+
58+
[keybase-url]: https://keybase.io/tunnckocore
59+
[keybase-img]: https://img.shields.io/badge/keybase-tunnckocore-8a7967.svg
60+
61+
[author-npm-url]: https://www.npmjs.com/~tunnckocore
62+
[author-npm-img]: https://img.shields.io/badge/npm-~tunnckocore-cb3837.svg
63+
64+
[author-twitter-url]: https://twitter.com/tunnckoCore
65+
[author-twitter-img]: https://img.shields.io/badge/[email protected]
66+
67+
[author-github-url]: https://github.com/tunnckoCore
68+
[author-github-img]: https://img.shields.io/badge/[email protected]
69+
70+
[freenode-url]: http://webchat.freenode.net/?channels=charlike
71+
[freenode-img]: https://img.shields.io/badge/freenode-%23charlike-5654a4.svg
72+
73+
[new-message-url]: https://github.com/tunnckoCore/ama
74+
[new-message-img]: https://img.shields.io/badge/ask%20me-anything-green.svg

Diff for: index.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*!
2+
* try-require-please <https://github.com/tunnckoCore/try-require-please>
3+
*
4+
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
5+
* Released under the MIT license.
6+
*/
7+
8+
'use strict'
9+
10+
var format = require('util').format
11+
var define = require('define-property')
12+
13+
module.exports = function tryRequirePlease (name, wantedBy) {
14+
var ret = null
15+
try {
16+
ret = require(name)
17+
} catch (err) {
18+
throw warnError(name, wantedBy, err)
19+
}
20+
return ret
21+
}
22+
23+
function warnError (name, wantedBy, err) {
24+
var message = formatMessage(name, wantedBy)
25+
define(err, '_moduleName', name)
26+
define(err, '_wantedBy', wantedBy)
27+
define(err, '_message', message)
28+
console.warn(message)
29+
return err
30+
}
31+
32+
function formatMessage (name, wantedBy) {
33+
var one = 'Please install `%s`, because '
34+
one += (typeof wantedBy !== 'undefined' ? '`%s`' : 'some package') + ' needs it.\n'
35+
var two = 'Maybe `%s` is devDependency, because adds optional functionality?'
36+
37+
one = format(one, name, wantedBy || '')
38+
two = format(two, name)
39+
return one + two
40+
}

Diff for: package.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "try-require-please",
3+
"version": "0.0.0",
4+
"description": "Try to require the given module, failing loudly with default message if module does not exists.",
5+
"repository": "tunnckoCore/try-require-please",
6+
"author": "Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)",
7+
"precommit.silent": true,
8+
"main": "index.js",
9+
"license": "MIT",
10+
"scripts": {
11+
"test": "standard && node test.js"
12+
},
13+
"dependencies": {
14+
"define-property": "^0.2.5"
15+
},
16+
"devDependencies": {
17+
"assertit": "^0.1.0",
18+
"pre-commit": "*"
19+
},
20+
"files": [
21+
"index.js"
22+
],
23+
"keywords": [
24+
"try-require-please"
25+
]
26+
}

Diff for: test.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*!
2+
* try-require-please <https://github.com/tunnckoCore/try-require-please>
3+
*
4+
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
5+
* Released under the MIT license.
6+
*/
7+
8+
/* jshint asi:true */
9+
10+
'use strict'
11+
12+
var test = require('assertit')
13+
var tryRequirePlease = require('./index')
14+
15+
test('should throw if module not found', function (done) {
16+
function fixture () {
17+
tryRequirePlease('foo-bar')
18+
}
19+
20+
test.throws(fixture, Error)
21+
test.throws(fixture, /Cannot find module/)
22+
done()
23+
})
24+
25+
test('should throw if not found and adds default `._message` property on `err`', function (done) {
26+
try {
27+
tryRequirePlease('baz-qux')
28+
} catch (err) {
29+
test.strictEqual(err._wantedBy, undefined)
30+
test.strictEqual(err._moduleName, 'baz-qux')
31+
test.strictEqual(/Please install `baz-qux`/.test(err._message), true)
32+
test.strictEqual(/because some package needs it/.test(err._message), true)
33+
done()
34+
}
35+
})
36+
37+
test('should throw and allow custom message to show on error', function (done) {
38+
try {
39+
tryRequirePlease('my-module', 'awesome-pkg')
40+
} catch (err) {
41+
test.strictEqual(err._moduleName, 'my-module')
42+
test.strictEqual(err._wantedBy, 'awesome-pkg')
43+
test.strictEqual(/because `awesome-pkg` needs it/.test(err._message), true)
44+
done()
45+
}
46+
})
47+
48+
test('should get module if exist and not throws', function (done) {
49+
var util = tryRequirePlease('util')
50+
51+
test.strictEqual(typeof util.format, 'function')
52+
test.strictEqual(util.format('foo %s bar', 'qux'), 'foo qux bar')
53+
done()
54+
})

0 commit comments

Comments
 (0)