Skip to content

Commit 54e668e

Browse files
FatmeFatme
Fatme
authored and
Fatme
committed
Merge pull request #634 from NativeScript/fatme/fix-prepare
Fix prepare to work correctly if you have npm module and tns plugin in same project
2 parents 01478b7 + d1e261d commit 54e668e

File tree

8 files changed

+28
-30
lines changed

8 files changed

+28
-30
lines changed

lib/declarations.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ interface INodePackageManager {
33
load(config?: any): IFuture<void>;
44
install(packageName: string, pathToSave: string, config?: any): IFuture<any>;
55
uninstall(packageName: string, config?: any): IFuture<any>;
6-
cache(packageName: string, version: string, cache?: any): IFuture<ICacheData>;
6+
cache(packageName: string, version: string, cache?: any): IFuture<IDependencyData>;
77
cacheUnpack(packageName: string, version: string, unpackTarget?: string): IFuture<void>;
88
view(packageName: string, propertyName: string): IFuture<any>;
99
}
@@ -22,12 +22,12 @@ interface INpmInstallOptions {
2222
version?: string;
2323
}
2424

25-
interface ICacheData {
25+
interface IDependencyData {
2626
name: string;
2727
version: string;
28-
dependencies: IStringDictionary;
29-
devDependencies: IStringDictionary;
30-
nativescript?: any;
28+
nativescript: any;
29+
dependencies?: IStringDictionary;
30+
devDependencies?: IStringDictionary;
3131
}
3232

3333
interface IStaticConfig extends Config.IStaticConfig { }

lib/definitions/plugins.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
interface IPluginsService {
22
add(plugin: string): IFuture<void>; // adds plugin by name, github url, local path and et.
33
remove(pluginName: string): IFuture<void>; // removes plugin only by name
4-
prepare(pluginData: IPluginData): IFuture<void>;
4+
prepare(pluginData: IDependencyData): IFuture<void>;
55
getAllInstalledPlugins(): IFuture<IPluginData[]>;
66
ensureAllDependenciesAreInstalled(): IFuture<void>;
77
}

lib/node-package-manager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class NodePackageManager implements INodePackageManager {
3535
return this.loadAndExecute("uninstall", [[packageName]], { config: config });
3636
}
3737

38-
public cache(packageName: string, version: string, config?: any): IFuture<ICacheData> {
38+
public cache(packageName: string, version: string, config?: any): IFuture<IDependencyData> {
3939
// function cache (pkg, ver, where, scrub, cb)
4040
return this.loadAndExecute("cache", [packageName, version, undefined, false], { subCommandName: "add", config: config });
4141
}

lib/services/platform-service.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,6 @@ export class PlatformService implements IPlatformService {
8888
this.$fs.deleteDirectory(platformPath).wait();
8989
throw err;
9090
}
91-
92-
// Prepare installed plugins
93-
let installedPlugins = this.$pluginsService.getAllInstalledPlugins().wait();
94-
_.each(installedPlugins, pluginData => this.$pluginsService.prepare(pluginData).wait());
9591

9692
this.$logger.out("Project successfully created.");
9793

@@ -179,15 +175,15 @@ export class PlatformService implements IPlatformService {
179175

180176
platformData.platformProjectService.prepareProject().wait();
181177

182-
// Process platform specific files
183-
let directoryPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
184-
let excludedDirs = [constants.APP_RESOURCES_FOLDER_NAME];
185-
this.$projectFilesManager.processPlatformSpecificFiles(directoryPath, platform, excludedDirs).wait();
186-
187178
// Process node_modules folder
188179
this.$pluginsService.ensureAllDependenciesAreInstalled().wait();
189180
var tnsModulesDestinationPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, PlatformService.TNS_MODULES_FOLDER_NAME);
190181
this.$broccoliBuilder.prepareNodeModules(tnsModulesDestinationPath, this.$projectData.projectDir, platform, lastModifiedTime).wait();
182+
183+
// Process platform specific files
184+
let directoryPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
185+
let excludedDirs = [constants.APP_RESOURCES_FOLDER_NAME];
186+
this.$projectFilesManager.processPlatformSpecificFiles(directoryPath, platform, excludedDirs).wait();
191187

192188
this.$logger.out("Project successfully prepared");
193189
}).future<void>()();

