Skip to content

Commit

Permalink
Merge pull request #2 from marcelobern/master
Browse files Browse the repository at this point in the history
Added StagingWorkflow (e.g. for streamlined use in gulp/grunt)

Fixed issue #1.
  • Loading branch information
marcelobern authored Nov 12, 2018
2 parents c274191 + 1aaeaee commit 21166ee
Show file tree
Hide file tree
Showing 11 changed files with 547 additions and 30 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

### 0.1.0 (Next)

* [#1](https://github.com/botbits/bin-minify/issues/1): Fixed issue with trash (actually just a test issue with mock-fs) - [@marcelobern](https://github.com/marcelobern).
* Added staging workflow (e.g. to streamline use of `bin-minify` under gulp/grunt) and updated documentation - [@marcelobern](https://github.com/marcelobern).
* Test cases now using disposable temporary folders - [@marcelobern](https://github.com/marcelobern).
* Improved documentation readability - [@marcelobern](https://github.com/marcelobern).
79 changes: 78 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

## Stable Release

You are reading the documentation for the next release of bin-minify, which should be 0.1.0. Please see [CHANGELOG](CHANGELOG.md) and make sure to read [UPGRADING](UPGRADING.md) when upgrading from a previous version. The current stable release is [0.0.0](https://github.com/botbits/bin-minify/tree/v0.0.0).
You are reading the documentation for the stable release of bin-minify, 0.1.0. Please see [CHANGELOG](CHANGELOG.md) and make sure to read [UPGRADING](UPGRADING.md) when upgrading from a previous version.


## Overview
Expand All @@ -35,6 +35,30 @@ $ npm install --save bin-minify

## Usage

### Streamlined Staging

It is typically preferred to use the streamlined staging in conjunction with `gulp` or `grunt`.

The following `gulp` task will analyze all binaries under `./bin/my-bin`, save the resulting ***minPack*** to `./.bin-minify/my-bin.json`, and remove any redundant binaries from `./bin/my-bin`.

```js
const gulp = require('gulp');
const file = require('gulp-file');
const path = require('path');
const stagingWorkflow = require('bin-minify').stagingWorkflow;

gulp.task('default', async (done) => {
stagingWorkflow(path.resolve(__dirname, path.join('bin', 'my-bin'))).then(minPack => {
file('my-bin.json', JSON.stringify(minPack, null, ' '), {src: true})
.pipe(gulp.dest('.bin-minify'));
done();
}, error => {
console.error(`Could not create minPack: ${error}`);
done();
});
});
```

### Staging

Before using bin-minify the binaries (e.g. from a tar file or a build from source (maybe from [`bin-build`](https://www.npmjs.com/package/bin-build)) should be placed in the desired destination (typically `vendor` or `bin` folders).
Expand Down Expand Up @@ -290,6 +314,59 @@ Resolved Promise: `{ loaded: true or false }`. `loaded` will be:

Rejected Promise: `{ error }`.

## Promise stagingWorkflow (targetPath, minifyBinOptions)

### targetPath

- Type: `string`
- Default: `./bin/bin-minify`

Location of the actual binaries.

**Note**: Typically the binaries under `targetPath` are source controlled (and should be included in the npm module or Lambda package).

### minifyBinOptions

- Type: `Object`
- Optional

The following are supported keys in the `minifyBinOptions` JSON object. Any other keys are ignored.

#### dryRun

- Type: `boolean`
- Default: `false`

If `true`, will not remove redundant files under `targetPath`.

If `false`, redundant files under `targetPath` will be handled according to `minifyBinOptions.sendToTrash`.

#### strict

- Type: `boolean`
- Default: `false`

If `true`, will only remove redundant files under `targetPath` if no difference exists between original and reconstructed binaries.

If `false`, will only remove redundant files under `targetPath` if only difference between original and reconstructed binaries are empty folders.

#### sendToTrash

- Type: `boolean`
- Default: `false`

If `true`, all redundant files under `targetPath` will be removed by sending them to the trash.

If `false`, all redundant files under `targetPath` will be permanently removed.

### returns Promise

Resolved Promise: ***minPack*** JSON.

**Note**: It is recommended to store this ***minPack*** JSON to a source controlled file for future use.

Rejected Promise: `{ error }`.


## Performance

Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "2"
services:
node:
image: "node:10"
user: "node"
working_dir: /home/node/app
environment:
- NODE_ENV=production
volumes:
- ./:/home/node/app
expose:
- "8081"
command: "npm run test"
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
module.exports = {
RuntimeBin: require('./lib/RuntimeBin'),
StagingBin: require('./lib/StagingBin'),
stagingWorkflow: require('./lib/StagingWorkflow').stagingWorkflow,
};
80 changes: 80 additions & 0 deletions lib/StagingWorkflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* eslint-env mocha */
'use strict';

const async = require('async');
const path = require('path');
const tmp = require('tmp');

const StagingBin = require('./StagingBin');
const RuntimeBin = require('./RuntimeBin');

const BIN_PATH = 'bin-minify';

const onlyFolders = (diffTreeResult) => {
for (var entry of diffTreeResult) {
if (!['mkdir', 'rmdir'].includes(entry[0])) return false;
}
return true;
};

exports.stagingWorkflow = function (targetPath, options) {
return new Promise((resolve, reject) => {

targetPath = targetPath || path.resolve(__dirname, '..', 'bin', 'bin-minify');
options = undefined !== options ? options : {};
const sendToTrash = options.sendToTrash || false;
const dryRun = options.dryRun || false;
const strict = options.strict || false;

var stagingBin
, runtimeBin
, tmpFromBase;

async.waterfall([
(callback) => callback(null, new StagingBin({ targetPath })),
(newStagingBin, callback) => {
stagingBin = newStagingBin;
return callback(null, stagingBin.createMinPack());
},
(createMinPackResult, callback) => {
createMinPackResult.then(minPack => {
runtimeBin = new RuntimeBin({
targetPath,
minPack,
});

tmp.dir({ unsafeCleanup: true }, (err, tmpBasePath) => {
if (err) return callback(new Error(`Could not create temp dir: ${err}`));
else {
tmpFromBase = path.join(tmpBasePath, BIN_PATH);
return callback(null, runtimeBin.applyMinPack(tmpFromBase));
}
});
}, err => callback(new Error(`createMinPack() failed: ${err}`)));
},
(applyMinPackResult, callback) => {
applyMinPackResult.then(result => {
if (result.loaded) return callback(null, stagingBin.checkMinPack(tmpFromBase));
else return callback(new Error(`Folder ${tmpFromBase} already exists`));
}, err => callback(new Error(`applyMinPack() failed: ${err}`))
);
},
(checkMinPackResult, callback) => {
checkMinPackResult.then(result => {
if (result.length > 0 && (strict || !onlyFolders(result))) {
return callback(new Error(`Folders differ ${JSON.stringify(result, null, ' ')}`));
}
if (dryRun) return callback(null, new Promise(resolve => resolve({ delCount: 0 })));
else return callback(null, stagingBin.minifyBin(sendToTrash));
}, err => callback(new Error(`checkMinPack() failed: ${err}`))
);
},
], (err, minifyBinResult) => {
if (err) reject(err);
else minifyBinResult.then(
() => resolve(stagingBin.minPack),
err => reject(new Error(`minifyBin() failed: ${err}`))
);
});
});
};
Loading

0 comments on commit 21166ee

Please sign in to comment.