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

Features/cache packages (Install from cache if offline) #87

Open
wants to merge 8 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
2 changes: 1 addition & 1 deletion .jshintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
coverage/**
node_modules/**
templates/**/**__name__*.js
templates/**/__name__/**/*.js
templates/**/__name__/**/*.js
4 changes: 2 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"node": true,
"bitwise": true,
"camelcase": true,
"curly": true,
Expand All @@ -9,11 +8,12 @@
"noempty": true,
"nonew": true,
"undef": true,
"unused": false,
"unused": "vars",
"trailing": true,
"indent": 4,
"boss": true,
"eqnull": true,
"node": true,
"browser": true,
"globals": {
"require": false,
Expand Down
3 changes: 1 addition & 2 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ var create = module.exports = function(templateName, config, templateModule) {
});

};
exports.create = create;
Copy link

Choose a reason for hiding this comment

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

Either this needs to be reinstated or line 82 needs to be updated to use create instead of exports.create

exports.create(filename, command, templateModule)


exports.addCommandsTo = create.addCommandsTo = function(program) {
var templatesDir = path.join(program.minitHome, "templates");
Expand All @@ -84,7 +83,7 @@ exports.addCommandsTo = create.addCommandsTo = function(program) {
.fail(function(error) {
var message = error.message;
if (error instanceof MinitError) {
console.error(error.message);
console.error(message);
} else {
throw error;
}
Expand Down
52 changes: 52 additions & 0 deletions lib/npm-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var Q = require("q");
var npm = require("npm");

// Because npm is a singleton there can actually only be one npm class active
// at one moment. It's checked and set to true in the NpmWrapper constructor
// and set to false in NpmWrapper#close.
var isInUse = false;

module.exports = NpmWrapper;
function NpmWrapper(config) {
if (isInUse) {
throw new Error("Someone else is using npm. Call #close() to release it.");
}
isInUse = true;
this.closed = false;

config = config || {};

var self = this;
this.loaded = Q.ninvoke(npm, "load", config)
.then(function () {
// npm is a singleton within a process; loading with a
// new config does not appear to update the configuration
// in particular, the prefix from the original configuration
// is always used npm.config.set and other approaches
// do not end up with a change to the necessary npm.dir
// or npm.globalDir.
// Changing npm.prefix directly here does work, though
// if the configuration differed in other ways those might
// need to be manually set directly on npm as well
if (config.prefix) {
npm.prefix = config.prefix;
}

return self;
});

}

NpmWrapper.prototype.install = function() {
return this.loaded.then(function (self) {
if (self.closed) {
return self.closed;
}
return Q.ninvoke(npm.commands, "install");
});
};

NpmWrapper.prototype.close = function() {
isInUse = false;
this.closed = Q.reject(new Error("This npm wrapper is closed"));
};
19 changes: 9 additions & 10 deletions lib/template-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ exports.TemplateBase = Object.create(Object.prototype, {
}));
});
});
}
}
},

