Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for non cli #21

Closed
wants to merge 15 commits into from
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[*]
indent_size = 2
indent_style = space
max_line_length = 120
tab_width = 2
24 changes: 24 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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
},
};
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $ npm install add-dependencies [-g]

### Usage

Run:
Run (for `nodejs` see Example):

```sh
$ add-dependencies [package_file] <dependencies> [target] [--no-overwrite]
Expand Down Expand Up @@ -46,3 +46,20 @@ or with `npx`:
```sh
$ npx add-dependencies /home/user/project/package.json [email protected] [email protected] 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'));
```
17 changes: 17 additions & 0 deletions cli-index.js
Original file line number Diff line number Diff line change
@@ -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);
});
11 changes: 1 addition & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -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');
33 changes: 27 additions & 6 deletions lib/AddDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}