Skip to content

feat: remove the auto-generated js, js.map and css files during app migration #4768

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 2 commits into from
Jun 26, 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
43 changes: 0 additions & 43 deletions lib/common/definitions/minimatch.d.ts

This file was deleted.

60 changes: 58 additions & 2 deletions lib/controllers/migrate-controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from "path";
import * as semver from "semver";
import * as constants from "../constants";
import * as glob from "glob";
import { UpdateControllerBase } from "./update-controller-base";
import { fromWindowsRelativePathToUnix } from "../common/helpers";

Expand Down Expand Up @@ -65,7 +66,8 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
{ packageName: "nativescript-vue", verifiedVersion: "2.3.0-rc.0" },
{ packageName: "nativescript-permissions", verifiedVersion: "1.3.0" },
{ packageName: "nativescript-cardview", verifiedVersion: "3.2.0" },
{ packageName: "nativescript-unit-test-runner", verifiedVersion: "0.6.3",
{
packageName: "nativescript-unit-test-runner", verifiedVersion: "0.6.3",
shouldMigrateAction: (projectData: IProjectData) => this.hasDependency({ packageName: "nativescript-unit-test-runner", isDev: false }, projectData),
migrateAction: this.migrateUnitTestRunner.bind(this)
}
Expand All @@ -92,6 +94,14 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
return;
}

try {
this.$logger.info("Backup auto-generated files.");
this.handleAutoGeneratedFiles(backupDir, projectData);
this.$logger.info("Backup auto-generated files complete.");
} catch (error) {
this.$logger.trace(`Error during auto-generated files handling. ${(error && error.message) || error}`);
}

try {
await this.cleanUpProject(projectData);
await this.migrateDependencies(projectData);
Expand Down Expand Up @@ -133,7 +143,7 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
}
}

private async cleanUpProject(projectData: IProjectData) {
private async cleanUpProject(projectData: IProjectData): Promise<void> {
this.$logger.info("Clean old project artefacts.");
this.$fs.deleteDirectory(path.join(projectData.projectDir, constants.HOOKS_DIR_NAME));
this.$fs.deleteDirectory(path.join(projectData.projectDir, constants.PLATFORMS_DIR_NAME));
Expand All @@ -147,6 +157,52 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
this.$logger.info("Clean old project artefacts complete.");
}

private handleAutoGeneratedFiles(backupDir: string, projectData: IProjectData): void {
const globOptions: glob.IOptions = {
silent: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we can set this based on the log level?

nocase: true,
matchBase: true,
nodir: true,
absolute: false,
root: projectData.appDirectoryPath
};

const jsFiles = glob.sync("*.@(js|ts|js.map)", globOptions);
const autoGeneratedJsFiles = this.getGeneratedFiles(jsFiles, [".js"], [".ts"]);
const autoGeneratedJsMapFiles = this.getGeneratedFiles(jsFiles, [".map"], [""]);
const cssFiles = glob.sync("*.@(le|sa|sc|c)ss", globOptions);
const autoGeneratedCssFiles = this.getGeneratedFiles(cssFiles, [".css"], [".scss", ".sass", ".less"]);

const allGeneratedFiles = autoGeneratedJsFiles.concat(autoGeneratedJsMapFiles).concat(autoGeneratedCssFiles);
for (const generatedFile of allGeneratedFiles) {
const sourceFile = path.join(projectData.projectDir, generatedFile);
const destinationFile = path.join(backupDir, generatedFile);
const destinationFileDir = path.dirname(destinationFile);
this.$fs.ensureDirectoryExists(destinationFileDir);
this.$fs.rename(sourceFile, destinationFile);
}
}

private getGeneratedFiles(allFiles: string[], generatedFileExts: string[], sourceFileExts: string[]): string[] {
const autoGeneratedFiles = allFiles.filter(file => {
let isGenerated = false;
const { dir, name, ext } = path.parse(file);
if (generatedFileExts.indexOf(ext) > -1) {
for (const sourceExt of sourceFileExts) {
const possibleSourceFile = path.format({ dir, name, ext: sourceExt });
isGenerated = allFiles.indexOf(possibleSourceFile) > -1;
if (isGenerated) {
break;
}
}
}

return isGenerated;
});

return autoGeneratedFiles;
}

private async migrateDependencies(projectData: IProjectData): Promise<void> {
this.$logger.info("Start dependencies migration.");
for (let i = 0; i < this.migrationDependencies.length; i++) {
Expand Down
Loading