Skip to content

Commit 11604a7

Browse files
committed
Amended promises and use of chdir
1 parent 0f020fe commit 11604a7

4 files changed

Lines changed: 144 additions & 51 deletions

File tree

lib/Constants.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var bower = require('bower'),
2-
fs = require('fs');
2+
fs = require('fs'),
3+
path = require('path');
34

45
module.exports = {
56
DefaultProjectManifestPath : './adapt.json',
@@ -15,7 +16,9 @@ module.exports = {
1516
ComponentRepositoryName : 'adapt-component',
1617
DefaultBranch : process.env.ADAPT_BRANCH || 'master',
1718
HomeDirectory : searchForHome(),
18-
getRegistry:getRegistry
19+
getRegistry:getRegistry,
20+
setCwd: setCwd,
21+
cwd: '.'
1922
};
2023

2124
var registry = null;
@@ -40,7 +43,7 @@ function getRegistry() {
4043
if (process.env.ADAPT_REGISTRY) {
4144
registry = process.env.ADAPT_REGISTRY;
4245

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

@@ -51,3 +54,17 @@ function getRegistry() {
5154

5255
return registry;
5356
}
57+
58+
function setCwd(cwd) {
59+
if (!cwd) return;
60+
61+
console.log('setting cwd to', cwd);
62+
63+
this.cwd = cwd;
64+
65+
this.DefaultProjectManifestPath = path.join(this.cwd, this.DefaultProjectManifestPath);
66+
this.DefaultProjectFrameworkPath = path.join(this.cwd, this.DefaultProjectFrameworkPath);
67+
68+
console.log(this.DefaultProjectManifestPath);
69+
console.log(this.DefaultProjectFrameworkPath);
70+
}

lib/commands/install.js

Lines changed: 72 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ var semver = require('semver');
1212
var Errors = require('../errors');
1313
var Constants = require('../Constants');
1414
var JsonLoader = require('../JsonLoader');
15-
var JsonWriter = require('../JsonWriter');
1615
var PackageMeta = require('../PackageMeta');
1716
var Plugin = require('../Plugin');
1817
var Project = require('../Project');
@@ -46,7 +45,7 @@ module.exports = function (dependencies) {
4645
apiinstall: function(pluginName, cwd) {
4746
isInteractive = false;
4847

49-
if (cwd) process.chdir(cwd);
48+
Constants.setCwd(cwd);
5049

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

@@ -57,7 +56,7 @@ module.exports = function (dependencies) {
5756
itinerary = {};
5857
plugins = [];
5958

60-
return init([pluginName])
59+
return init(pluginName ? [pluginName] : [])
6160
.then(createPlugins)
6261
.then(getInitialInfo)
6362
.then(findCompatibleVersions)
@@ -273,33 +272,28 @@ module.exports = function (dependencies) {
273272
var compatibleWithUnmetConstraint = present.filter(isCompatibleWithUnmetConstraint);
274273

275274
if (!isInteractive) {
276-
var errors = [];
277275

278-
if (incompatibleWithOldConstraint.length > 0) {
279-
errors.push({error:Errors.ERROR_INCOMPATIBLE_VALID_REQUEST, context:incompatibleWithOldConstraint.map(getPackageName)});
280-
}
281-
if (incompatibleWithLatestConstraint.length > 0) {
282-
errors.push({error:Errors.ERROR_INCOMPATIBLE_VALID_REQUEST, context:incompatibleWithLatestConstraint.map(getPackageName)});
283-
}
284-
if (incompatibleWithBadConstraint.length > 0) {
285-
errors.push({error:Errors.ERROR_INCOMPATIBLE_BAD_REQUEST, context:incompatibleWithBadConstraint.map(getPackageName)});
286-
}
287-
if (incompatibleWithNoConstraint.length > 0) {
288-
errors.push({error:Errors.ERROR_INCOMPATIBLE, context:incompatibleWithNoConstraint.map(getPackageName)});
289-
}
290-
if (compatibleWithOldIncompatibleConstraint.length > 0) {
291-
errors.push({error:Errors.ERROR_COMPATIBLE_INC_REQUEST, context:compatibleWithOldIncompatibleConstraint.map(getPackageName)});
292-
}
293-
if (compatibleWithBadConstraint.length > 0) {
294-
errors.push({error:Errors.ERROR_COMPATIBLE_BAD_REQUEST, context:compatibleWithBadConstraint.map(getPackageName)});
295-
}
296-
if (compatibleWithUnmetConstraint.length > 0) {
297-
errors.push({error:Errors.ERROR_COMPATIBLE_INC_REQUEST, context:compatibleWithUnmetConstraint.map(getPackageName)});
298-
}
299-
300-
if (errors.length > 0) {
301-
return Q.reject(errors);
302-
}
276+
incompatibleWithOldConstraint.forEach(function(p) {
277+
p._error = Errors.ERROR_INCOMPATIBLE_VALID_REQUEST;
278+
});
279+
incompatibleWithLatestConstraint.forEach(function(p) {
280+
p._error = Errors.ERROR_INCOMPATIBLE_VALID_REQUEST;
281+
});
282+
incompatibleWithBadConstraint.forEach(function(p) {
283+
p._error = Errors.ERROR_INCOMPATIBLE_BAD_REQUEST;
284+
});
285+
incompatibleWithNoConstraint.forEach(function(p) {
286+
p._error = Errors.ERROR_INCOMPATIBLE;
287+
});
288+
compatibleWithOldIncompatibleConstraint.forEach(function(p) {
289+
p._error = Errors.ERROR_COMPATIBLE_INC_REQUEST;
290+
});
291+
compatibleWithBadConstraint.forEach(function(p) {
292+
p._error = Errors.ERROR_COMPATIBLE_BAD_REQUEST;
293+
});
294+
compatibleWithUnmetConstraint.forEach(function(p) {
295+
p._error = Errors.ERROR_COMPATIBLE_INC_REQUEST;
296+
});
303297

304298
compatibleWithOldCompatibleConstraint.forEach(function(p) {
305299
p.markRequestedForInstallation();
@@ -556,25 +550,63 @@ module.exports = function (dependencies) {
556550
var successMsg;
557551

558552
if (!isInteractive) {
559-
var report = {
560-
success:{},
561-
errored:{},
562-
missing:[]
553+
var report = [];
554+
555+
if (plugins.length == 1) {
556+
var p = plugins[0];
557+
558+
if (installSucceeded.length == 1) {
559+
var bowerPath = path.join(Constants.cwd, 'src', p._belongsTo, p.packageName, 'bower.json');
560+
return Q.resolve(JsonLoader.readJSONSync(bowerPath));
561+
}
562+
if (installSkipped.length == 1) {
563+
return Q.reject(p._error);
564+
}
565+
if (installErrored.length == 1) {
566+
var error = _.clone(Errors.ERROR_INSTALL_ERROR);
567+
568+
if (p._installError) error.msg = p._installError;
569+
570+
return Q.reject(error);
571+
}
572+
return Q.reject(Errors.ERROR_NOT_FOUND);
563573
}
564574

565575
installSucceeded.forEach(function(p) {
566-
var bowerPath = path.join('src', p._belongsTo, p.packageName, 'bower.json');
567-
report.success[p.packageName] = JsonLoader.readJSONSync(bowerPath);
576+
var bowerPath = path.join(Constants.cwd, 'src', p._belongsTo, p.packageName, 'bower.json');
577+
report.push({
578+
name:p.packageName,
579+
status:'fulfilled',
580+
pluginData:JsonLoader.readJSONSync(bowerPath)
581+
});
582+
});
583+
584+
installSkipped.forEach(function(p) {
585+
report.push({
586+
name:p.packageName,
587+
status:'rejected',
588+
reason:p._error
589+
});
568590
});
569591

570592
installErrored.forEach(function(p) {
571-
report.errored[p.packageName] = {
572-
errorMessage: p._installError ? p._installError : 'unknown error'
573-
};
593+
var error = _.clone(Errors.ERROR_INSTALL_ERROR);
594+
595+
if (p._installError) error.msg = p._installError;
596+
597+
report.push({
598+
name:p.packageName,
599+
status:'rejected',
600+
reason:error
601+
});
574602
});
575603

576604
missing.forEach(function(p) {
577-
report.missing.push(p.packageName);
605+
report.push({
606+
name:p.packageName,
607+
status:'rejected',
608+
reason:Errors.ERROR_NOT_FOUND
609+
});
578610
});
579611

580612
return Q.resolve(report);
@@ -809,7 +841,7 @@ module.exports = function (dependencies) {
809841
plugin._belongsTo = pluginType.belongsTo;
810842

811843
return install(plugin, {
812-
directory: path.join('src', pluginType.belongsTo),
844+
directory: path.join(Constants.cwd, 'src', pluginType.belongsTo),
813845
registry: Constants.getRegistry()
814846
});
815847
}

lib/commands/update.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module.exports = function() {
5353
apiupdate: function(pluginName, cwd) {
5454
isInteractive = false;
5555

56-
if (cwd) process.chdir(cwd);
56+
Constants.setCwd(cwd);
5757

5858
clean();
5959

@@ -144,7 +144,7 @@ module.exports = function() {
144144
var theme = discoverNamedGroup('theme');
145145

146146
function discoverNamedGroup(group) {
147-
var srcpath = path.join('src', group);
147+
var srcpath = path.join(Constants.cwd, 'src', group);
148148

149149
if (!fs.existsSync(srcpath)) return;
150150

@@ -520,7 +520,7 @@ module.exports = function() {
520520
plugins.filter(isPresent).forEach(function(plugin) {
521521
if (!plugin._wasUpdated) return;
522522

523-
var p = path.join('src', plugin._belongsTo, plugin.packageName, 'bower.json');
523+
var p = path.join(Constants.cwd, 'src', plugin._belongsTo, plugin.packageName, 'bower.json');
524524

525525
plugin._bowerInfo = JsonLoader.readJSONSync(p);
526526
plugin._updatedVersion = plugin._bowerInfo.version;
@@ -641,6 +641,30 @@ module.exports = function() {
641641
if (!isInteractive) {
642642
var report = [];
643643

644+
if (plugins.length == 1) {
645+
var p = plugins[0];
646+
647+
if (latest.length == 1 || updated.length == 1) {
648+
var bowerPath = path.join(Constants.cwd, 'src', p._belongsTo, p.packageName, 'bower.json');
649+
return Q.resolve(JsonLoader.readJSONSync(bowerPath));
650+
}
651+
if (errored.length == 1) {
652+
var error = _.clone(Errors.ERROR_UPDATE_ERROR);
653+
654+
if (p._updateError) error.msg = p._updateError;
655+
656+
return Q.reject(error);
657+
}
658+
if (incompatible.length == 1) {
659+
return Q.reject(Errors.ERROR_NO_UPDATE);
660+
}
661+
if (untagged.length == 1) {
662+
return Q.reject(Errors.ERROR_NO_RELEASES);
663+
}
664+
665+
return Q.reject(Errors.ERROR_NOT_FOUND);
666+
}
667+
644668
latest.forEach(function(p) {
645669
report.push({
646670
name: p.packageName,
@@ -660,11 +684,15 @@ module.exports = function() {
660684
// N.B. there will not be any incompatibleConstrained as this results in a rejected promise
661685

662686
errored.forEach(function(p) {
687+
var error = _.clone(Errors.ERROR_UPDATE_ERROR);
688+
689+
if (p._updateError) error.msg = p._updateError;
690+
663691
report.push({
664692
name: p.packageName,
665693
status:'rejected',
666694
pluginData: p._bowerInfo,
667-
reason: p._updateError
695+
reason: error
668696
});
669697
});
670698

@@ -673,7 +701,7 @@ module.exports = function() {
673701
name: p.packageName,
674702
status:'rejected',
675703
pluginData: p._bowerInfo,
676-
reason: 'no update available'
704+
reason: Errors.ERROR_NO_UPDATE
677705
});
678706
});
679707

@@ -682,7 +710,7 @@ module.exports = function() {
682710
name: p.packageName,
683711
status:'rejected',
684712
pluginData: p._bowerInfo,
685-
reason: 'no published releases'
713+
reason: Errors.ERROR_NO_RELEASES
686714
});
687715
});
688716

@@ -691,7 +719,7 @@ module.exports = function() {
691719
name: p.packageName,
692720
status:'rejected',
693721
pluginData: p._bowerInfo,
694-
reason: 'not registered'
722+
reason: Errors.ERROR_NOT_FOUND
695723
});
696724
});
697725

@@ -787,7 +815,7 @@ module.exports = function() {
787815
JsonWriter.writeJSONSync('bower.json', manifest);
788816
//console.log(JSON.stringify(JsonLoader.readJSONSync('bower.json'), null, 4));
789817
return update(plugin, null, {
790-
directory: path.join('src', plugin._belongsTo),
818+
directory: path.join(Constants.cwd, 'src', plugin._belongsTo),
791819
registry: Constants.getRegistry(),
792820
force:true
793821
})

lib/errors.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,21 @@ module.exports = {
3838
ERROR_UPDATE_INCOMPATIBLE: {
3939
code: 9,
4040
msg: "Incompatible update requested"
41+
},
42+
ERROR_INSTALL_ERROR: {
43+
code: 10,
44+
msg: "Unknown installation error"
45+
},
46+
ERROR_UPDATE_ERROR: {
47+
code: 11,
48+
msg: "Unknown update error"
49+
},
50+
ERROR_NO_RELEASES: {
51+
code: 12,
52+
msg: "No published releases"
53+
},
54+
ERROR_NO_UPDATE: {
55+
code: 13,
56+
msg: "No update available"
4157
}
4258
}

0 commit comments

Comments
 (0)