diff --git a/README.md b/README.md index fe23119..115ce0d 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,31 @@ 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'); +``` +OR + +```js +// Note: to use import you must have `"type": "module"` in your projects package.json +import npmAdd from 'add-dependencies'; +``` + +THEN + +```js +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 100644 index 0000000..c4b8d38 --- /dev/null +++ b/cli-index.js @@ -0,0 +1,15 @@ +#!/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.run().catch((error) => { + console.error('\x1b[31m%s\x1b[0m', error); + process.exit(1); +}); \ No newline at end of file diff --git a/index.js b/index.js index 1e8cf44..335aa32 100755 --- a/index.js +++ b/index.js @@ -1,11 +1,3 @@ #!/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'); \ No newline at end of file diff --git a/lib/AddDependencies.js b/lib/AddDependencies.js index 8b78554..5f41b22 100644 --- a/lib/AddDependencies.js +++ b/lib/AddDependencies.js @@ -3,12 +3,17 @@ 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() { @@ -59,7 +64,20 @@ class AddDependencies { return Promise.all(this.dependencies.map((dep) => this.runNpmShow(dep))); } + run() { + return this.addDependencies().then(() => this.saveToPackage()); + } + runNpmShow(dep) { + if (!dep) { + console.error( + '\x1b[31m%s\x1b[0m', + `No Dependency Provided to runNpmShow()` + ); + + return new Promise((resolve) => resolve()); + } + const depSplit = dep.split('@'); const [depName, depVersion] = dep.charAt(0) !== '@' ? depSplit : [`@${depSplit[1]}`, depSplit[2]]; @@ -158,6 +176,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..f6b4f05 100644 --- a/package.json +++ b/package.json @@ -23,10 +23,10 @@ "license": "MIT", "preferGlobal": true, "bin": { - "add-dependencies": "index.js" + "add-dependencies": "cli-index.js" }, "dependencies": { "npm-run": "^5.0.1", "semver": "^6.3.0" } -} +} \ No newline at end of file