npm-package-json-lint
Advanced tools
Comparing version 3.0.0-alpha2 to 3.0.0-alpha3
@@ -14,3 +14,3 @@ # Change Log | ||
## [3.0.0-alpha1] - 2018-04-29 | ||
## [3.0.0] - 2018-05-06 | ||
### Added | ||
@@ -20,2 +20,3 @@ - Added support for glob based package.json file detection. Addresses [#74](https://github.com/tclindner/npm-package-json-lint/issues/74). | ||
- Added support for running npm-package-json-lint programmatically. Addresses [#76](https://github.com/tclindner/npm-package-json-lint/issues/76). | ||
- New rule: [valid-values-publishConfig](https://github.com/tclindner/npm-package-json-lint/wiki/valid-values-publishConfig). Addresses [#80] | ||
@@ -22,0 +23,0 @@ ### Changed |
{ | ||
"name": "npm-package-json-lint", | ||
"version": "3.0.0-alpha2", | ||
"description": "CLI app for linting package.json files.", | ||
"version": "3.0.0-alpha3", | ||
"description": "Configurable linter for package.json files.", | ||
"keywords": [ | ||
@@ -34,9 +34,8 @@ "lint", | ||
"test": "mocha tests/unit --recursive", | ||
"coverage": "nyc --extension .js --check-coverage --lines 94 --branches 81 --functions 94 npm test" | ||
"coverage": "nyc --extension .js --check-coverage --lines 99 --branches 94 --functions 99 npm test" | ||
}, | ||
"dependencies": { | ||
"ajv": "^6.4.0", | ||
"chalk": "^2.3.1", | ||
"chalk": "^2.4.1", | ||
"glob": "^7.1.2", | ||
"in-array": "^0.1.2", | ||
"is-path-inside": "^2.0.0", | ||
@@ -46,17 +45,18 @@ "is-plain-obj": "^1.1.0", | ||
"log-symbols": "^2.2.0", | ||
"meow": "^4.0.0", | ||
"plur": "^2.1.2", | ||
"meow": "^5.0.0", | ||
"plur": "^3.0.1", | ||
"semver": "^5.5.0", | ||
"strip-json-comments": "^2.0.1", | ||
"validator": "^9.4.1" | ||
"validator": "^10.1.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"eslint": "^4.18.0", | ||
"eslint-config-tc": "^2.3.0", | ||
"eslint": "^4.19.1", | ||
"eslint-config-tc": "^2.4.0", | ||
"eslint-formatter-pretty": "^1.3.0", | ||
"figures": "^2.0.0", | ||
"mocha": "^5.0.1", | ||
"nyc": "^11.4.1", | ||
"sinon": "^4.3.0" | ||
"mocha": "^5.1.1", | ||
"npm-package-json-lint-config-default": "^1.1.0", | ||
"nyc": "^11.7.1", | ||
"sinon": "^4.5.0" | ||
}, | ||
@@ -63,0 +63,0 @@ "engines": { |
182
README.md
@@ -48,3 +48,3 @@ # npm-package-json-lint | ||
## Commands and configuration | ||
## CLI commands and configuration | ||
@@ -103,2 +103,182 @@ | Command | Alias | Description | | ||
## Node.js API | ||
npm-package-json-lint exports two main objects: `CLIEngine` and `NpmPackageJsonLint`. | ||
### NpmPackageJsonLint() | ||
Creates an instance of NpmPackageJsonLint | ||
`NpmPackageJsonLint` has one public method, `lint`. `lint` takes a package.json object in object form and a config object as parameters. | ||
#### .lint(packageJsonData, configObj) | ||
Runs configured rules against the provided package.json object. | ||
##### packageJsonData | ||
Type: `object` | ||
A package.json file in object form. | ||
##### configObj | ||
Type: `object` | ||
A valid configuration object. | ||
##### Example | ||
The following example demostrates how to use `lint`. | ||
```js | ||
const NpmPackageJsonLint = require('npm-package-json-lint').NpmPackageJsonLint; | ||
const npmPackageJsonLint = new NpmPackageJsonLint(); | ||
const results = npmPackageJsonLint.lint(packageJsonDataAsObject, configObject); | ||
``` | ||
##### Return | ||
`lint` returns an object with an array of `LintIssue`s. Please see `LintIssue` section for more detail. | ||
```js | ||
{ | ||
issues: [ | ||
{ | ||
lintId: 'require-name', | ||
severity: 'error', | ||
node: 'name', | ||
lintMessage: 'name is required' | ||
} | ||
] | ||
} | ||
``` | ||
#### .version | ||
Calling `.version` on an instance of `NpmPackageJsonLint` will return the version number of npm-package-json-lint that the linter is associated with. | ||
##### Example | ||
```js | ||
const NpmPackageJsonLint = require('npm-package-json-lint').NpmPackageJsonLint; | ||
const npmPackageJsonLint = new NpmPackageJsonLint(); | ||
npmPackageJsonLint.version; // => '3.0.0' | ||
``` | ||
### CLIEngine(options) | ||
Creates an instance of CLIEngine | ||
##### options | ||
Type: `object` | ||
CLIEngine configuration object | ||
* `configFile` {string} Name of module/file to use. | ||
* `cwd` {string} The current working diretory for all file operations. | ||
* `useConfigFiles` {boolean} False disables use of .npmpackagejsonlintrc.json files and npmpackagejsonlint.config.js files. | ||
* `rules` {object} An object of rules to use. | ||
##### Example | ||
The following example demostrates how to initialize a `CLIEngine`. | ||
```js | ||
const CLIEngine = require('npm-package-json-lint').CLIEngine; | ||
const cliEngineOptions = { | ||
configFile: '', | ||
cwd: process.cwd(), | ||
useConfigFiles: true, | ||
rules: {} | ||
}; | ||
const cliEngine = new CLIEngine(cliEngineOptions); | ||
``` | ||
#### .executeOnPackageJsonFiles(patterns) | ||
Runs npm-package-json-lint against the array a patterns. | ||
##### patterns | ||
Type: `array` | ||
An array of glob patterns | ||
##### Example | ||
The following example demostrates how to use `executeOnPackageJsonFiles`. | ||
```js | ||
const CLIEngine = require('npm-package-json-lint').CLIEngine; | ||
const cliEngineOptions = { | ||
configFile: '', | ||
cwd: process.cwd(), | ||
useConfigFiles: true, | ||
rules: {} | ||
}; | ||
const patterns = ['.']; | ||
const cliEngine = new CLIEngine(cliEngineOptions); | ||
const results = cliEngine.executeOnPackageJsonFiles(patterns); | ||
``` | ||
##### Return | ||
`executeOnPackageJsonFiles` returns an object with an array of results. | ||
```js | ||
{ | ||
results: [ | ||
{ | ||
filePath: './package.json', | ||
issues: [ | ||
{ | ||
lintId: 'require-name', | ||
severity: 'error', | ||
node: 'name', | ||
lintMessage: 'name is required' | ||
} | ||
], | ||
errorCount: 1, | ||
warningCount: 0 | ||
} | ||
], | ||
errorCount: 1, | ||
warningCount: 0 | ||
} | ||
``` | ||
#### .version | ||
Calling `.version` on an instance of `CLIEngine` will return the version number of npm-package-json-lint that the CLIEngine is associated with. | ||
##### Example | ||
```js | ||
const CLIEngine = require('npm-package-json-lint').CLIEngine; | ||
const cliEngineOptions = { | ||
configFile: '', | ||
cwd: process.cwd(), | ||
useConfigFiles: true, | ||
rules: {} | ||
}; | ||
const cliEngine = new CLIEngine(cliEngineOptions); | ||
cliEngine.version; // => '3.0.0' | ||
``` | ||
> **WARNING** | ||
Only the functions documented above are supported. All other functions that are exposed may change with any release. Please refrain from using them. | ||
## Lint Rules | ||
@@ -105,0 +285,0 @@ |
@@ -207,2 +207,3 @@ 'use strict'; | ||
addedFiles.add(file); | ||
files.push(file); | ||
@@ -230,2 +231,3 @@ } else { | ||
addedFiles.add(filePath); | ||
files.push(filePath); | ||
@@ -232,0 +234,0 @@ }); |
@@ -15,3 +15,3 @@ 'use strict'; | ||
const userHomeDir = os.homedir(); | ||
const noRules = 0; | ||
@@ -60,14 +60,2 @@ /** | ||
/** | ||
* Loads configuration from current package.json file. | ||
* | ||
* @param {String} filePath The file to load. | ||
* @returns {Object} The configuration object from the file. | ||
* @throws {Error} If the file cannot be read. | ||
* @private | ||
*/ | ||
getConfigFromPkgJsonProp(filePath) { | ||
return ConfigFile.loadFromPackageJson(filePath, this); | ||
} | ||
/** | ||
* Loads the config options from a config specified on the command line. | ||
@@ -100,2 +88,3 @@ * | ||
if (typeof this.personalConfig === 'undefined') { | ||
const userHomeDir = os.homedir(); | ||
let configObj = {}; | ||
@@ -126,3 +115,3 @@ | ||
getProjectHierarchyConfig(filePath) { | ||
let config = {}; | ||
let config = ConfigFile.createEmptyConfig(); | ||
@@ -132,19 +121,30 @@ const directory = filePath ? path.dirname(filePath) : this.options.cwd; | ||
if (directory === getProjectDir() || isPathInside(directory, getProjectDir())) { | ||
const pkgJsonFilePath = path.join(directory, 'package.json'); | ||
const jsonRcFilePath = path.join(directory, ConfigFileType.rcFileName); | ||
const javaScriptConfigFilePath = path.join(directory, ConfigFileType.javaScriptConfigFileName); | ||
if (fs.existsSync(jsonRcFilePath) && fs.statSync(jsonRcFilePath).isFile()) { | ||
const rcConfig = ConfigFile.load(jsonRcFilePath, this); | ||
if (fs.existsSync(pkgJsonFilePath) && fs.statSync(pkgJsonFilePath).isFile()) { | ||
config = ConfigFile.loadFromPackageJson(pkgJsonFilePath, this); | ||
} | ||
config = Object.assign({}, rcConfig, config); | ||
} else if (fs.existsSync(javaScriptConfigFilePath) && fs.statSync(javaScriptConfigFilePath).isFile()) { | ||
const jsonConfig = ConfigFile.load(javaScriptConfigFilePath, this); | ||
config = Object.assign({}, jsonConfig, config); | ||
if (this.useConfigFiles && Object.keys(config.rules).length === noRules && fs.existsSync(jsonRcFilePath) && fs.statSync(jsonRcFilePath).isFile()) { | ||
config = ConfigFile.load(jsonRcFilePath, this); | ||
} else if (this.useConfigFiles && Object.keys(config.rules).length === noRules && fs.existsSync(javaScriptConfigFilePath) && fs.statSync(javaScriptConfigFilePath).isFile()) { | ||
config = ConfigFile.load(javaScriptConfigFilePath, this); | ||
} | ||
if (!config.root) { | ||
const parentDir = path.join(directory, '../'); | ||
if (config.hasOwnProperty('root') && !config.root) { | ||
const parentDir = path.resolve(directory, '../'); | ||
const parentConfig = this.getProjectHierarchyConfig(parentDir); | ||
config = Object.assign({}, this.getProjectHierarchyConfig(parentDir), config); | ||
// Merge base object | ||
const mergedConfig = Object.assign({}, parentConfig, config); | ||
// Merge rules | ||
const rules = Object.assign({}, parentConfig.rules, config.rules); | ||
// Override merged rules | ||
mergedConfig.rules = rules; | ||
config = mergedConfig; | ||
} | ||
@@ -170,25 +170,18 @@ } | ||
// Step 1: Get the package.json config object | ||
const packageConfig = this.getConfigFromPkgJsonProp(filePath); | ||
// Step 1: Get project hierarchy config from | ||
// package.json property, .npmpackagejsonlintrc.json, and npmpackagejsonlint.config.js files | ||
const projectHierarchyConfig = this.getProjectHierarchyConfig(filePath); | ||
// Step 2: Get project hierarchy config from | ||
// .npmpackagejsonlintrc.json and npmpackagejsonlint.config.js files | ||
let projectHierarchyConfig = ConfigFile.createEmptyConfig(); | ||
if (this.useConfigFiles) { | ||
projectHierarchyConfig = this.getProjectHierarchyConfig(filePath); | ||
} | ||
// Step 3: Load cli specified config | ||
// Step 2: Load cli specified config | ||
const cliSpecifiedCfgFileConfig = this.loadCliSpecifiedCfgFile(this.options.configFile); | ||
// Step 4: Merge config | ||
// Step 3: Merge config | ||
// NOTE: Object.assign does a shallow copy of objects, so we need to | ||
// do this for all of it properties then create a new final object | ||
const finalRules = Object.assign({}, packageConfig.rules, projectHierarchyConfig.rules, cliSpecifiedCfgFileConfig.rules, this.cliConfig); | ||
const finalRules = Object.assign({}, projectHierarchyConfig.rules, cliSpecifiedCfgFileConfig.rules, this.cliConfig); | ||
finalConfig = {rules: finalRules}; | ||
// Step 5: Check if any config has been found. | ||
// Step 4: Check if any config has been found. | ||
// If no, try to load personal config from user home directory | ||
@@ -209,3 +202,3 @@ if (!Object.keys(finalConfig.rules).length) { | ||
// Step 6: return final config | ||
// Step 5: return final config | ||
return finalConfig; | ||
@@ -212,0 +205,0 @@ } |
@@ -110,23 +110,2 @@ 'use strict'; | ||
/** | ||
* Loads a configuration file from the given file path. | ||
* | ||
* @param {Object} filePath The path of the config file. | ||
* @param {Config} configContext Plugins context | ||
* @returns {Object} The configuration information. | ||
*/ | ||
const loadFromDisk = function(filePath, configContext) { | ||
let config = loadConfigFile(filePath); | ||
if (config) { | ||
ConfigValidator.validate(config, filePath, configContext.linterContext); | ||
if (config.extends) { | ||
config = applyExtends(config, configContext, filePath, filePath); | ||
} | ||
} | ||
return config; | ||
}; | ||
/** | ||
* Public ConfigFile class | ||
@@ -138,4 +117,3 @@ * @class | ||
/** | ||
* Loads a config object from the config cache based on its filename, falling back to the disk if the file is not yet | ||
* cached. | ||
* Loads a configuration file from the given file path. | ||
* | ||
@@ -148,3 +126,13 @@ * @param {string} filePath the path to the config file | ||
static load(filePath, configContext) { | ||
return loadFromDisk(filePath, configContext); | ||
let config = loadConfigFile(filePath); | ||
if (config) { | ||
ConfigValidator.validate(config, filePath, configContext.linterContext); | ||
if (config.hasOwnProperty('extends') && config.extends) { | ||
config = applyExtends(config, configContext, filePath, filePath); | ||
} | ||
} | ||
return config; | ||
} | ||
@@ -164,8 +152,6 @@ | ||
if (config) { | ||
ConfigValidator.validate(config, filePath, configContext.linterContext); | ||
ConfigValidator.validate(config, filePath, configContext.linterContext); | ||
if (config.extends) { | ||
config = applyExtends(config, configContext, filePath, filePath); | ||
} | ||
if (config.hasOwnProperty('extends') && config.extends) { | ||
config = applyExtends(config, configContext, filePath, filePath); | ||
} | ||
@@ -172,0 +158,0 @@ |
@@ -20,3 +20,3 @@ 'use strict'; | ||
/** | ||
* Main execution method for package json lint. | ||
* Runs configured rules against the provided package.json object. | ||
* | ||
@@ -23,0 +23,0 @@ * @param {Object} packageJsonData Valid package.json data |
@@ -5,4 +5,4 @@ 'use strict'; | ||
const LintIssue = require('./../LintIssue'); | ||
const lintId = 'os-type'; | ||
const nodeName = 'os'; | ||
const lintId = 'cpu-type'; | ||
const nodeName = 'cpu'; | ||
const message = 'Type should be an array'; | ||
@@ -9,0 +9,0 @@ const ruleType = 'standard'; |
'use strict'; | ||
const inArray = require('in-array'); | ||
const semver = require('semver'); | ||
@@ -19,3 +18,3 @@ | ||
for (const dependencyName in packageJsonData[nodeName]) { | ||
if (inArray(depsToCheckFor, dependencyName)) { | ||
if (depsToCheckFor.includes(dependencyName)) { | ||
return true; | ||
@@ -41,3 +40,3 @@ } | ||
for (const dependencyName in packageJsonData[nodeName]) { | ||
if (inArray(depsToCheckFor, dependencyName)) { | ||
if (depsToCheckFor.includes(dependencyName)) { | ||
const dependencyVersion = packageJsonData[nodeName][dependencyName]; | ||
@@ -44,0 +43,0 @@ |
'use strict'; | ||
const inArray = require('in-array'); | ||
/** | ||
@@ -18,5 +16,5 @@ * Determines whether a node has a valid value | ||
return inArray(validValues, value); | ||
return validValues.includes(value); | ||
}; | ||
module.exports.isValidValue = isValidValue; |
125531
12
102
2742
339
9
+ Addedirregular-plurals@2.0.0(transitive)
+ Addedmeow@5.0.0(transitive)
+ Addedplur@3.1.1(transitive)
+ Addedvalidator@10.11.0(transitive)
+ Addedyargs-parser@10.1.0(transitive)
- Removedin-array@^0.1.2
- Removedin-array@0.1.2(transitive)
- Removedirregular-plurals@1.4.0(transitive)
- Removedmeow@4.0.1(transitive)
- Removedminimist@1.2.8(transitive)
- Removedplur@2.1.2(transitive)
- Removedvalidator@9.4.1(transitive)
Updatedchalk@^2.4.1
Updatedmeow@^5.0.0
Updatedplur@^3.0.1
Updatedvalidator@^10.1.0