1+ import { join } from 'node:path' ;
2+ import { log , formatError , fileHelper , fsUtil } from '../../utils' ;
3+ import BaseClass from './base-class' ;
4+ import { ModuleClassParams } from '../../types' ;
5+
6+ export default class ImportStack extends BaseClass { // classname
7+ private stackSettingsPath : string ;
8+ private envUidMapperPath : string ;
9+ private stackSettings : Record < string , any > | null = null ;
10+ private envUidMapper : Record < string , string > = { } ;
11+
12+ constructor ( { importConfig, stackAPIClient } : ModuleClassParams ) {
13+ super ( { importConfig, stackAPIClient } ) ;
14+ this . stackSettingsPath = join ( this . importConfig . backupDir , 'stack' , 'settings.json' ) ;
15+ this . envUidMapperPath = join ( this . importConfig . backupDir , 'mapper' , 'environments' , 'uid-mapping.json' ) ;
16+ }
17+
18+ async start ( ) : Promise < void > {
19+ log ( this . importConfig , 'Migrating stack...' , 'info' ) ;
20+
21+ if ( fileHelper . fileExistsSync ( this . envUidMapperPath ) ) {
22+ this . envUidMapper = fsUtil . readFile ( this . envUidMapperPath , true ) as Record < string , string > ;
23+ } else {
24+ throw new Error ( 'Please run the environments migration first.' ) ;
25+ }
26+
27+ if ( fileHelper . fileExistsSync ( this . stackSettingsPath ) ) {
28+ this . stackSettings = fsUtil . readFile ( this . stackSettingsPath , true ) as Record < string , any > ;
29+ } else {
30+ log ( this . importConfig , 'No stack Found!' , 'info' ) ;
31+ return ;
32+ }
33+
34+ if ( this . stackSettings ?. live_preview && this . stackSettings ?. live_preview [ 'default-env' ] ) {
35+ const oldEnvUid = this . stackSettings . live_preview [ 'default-env' ] ;
36+ const mappedEnvUid = this . envUidMapper [ oldEnvUid ] ;
37+ this . stackSettings . live_preview [ 'default-env' ] = mappedEnvUid ;
38+ }
39+
40+ try {
41+ await this . stack . addSettings ( this . stackSettings ) ;
42+ log ( this . importConfig , 'Successfully imported stack' , 'success' ) ;
43+ } catch ( error ) {
44+ log ( this . importConfig , `Stack failed to be imported! ${ formatError ( error ) } ` , 'error' ) ;
45+ }
46+ }
47+ }
0 commit comments