Skip to content
Merged
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
24 changes: 19 additions & 5 deletions lib/Constants.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
var bower = require('bower'),
fs = require('fs');
fs = require('fs'),
path = require('path');

module.exports = {
DefaultProjectManifestPath : './adapt.json',
DefaultProjectFrameworkPath: './package.json',
ManifestFilename: 'adapt.json',
FrameworkFilename: 'package.json',
DefaultProjectManifestPath : './'+this.ManifestFilename,
DefaultProjectFrameworkPath: './'+this.FrameworkFilename,
DefaultCreateType : 'course',
DefaultTypeNames : {
'course':'my-adapt-course',
Expand All @@ -15,7 +18,9 @@ module.exports = {
ComponentRepositoryName : 'adapt-component',
DefaultBranch : process.env.ADAPT_BRANCH || 'master',
HomeDirectory : searchForHome(),
getRegistry:getRegistry
getRegistry:getRegistry,
setCwd: setCwd,
cwd: '.'
};

var registry = null;
Expand All @@ -40,7 +45,7 @@ function getRegistry() {
if (process.env.ADAPT_REGISTRY) {
registry = process.env.ADAPT_REGISTRY;

} else if (fs.existsSync('./.bowerrc')) {
} else if (fs.existsSync(path.join(this.cwd, './.bowerrc'))) {
// a manifest exists; let bower determine the registry
registry = undefined;

Expand All @@ -51,3 +56,12 @@ function getRegistry() {

return registry;
}

function setCwd(cwd) {
if (!cwd) return;

this.cwd = cwd;

this.DefaultProjectManifestPath = path.join(this.cwd, this.ManifestFilename);
this.DefaultProjectFrameworkPath = path.join(this.cwd, this.FrameworkFilename);
}
19 changes: 18 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,26 @@ function execute() {
});
}

function getApi() {
var commands = require('./commands');
var apiCommands = {};

_.each(commands, function(command, commandName) {
var prefix = 'api';
if (commandName.startsWith(prefix)) {
apiCommands[commandName.split(prefix)[1]] = command;
}
});

return {
commands:apiCommands
}
}

module.exports = {
command: null,
withOptions: withOptions,
withPackage: withPackage,
execute: execute
execute: execute,
api: getApi()
};
154 changes: 147 additions & 7 deletions lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ var path = require('path');
var prompt = require('prompt')
var Q = require('q');
var semver = require('semver');
var Errors = require('../errors');
var Constants = require('../Constants');
var JsonLoader = require('../JsonLoader');
var JsonWriter = require('../JsonWriter');
var PackageMeta = require('../PackageMeta');
var Plugin = require('../Plugin');
var Project = require('../Project');
Expand All @@ -38,8 +38,33 @@ module.exports = function (dependencies) {
var isCompatibleEnabled = false;
// whether adapt.json is being used to compile the list of plugins to install
var isUsingManifest = false;
// whether this command is being performed on the command line
var isInteractive = true;

return {
apiinstall: function(pluginName, cwd) {
isInteractive = false;

Constants.setCwd(cwd);

project = new Project(Constants.DefaultProjectManifestPath, Constants.DefaultProjectFrameworkPath);

if(!project.isProjectContainsManifestFile()) {
return Q.reject({error:Errors.ERROR_COURSE_DIR});
}

itinerary = {};
plugins = [];

return init(pluginName ? [pluginName] : [])
.then(createPlugins)
.then(getInitialInfo)
.then(findCompatibleVersions)
.then(checkConstraints)
.then(createInstallationManifest)
.then(performInstallation)
.then(summariseInstallation);
},
install: function(renderer) {
var args = [].slice.call(arguments, 1);
var done = args.pop() || function() {};
Expand Down Expand Up @@ -145,7 +170,11 @@ module.exports = function (dependencies) {
promises.push(plugins[i].getInitialInfo());
}

return Q.all(promises).progress(progressUpdate).then(conclude);
if (isInteractive) {
return Q.all(promises).progress(progressUpdate).then(conclude);
}

return Q.all(promises);

function progressUpdate() {
var settled = plugins.filter(function(plugin) {return plugin._rawInfo || plugin._isMissingAtRepo;}).length;
Expand All @@ -169,7 +198,11 @@ module.exports = function (dependencies) {
promises.push(present[i].findCompatibleVersion(project.getFrameworkVersion()));
}

return Q.all(promises).progress(progressUpdate).then(conclude);
if (isInteractive) {
return Q.all(promises).progress(progressUpdate).then(conclude);
}

return Q.all(promises);

function progressUpdate() {
var settled = present.filter(function(plugin) {return plugin._latestCompatibleVersion != undefined;}).length;
Expand All @@ -193,7 +226,11 @@ module.exports = function (dependencies) {
promises.push(present[i].checkConstraint(project.getFrameworkVersion()));
}

return Q.all(promises).progress(progressUpdate).then(conclude);
if (isInteractive) {
return Q.all(promises).progress(progressUpdate).then(conclude);
}

return Q.all(promises);

function progressUpdate() {
var settled = present.filter(function(plugin) {return plugin._constraintChecked != undefined;}).length;
Expand Down Expand Up @@ -234,6 +271,37 @@ module.exports = function (dependencies) {
// a compatible version exists but user has requested a valid version that is later than the latest compatible version (prompt for (r)equested, (l)atest compatible or (s)kip)
var compatibleWithUnmetConstraint = present.filter(isCompatibleWithUnmetConstraint);

if (!isInteractive) {

incompatibleWithOldConstraint.forEach(function(p) {
p._error = Errors.ERROR_INCOMPATIBLE_VALID_REQUEST;
});
incompatibleWithLatestConstraint.forEach(function(p) {
p._error = Errors.ERROR_INCOMPATIBLE_VALID_REQUEST;
});
incompatibleWithBadConstraint.forEach(function(p) {
p._error = Errors.ERROR_INCOMPATIBLE_BAD_REQUEST;
});
incompatibleWithNoConstraint.forEach(function(p) {
p._error = Errors.ERROR_INCOMPATIBLE;
});
compatibleWithOldIncompatibleConstraint.forEach(function(p) {
p._error = Errors.ERROR_COMPATIBLE_INC_REQUEST;
});
compatibleWithBadConstraint.forEach(function(p) {
p._error = Errors.ERROR_COMPATIBLE_BAD_REQUEST;
});
compatibleWithUnmetConstraint.forEach(function(p) {
p._error = Errors.ERROR_COMPATIBLE_INC_REQUEST;
});

compatibleWithOldCompatibleConstraint.forEach(function(p) {
p.markRequestedForInstallation();
});

return Q.resolve();
}

var allPromises = [];

add(incompatibleWithOldConstraint, 'There is no compatible version of the following plugins:', getPrompt_incompatibleGeneric);
Expand Down Expand Up @@ -270,6 +338,10 @@ module.exports = function (dependencies) {
console.log(obj.header);
return promise.serialise(obj.list, obj.prompt);
}

function getPackageName(p) {
return p.packageName;
}
}

function updateManifest() {
Expand Down Expand Up @@ -477,6 +549,69 @@ module.exports = function (dependencies) {
var noSuccess = 'None of the requested plugins could be installed';
var successMsg;

if (!isInteractive) {
var report = [];

if (plugins.length == 1) {
var p = plugins[0];

if (installSucceeded.length == 1) {
var bowerPath = path.join(Constants.cwd, 'src', p._belongsTo, p.packageName, 'bower.json');
return Q.resolve(JsonLoader.readJSONSync(bowerPath));
}
if (installSkipped.length == 1) {
return Q.reject(p._error);
}
if (installErrored.length == 1) {
var error = _.clone(Errors.ERROR_INSTALL_ERROR);

if (p._installError) error.message = p._installError;

return Q.reject(error);
}
return Q.reject(Errors.ERROR_NOT_FOUND);
}

installSucceeded.forEach(function(p) {
var bowerPath = path.join(Constants.cwd, 'src', p._belongsTo, p.packageName, 'bower.json');
report.push({
name:p.packageName,
status:'fulfilled',
pluginData:JsonLoader.readJSONSync(bowerPath)
});
});

installSkipped.forEach(function(p) {
report.push({
name:p.packageName,
status:'rejected',
reason:p._error
});
});

installErrored.forEach(function(p) {
var error = _.clone(Errors.ERROR_INSTALL_ERROR);

if (p._installError) error.message = p._installError;

report.push({
name:p.packageName,
status:'rejected',
reason:error
});
});

missing.forEach(function(p) {
report.push({
name:p.packageName,
status:'rejected',
reason:Errors.ERROR_NOT_FOUND
});
});

return Q.resolve(report);
}

if (installErrored.length == 0 && missing.length == 0) successMsg = allSuccess;
else if (installSucceeded.length == 0) successMsg = noSuccess;
else successMsg = someSuccess;
Expand Down Expand Up @@ -676,7 +811,11 @@ module.exports = function (dependencies) {
}

function performInstallation() {
return Q.all(plugins.filter(isToBeInstalled).map(createInstallationTask)).progress(progressUpdate).then(conclude);
if (isInteractive) {
return Q.all(plugins.filter(isToBeInstalled).map(createInstallationTask)).progress(progressUpdate).then(conclude);
}

return Q.all(plugins.filter(isToBeInstalled).map(createInstallationTask));

function progressUpdate() {
var list = plugins.filter(isPresent).filter(isToBeInstalled);
Expand All @@ -692,7 +831,7 @@ module.exports = function (dependencies) {
}

function createInstallationTask(plugin) {
return PackageMeta.getKeywords(plugin, { registry: Constants.getRegistry() }).then(doInstall).then(conclude);
return PackageMeta.getKeywords(plugin, { registry: Constants.getRegistry(), cwd: Constants.cwd }).then(doInstall).then(conclude);

function doInstall(keywords) {
var resolver = new PluginTypeResolver(),
Expand All @@ -703,7 +842,8 @@ module.exports = function (dependencies) {

return install(plugin, {
directory: path.join('src', pluginType.belongsTo),
registry: Constants.getRegistry()
registry: Constants.getRegistry(),
cwd: Constants.cwd
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/install/InstallTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var InstallTarget = extend(Plugin, {
try {
var versionString = this._versions ? '#'+this._versions[this._versionIndex] : '';
this._bowerCmdCount++;
bower.commands.info(this.packageName+versionString, null, {registry:Constants.getRegistry()}).on('end', _.bind(onSuccess, this)).on('error', _.bind(onFail, this));
bower.commands.info(this.packageName+versionString, null, {registry:Constants.getRegistry(), cwd: Constants.cwd}).on('end', _.bind(onSuccess, this)).on('error', _.bind(onFail, this));
} catch(err) {
onFail.call(this, err);
}
Expand Down
Loading