Skip to content

Commit 00e8b32

Browse files
committed
PR Comments
1 parent beffd1d commit 00e8b32

File tree

7 files changed

+72
-46
lines changed

7 files changed

+72
-46
lines changed

lib/definitions/plugins.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ interface IPluginsService {
33
remove(pluginName: string): IFuture<void>; // removes plugin only by name
44
prepare(pluginData: IPluginData): IFuture<void>;
55
getAllInstalledPlugins(): IFuture<IPluginData[]>;
6+
ensureAllDependenciesAreInstalled(): IFuture<void>;
67
}
78

89
interface IPluginData extends INodeModuleData {

lib/services/platform-service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export class PlatformService implements IPlatformService {
180180
this.processPlatformSpecificFiles(platform, files).wait();
181181

182182
// Process node_modules folder
183+
this.$pluginsService.ensureAllDependenciesAreInstalled().wait();
183184
var tnsModulesDestinationPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, PlatformService.TNS_MODULES_FOLDER_NAME);
184185
this.$broccoliBuilder.prepareNodeModules(tnsModulesDestinationPath, this.$projectData.projectDir).wait();
185186

@@ -318,7 +319,7 @@ export class PlatformService implements IPlatformService {
318319
}
319320

320321
private isPlatformInstalled(platform: string): IFuture<boolean> {
321-
return this.$fs.exists(path.join(this.$projectData.platformsDir, platform));
322+
return this.$fs.exists(path.join(this.$projectData.platformsDir, platform.toLowerCase()));
322323
}
323324

324325
private isValidPlatform(platform: string) {

lib/services/plugins-service.ts

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ export class PluginsService implements IPluginsService {
2323

2424
public add(plugin: string): IFuture<void> {
2525
return (() => {
26+
let dependencies = this.getAllInstalledModules().wait();
2627
let pluginName = this.executeNpmCommand(PluginsService.INSTALL_COMMAND_NAME, plugin).wait();
27-
let nodeModuleData = this.getNodeModuleData(pluginName);
28-
if(!nodeModuleData.isPlugin) {
28+
let nodeModuleData = this.getNodeModuleData(pluginName).wait();
29+
if(nodeModuleData && !nodeModuleData.isPlugin) {
2930
// We should remove already downloaded plugin and show an error message
30-
this.executeNpmCommand(PluginsService.UNINSTALL_COMMAND_NAME, pluginName).wait();
31+
let pluginNameLowerCase = pluginName.toLowerCase();
32+
if(!_.any(dependencies, dependency => dependency.name.toLowerCase() === pluginNameLowerCase)) {
33+
this.executeNpmCommand(PluginsService.UNINSTALL_COMMAND_NAME, pluginName).wait();
34+
}
3135
this.$errors.failWithoutHelp(`${plugin} is not a valid NativeScript plugin. Verify that the plugin package.json file contains a nativescript key and try again.`);
3236
}
3337

@@ -93,21 +97,27 @@ export class PluginsService implements IPluginsService {
9397
}).future<void>()();
9498
}
9599

100+
public ensureAllDependenciesAreInstalled(): IFuture<void> {
101+
return this.$childProcess.exec("npm install ");
102+
}
103+
96104
public getAllInstalledPlugins(): IFuture<IPluginData[]> {
97105
return (() => {
98-
let nodeModules = this.$fs.readDirectory(this.nodeModulesPath).wait();
99-
let plugins: IPluginData[] = [];
100-
_.each(nodeModules, nodeModuleName => {
101-
let nodeModuleData = this.getNodeModuleData(nodeModuleName);
102-
if(nodeModuleData.isPlugin) {
103-
plugins.push(this.convertToPluginData(nodeModuleData));
104-
}
105-
});
106-
107-
return plugins;
106+
let nodeModules = this.getAllInstalledModules().wait();
107+
return _.filter(nodeModules, nodeModuleData => nodeModuleData && nodeModuleData.isPlugin);
108108
}).future<IPluginData[]>()();
109109
}
110110

111+
private getAllInstalledModules(): IFuture<INodeModuleData[]> {
112+
return (() => {
113+
this.ensureAllDependenciesAreInstalled().wait();
114+
this.$fs.ensureDirectoryExists(this.nodeModulesPath).wait();
115+
116+
let nodeModules = this.$fs.readDirectory(this.nodeModulesPath).wait();
117+
return _.map(nodeModules, nodeModuleName => this.getNodeModuleData(nodeModuleName).wait());
118+
}).future<INodeModuleData[]>()();
119+
}
120+
111121
private executeNpmCommand(npmCommandName: string, npmCommandArguments: string): IFuture<string> {
112122
return (() => {
113123
let command = this.composeNpmCommand(npmCommandName, npmCommandArguments);
@@ -117,7 +127,7 @@ export class PluginsService implements IPluginsService {
117127
}
118128

119129
private composeNpmCommand(npmCommandName: string, npmCommandArguments: string): string {
120-
let command = ` npm ${npmCommandName} ${npmCommandArguments} --save `;
130+
let command = ` npm ${npmCommandName} "${npmCommandArguments}" --save `;
121131
if(this.$options.production) {
122132
command += " --production ";
123133
}
@@ -141,19 +151,23 @@ export class PluginsService implements IPluginsService {
141151
});
142152
}
143153

144-
private getNodeModuleData(moduleName: string): INodeModuleData {
145-
let fullNodeModulePath = path.join(this.nodeModulesPath, moduleName);
146-
let packageJsonFilePath = path.join(fullNodeModulePath, "package.json");
147-
let data = require(packageJsonFilePath);
148-
let result = {
149-
name: data.name,
150-
version: data.version,
151-
fullPath: fullNodeModulePath,
152-
isPlugin: data.nativescript !== undefined,
153-
moduleInfo: data.nativescript
154-
};
155-
156-
return result;
154+
private getNodeModuleData(moduleName: string): IFuture<INodeModuleData> {
155+
return (() => {
156+
let fullNodeModulePath = path.join(this.nodeModulesPath, moduleName);
157+
let packageJsonFilePath = path.join(fullNodeModulePath, "package.json");
158+
if(this.$fs.exists(packageJsonFilePath).wait()) {
159+
let data = require(packageJsonFilePath);
160+
return {
161+
name: data.name,
162+
version: data.version,
163+
fullPath: fullNodeModulePath,
164+
isPlugin: data.nativescript !== undefined,
165+
moduleInfo: data.nativescript
166+
};
167+
}
168+
169+
return null;
170+
}).future<INodeModuleData>()();
157171
}
158172

159173
private convertToPluginData(nodeModuleData: INodeModuleData): IPluginData {
@@ -178,7 +192,7 @@ export class PluginsService implements IPluginsService {
178192
return frameworkData.version;
179193
}).future<string>()();
180194
}
181-
195+
182196
private mergeXml(xml1: string, xml2: string): IFuture<string> {
183197
let future = new Future<string>();
184198

lib/tools/broccoli/builder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ export class Builder implements IBroccoliBuilder {
5151
if(err.file) {
5252
this.$logger.error("File: " + err.file);
5353
}
54-
if(err.statck) {
54+
if(err.stack) {
5555
this.$logger.error(err.stack);
5656
}
57-
future.throw(err.toString());
57+
future.throw(err);
5858
});
5959

6060
return future;

lib/tools/broccoli/node-modules-dest-copy.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ export class DestCopy implements IBroccoliPlugin {
4545
let currentDependencyMajorVersion = semver.major(currentDependency.version);
4646
let addedDependencyMajorVersion = semver.major(addedDependency.version);
4747

48-
let message = util.format("Old version %s, new version %s. Old location %s, new location %s.", addedDependency.version, currentDependency.version, addedDependency.directory, currentDependency.directory);
49-
currentDependencyMajorVersion === addedDependencyMajorVersion ? console.log(message) : console.log(message);
48+
let message = `The depedency located at ${addedDependency.directory} with version ${addedDependency.version} will be replaced with dependency located at ${currentDependency.directory} with version ${currentDependency.version}`;
49+
let logger = $injector.resolve("$logger");
50+
currentDependencyMajorVersion === addedDependencyMajorVersion ? logger.out(message) : logger.warn(message);
5051

5152
dependencies[currentDependency.name] = currentDependency;
5253
}
@@ -73,18 +74,19 @@ export class DestCopy implements IBroccoliPlugin {
7374
}
7475

7576
private getDependencies(): IDictionary<any> {
76-
let result = Object.create(null);
77-
try {
78-
let dirs = fs.readdirSync(this.outputRoot);
79-
_.each(dirs, dir => {
80-
let fileContent = require(path.join(dir, constants.PACKAGE_JSON_FILE_NAME));
81-
result[fileContent.name] = fileContent;
82-
});
83-
} catch(err) {
84-
result = Object.create(null);
85-
}
86-
87-
return result;
77+
let result = Object.create(null);
78+
if(fs.existsSync(this.outputRoot)) {
79+
let dirs = fs.readdirSync(this.outputRoot);
80+
_.each(dirs, dir => {
81+
let filePath = path.join(dir, constants.PACKAGE_JSON_FILE_NAME);
82+
if(fs.existsSync(filePath)) {
83+
let fileContent = require(filePath);
84+
result[fileContent.name] = fileContent;
85+
}
86+
});
87+
}
88+
89+
return result;
8890
}
8991

9092
private getDevDependencies(projectDir: string): IDictionary<any> {

test/platform-service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,18 @@ function createTestInjector() {
4949
testInjector.register("hostInfo", hostInfoLib.HostInfo);
5050
testInjector.register("staticConfig", StaticConfigLib.StaticConfig);
5151
testInjector.register("broccoliBuilder", {
52-
prepareNodeModulesFolder: () => {}
52+
prepareNodeModules: () => {
53+
return (() => { }).future<void>()();
54+
}
5355
});
5456
testInjector.register("pluginsService", {
5557
getAllInstalledPlugins: () => {
5658
return (() => {
5759
return <any>[];
5860
}).future<IPluginData[]>()();
61+
},
62+
ensureAllDependenciesAreInstalled: () => {
63+
return (() => { }).future<void>()();
5964
}
6065
});
6166

test/plugins-service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ function addPluginWhenExpectingToFail(testInjector: IInjector, plugin: string, e
111111
}];
112112
}).future<IPluginData[]>()();
113113
}
114+
pluginsService.ensureAllDependenciesAreInstalled = () => {
115+
return (() => { }).future<void>()();
116+
}
114117

115118
mockBeginCommand(testInjector, "Exception: " + expectedErrorMessage);
116119

0 commit comments

Comments
 (0)