lib/services/plugins-service.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ export class PluginsService implements IPluginsService {
2929
public add(plugin: string): IFuture<void> {
3030
return (() => {
3131
let dependencies = this.getAllInstalledModules().wait();
32-
let cacheData = this.$npm.cache(plugin, undefined, PluginsService.NPM_CONFIG).wait();
33-
if(cacheData.nativescript) {
32+
let dependencyData = this.$npm.cache(plugin, undefined, PluginsService.NPM_CONFIG).wait();
33+
if(dependencyData.nativescript) {
3434
let pluginName = this.executeNpmCommand(PluginsService.INSTALL_COMMAND_NAME, plugin).wait();
35-
this.prepare(this.convertToPluginData(cacheData)).wait();
36-
this.$logger.out(`Successfully installed plugin ${cacheData.name}.`);
35+
this.prepare(dependencyData).wait();
36+
this.$logger.out(`Successfully installed plugin ${dependencyData.name}.`);
3737
} else {
3838
this.$errors.failWithoutHelp(`${plugin} is not a valid NativeScript plugin. Verify that the plugin package.json file contains a nativescript key and try again.`);
3939
}
@@ -68,8 +68,10 @@ export class PluginsService implements IPluginsService {
6868
}).future<void>()();
6969
}
7070

71-
public prepare(pluginData: IPluginData): IFuture<void> {
71+
public prepare(dependencyData: IDependencyData): IFuture<void> {
7272
return (() => {
73+
let pluginData = this.convertToPluginData(dependencyData);
74+
7375
let action = (pluginDestinationPath: string, platform: string, platformData: IPlatformData) => {
7476
return (() => {
7577
// Process .js files
@@ -89,7 +91,7 @@ export class PluginsService implements IPluginsService {
8991
}
9092

9193
this.$fs.ensureDirectoryExists(pluginDestinationPath).wait();
92-
shelljs.cp("-R", pluginData.fullPath, pluginDestinationPath);
94+
shelljs.cp("-Rf", pluginData.fullPath, pluginDestinationPath);
9395

9496
let pluginPlatformsFolderPath = path.join(pluginDestinationPath, pluginData.name, "platforms", platform);
9597
let pluginConfigurationFilePath = path.join(pluginPlatformsFolderPath, platformData.configurationFileName);

lib/tools/broccoli/builder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class Builder implements IBroccoliBuilder {
6464
}
6565

6666
if(!lastModifiedTime || isNodeModulesModified) {
67-
let nodeModulesDirectories = this.$fs.readDirectory(nodeModulesPath).wait();
67+
let nodeModulesDirectories = this.$fs.exists(nodeModulesPath).wait() ? this.$fs.readDirectory(nodeModulesPath).wait() : [];
6868
_.each(nodeModulesDirectories, nodeModuleDirectoryName => {
6969
let nodeModuleFullPath = path.join(nodeModulesPath, nodeModuleDirectoryName);
7070
this.nodeModules[nodeModuleFullPath] = nodeModuleFullPath;

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export class DestCopy implements IBroccoliPlugin {
2323
private projectDir: string,
2424
private platform: string,
2525
private $fs: IFileSystem,
26-
private $projectFilesManager: IProjectFilesManager) {
26+
private $projectFilesManager: IProjectFilesManager,
27+
private $pluginsService: IPluginsService) {
2728
this.dependencies = Object.create(null);
2829
this.devDependencies = this.getDevDependencies(projectDir);
2930
}
@@ -40,13 +41,11 @@ export class DestCopy implements IBroccoliPlugin {
4041
let fileContent = require(packageJsonFilePath);
4142

4243
if(!this.devDependencies[fileContent.name]) { // Don't flatten dev dependencies
43-
let isPlugin = fileContent.nativescript;
44-
4544
let currentDependency = {
4645
name: fileContent.name,
4746
version: fileContent.version,
4847
directory: path.dirname(packageJsonFilePath),
49-
isPlugin: isPlugin
48+
nativescript: fileContent.nativescript
5049
};
5150

5251
let addedDependency = this.dependencies[currentDependency.name];
@@ -72,8 +71,9 @@ export class DestCopy implements IBroccoliPlugin {
7271
_.each(this.dependencies, dependency => {
7372
shelljs.cp("-Rf", dependency.directory, this.outputRoot);
7473
shelljs.rm("-rf", path.join(this.outputRoot, dependency.name, "node_modules"));
75-
if(dependency.isPlugin) {
76-
this.$projectFilesManager.processPlatformSpecificFiles(path.join(this.outputRoot, dependency.name), platform).wait();
74+
let isPlugin = !!dependency.nativescript;
75+
if(isPlugin) {
76+
this.$pluginsService.prepare(dependency).wait();
7777
shelljs.rm("-rf", path.join(this.outputRoot, dependency.name, "platforms"));
7878
}
7979
});

0 commit comments

Comments
 (0)