-
-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9efa7f3
commit 8cc8aaa
Showing
12 changed files
with
487 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# disallow 'bin' files that npm ignores (no-unpublished-bin) | ||
|
||
We can publish CLI commands by `npm`. It uses `bin` field of `package.json`. | ||
|
||
```json | ||
{ | ||
"name": "command-name", | ||
"bin": "bin/index.js" | ||
} | ||
``` | ||
|
||
At this time, if `npm` ignores the file, your package will fail to install. | ||
|
||
## Rule Details | ||
|
||
If `npm` ignores the files in `bin` field, this rule warns the files. | ||
|
||
- If `files` field does not includes the files in `bin` field. | ||
- If `.npmignore` file includes the files in `bin` field. | ||
|
||
## Options | ||
|
||
```json | ||
{ | ||
"rules": { | ||
"node/no-unpublished-bin": ["error", { | ||
"convertPath": null | ||
}] | ||
} | ||
} | ||
``` | ||
|
||
### `convertPath` | ||
|
||
If we use transpilers (e.g. Babel), perhaps the file path to a source code is never published. | ||
`convertPath` option tells to the rule, it needs to convert file paths. | ||
|
||
For example: | ||
|
||
```json | ||
{ | ||
"rules": { | ||
"node/no-unpublished-bin": ["error", { | ||
"convertPath": { | ||
"src/bin/**/*.js": ["^src/bin/(.+)$", "bin/$1"] | ||
} | ||
}] | ||
} | ||
} | ||
``` | ||
|
||
This option has the following shape: `<targetFiles>: [<fromRegExp>, <toString>]` | ||
|
||
`targetFiles` is a glob pattern. | ||
It converts paths which are matched to the pattern with the following way. | ||
|
||
```js | ||
path.replace(new RegExp(fromRegExp), toString); | ||
``` | ||
|
||
So on this example, `src/bin/index.js` is handled as `bin/index.js`. | ||
|
||
## Shared Settings | ||
|
||
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings). | ||
Several rules have the same option, but we can set this option at once. | ||
|
||
- `convertPath` | ||
|
||
For Example: | ||
|
||
```json | ||
{ | ||
"settings": { | ||
"node": { | ||
"convertPath": { | ||
"src/bin/**/*.js": ["^src/bin/(.+)$", "bin/$1"] | ||
} | ||
} | ||
}, | ||
"rules": { | ||
"node/no-unpublished-bin": "error" | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* @author Toru Nagashima | ||
* @copyright 2016 Toru Nagashima. All rights reserved. | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var path = require("path") | ||
var getConvertPath = require("../util/get-convert-path") | ||
var getNpmignore = require("../util/get-npmignore") | ||
var getPackageJson = require("../util/get-package-json") | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
/** | ||
* Checks whether or not a given path is a `bin` file. | ||
* | ||
* @param {string} filePath - A file path to check. | ||
* @param {string|object|undefined} binField - A value of the `bin` field of `package.json`. | ||
* @param {string} basedir - A directory path that `package.json` exists. | ||
* @returns {boolean} `true` if the file is a `bin` file. | ||
*/ | ||
function isBinFile(filePath, binField, basedir) { | ||
if (!binField) { | ||
return false | ||
} | ||
if (typeof binField === "string") { | ||
return filePath === path.resolve(basedir, binField) | ||
} | ||
return Object.keys(binField).some(function(key) { | ||
return filePath === path.resolve(basedir, binField[key]) | ||
}) | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "disallow 'bin' files that npm ignores", | ||
category: "Possible Errors", | ||
recommended: false, | ||
}, | ||
fixable: false, | ||
schema: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
convertPath: { | ||
type: "object", | ||
properties: {}, | ||
patternProperties: { | ||
"^.+$": { | ||
type: "array", | ||
items: {type: "string"}, | ||
minItems: 2, | ||
maxItems: 2, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
|
||
create: function(context) { | ||
return { | ||
Program: function(node) { | ||
// Check file path. | ||
var rawFilePath = context.getFilename() | ||
if (rawFilePath === "<input>") { | ||
return | ||
} | ||
rawFilePath = path.resolve(rawFilePath) | ||
|
||
// Find package.json | ||
var p = getPackageJson(rawFilePath) | ||
if (!p) { | ||
return | ||
} | ||
|
||
// Convert by convertPath option | ||
var basedir = path.dirname(p.filePath) | ||
var relativePath = getConvertPath(context)( | ||
path.relative(basedir, rawFilePath).replace(/\\/g, "/") | ||
) | ||
var filePath = path.join(basedir, relativePath) | ||
|
||
// Check this file is bin. | ||
if (!isBinFile(filePath, p.bin, basedir)) { | ||
return | ||
} | ||
|
||
// Check ignored or not | ||
var npmignore = getNpmignore(filePath) | ||
if (!npmignore.match(relativePath)) { | ||
return | ||
} | ||
|
||
// Report. | ||
context.report({ | ||
node: node, | ||
message: | ||
"npm ignores '{{name}}'. " + | ||
"Check 'files' field of 'package.json' or '.npmignore'.", | ||
data: {name: relativePath}, | ||
}) | ||
}, | ||
} | ||
}, | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/fixtures/no-unpublished-bin/multi-files/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"private": true, | ||
"name": "test", | ||
"version": "1.0.0", | ||
"bin": { | ||
"a": "a.js", | ||
"b": "b.js" | ||
}, | ||
"files": [ | ||
"lib" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/a.js |
9 changes: 9 additions & 0 deletions
9
tests/fixtures/no-unpublished-bin/multi-npmignore/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"private": true, | ||
"name": "test", | ||
"version": "1.0.0", | ||
"bin": { | ||
"a": "a.js", | ||
"b": "b.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"private": true, | ||
"name": "test", | ||
"version": "1.0.0", | ||
"bin": { | ||
"a": "a.js", | ||
"b": "b.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"private": true, | ||
"name": "test", | ||
"version": "1.0.0", | ||
"bin": "a.js", | ||
"files": [ | ||
"lib" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/a.js |
6 changes: 6 additions & 0 deletions
6
tests/fixtures/no-unpublished-bin/simple-npmignore/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"private": true, | ||
"name": "test", | ||
"version": "1.0.0", | ||
"bin": "a.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"private": true, | ||
"name": "test", | ||
"version": "1.0.0", | ||
"bin": "a.js" | ||
} |
Oops, something went wrong.