From a211d30e205374c3bf0499390c9b022cc000848d Mon Sep 17 00:00:00 2001 From: jack Date: Fri, 19 Jun 2020 21:46:39 +0800 Subject: [PATCH 01/14] trying to test non cli --- index.js | 2 ++ lib/AddDependencies.js | 14 +++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 1e8cf44..70cbff6 100755 --- a/index.js +++ b/index.js @@ -9,3 +9,5 @@ app.addDependencies().then(app.saveToPackage.bind(app)).catch((error) => { console.error('\x1b[31m%s\x1b[0m', error); process.exit(1); }); + +export default AddDependencies; \ No newline at end of file diff --git a/lib/AddDependencies.js b/lib/AddDependencies.js index 8b78554..32a3904 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() { From 9d892b51156eaa68c28e6c1874dde45b2aad3c7a Mon Sep 17 00:00:00 2001 From: jack Date: Fri, 19 Jun 2020 22:45:07 +0800 Subject: [PATCH 02/14] lets try this --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 70cbff6..8886b89 100755 --- a/index.js +++ b/index.js @@ -10,4 +10,4 @@ app.addDependencies().then(app.saveToPackage.bind(app)).catch((error) => { process.exit(1); }); -export default AddDependencies; \ No newline at end of file +module.exports = AddDependencies; \ No newline at end of file From 2f81d1afc6e59d3450a0cd6f5fe89d7db273584a Mon Sep 17 00:00:00 2001 From: jack Date: Fri, 19 Jun 2020 22:45:07 +0800 Subject: [PATCH 03/14] addeding a log to test --- index.js | 2 +- lib/AddDependencies.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 70cbff6..8886b89 100755 --- a/index.js +++ b/index.js @@ -10,4 +10,4 @@ app.addDependencies().then(app.saveToPackage.bind(app)).catch((error) => { process.exit(1); }); -export default AddDependencies; \ No newline at end of file +module.exports = AddDependencies; \ No newline at end of file diff --git a/lib/AddDependencies.js b/lib/AddDependencies.js index 32a3904..cf02644 100644 --- a/lib/AddDependencies.js +++ b/lib/AddDependencies.js @@ -8,6 +8,7 @@ class AddDependencies { overwrite = true, packageFilePath = './package.json' ) { + console.log(dependencies) this.result = {}; this.dependencies = dependencies; this.target = target; From 94adce39b491aceed7fedc195ed428b9a3f5bd26 Mon Sep 17 00:00:00 2001 From: jack Date: Fri, 19 Jun 2020 23:06:42 +0800 Subject: [PATCH 04/14] lets just try using lib --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index 8886b89..8a48ac7 100755 --- a/index.js +++ b/index.js @@ -8,6 +8,4 @@ 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 = AddDependencies; \ No newline at end of file +}); \ No newline at end of file From 4a75de5c3fcae60c256a91af2a00de79d1c8f227 Mon Sep 17 00:00:00 2001 From: jack Date: Fri, 19 Jun 2020 23:18:46 +0800 Subject: [PATCH 05/14] * added the ability to simply call run --- lib/AddDependencies.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/AddDependencies.js b/lib/AddDependencies.js index cf02644..341bcf3 100644 --- a/lib/AddDependencies.js +++ b/lib/AddDependencies.js @@ -8,7 +8,6 @@ class AddDependencies { overwrite = true, packageFilePath = './package.json' ) { - console.log(dependencies) this.result = {}; this.dependencies = dependencies; this.target = target; @@ -61,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) { From 8113b0cc9fec359958fecf05b0bb988cf02da6e0 Mon Sep 17 00:00:00 2001 From: jack Date: Fri, 19 Jun 2020 23:38:15 +0800 Subject: [PATCH 06/14] added readme change --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index fe23119..8457fa6 100644 --- a/README.md +++ b/README.md @@ -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 = '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 From ccf627a9e67f973c4c18302504e7d4bd7306f0c4 Mon Sep 17 00:00:00 2001 From: jack Date: Fri, 19 Jun 2020 23:39:41 +0800 Subject: [PATCH 07/14] one more readme modifcaiton --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8457fa6..04298cc 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] From 522d8d3ea7e76627523867adc7ff0c52d9593ca9 Mon Sep 17 00:00:00 2001 From: jack Date: Fri, 19 Jun 2020 23:43:32 +0800 Subject: [PATCH 08/14] revert removal of new line --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 8a48ac7..1e8cf44 100755 --- a/index.js +++ b/index.js @@ -8,4 +8,4 @@ const app = new AddDependencies(); app.addDependencies().then(app.saveToPackage.bind(app)).catch((error) => { console.error('\x1b[31m%s\x1b[0m', error); process.exit(1); -}); \ No newline at end of file +}); From 169bf72441ac6687bb308b0b5ff10e67fe8cca66 Mon Sep 17 00:00:00 2001 From: jack Date: Sat, 20 Jun 2020 00:08:38 +0800 Subject: [PATCH 09/14] added constants to support the specification of target from nodejs side --- lib/AddDependencies.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/AddDependencies.js b/lib/AddDependencies.js index 341bcf3..fa4fa8d 100644 --- a/lib/AddDependencies.js +++ b/lib/AddDependencies.js @@ -172,4 +172,12 @@ class AddDependencies { } } -module.exports = AddDependencies; +module.exports = { + CONSTANTS: { + DEPENDENCIES: 'dependencies', + DEV_DEPENDENCIES: 'devDependencies', + PEER_DEPENDENCIES: 'peerDependencies', + OPTIONAL_DEPENDENCIES: 'optionalDependencies', + }, + default: AddDependencies +}; From 8bcf0cdc943b6583eb08631e406d99fd908e0fdf Mon Sep 17 00:00:00 2001 From: jack Date: Sat, 20 Jun 2020 00:13:41 +0800 Subject: [PATCH 10/14] readme update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 04298cc..55089cd 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ const dependencies = [ 'package2', 'package3', ]; -const target = 'dependencies'; +const target = npmAdd.CONSTANTS.DEPENDENCIES; const overwrite = false; const packageFilePath = 'package.json'; new npmAdd(dependencies, target, overwrite, packageFilePath) From c8636cd7c60e87132ddd54793ed469be8b362c5b Mon Sep 17 00:00:00 2001 From: jack Date: Sat, 20 Jun 2020 00:52:12 +0800 Subject: [PATCH 11/14] fix to how i defined constants --- lib/AddDependencies.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/AddDependencies.js b/lib/AddDependencies.js index fa4fa8d..e377036 100644 --- a/lib/AddDependencies.js +++ b/lib/AddDependencies.js @@ -170,14 +170,15 @@ class AddDependencies { process.exit(1); }); } + + static get CONSTANTS() { + return { + DEPENDENCIES: 'dependencies', + DEV_DEPENDENCIES: 'devDependencies', + PEER_DEPENDENCIES: 'peerDependencies', + OPTIONAL_DEPENDENCIES: 'optionalDependencies', + }; + } } -module.exports = { - CONSTANTS: { - DEPENDENCIES: 'dependencies', - DEV_DEPENDENCIES: 'devDependencies', - PEER_DEPENDENCIES: 'peerDependencies', - OPTIONAL_DEPENDENCIES: 'optionalDependencies', - }, - default: AddDependencies -}; +module.exports = AddDependencies; From d077c7f89e4b4ad5ee3be2fd5ff6b37a68cacb13 Mon Sep 17 00:00:00 2001 From: jack Date: Sat, 20 Jun 2020 13:19:34 +0800 Subject: [PATCH 12/14] * added editorconfig * added eslintrc * added packagejson changes to support linting * QOL changes :) --- .editorconfig | 5 +++++ .eslintrc.js | 24 ++++++++++++++++++++++++ package.json | 8 ++++++++ 3 files changed, 37 insertions(+) create mode 100644 .editorconfig create mode 100644 .eslintrc.js 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/package.json b/package.json index 6fa9dc1..b68ba97 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", @@ -28,5 +29,12 @@ "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" } } From 2e95ed86b878c785ca0ffa2fa7ff867f548b7bbc Mon Sep 17 00:00:00 2001 From: jack Date: Sat, 20 Jun 2020 13:19:56 +0800 Subject: [PATCH 13/14] * added a change to attempt to see if we can use require now --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b68ba97..5de0b67 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "license": "MIT", "preferGlobal": true, "bin": { - "add-dependencies": "index.js" + "add-dependencies": "cli-index.js" }, "dependencies": { "npm-run": "^5.0.1", From eb051d0a59d55a6965de9f4d92ba4cb19d857e00 Mon Sep 17 00:00:00 2001 From: jack Date: Sat, 20 Jun 2020 13:20:35 +0800 Subject: [PATCH 14/14] and the file change itself --- cli-index.js | 17 +++++++++++++++++ index.js | 11 +---------- 2 files changed, 18 insertions(+), 10 deletions(-) create mode 100755 cli-index.js 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');