11import * as path from "path" ;
22import * as semver from "semver" ;
33import * as constants from "../constants" ;
4+ import * as glob from "glob" ;
45import { UpdateControllerBase } from "./update-controller-base" ;
56import { fromWindowsRelativePathToUnix } from "../common/helpers" ;
67
@@ -65,7 +66,8 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
6566 { packageName : "nativescript-vue" , verifiedVersion : "2.3.0-rc.0" } ,
6667 { packageName : "nativescript-permissions" , verifiedVersion : "1.3.0" } ,
6768 { packageName : "nativescript-cardview" , verifiedVersion : "3.2.0" } ,
68- { packageName : "nativescript-unit-test-runner" , verifiedVersion : "0.6.3" ,
69+ {
70+ packageName : "nativescript-unit-test-runner" , verifiedVersion : "0.6.3" ,
6971 shouldMigrateAction : ( projectData : IProjectData ) => this . hasDependency ( { packageName : "nativescript-unit-test-runner" , isDev : false } , projectData ) ,
7072 migrateAction : this . migrateUnitTestRunner . bind ( this )
7173 }
@@ -92,6 +94,14 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
9294 return ;
9395 }
9496
97+ try {
98+ this . $logger . info ( "Backup auto-generated files." ) ;
99+ this . handleAutoGeneratedFiles ( backupDir , projectData ) ;
100+ this . $logger . info ( "Backup auto-generated files complete." ) ;
101+ } catch ( error ) {
102+ this . $logger . trace ( `Error during auto-generated files handling. ${ ( error && error . message ) || error } ` ) ;
103+ }
104+
95105 try {
96106 await this . cleanUpProject ( projectData ) ;
97107 await this . migrateDependencies ( projectData ) ;
@@ -133,7 +143,7 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
133143 }
134144 }
135145
136- private async cleanUpProject ( projectData : IProjectData ) {
146+ private async cleanUpProject ( projectData : IProjectData ) : Promise < void > {
137147 this . $logger . info ( "Clean old project artefacts." ) ;
138148 this . $fs . deleteDirectory ( path . join ( projectData . projectDir , constants . HOOKS_DIR_NAME ) ) ;
139149 this . $fs . deleteDirectory ( path . join ( projectData . projectDir , constants . PLATFORMS_DIR_NAME ) ) ;
@@ -147,6 +157,52 @@ export class MigrateController extends UpdateControllerBase implements IMigrateC
147157 this . $logger . info ( "Clean old project artefacts complete." ) ;
148158 }
149159
160+ private handleAutoGeneratedFiles ( backupDir : string , projectData : IProjectData ) : void {
161+ const globOptions : glob . IOptions = {
162+ silent : true ,
163+ nocase : true ,
164+ matchBase : true ,
165+ nodir : true ,
166+ absolute : false ,
167+ root : projectData . appDirectoryPath
168+ } ;
169+
170+ const jsFiles = glob . sync ( "*.@(js|ts|js.map)" , globOptions ) ;
171+ const autoGeneratedJsFiles = this . getGeneratedFiles ( jsFiles , [ ".js" ] , [ ".ts" ] ) ;
172+ const autoGeneratedJsMapFiles = this . getGeneratedFiles ( jsFiles , [ ".map" ] , [ "" ] ) ;
173+ const cssFiles = glob . sync ( "*.@(le|sa|sc|c)ss" , globOptions ) ;
174+ const autoGeneratedCssFiles = this . getGeneratedFiles ( cssFiles , [ ".css" ] , [ ".scss" , ".sass" , ".less" ] ) ;
175+
176+ const allGeneratedFiles = autoGeneratedJsFiles . concat ( autoGeneratedJsMapFiles ) . concat ( autoGeneratedCssFiles ) ;
177+ for ( const generatedFile of allGeneratedFiles ) {
178+ const sourceFile = path . join ( projectData . projectDir , generatedFile ) ;
179+ const destinationFile = path . join ( backupDir , generatedFile ) ;
180+ const destinationFileDir = path . dirname ( destinationFile ) ;
181+ this . $fs . ensureDirectoryExists ( destinationFileDir ) ;
182+ this . $fs . rename ( sourceFile , destinationFile ) ;
183+ }
184+ }
185+
186+ private getGeneratedFiles ( allFiles : string [ ] , generatedFileExts : string [ ] , sourceFileExts : string [ ] ) : string [ ] {
187+ const autoGeneratedFiles = allFiles . filter ( file => {
188+ let isGenerated = false ;
189+ const { dir, name, ext } = path . parse ( file ) ;
190+ if ( generatedFileExts . indexOf ( ext ) > - 1 ) {
191+ for ( const sourceExt of sourceFileExts ) {
192+ const possibleSourceFile = path . format ( { dir, name, ext : sourceExt } ) ;
193+ isGenerated = allFiles . indexOf ( possibleSourceFile ) > - 1 ;
194+ if ( isGenerated ) {
195+ break ;
196+ }
197+ }
198+ }
199+
200+ return isGenerated ;
201+ } ) ;
202+
203+ return autoGeneratedFiles ;
204+ }
205+
150206 private async migrateDependencies ( projectData : IProjectData ) : Promise < void > {
151207 this . $logger . info ( "Start dependencies migration." ) ;
152208 for ( let i = 0 ; i < this . migrationDependencies . length ; i ++ ) {
0 commit comments