Skip to content

Commit

Permalink
Read packages from package.json, closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbrazier committed May 7, 2017
1 parent c263ec5 commit caf8a9e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,29 @@ You can also download multiple packages by passing in a list:
package-bundle request bluebird
```

If no packages are entered then it will check for a package.json file, and read in the dependencies.

## Usage

```
Usage: package-bundle|pb <packages...> [options]
Usage: package-bundle|pb [packages...] [options]
where <packages> are in the format: [@scope/]<pkg>[@<version>]
If no packages are provided it will check for a package.json
Create a bundle of packages including their dependencies in archive format
Options:
-h, --help output usage information
-V, --version output the version number
-d, --dev include dev dependencies
-o, --optional include optional dependencies
-d, --no-dev ignore dev dependencies in package.json
-o, --no-optional ignore optional dependencies in package.json
-D, --dev-recursive include all dev dependencies recursively
-O, --optional-recursive include all optional dependencies recursively
-f, --flat save in a flat file structure, instead of individual folders
-z, --no-archive leave dependencies in folder, and don't archive
-x, --no-cache don't use cache file to avoid repeat downloads
-O, --out-file <file> output file name
-F, --out-file <file> output file name
-a, --all-versions download all versions of specified packages
-A, --all-versions-recursive download all versions of specified packages and dependencies
-c, --concurrency <n> number of requests to make at the same time - default=50
Expand All @@ -62,6 +67,8 @@ Options:
* Cache previous downloads, so you only download dependencies once
* Download all package dependencies
* Maintains npm registry folder structure to upload to package manager
* Specify version of package
* Read packages from package.json


## Importing into Artifactory
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "package-bundle",
"version": "0.1.1",
"version": "0.2.0",
"description": "Download npm packages and dependencies as tgz's to then import into package manager or project",
"main": "lib/index.js",
"preferGlobal": true,
Expand Down
29 changes: 26 additions & 3 deletions src/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const writeFile = Promise.promisify(fs.writeFile);

const REGISTRY_URL = 'http://registry.npmjs.org';
const CACHE_FILE = 'package-bundle-cache.json';
const PACKAGE_JSON = 'package.json';

export default class Resolver extends Step {
static getMatchingVersion(pkg, versions, range) {
Expand Down Expand Up @@ -41,7 +42,7 @@ export default class Resolver extends Step {
this.packages = args.args;

if (!this.packages.length) {
args.help();
this.checkPackageJson();
}

if (args.cache) {
Expand All @@ -63,6 +64,28 @@ export default class Resolver extends Step {
.then(() => this.getResult());
}

checkPackageJson() {
try {
const packageFile = fs.readFileSync(PACKAGE_JSON);
const { dependencies, devDependencies, optionalDependencies } = JSON.parse(packageFile);

const combinedDependencies = Object.assign(
{},
dependencies,
this.args.dev && devDependencies,
this.args.optional && optionalDependencies
);

this.packages = Object.entries(combinedDependencies).map(([k, v]) => `${k}@${v}`);
} catch (err) {
if (err.code === 'ENOENT') {
this.args.help();
} else {
throw err;
}
}
}

getResult() {
this.complete(`Found ${this.downloads.size} package${this.downloads.size === 1 ? '' : 's'}`);
if (this.downloads.size === 0) {
Expand Down Expand Up @@ -143,8 +166,8 @@ export default class Resolver extends Step {
const combinedDependencies = Object.assign(
{},
dependencies,
this.args.dev && devDependencies,
this.args.optional && optionalDependencies
this.args.devRecursive && devDependencies,
this.args.optionalRecursive && optionalDependencies
);

const keys = Object.keys(combinedDependencies);
Expand Down
12 changes: 7 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ args._name = 'package-bundle'; // eslint-disable-line no-underscore-dangle

args
.version(require('../package').version)
.usage('<packages...> [options] \n where <packages> are in the format: ' +
'[@scope/]<pkg>[@<version>]')
.usage('[packages...] [options]\n where <packages> are in the format: ' +
'[@scope/]<pkg>[@<version>]\n If no packages are provided it will check for a package.json')
.alias('pb')
.description('Create a bundle of packages including their dependencies in archive format')
.option('-d, --dev', 'include dev dependencies')
.option('-o, --optional', 'include optional dependencies')
.option('-d, --no-dev', 'ignore dev dependencies in package.json')
.option('-o, --no-optional', 'ignore optional dependencies in package.json')
.option('-D, --dev-recursive', 'include all dev dependencies recursively')
.option('-O, --optional-recursive', 'include all optional dependencies recursively')
.option('-f, --flat', 'save in a flat file structure, instead of individual folders')
.option('-z, --no-archive', 'leave dependencies in folder, and don\'t archive')
.option('-x, --no-cache', 'don\'t use cache file to avoid repeat downloads')
.option('-O, --out-file <file>', 'output file name')
.option('-F, --out-file <file>', 'output file name')
.option('-a, --all-versions', 'download all versions of specified packages')
.option('-A, --all-versions-recursive', 'download all versions of specified packages and dependencies')
.option('-c, --concurrency <n>', 'number of requests to make at the same time - default=50', parseInt)
Expand Down

0 comments on commit caf8a9e

Please sign in to comment.