Skip to content

chore: remove the code related to js prepare as webpack overrides prepareJS method with own logic #4516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/definitions/platform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ interface INodeModulesBuilderData {

interface INodeModulesBuilder {
prepareNodeModules(opts: INodeModulesBuilderData): Promise<void>;
prepareJSNodeModules(opts: INodeModulesBuilderData): Promise<void>;
}

interface INodeModulesDependenciesBuilder {
Expand Down
2 changes: 0 additions & 2 deletions lib/definitions/plugins.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
interface IPluginsService {
add(plugin: string, projectData: IProjectData): Promise<void>; // adds plugin by name, github url, local path and et.
remove(pluginName: string, projectData: IProjectData): Promise<void>; // removes plugin only by name
prepare(pluginData: IDependencyData, platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): Promise<void>;
getAllInstalledPlugins(projectData: IProjectData): Promise<IPluginData[]>;
ensureAllDependenciesAreInstalled(projectData: IProjectData): Promise<void>;
preparePluginScripts(pluginData: IPluginData, platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): void

/**
* Returns all dependencies and devDependencies from pacakge.json file.
Expand Down
35 changes: 0 additions & 35 deletions lib/services/plugins-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ export class PluginsService implements IPluginsService {
private get $projectDataService(): IProjectDataService {
return this.$injector.resolve("projectDataService");
}
private get $projectFilesManager(): IProjectFilesManager {
return this.$injector.resolve("projectFilesManager");
}

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

public async prepare(dependencyData: IDependencyData, platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): Promise<void> {
platform = platform.toLowerCase();
const platformData = this.$platformsData.getPlatformData(platform, projectData);
const pluginData = this.convertToPluginData(dependencyData, projectData.projectDir);

const appFolderExists = this.$fs.exists(path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME));
if (appFolderExists) {
this.preparePluginScripts(pluginData, platform, projectData, projectFilesConfig);
await this.preparePluginNativeCode(pluginData, platform, projectData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved somewhere maybe.


// Show message
this.$logger.out(`Successfully prepared plugin ${pluginData.name} for ${platform}.`);
}
}

public preparePluginScripts(pluginData: IPluginData, platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): void {
const platformData = this.$platformsData.getPlatformData(platform, projectData);
const pluginScriptsDestinationPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, "tns_modules");
const scriptsDestinationExists = this.$fs.exists(pluginScriptsDestinationPath);
if (!scriptsDestinationExists) {
//tns_modules/<plugin> doesn't exist. Assuming we're running a bundled prepare.
return;
}

if (!this.isPluginDataValidForPlatform(pluginData, platform, projectData)) {
return;
}

//prepare platform speciffic files, .map and .ts files
this.$projectFilesManager.processPlatformSpecificFiles(pluginScriptsDestinationPath, platform, projectFilesConfig);
}

public async preparePluginNativeCode(pluginData: IPluginData, platform: string, projectData: IProjectData): Promise<void> {
const platformData = this.$platformsData.getPlatformData(platform, projectData);
pluginData.pluginPlatformsFolderPath = (_platform: string) => path.join(pluginData.fullPath, "platforms", _platform.toLowerCase());
Expand Down
53 changes: 1 addition & 52 deletions lib/services/prepare-platform-js-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as constants from "../constants";
import * as path from "path";
import * as shell from "shelljs";
import * as temp from "temp";
import { hook } from "../common/helpers";
import { PreparePlatformService } from "./prepare-platform-service";
Expand All @@ -16,7 +15,6 @@ export class PreparePlatformJSService extends PreparePlatformService implements
private $errors: IErrors,
private $logger: ILogger,
private $projectDataService: IProjectDataService,
private $nodeModulesBuilder: INodeModulesBuilder,
private $packageManager: INodePackageManager) {
super($fs, $hooksService, $xmlValidator);
}
Expand All @@ -36,23 +34,7 @@ export class PreparePlatformJSService extends PreparePlatformService implements
@performanceLog()
@hook('prepareJSApp')
public async preparePlatform(config: IPreparePlatformJSInfo): Promise<void> {
if (!config.changesInfo || config.changesInfo.appFilesChanged || config.changesInfo.changesRequirePrepare) {
await this.copyAppFiles(config);
this.copyAppResourcesFiles(config);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to move this call somewhere else.

}

if (config.changesInfo && !config.changesInfo.changesRequirePrepare) {
// remove the App_Resources folder from the app/assets as here we're applying other files changes.
const appDestinationDirectoryPath = path.join(config.platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
const appResourcesDirectoryPath = path.join(appDestinationDirectoryPath, path.basename(config.projectData.appResourcesDirectoryPath));
if (this.$fs.exists(appResourcesDirectoryPath)) {
this.$fs.deleteDirectory(appResourcesDirectoryPath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use this clean up too.

}
}

if (!config.changesInfo || config.changesInfo.modulesChanged) {
await this.copyTnsModules(config.platform, config.platformData, config.projectData, config.appFilesUpdaterOptions, config.projectFilesConfig);
}
// intentionally left blank, keep the support for before-prepareJSApp and after-prepareJSApp hooks
}

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

return null;
}

private async copyTnsModules(platform: string, platformData: IPlatformData, projectData: IProjectData, appFilesUpdaterOptions: IAppFilesUpdaterOptions, projectFilesConfig?: IProjectFilesConfig): Promise<void> {
const appDestinationDirectoryPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
const lastModifiedTime = this.$fs.exists(appDestinationDirectoryPath) ? this.$fs.getFsStats(appDestinationDirectoryPath).mtime : null;

try {
const absoluteOutputPath = path.join(appDestinationDirectoryPath, constants.TNS_MODULES_FOLDER_NAME);
// Process node_modules folder
await this.$nodeModulesBuilder.prepareJSNodeModules({
nodeModulesData: {
absoluteOutputPath,
platform,
lastModifiedTime,
projectData,
appFilesUpdaterOptions,
projectFilesConfig
},
release: appFilesUpdaterOptions.release,
copyNodeModules: true
});
} catch (error) {
this.$logger.debug(error);
shell.rm("-rf", appDestinationDirectoryPath);
this.$errors.failWithoutHelp(`Processing node_modules failed. ${error}`);
}
}

private copyAppResourcesFiles(config: IPreparePlatformJSInfo): void {
const appDestinationDirectoryPath = path.join(config.platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
const appResourcesSourcePath = config.projectData.appResourcesDirectoryPath;

shell.cp("-Rf", appResourcesSourcePath, path.join(appDestinationDirectoryPath, constants.APP_RESOURCES_FOLDER_NAME));
}
}

$injector.register("preparePlatformJSService", PreparePlatformJSService);
40 changes: 4 additions & 36 deletions lib/tools/node-modules/node-modules-builder.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,15 @@
import { TnsModulesCopy, NpmPluginPrepare } from "./node-modules-dest-copy";
import { NpmPluginPrepare } from "./node-modules-dest-copy";

export class NodeModulesBuilder implements INodeModulesBuilder {
constructor(private $fs: IFileSystem,
constructor(
private $injector: IInjector,
private $nodeModulesDependenciesBuilder: INodeModulesDependenciesBuilder
) { }

public async prepareNodeModules(opts: INodeModulesBuilderData): Promise<void> {
const productionDependencies = this.intialPrepareNodeModulesIfRequired(opts);
const productionDependencies = this.$nodeModulesDependenciesBuilder.getProductionDependencies(opts.nodeModulesData.projectData.projectDir);
const npmPluginPrepare: NpmPluginPrepare = this.$injector.resolve(NpmPluginPrepare);
await npmPluginPrepare.preparePlugins(productionDependencies, opts.nodeModulesData.platform, opts.nodeModulesData.projectData, opts.nodeModulesData.projectFilesConfig);
}

public async prepareJSNodeModules(opts: INodeModulesBuilderData): Promise<void> {
const productionDependencies = this.intialPrepareNodeModulesIfRequired(opts);
const npmPluginPrepare: NpmPluginPrepare = this.$injector.resolve(NpmPluginPrepare);
await npmPluginPrepare.prepareJSPlugins(productionDependencies, opts.nodeModulesData.platform, opts.nodeModulesData.projectData, opts.nodeModulesData.projectFilesConfig);
}

private intialPrepareNodeModulesIfRequired(opts: INodeModulesBuilderData): IDependencyData[] {
const { nodeModulesData } = opts;
const productionDependencies = this.$nodeModulesDependenciesBuilder.getProductionDependencies(nodeModulesData.projectData.projectDir);

if (opts.copyNodeModules && !nodeModulesData.appFilesUpdaterOptions.bundle) {
this.initialPrepareNodeModules(opts, productionDependencies);
}

return productionDependencies;
}

private initialPrepareNodeModules(opts: INodeModulesBuilderData, productionDependencies: IDependencyData[]): void {
const { nodeModulesData, release } = opts;

if (!this.$fs.exists(nodeModulesData.absoluteOutputPath)) {
// Force copying if the destination doesn't exist.
nodeModulesData.lastModifiedTime = null;
}

const tnsModulesCopy: TnsModulesCopy = this.$injector.resolve(TnsModulesCopy, {
outputRoot: nodeModulesData.absoluteOutputPath
});

tnsModulesCopy.copyModules({ dependencies: productionDependencies, release });
await npmPluginPrepare.preparePlugins(productionDependencies, opts.nodeModulesData.platform, opts.nodeModulesData.projectData);
}
}

Expand Down
173 changes: 2 additions & 171 deletions lib/tools/node-modules/node-modules-dest-copy.ts
Original file line number Diff line number Diff line change
@@ -1,155 +1,10 @@
import * as path from "path";
import * as shelljs from "shelljs";
import * as constants from "../../constants";
import * as minimatch from "minimatch";

export interface ILocalDependencyData extends IDependencyData {
directory: string;
}

export class TnsModulesCopy {
constructor(
private outputRoot: string,
private $fs: IFileSystem,
private $pluginsService: IPluginsService
) {
}

public copyModules(opts: { dependencies: IDependencyData[], release: boolean }): void {
const filePatternsToDelete = opts.release ? "**/*.ts" : "**/*.d.ts";
for (const entry in opts.dependencies) {
const dependency = opts.dependencies[entry];

this.copyDependencyDir(dependency, filePatternsToDelete);
}
}

private copyDependencyDir(dependency: IDependencyData, filePatternsToDelete: string): void {
if (dependency.depth === 0) {
const targetPackageDir = path.join(this.outputRoot, dependency.name);

shelljs.mkdir("-p", targetPackageDir);

const isScoped = dependency.name.indexOf("@") === 0;
const destinationPath = isScoped ? path.join(this.outputRoot, dependency.name.substring(0, dependency.name.indexOf("/"))) : this.outputRoot;
shelljs.cp("-RfL", dependency.directory, destinationPath);

// remove platform-specific files (processed separately by plugin services)
shelljs.rm("-rf", path.join(targetPackageDir, "platforms"));

this.removeNonProductionDependencies(dependency, targetPackageDir);
this.removeDependenciesPlatformsDirs(targetPackageDir);
const allFiles = this.$fs.enumerateFilesInDirectorySync(targetPackageDir);
allFiles.filter(file => minimatch(file, filePatternsToDelete, { nocase: true })).map(file => this.$fs.deleteFile(file));
}
}

private removeDependenciesPlatformsDirs(dependencyDir: string): void {
const dependenciesFolder = path.join(dependencyDir, constants.NODE_MODULES_FOLDER_NAME);

if (this.$fs.exists(dependenciesFolder)) {
const dependencies = this.getDependencies(dependenciesFolder);

dependencies
.forEach(d => {
const pathToDependency = path.join(dependenciesFolder, d);
const pathToPackageJson = path.join(pathToDependency, constants.PACKAGE_JSON_FILE_NAME);

if (this.$pluginsService.isNativeScriptPlugin(pathToPackageJson)) {
this.$fs.deleteDirectory(path.join(pathToDependency, constants.PLATFORMS_DIR_NAME));
}

this.removeDependenciesPlatformsDirs(pathToDependency);
});
}
}

private removeNonProductionDependencies(dependency: IDependencyData, targetPackageDir: string): void {
const packageJsonFilePath = path.join(dependency.directory, constants.PACKAGE_JSON_FILE_NAME);
if (!this.$fs.exists(packageJsonFilePath)) {
return;
}

const packageJsonContent = this.$fs.readJson(packageJsonFilePath);
const productionDependencies = packageJsonContent.dependencies;

const dependenciesFolder = path.join(targetPackageDir, constants.NODE_MODULES_FOLDER_NAME);
if (this.$fs.exists(dependenciesFolder)) {
const dependencies = this.getDependencies(dependenciesFolder);

dependencies.filter(dir => !productionDependencies || !productionDependencies.hasOwnProperty(dir))
.forEach(dir => shelljs.rm("-rf", path.join(dependenciesFolder, dir)));
}
}

private getDependencies(dependenciesFolder: string): string[] {
const dependencies = _.flatten(this.$fs.readDirectory(dependenciesFolder)
.map(dir => {
if (_.startsWith(dir, "@")) {
const pathToDir = path.join(dependenciesFolder, dir);
const contents = this.$fs.readDirectory(pathToDir);
return _.map(contents, subDir => `${dir}/${subDir}`);
}

return dir;
}));

return dependencies;
}
}

export class NpmPluginPrepare {
constructor(
private $fs: IFileSystem,
private $pluginsService: IPluginsService,
private $platformsData: IPlatformsData,
private $logger: ILogger
) {
}

protected async afterPrepare(dependencies: IDependencyData[], platform: string, projectData: IProjectData): Promise<void> {
const prepareData: IDictionary<boolean> = {};
_.each(dependencies, d => {
prepareData[d.name] = true;
});
this.$fs.createDirectory(this.preparedPlatformsDir(platform, projectData));
this.$fs.writeJson(this.preparedPlatformsFile(platform, projectData), prepareData, " ", "utf8");
}

private preparedPlatformsDir(platform: string, projectData: IProjectData): string {
const platformRoot = this.$platformsData.getPlatformData(platform, projectData).projectRoot;
if (/android/i.test(platform)) {
return path.join(platformRoot, "build", "intermediates");
} else if (/ios/i.test(platform)) {
return path.join(platformRoot, "build");
} else {
throw new Error("Invalid platform: " + platform);
}
}

private preparedPlatformsFile(platform: string, projectData: IProjectData): string {
return path.join(this.preparedPlatformsDir(platform, projectData), "prepared-platforms.json");
}

protected getPreviouslyPreparedDependencies(platform: string, projectData: IProjectData): IDictionary<boolean> {
if (!this.$fs.exists(this.preparedPlatformsFile(platform, projectData))) {
return {};
}
return this.$fs.readJson(this.preparedPlatformsFile(platform, projectData), "utf8");
}
) { }

private allPrepared(dependencies: IDependencyData[], platform: string, projectData: IProjectData): boolean {
let result = true;
const previouslyPrepared = this.getPreviouslyPreparedDependencies(platform, projectData);
_.each(dependencies, d => {
if (!previouslyPrepared[d.name]) {
result = false;
}
});
return result;
}

public async preparePlugins(dependencies: IDependencyData[], platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): Promise<void> {
public async preparePlugins(dependencies: IDependencyData[], platform: string, projectData: IProjectData): Promise<void> {
if (_.isEmpty(dependencies)) {
return;
}
Expand All @@ -164,29 +19,5 @@ export class NpmPluginPrepare {
await this.$pluginsService.preparePluginNativeCode(pluginData, platform, projectData);
}
}

await this.afterPrepare(dependencies, platform, projectData);
}

public async prepareJSPlugins(dependencies: IDependencyData[], platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): Promise<void> {
if (_.isEmpty(dependencies) || this.allPrepared(dependencies, platform, projectData)) {
return;
}

for (const dependencyKey in dependencies) {
const dependency = dependencies[dependencyKey];
const isPlugin = !!dependency.nativescript;
if (isPlugin) {
platform = platform.toLowerCase();
const pluginData = this.$pluginsService.convertToPluginData(dependency, projectData.projectDir);
const platformData = this.$platformsData.getPlatformData(platform, projectData);
const appFolderExists = this.$fs.exists(path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME));
if (appFolderExists) {
this.$pluginsService.preparePluginScripts(pluginData, platform, projectData, projectFilesConfig);
// Show message
this.$logger.out(`Successfully prepared plugin ${pluginData.name} for ${platform}.`);
}
}
}
}
}
Loading