Skip to content

Commit

Permalink
feat(shrinkpack): decompress by default, provide --compress option
Browse files Browse the repository at this point in the history
closes #40
  • Loading branch information
JamieMason committed Aug 4, 2016
1 parent c0c19b0 commit 7b2f341
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 20 deletions.
20 changes: 13 additions & 7 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@ var directoryValue;

program
.version(version)
.option('-c, --compress', 'use compressed .tgz tarballs instead of .tar')
.arguments('[directory]')
.action(function (directory) {
directoryValue = directory;
directoryValue = directory ? path.resolve(directory) : process.cwd();
});

program.on('--help', onHelp);

program.parse(process.argv);

if (directoryValue) {
cli.run(path.resolve(directoryValue));
} else {
cli.run(process.cwd());
}
cli.run({
compress: program.compress === true,
directory: directoryValue
});

function onHelp() {
console.log(' Icons:');
Expand All @@ -42,6 +41,13 @@ function onHelp() {
logIcon(chalk.green, '✓', 'Resolved');
logIcon(chalk.grey, '12:34', 'Time Taken');
console.log('');
console.log(' Compression:');
console.log('');
console.log(' Although compressed .tgz files have lower filesizes, storing binary files in');
console.log(' Git can result in a gradual increase in the time it takes to push to your');
console.log(' repository. Shrinkpack uses uncompressed, plain text .tar files by default,');
console.log(' which are handled optimally by Git in the same way that .md, .js, and .css');
console.log(' files are for example.');

function logIcon(colour, icon, label) {
console.log(' ' + colour(icon) + ' ' + label);
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
"dependencies": {
"chalk": "1.1.3",
"commander": "2.9.0",
"glob": "7.0.5",
"graceful-fs": "4.1.5",
"lodash.uniq": "4.4.0",
"when": "3.7.7"
},
"devDependencies": {
Expand Down
12 changes: 8 additions & 4 deletions src/analyse/dependency/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Dependency.prototype = {
bundle: function () {
if (!this.isBundled()) {
return bundle(this).then(function (dependency) {
console.info(chalk.green('+ %s'), dependency.getId());
console.info(chalk.green('+ %s'), dependency.getBundleName());
return dependency;
});
}
Expand All @@ -45,14 +45,18 @@ Dependency.prototype = {
return when(this);
},
config: null,
getBundleName: function () {
var extension = this.config.options.compress ? '.tgz' : '.tar';
var name = this.name.replace(/\//g, '-');
var version = this.graph.version;
return name + '-' + version + extension;
},
getId: function () {
return this.name + '@' + this.graph.version;
},
getPathToBundle: function () {
var directory = this.config.path.shrinkpack;
var name = this.name.replace(/\//g, '-');
var version = this.graph.version;
return path.join(directory, name + '-' + version + '.tgz');
return path.join(directory, this.getBundleName());
},
getPathToNpmCache: function () {
return path.join(this.config.path.npmCache, this.name, this.graph.version, 'package.tgz');
Expand Down
6 changes: 3 additions & 3 deletions src/analyse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var readNpmCache = require('./read-npm-cache');
module.exports = init;

// implementation
function init(directory) {
return when({startTime: new Date()})
function init(options) {
return when({options: options, startTime: new Date()})
.then(getConfigWithPaths)
.then(getConfigWithGraph)
.then(ensureBundleExists)
Expand All @@ -28,7 +28,7 @@ function init(directory) {
.then(getConfigWithStats);

function getConfigWithPaths(config) {
return getPaths(directory)
return getPaths(config.options.directory)
.then(function (paths) {
return assign(config, {path: paths});
});
Expand Down
4 changes: 2 additions & 2 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module.exports = {
};

// implementation
function runCli(directory) {
return analyse(directory)
function runCli(options) {
return analyse(options)
.then(update, onFail)
.then(displaySummary, onFail)
.catch(onFail);
Expand Down
17 changes: 15 additions & 2 deletions src/lib/copy-file.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
// node modules
var zlib = require('zlib');

// 3rd party modules
var chalk = require('chalk');
var fs = require('graceful-fs');
var when = require('when');

// public
module.exports = copyFile;

// implementation
function copyFile(source, target) {
return new Promise(function (resolve, reject) {
return when.promise(function (resolve, reject) {
var read$ = fs.createReadStream(source);
var write$ = fs.createWriteStream(target);

read$.on('error', onReadError);
write$.on('error', onWriteError);
write$.on('finish', onWriteEnd);
read$.pipe(write$);

if (isCompressed()) {
read$.pipe(write$);
} else {
read$.pipe(zlib.createGunzip()).pipe(write$);
}

function onReadError(err) {
console.error(chalk.red('! failed to read file %s'), source);
Expand All @@ -29,5 +38,9 @@ function copyFile(source, target) {
function onWriteEnd() {
resolve();
}

function isCompressed() {
return target.search(/\.tar$/) === -1;
}
});
}

2 comments on commit 7b2f341

@DrewML
Copy link
Contributor

@DrewML DrewML commented on 7b2f341 Aug 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JamieMason Thanks for doing this! It looks like my team's npm install times saved an average of ~15 more seconds now that npm isn't decompressing the gzipped file on each npm i. We're down to about 1:15 install times on average (for a massive dep tree), and ~15 seconds of that is the postInstall time for some native deps building.

@JamieMason
Copy link
Owner Author

@JamieMason JamieMason commented on 7b2f341 Aug 10, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.