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

Add yarn #104

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
npm-cache
=========

`npm-cache` is a command line utility that caches dependencies installed via `npm`, `bower`, `jspm` and `composer`.
`npm-cache` is a command line utility that caches dependencies installed via `npm`, `bower`, `jspm`, `composer` and `yarn`.

It is useful for build processes that run `[npm|bower|composer|jspm] install` every time as part of their
It is useful for build processes that run `[npm|bower|composer|jspm|yarn] install` every time as part of their
build process. Since dependencies don't change often, this often means slower build times. `npm-cache`
helps alleviate this problem by caching previously installed dependencies on the build machine.
`npm-cache` can be a drop-in replacement for any build script that runs `[npm|bower|composer|jspm] install`.

## How it Works
When you run `npm-cache install [npm|bower|jspm|composer]`, it first looks for `package.json`, `bower.json`,
When you run `npm-cache install [npm|bower|jspm|composer|yarn]`, it first looks for `package.json`, `bower.json`,
or `composer.json` in the current working directory depending on which dependency manager is requested.
It then calculates the MD5 hash of the configuration file and looks for a filed named
<MD5 of config.json>.tar.gz in the cache directory ($HOME/.package_cache by default). If the file does not
Expand Down
51 changes: 51 additions & 0 deletions cacheDependencyManagers/yarnConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

var path = require('path');
var shell = require('shelljs');
var fs = require('fs');
var md5 = require('md5');
var logger = require('../util/logger');
var isUsingYarnLock = null;


// Returns path to configuration file for yarn. Uses
// yarn.lock
var getYarnConfigPath = function () {
var yarnLockPath = path.resolve(process.cwd(), 'yarn.lock');
var packageJsonPath = path.resolve(process.cwd(), 'package.json');

if (isUsingYarnLock === null) {
if (fs.existsSync(yarnLockPath)) {
logger.logInfo('[yarn] using yarn.lock instead of package.json');
isUsingYarnLock = true;
} else {
isUsingYarnLock = false;
}
}

return isUsingYarnLock ? yarnLockPath : packageJsonPath;
};

function getFileHash(filePath) {
if (isUsingYarnLock) {
var yarnlockfile = fs.readFileSync(filePath);
return md5(yarnlockfile);
} else {
var json = JSON.parse(fs.readFileSync(filePath));
return md5(JSON.stringify({
dependencies: json.dependencies,
devDependencies: json.devDependencies
}));
}
}

module.exports = {
cliName: 'yarn',
getCliVersion: function getYarnVersion () {
return shell.exec('yarn --version', {silent: true}).output.trim();
},
configPath: getYarnConfigPath(),
installDirectory: 'node_modules',
installCommand: 'yarn',
getFileHash: getFileHash
};
3 changes: 2 additions & 1 deletion util/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports.logError = function (errorMessage) {
};

exports.logInfo = function (message) {
console.log('[npm-cache] [INFO] ' + message);
var date = new Date();
console.log('[npm-cache] [INFO] ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds() + ' ' + message);
};