Skip to content

Commit 573da91

Browse files
author
Fatme
authored
Merge pull request #4516 from NativeScript/fatme/remove-prepareJS
chore: remove the code related to js prepare as webpack overrides prepareJS method with own logic
2 parents 34f4e45 + 2f8a11c commit 573da91

9 files changed

+535
-937
lines changed

lib/definitions/platform.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ interface INodeModulesBuilderData {
283283

284284
interface INodeModulesBuilder {
285285
prepareNodeModules(opts: INodeModulesBuilderData): Promise<void>;
286-
prepareJSNodeModules(opts: INodeModulesBuilderData): Promise<void>;
287286
}
288287

289288
interface INodeModulesDependenciesBuilder {

lib/definitions/plugins.d.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
interface IPluginsService {
22
add(plugin: string, projectData: IProjectData): Promise<void>; // adds plugin by name, github url, local path and et.
33
remove(pluginName: string, projectData: IProjectData): Promise<void>; // removes plugin only by name
4-
prepare(pluginData: IDependencyData, platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): Promise<void>;
54
getAllInstalledPlugins(projectData: IProjectData): Promise<IPluginData[]>;
65
ensureAllDependenciesAreInstalled(projectData: IProjectData): Promise<void>;
7-
preparePluginScripts(pluginData: IPluginData, platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): void
86

97
/**
108
* Returns all dependencies and devDependencies from pacakge.json file.

lib/services/plugins-service.ts

-35
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ export class PluginsService implements IPluginsService {
1818
private get $projectDataService(): IProjectDataService {
1919
return this.$injector.resolve("projectDataService");
2020
}
21-
private get $projectFilesManager(): IProjectFilesManager {
22-
return this.$injector.resolve("projectFilesManager");
23-
}
2421

2522
private get npmInstallOptions(): INodePackageManagerInstallOptions {
2623
return _.merge({
@@ -108,38 +105,6 @@ export class PluginsService implements IPluginsService {
108105
return await platformData.platformProjectService.validatePlugins(projectData);
109106
}
110107

111-
public async prepare(dependencyData: IDependencyData, platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): Promise<void> {
112-
platform = platform.toLowerCase();
113-
const platformData = this.$platformsData.getPlatformData(platform, projectData);
114-
const pluginData = this.convertToPluginData(dependencyData, projectData.projectDir);
115-
116-
const appFolderExists = this.$fs.exists(path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME));
117-
if (appFolderExists) {
118-
this.preparePluginScripts(pluginData, platform, projectData, projectFilesConfig);
119-
await this.preparePluginNativeCode(pluginData, platform, projectData);
120-
121-
// Show message
122-
this.$logger.out(`Successfully prepared plugin ${pluginData.name} for ${platform}.`);
123-
}
124-
}
125-
126-
public preparePluginScripts(pluginData: IPluginData, platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): void {
127-
const platformData = this.$platformsData.getPlatformData(platform, projectData);
128-
const pluginScriptsDestinationPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, "tns_modules");
129-
const scriptsDestinationExists = this.$fs.exists(pluginScriptsDestinationPath);
130-
if (!scriptsDestinationExists) {
131-
//tns_modules/<plugin> doesn't exist. Assuming we're running a bundled prepare.
132-
return;
133-
}
134-
135-
if (!this.isPluginDataValidForPlatform(pluginData, platform, projectData)) {
136-
return;
137-
}
138-
139-
//prepare platform speciffic files, .map and .ts files
140-
this.$projectFilesManager.processPlatformSpecificFiles(pluginScriptsDestinationPath, platform, projectFilesConfig);
141-
}
142-
143108
public async preparePluginNativeCode(pluginData: IPluginData, platform: string, projectData: IProjectData): Promise<void> {
144109
const platformData = this.$platformsData.getPlatformData(platform, projectData);
145110
pluginData.pluginPlatformsFolderPath = (_platform: string) => path.join(pluginData.fullPath, "platforms", _platform.toLowerCase());
+1-52
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as constants from "../constants";
22
import * as path from "path";
3-
import * as shell from "shelljs";
43
import * as temp from "temp";
54
import { hook } from "../common/helpers";
65
import { PreparePlatformService } from "./prepare-platform-service";
@@ -16,7 +15,6 @@ export class PreparePlatformJSService extends PreparePlatformService implements
1615
private $errors: IErrors,
1716
private $logger: ILogger,
1817
private $projectDataService: IProjectDataService,
19-
private $nodeModulesBuilder: INodeModulesBuilder,
2018
private $packageManager: INodePackageManager) {
2119
super($fs, $hooksService, $xmlValidator);
2220
}
@@ -36,23 +34,7 @@ export class PreparePlatformJSService extends PreparePlatformService implements
3634
@performanceLog()
3735
@hook('prepareJSApp')
3836
public async preparePlatform(config: IPreparePlatformJSInfo): Promise<void> {
39-
if (!config.changesInfo || config.changesInfo.appFilesChanged || config.changesInfo.changesRequirePrepare) {
40-
await this.copyAppFiles(config);
41-
this.copyAppResourcesFiles(config);
42-
}
43-
44-
if (config.changesInfo && !config.changesInfo.changesRequirePrepare) {
45-
// remove the App_Resources folder from the app/assets as here we're applying other files changes.
46-
const appDestinationDirectoryPath = path.join(config.platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
47-
const appResourcesDirectoryPath = path.join(appDestinationDirectoryPath, path.basename(config.projectData.appResourcesDirectoryPath));
48-
if (this.$fs.exists(appResourcesDirectoryPath)) {
49-
this.$fs.deleteDirectory(appResourcesDirectoryPath);
50-
}
51-
}
52-
53-
if (!config.changesInfo || config.changesInfo.modulesChanged) {
54-
await this.copyTnsModules(config.platform, config.platformData, config.projectData, config.appFilesUpdaterOptions, config.projectFilesConfig);
55-
}
37+
// intentionally left blank, keep the support for before-prepareJSApp and after-prepareJSApp hooks
5638
}
5739

5840
private async getPathToPlatformTemplate(selectedTemplate: string, frameworkPackageName: string, projectDir: string): Promise<{ selectedTemplate: string, pathToTemplate: string }> {
@@ -82,39 +64,6 @@ export class PreparePlatformJSService extends PreparePlatformService implements
8264

8365
return null;
8466
}
85-
86-
private async copyTnsModules(platform: string, platformData: IPlatformData, projectData: IProjectData, appFilesUpdaterOptions: IAppFilesUpdaterOptions, projectFilesConfig?: IProjectFilesConfig): Promise<void> {
87-
const appDestinationDirectoryPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
88-
const lastModifiedTime = this.$fs.exists(appDestinationDirectoryPath) ? this.$fs.getFsStats(appDestinationDirectoryPath).mtime : null;
89-
90-
try {
91-
const absoluteOutputPath = path.join(appDestinationDirectoryPath, constants.TNS_MODULES_FOLDER_NAME);
92-
// Process node_modules folder
93-
await this.$nodeModulesBuilder.prepareJSNodeModules({
94-
nodeModulesData: {
95-
absoluteOutputPath,
96-
platform,
97-
lastModifiedTime,
98-
projectData,
99-
appFilesUpdaterOptions,
100-
projectFilesConfig
101-
},
102-
release: appFilesUpdaterOptions.release,
103-
copyNodeModules: true
104-
});
105-
} catch (error) {
106-
this.$logger.debug(error);
107-
shell.rm("-rf", appDestinationDirectoryPath);
108-
this.$errors.failWithoutHelp(`Processing node_modules failed. ${error}`);
109-
}
110-
}
111-
112-
private copyAppResourcesFiles(config: IPreparePlatformJSInfo): void {
113-
const appDestinationDirectoryPath = path.join(config.platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
114-
const appResourcesSourcePath = config.projectData.appResourcesDirectoryPath;
115-
116-
shell.cp("-Rf", appResourcesSourcePath, path.join(appDestinationDirectoryPath, constants.APP_RESOURCES_FOLDER_NAME));
117-
}
11867
}
11968

12069
$injector.register("preparePlatformJSService", PreparePlatformJSService);

lib/tools/node-modules/node-modules-builder.ts

+4-36
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,15 @@
1-
import { TnsModulesCopy, NpmPluginPrepare } from "./node-modules-dest-copy";
1+
import { NpmPluginPrepare } from "./node-modules-dest-copy";
22

33
export class NodeModulesBuilder implements INodeModulesBuilder {
4-
constructor(private $fs: IFileSystem,
4+
constructor(
55
private $injector: IInjector,
66
private $nodeModulesDependenciesBuilder: INodeModulesDependenciesBuilder
77
) { }
88

99
public async prepareNodeModules(opts: INodeModulesBuilderData): Promise<void> {
10-
const productionDependencies = this.intialPrepareNodeModulesIfRequired(opts);
10+
const productionDependencies = this.$nodeModulesDependenciesBuilder.getProductionDependencies(opts.nodeModulesData.projectData.projectDir);
1111
const npmPluginPrepare: NpmPluginPrepare = this.$injector.resolve(NpmPluginPrepare);
12-
await npmPluginPrepare.preparePlugins(productionDependencies, opts.nodeModulesData.platform, opts.nodeModulesData.projectData, opts.nodeModulesData.projectFilesConfig);
13-
}
14-
15-
public async prepareJSNodeModules(opts: INodeModulesBuilderData): Promise<void> {
16-
const productionDependencies = this.intialPrepareNodeModulesIfRequired(opts);
17-
const npmPluginPrepare: NpmPluginPrepare = this.$injector.resolve(NpmPluginPrepare);
18-
await npmPluginPrepare.prepareJSPlugins(productionDependencies, opts.nodeModulesData.platform, opts.nodeModulesData.projectData, opts.nodeModulesData.projectFilesConfig);
19-
}
20-
21-
private intialPrepareNodeModulesIfRequired(opts: INodeModulesBuilderData): IDependencyData[] {
22-
const { nodeModulesData } = opts;
23-
const productionDependencies = this.$nodeModulesDependenciesBuilder.getProductionDependencies(nodeModulesData.projectData.projectDir);
24-
25-
if (opts.copyNodeModules && !nodeModulesData.appFilesUpdaterOptions.bundle) {
26-
this.initialPrepareNodeModules(opts, productionDependencies);
27-
}
28-
29-
return productionDependencies;
30-
}
31-
32-
private initialPrepareNodeModules(opts: INodeModulesBuilderData, productionDependencies: IDependencyData[]): void {
33-
const { nodeModulesData, release } = opts;
34-
35-
if (!this.$fs.exists(nodeModulesData.absoluteOutputPath)) {
36-
// Force copying if the destination doesn't exist.
37-
nodeModulesData.lastModifiedTime = null;
38-
}
39-
40-
const tnsModulesCopy: TnsModulesCopy = this.$injector.resolve(TnsModulesCopy, {
41-
outputRoot: nodeModulesData.absoluteOutputPath
42-
});
43-
44-
tnsModulesCopy.copyModules({ dependencies: productionDependencies, release });
12+
await npmPluginPrepare.preparePlugins(productionDependencies, opts.nodeModulesData.platform, opts.nodeModulesData.projectData);
4513
}
4614
}
4715

Original file line numberDiff line numberDiff line change
@@ -1,155 +1,10 @@
1-
import * as path from "path";
2-
import * as shelljs from "shelljs";
3-
import * as constants from "../../constants";
4-
import * as minimatch from "minimatch";
5-
6-
export interface ILocalDependencyData extends IDependencyData {
7-
directory: string;
8-
}
9-
10-
export class TnsModulesCopy {
11-
constructor(
12-
private outputRoot: string,
13-
private $fs: IFileSystem,
14-
private $pluginsService: IPluginsService
15-
) {
16-
}
17-
18-
public copyModules(opts: { dependencies: IDependencyData[], release: boolean }): void {
19-
const filePatternsToDelete = opts.release ? "**/*.ts" : "**/*.d.ts";
20-
for (const entry in opts.dependencies) {
21-
const dependency = opts.dependencies[entry];
22-
23-
this.copyDependencyDir(dependency, filePatternsToDelete);
24-
}
25-
}
26-
27-
private copyDependencyDir(dependency: IDependencyData, filePatternsToDelete: string): void {
28-
if (dependency.depth === 0) {
29-
const targetPackageDir = path.join(this.outputRoot, dependency.name);
30-
31-
shelljs.mkdir("-p", targetPackageDir);
32-
33-
const isScoped = dependency.name.indexOf("@") === 0;
34-
const destinationPath = isScoped ? path.join(this.outputRoot, dependency.name.substring(0, dependency.name.indexOf("/"))) : this.outputRoot;
35-
shelljs.cp("-RfL", dependency.directory, destinationPath);
36-
37-
// remove platform-specific files (processed separately by plugin services)
38-
shelljs.rm("-rf", path.join(targetPackageDir, "platforms"));
39-
40-
this.removeNonProductionDependencies(dependency, targetPackageDir);
41-
this.removeDependenciesPlatformsDirs(targetPackageDir);
42-
const allFiles = this.$fs.enumerateFilesInDirectorySync(targetPackageDir);
43-
allFiles.filter(file => minimatch(file, filePatternsToDelete, { nocase: true })).map(file => this.$fs.deleteFile(file));
44-
}
45-
}
46-
47-
private removeDependenciesPlatformsDirs(dependencyDir: string): void {
48-
const dependenciesFolder = path.join(dependencyDir, constants.NODE_MODULES_FOLDER_NAME);
49-
50-
if (this.$fs.exists(dependenciesFolder)) {
51-
const dependencies = this.getDependencies(dependenciesFolder);
52-
53-
dependencies
54-
.forEach(d => {
55-
const pathToDependency = path.join(dependenciesFolder, d);
56-
const pathToPackageJson = path.join(pathToDependency, constants.PACKAGE_JSON_FILE_NAME);
57-
58-
if (this.$pluginsService.isNativeScriptPlugin(pathToPackageJson)) {
59-
this.$fs.deleteDirectory(path.join(pathToDependency, constants.PLATFORMS_DIR_NAME));
60-
}
61-
62-
this.removeDependenciesPlatformsDirs(pathToDependency);
63-
});
64-
}
65-
}
66-
67-
private removeNonProductionDependencies(dependency: IDependencyData, targetPackageDir: string): void {
68-
const packageJsonFilePath = path.join(dependency.directory, constants.PACKAGE_JSON_FILE_NAME);
69-
if (!this.$fs.exists(packageJsonFilePath)) {
70-
return;
71-
}
72-
73-
const packageJsonContent = this.$fs.readJson(packageJsonFilePath);
74-
const productionDependencies = packageJsonContent.dependencies;
75-
76-
const dependenciesFolder = path.join(targetPackageDir, constants.NODE_MODULES_FOLDER_NAME);
77-
if (this.$fs.exists(dependenciesFolder)) {
78-
const dependencies = this.getDependencies(dependenciesFolder);
79-
80-
dependencies.filter(dir => !productionDependencies || !productionDependencies.hasOwnProperty(dir))
81-
.forEach(dir => shelljs.rm("-rf", path.join(dependenciesFolder, dir)));
82-
}
83-
}
84-
85-
private getDependencies(dependenciesFolder: string): string[] {
86-
const dependencies = _.flatten(this.$fs.readDirectory(dependenciesFolder)
87-
.map(dir => {
88-
if (_.startsWith(dir, "@")) {
89-
const pathToDir = path.join(dependenciesFolder, dir);
90-
const contents = this.$fs.readDirectory(pathToDir);
91-
return _.map(contents, subDir => `${dir}/${subDir}`);
92-
}
93-
94-
return dir;
95-
}));
96-
97-
return dependencies;
98-
}
99-
}
100-
1011
export class NpmPluginPrepare {
1022
constructor(
103-
private $fs: IFileSystem,
1043
private $pluginsService: IPluginsService,
1054
private $platformsData: IPlatformsData,
106-
private $logger: ILogger
107-
) {
108-
}
109-
110-
protected async afterPrepare(dependencies: IDependencyData[], platform: string, projectData: IProjectData): Promise<void> {
111-
const prepareData: IDictionary<boolean> = {};
112-
_.each(dependencies, d => {
113-
prepareData[d.name] = true;
114-
});
115-
this.$fs.createDirectory(this.preparedPlatformsDir(platform, projectData));
116-
this.$fs.writeJson(this.preparedPlatformsFile(platform, projectData), prepareData, " ", "utf8");
117-
}
118-
119-
private preparedPlatformsDir(platform: string, projectData: IProjectData): string {
120-
const platformRoot = this.$platformsData.getPlatformData(platform, projectData).projectRoot;
121-
if (/android/i.test(platform)) {
122-
return path.join(platformRoot, "build", "intermediates");
123-
} else if (/ios/i.test(platform)) {
124-
return path.join(platformRoot, "build");
125-
} else {
126-
throw new Error("Invalid platform: " + platform);
127-
}
128-
}
129-
130-
private preparedPlatformsFile(platform: string, projectData: IProjectData): string {
131-
return path.join(this.preparedPlatformsDir(platform, projectData), "prepared-platforms.json");
132-
}
133-
134-
protected getPreviouslyPreparedDependencies(platform: string, projectData: IProjectData): IDictionary<boolean> {
135-
if (!this.$fs.exists(this.preparedPlatformsFile(platform, projectData))) {
136-
return {};
137-
}
138-
return this.$fs.readJson(this.preparedPlatformsFile(platform, projectData), "utf8");
139-
}
5+
) { }
1406

141-
private allPrepared(dependencies: IDependencyData[], platform: string, projectData: IProjectData): boolean {
142-
let result = true;
143-
const previouslyPrepared = this.getPreviouslyPreparedDependencies(platform, projectData);
144-
_.each(dependencies, d => {
145-
if (!previouslyPrepared[d.name]) {
146-
result = false;
147-
}
148-
});
149-
return result;
150-
}
151-
152-
public async preparePlugins(dependencies: IDependencyData[], platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): Promise<void> {
7+
public async preparePlugins(dependencies: IDependencyData[], platform: string, projectData: IProjectData): Promise<void> {
1538
if (_.isEmpty(dependencies)) {
1549
return;
15510
}
@@ -164,29 +19,5 @@ export class NpmPluginPrepare {
16419
await this.$pluginsService.preparePluginNativeCode(pluginData, platform, projectData);
16520
}
16621
}
167-
168-
await this.afterPrepare(dependencies, platform, projectData);
169-
}
170-
171-
public async prepareJSPlugins(dependencies: IDependencyData[], platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): Promise<void> {
172-
if (_.isEmpty(dependencies) || this.allPrepared(dependencies, platform, projectData)) {
173-
return;
174-
}
175-
176-
for (const dependencyKey in dependencies) {
177-
const dependency = dependencies[dependencyKey];
178-
const isPlugin = !!dependency.nativescript;
179-
if (isPlugin) {
180-
platform = platform.toLowerCase();
181-
const pluginData = this.$pluginsService.convertToPluginData(dependency, projectData.projectDir);
182-
const platformData = this.$platformsData.getPlatformData(platform, projectData);
183-
const appFolderExists = this.$fs.exists(path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME));
184-
if (appFolderExists) {
185-
this.$pluginsService.preparePluginScripts(pluginData, platform, projectData, projectFilesConfig);
186-
// Show message
187-
this.$logger.out(`Successfully prepared plugin ${pluginData.name} for ${platform}.`);
188-
}
189-
}
190-
}
19122
}
19223
}

0 commit comments

Comments
 (0)