_finish: {
Expand Down Expand Up @@ -233,15 +233,13 @@ exports.TemplateBase = Object.create(Object.prototype, {

rename: {
value: function(filename) {
var newName = filename,
options = this.options;
for (var name in options) {
if (options.hasOwnProperty(name)) {
newName = newName.replace(
FILENAME_VARIABLE_START + name + FILENAME_VARIABLE_END,
options[name]
);
}
var newName = filename;
//jshint -W089
for (var name in this.options) {
newName = newName.replace(
FILENAME_VARIABLE_START + name + FILENAME_VARIABLE_END,
this.options[name]
);
}
if(newName === filename) {
return Q.resolve(filename);
Expand All @@ -252,6 +250,7 @@ exports.TemplateBase = Object.create(Object.prototype, {
}).done();
return done.promise;
}
//jshint +W089
}
}

Expand Down
35 changes: 21 additions & 14 deletions run-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ var junitreport = {
useDotNotation: false
};

process.argv.forEach(function(arg){
switch(arg) {
case '--color': showColors = true; break;
case '--noColor': showColors = false; break;
case '--verbose': isVerbose = true; break;
case '--junit': junitreport.report = true; break;
}
process.argv.forEach(function(arg) {
switch (arg) {
case '--color':
showColors = true;
break;
case '--noColor':
showColors = false;
break;
case '--verbose':
isVerbose = true;
break;
case '--junit':
junitreport.report = true;
break;
}
});

/**
Expand Down Expand Up @@ -55,13 +63,12 @@ jasmine.Block.prototype.execute = function (onComplete) {

jasmine.executeSpecsInFolder({
"specFolders": [__dirname+ "/test"],
"onComplete": function(runner/*, log*/){
if (runner.results().failedCount === 0) {
process.exit(0);
}
else {
process.exit(1);
}
"onComplete": function(runner, log){
if (runner.results().failedCount === 0) {
process.exit(0);
} else {
process.exit(1);
}
},
"isVerbose": isVerbose,
"showColors": showColors,
Expand Down
2 changes: 0 additions & 2 deletions templates/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,4 @@ exports.Template = Object.create(PackageTemplate, {
return process.cwd();
}
}


});
1 change: 0 additions & 1 deletion templates/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,4 @@ exports.Template = Object.create(ModuleTemplate, {
});
}
}

});
2 changes: 1 addition & 1 deletion templates/digit.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ exports.Template = Object.create(PackageTemplate, {
},

defaultPackageHome: {
value: function () {
value: function (value) {
return process.cwd();
}
}
Expand Down
77 changes: 57 additions & 20 deletions templates/package.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var TemplateBase = require("../lib/template-base.js").TemplateBase;
var TemplateBase = require("../lib/template-base").TemplateBase;
var path = require('path');
var fs = require('fs');
var npm = require("npm");
var NpmWrapper = require("../lib/npm-wrapper");
var Q = require('q');
var http = require("http");

exports.Template = Object.create(TemplateBase, {

Expand Down Expand Up @@ -42,6 +43,9 @@ exports.Template = Object.create(TemplateBase, {
production : true,
loglevel: "warn"
};
if (self.options.npmCache) {
config.cache = self.options.npmCache;
}
return self.installDependencies(config);
}).then(function() {
console.log("#");
Expand All @@ -53,32 +57,65 @@ exports.Template = Object.create(TemplateBase, {
}
},

installDependencies: {
value: function (config) {
return Q.ninvoke(npm, "load", (config || null))
.then(function () {
/**
* Performs a HEAD request on `registry.npmjs.org` to check if the user
* and registry are online and available.
* @function
* @returns {Promise.<boolean>} `true` if the npm registry can be reached,
* `false` otherwise
*/
_isOnline: {
value: function () {
var isOnline = Q.defer();

// npm is a singleton within a process; loading with a
// new config does not appear to update the configuration
// in particular, the prefix from the original configuration
// is always used npm.config.set and other approaches
// do not end up with a change to the necessary npm.dir
// or npm.globalDir.
// Changing npm.prefix directly here does work, though
// if the configuration differed in other ways those might
// need to be manually set directly on npm as well
var req = http.request({
hostname: "registry.npmjs.org",
method: "HEAD"
}, function (res) {
// Listen to the data event so that the response stream can
// start, but we actually don't care about the response
res.on("data", function () {});

if (config.prefix) {
npm.prefix = config.prefix;
}
if (res.statusCode === 200) {
isOnline.resolve(true);
} else {
isOnline.resolve(false);
}
});
req.on("error", function (error) {
isOnline.resolve(false);
});
req.end();

return Q.ninvoke(npm.commands, "install");
return isOnline.promise;
}
},

installDependencies: {
value: function (config) {
return this._isOnline()
.then(function (isOnline) {
var npm;
if (isOnline) {
npm = new NpmWrapper(config);
} else {
// If we're not online then set the registry to null which
// prevents any contact with the outside world
config = Object.create(config);
config.registry = null;
npm = new NpmWrapper(config);
}

return npm.install()
.finally(function () {
return npm.close();
});
});
}
},

defaultPackageHome: {
value: function () {
value: function (value) {
return process.cwd();
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/cli-spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global xdescribe,describe,beforeEach,it,expect,spyOn,xit */
/*global describe,beforeEach,it,expect,spyOn,xit */
var SandboxedModule = require('sandboxed-module');

describe("minit", function () {
Expand All @@ -16,7 +16,7 @@ describe("minit", function () {
mainSandbox = {
requires: {'./lib/create': mockCreate}
};
});
});

it("should add creation commands", function () {
var addCommandsToSpy = spyOn(mockCreate, "addCommandsTo");
Expand Down
1 change: 0 additions & 1 deletion test/lib/create-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,5 @@ describe("create", function () {
expect(results.resultPath).toEqual("destination/path/my-component.bar.ext");
});
});

});
});
Loading