diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e85cf81 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,5 @@ +[*] +indent_size = 2 +indent_style = space +max_line_length = 120 +tab_width = 2 \ No newline at end of file diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..64c3b03 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,24 @@ +module.exports = { + extends: ['eslint:recommended', 'prettier'], // extending recommended config and config derived from eslint-config-prettier + plugins: ['prettier'], // extending recommended config and config derived from eslint-config-prettier + env: { + browser: true, + node: true, + amd: true, + es6: true, + commonjs: true, + jest: true, + }, + parser: 'babel-eslint', + rules: { + 'prettier/prettier': [ + // customizing prettier rules (unfortunately not many of them are customizable) + 'error', + { + singleQuote: true, + tabWidth: 2, + }, + ], + eqeqeq: ['error', 'always'], // adding some custom ESLint rules + }, +}; diff --git a/README.md b/README.md index fe23119..55089cd 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ $ npm install add-dependencies [-g] ### Usage -Run: +Run (for `nodejs` see Example): ```sh $ add-dependencies [package_file] [target] [--no-overwrite] @@ -46,3 +46,20 @@ or with `npx`: ```sh $ npx add-dependencies /home/user/project/package.json moment@2.0.0 react@16.8 redux eslint --dev ``` + +or via nodejs + +```js +const npmAdd = require('add-dependencies/lib/AddDependencies'); +const dependencies = [ + 'package1', + 'package2', + 'package3', +]; +const target = npmAdd.CONSTANTS.DEPENDENCIES; +const overwrite = false; +const packageFilePath = 'package.json'; +new npmAdd(dependencies, target, overwrite, packageFilePath) + .run() + .then(() => console.log('completed')); +``` \ No newline at end of file diff --git a/cli-index.js b/cli-index.js new file mode 100755 index 0000000..7f37807 --- /dev/null +++ b/cli-index.js @@ -0,0 +1,17 @@ +#!/usr/bin/env node +const AddDependencies = require('./lib/AddDependencies'); + +console.log( + '\x1b[33m%s\x1b[0m', + 'This script adds dependencies (latest or specified versions) to the package.json file skipping the installation process.' +); + +const app = new AddDependencies(); + +app + .addDependencies() + .then(app.saveToPackage.bind(app)) + .catch((error) => { + console.error('\x1b[31m%s\x1b[0m', error); + process.exit(1); + }); diff --git a/index.js b/index.js index 1e8cf44..23c368d 100755 --- a/index.js +++ b/index.js @@ -1,11 +1,2 @@ #!/usr/bin/env node -const AddDependencies = require('./lib/AddDependencies'); - -console.log('\x1b[33m%s\x1b[0m', 'This script adds dependencies (latest or specified versions) to the package.json file skipping the installation process.'); - -const app = new AddDependencies(); - -app.addDependencies().then(app.saveToPackage.bind(app)).catch((error) => { - console.error('\x1b[31m%s\x1b[0m', error); - process.exit(1); -}); +module.exports = require('./lib/AddDependencies'); diff --git a/lib/AddDependencies.js b/lib/AddDependencies.js index 8b78554..e377036 100644 --- a/lib/AddDependencies.js +++ b/lib/AddDependencies.js @@ -3,12 +3,16 @@ const semver = require('semver'); const Files = require('./Files'); class AddDependencies { - constructor() { + constructor(dependencies = [], + target = 'dependencies', + overwrite = true, + packageFilePath = './package.json' + ) { this.result = {}; - this.dependencies = []; - this.target = 'dependencies'; - this.overwrite = true; - this.packageFilePath = './package.json'; + this.dependencies = dependencies; + this.target = target; + this.overwrite = overwrite; + this.packageFilePath = packageFilePath; } addDependencies() { @@ -56,7 +60,15 @@ class AddDependencies { console.log(`Adding packages to '${this.target}'...`); - return Promise.all(this.dependencies.map((dep) => this.runNpmShow(dep))); + return this.mapDependencies(); + } + + mapDependencies() { + return Promise.all(this.dependencies.map((dep) => this.runNpmShow(dep))) + } + + run(){ + return this.mapDependencies().then(() => this.saveToPackage()) } runNpmShow(dep) { @@ -158,6 +170,15 @@ class AddDependencies { process.exit(1); }); } + + static get CONSTANTS() { + return { + DEPENDENCIES: 'dependencies', + DEV_DEPENDENCIES: 'devDependencies', + PEER_DEPENDENCIES: 'peerDependencies', + OPTIONAL_DEPENDENCIES: 'optionalDependencies', + }; + } } module.exports = AddDependencies; diff --git a/package.json b/package.json index 6fa9dc1..5de0b67 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "homepage": "https://github.com/arfeo/npm-add-dependencies#readme", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" + "lint": "eslint --fix --ext js lib/**.js index.js" }, "keywords": [ "dependencies", @@ -23,10 +24,17 @@ "license": "MIT", "preferGlobal": true, "bin": { - "add-dependencies": "index.js" + "add-dependencies": "cli-index.js" }, "dependencies": { "npm-run": "^5.0.1", "semver": "^6.3.0" + }, + "devDependencies": { + "eslint": "^7.3.0", + "babel-eslint": "^10.1.0", + "eslint-config-prettier": "^3.3.0", + "eslint-plugin-prettier": "^3.0.0", + "prettier": "^2.0.5" } }