-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathplugins-service.ts
342 lines (287 loc) · 13.6 KB
/
plugins-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import * as path from "path";
import * as shelljs from "shelljs";
import * as semver from "semver";
import * as constants from "../constants";
export class PluginsService implements IPluginsService {
private static INSTALL_COMMAND_NAME = "install";
private static UNINSTALL_COMMAND_NAME = "uninstall";
private static NPM_CONFIG = {
save: true
};
private get $projectData(): IProjectData {
return this.$injector.resolve("projectData");
}
private get $platformsData(): IPlatformsData {
return this.$injector.resolve("platformsData");
}
private get $pluginVariablesService(): IPluginVariablesService {
return this.$injector.resolve("pluginVariablesService");
}
private get $projectDataService(): IProjectDataService {
return this.$injector.resolve("projectDataService");
}
private get $projectFilesManager(): IProjectFilesManager {
return this.$injector.resolve("projectFilesManager");
}
private get $broccoliBuilder(): IBroccoliBuilder {
return this.$injector.resolve("broccoliBuilder");
}
constructor(private $npm: INodePackageManager,
private $fs: IFileSystem,
private $childProcess: IChildProcess,
private $options: IOptions,
private $logger: ILogger,
private $errors: IErrors,
private $injector: IInjector) { }
public add(plugin: string): IFuture<void> {
return (() => {
this.ensure().wait();
let dependencyData = this.$npm.cache(plugin, undefined, PluginsService.NPM_CONFIG).wait();
if (dependencyData.nativescript) {
let pluginData = this.convertToPluginData(dependencyData);
// Validate
let action = (pluginDestinationPath: string, platform: string, platformData: IPlatformData) => {
return (() => {
this.isPluginDataValidForPlatform(pluginData, platform).wait();
}).future<void>()();
};
this.executeForAllInstalledPlatforms(action).wait();
try {
this.$pluginVariablesService.savePluginVariablesInProjectFile(pluginData).wait();
this.executeNpmCommand(PluginsService.INSTALL_COMMAND_NAME, plugin).wait();
} catch (err) {
// Revert package.json
this.$projectDataService.initialize(this.$projectData.projectDir);
this.$projectDataService.removeProperty(this.$pluginVariablesService.getPluginVariablePropertyName(pluginData.name)).wait();
this.$projectDataService.removeDependency(pluginData.name).wait();
throw err;
}
this.$logger.out(`Successfully installed plugin ${dependencyData.name}.`);
} else {
this.$errors.failWithoutHelp(`${plugin} is not a valid NativeScript plugin. Verify that the plugin package.json file contains a nativescript key and try again.`);
}
}).future<void>()();
}
public remove(pluginName: string): IFuture<void> {
return (() => {
let removePluginNativeCodeAction = (modulesDestinationPath: string, platform: string, platformData: IPlatformData) => {
return (() => {
let pluginData = this.convertToPluginData(this.getNodeModuleData(pluginName).wait());
platformData.platformProjectService.removePluginNativeCode(pluginData).wait();
}).future<void>()();
};
this.$pluginVariablesService.removePluginVariablesFromProjectFile(pluginName.toLowerCase()).wait();
this.executeForAllInstalledPlatforms(removePluginNativeCodeAction).wait();
this.executeNpmCommand(PluginsService.UNINSTALL_COMMAND_NAME, pluginName).wait();
let showMessage = true;
let action = (modulesDestinationPath: string, platform: string, platformData: IPlatformData) => {
return (() => {
shelljs.rm("-rf", path.join(modulesDestinationPath, pluginName));
this.$logger.out(`Successfully removed plugin ${pluginName} for ${platform}.`);
showMessage = false;
}).future<void>()();
};
this.executeForAllInstalledPlatforms(action).wait();
if (showMessage) {
this.$logger.out(`Succsessfully removed plugin ${pluginName}`);
}
}).future<void>()();
}
public getAvailable(filter: string[]): IFuture<IDictionary<any>> {
let silent: boolean = true;
return this.$npm.search(filter, silent);
}
public prepare(dependencyData: IDependencyData, platform: string): IFuture<void> {
return (() => {
platform = platform.toLowerCase();
let platformData = this.$platformsData.getPlatformData(platform);
let pluginDestinationPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, "tns_modules");
let pluginData = this.convertToPluginData(dependencyData);
let exists = this.$fs.exists(path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME)).wait();
if (!this.isPluginDataValidForPlatform(pluginData, platform).wait()) {
// Still clean up platform in case other platforms were supported.
shelljs.rm("-rf", path.join(pluginDestinationPath, pluginData.name, "platforms"));
return;
}
if (exists) {
//prepare platform speciffic files, .map and .ts files
this.$projectFilesManager.processPlatformSpecificFiles(pluginDestinationPath, platform).wait();
//deal with platforms/android folder in ns plugin
pluginData.pluginPlatformsFolderPath = (_platform: string) => path.join(pluginData.fullPath, "platforms", _platform);
platformData.platformProjectService.preparePluginNativeCode(pluginData).wait();
shelljs.rm("-rf", path.join(pluginDestinationPath, pluginData.name, "platforms"));
// Show message
this.$logger.out(`Successfully prepared plugin ${pluginData.name} for ${platform}.`);
}
}).future<void>()();
}
public ensureAllDependenciesAreInstalled(): IFuture<void> {
return (() => {
let installedDependencies = this.$fs.exists(this.nodeModulesPath).wait() ? this.$fs.readDirectory(this.nodeModulesPath).wait() : [];
// Scoped dependencies are not on the root of node_modules,
// so we have to list the contents of all directories, starting with @
// and add them to installed dependencies, so we can apply correct comparison against package.json's dependencies.
_(installedDependencies)
.filter(dependencyName => _.startsWith(dependencyName, "@"))
.each(scopedDependencyDir => {
let contents = this.$fs.readDirectory(path.join(this.nodeModulesPath, scopedDependencyDir)).wait();
installedDependencies = installedDependencies.concat(...contents.map(dependencyName => `${scopedDependencyDir}/${dependencyName}`));
});
let packageJsonContent = this.$fs.readJson(this.getPackageJsonFilePath()).wait();
let allDependencies = _.keys(packageJsonContent.dependencies).concat(_.keys(packageJsonContent.devDependencies));
let notInstalledDependencies = _.difference(allDependencies, installedDependencies);
if (this.$options.force || notInstalledDependencies.length) {
this.$logger.trace("Npm install will be called from CLI. Force option is: ", this.$options.force, " Not installed dependencies are: ", notInstalledDependencies);
this.$npm.install(this.$projectData.projectDir, this.$projectData.projectDir, { "ignore-scripts": this.$options.ignoreScripts }).wait();
}
}).future<void>()();
}
public getAllInstalledPlugins(): IFuture<IPluginData[]> {
return (() => {
let nodeModules = this.getAllInstalledModules().wait().map(nodeModuleData => this.convertToPluginData(nodeModuleData));
return _.filter(nodeModules, nodeModuleData => nodeModuleData && nodeModuleData.isPlugin);
}).future<IPluginData[]>()();
}
public afterPrepareAllPlugins(): IFuture<void> {
let action = (pluginDestinationPath: string, platform: string, platformData: IPlatformData) => {
return platformData.platformProjectService.afterPrepareAllPlugins();
};
return this.executeForAllInstalledPlatforms(action);
}
public beforePrepareAllPlugins(): IFuture<void> {
let action = (pluginDestinationPath: string, platform: string, platformData: IPlatformData) => {
return platformData.platformProjectService.beforePrepareAllPlugins();
};
return this.executeForAllInstalledPlatforms(action);
}
public getDependenciesFromPackageJson(): IFuture<IPackageJsonDepedenciesResult> {
return (() => {
let packageJson = this.$fs.readJson(this.getPackageJsonFilePath()).wait();
let dependencies: IBasePluginData[] = this.getBasicPluginInformation(packageJson.dependencies);
let devDependencies: IBasePluginData[] = this.getBasicPluginInformation(packageJson.devDependencies);
return {
dependencies,
devDependencies
};
}).future<IPackageJsonDepedenciesResult>()();
}
private getBasicPluginInformation(dependencies: any): IBasePluginData[] {
return _.map(dependencies, (version: string, key: string) => ({
name: key,
version: version
}));
}
private get nodeModulesPath(): string {
return path.join(this.$projectData.projectDir, "node_modules");
}
private getPackageJsonFilePath(): string {
return path.join(this.$projectData.projectDir, "package.json");
}
private getPackageJsonFilePathForModule(moduleName: string): string {
return path.join(this.nodeModulesPath, moduleName, "package.json");
}
private getDependencies(): string[] {
let packageJsonFilePath = this.getPackageJsonFilePath();
return _.keys(require(packageJsonFilePath).dependencies);
}
private getNodeModuleData(module: string): IFuture<INodeModuleData> { // module can be modulePath or moduleName
return (() => {
if (!this.$fs.exists(module).wait() || path.basename(module) !== "package.json") {
module = this.getPackageJsonFilePathForModule(module);
}
let data = this.$fs.readJson(module).wait();
return {
name: data.name,
version: data.version,
fullPath: path.dirname(module),
isPlugin: data.nativescript !== undefined,
moduleInfo: data.nativescript
};
}).future<INodeModuleData>()();
}
private convertToPluginData(cacheData: any): IPluginData {
let pluginData: any = {};
pluginData.name = cacheData.name;
pluginData.version = cacheData.version;
pluginData.fullPath = cacheData.directory || path.dirname(this.getPackageJsonFilePathForModule(cacheData.name));
pluginData.isPlugin = !!cacheData.nativescript || !!cacheData.moduleInfo;
pluginData.pluginPlatformsFolderPath = (platform: string) => path.join(pluginData.fullPath, "platforms", platform);
let data = cacheData.nativescript || cacheData.moduleInfo;
if (pluginData.isPlugin) {
pluginData.platformsData = data.platforms;
pluginData.pluginVariables = data.variables;
}
return pluginData;
}
private ensure(): IFuture<void> {
return (() => {
this.ensureAllDependenciesAreInstalled().wait();
this.$fs.ensureDirectoryExists(this.nodeModulesPath).wait();
}).future<void>()();
}
private getAllInstalledModules(): IFuture<INodeModuleData[]> {
return (() => {
this.ensure().wait();
let nodeModules = this.getDependencies();
return _.map(nodeModules, nodeModuleName => this.getNodeModuleData(nodeModuleName).wait());
}).future<INodeModuleData[]>()();
}
private executeNpmCommand(npmCommandName: string, npmCommandArguments: string): IFuture<string> {
return (() => {
let result = "";
if (npmCommandName === PluginsService.INSTALL_COMMAND_NAME) {
result = this.$npm.install(npmCommandArguments, this.$projectData.projectDir, PluginsService.NPM_CONFIG).wait();
} else if (npmCommandName === PluginsService.UNINSTALL_COMMAND_NAME) {
result = this.$npm.uninstall(npmCommandArguments, PluginsService.NPM_CONFIG, this.$projectData.projectDir).wait();
if (!result || !result.length) {
// indicates something's wrong with the data in package.json, for example version of the plugin that we are trying to remove is invalid.
return npmCommandArguments.toLowerCase();
}
}
return this.parseNpmCommandResult(result);
}).future<string>()();
}
private parseNpmCommandResult(npmCommandResult: string): string { // [[name@version, node_modules/name]]
return npmCommandResult[0][0].split("@")[0]; // returns plugin name
}
private executeForAllInstalledPlatforms(action: (_pluginDestinationPath: string, pl: string, _platformData: IPlatformData) => IFuture<void>): IFuture<void> {
return (() => {
let availablePlatforms = _.keys(this.$platformsData.availablePlatforms);
_.each(availablePlatforms, platform => {
let isPlatformInstalled = this.$fs.exists(path.join(this.$projectData.platformsDir, platform.toLowerCase())).wait();
if (isPlatformInstalled) {
let platformData = this.$platformsData.getPlatformData(platform.toLowerCase());
let pluginDestinationPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, "tns_modules");
action(pluginDestinationPath, platform.toLowerCase(), platformData).wait();
}
});
}).future<void>()();
}
private getInstalledFrameworkVersion(platform: string): IFuture<string> {
return (() => {
let platformData = this.$platformsData.getPlatformData(platform);
this.$projectDataService.initialize(this.$projectData.projectDir);
let frameworkData = this.$projectDataService.getValue(platformData.frameworkPackageName).wait();
return frameworkData.version;
}).future<string>()();
}
private isPluginDataValidForPlatform(pluginData: IPluginData, platform: string): IFuture<boolean> {
return (() => {
let isValid = true;
let installedFrameworkVersion = this.getInstalledFrameworkVersion(platform).wait();
let pluginPlatformsData = pluginData.platformsData;
if (pluginPlatformsData) {
let pluginVersion = (<any>pluginPlatformsData)[platform];
if (!pluginVersion) {
this.$logger.warn(`${pluginData.name} is not supported for ${platform}.`);
isValid = false;
} else if (semver.gt(pluginVersion, installedFrameworkVersion)) {
this.$logger.warn(`${pluginData.name} ${pluginVersion} for ${platform} is not compatible with the currently installed framework version ${installedFrameworkVersion}.`);
isValid = false;
}
}
return isValid;
}).future<boolean>()();
}
}
$injector.register("pluginsService", PluginsService);