-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathexport.ts
More file actions
175 lines (155 loc) · 5.52 KB
/
export.ts
File metadata and controls
175 lines (155 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import path from 'path';
import { DEFAULT_PHP_VERSION } from '@studio/common/constants';
import { SiteCommandLoggerAction as LoggerAction } from '@studio/common/logger-actions';
import { __, _n, sprintf } from '@wordpress/i18n';
import { getSiteByFolder } from 'cli/lib/cli-config/sites';
import { connectToDaemon, disconnectFromDaemon } from 'cli/lib/daemon-client';
import { ExportEvents } from 'cli/lib/import-export/export/events';
import { exportBackup } from 'cli/lib/import-export/export/export-manager';
import { BackupCreateProgressEventData, ExportOptions } from 'cli/lib/import-export/export/types';
import { ImportExportEventData } from 'cli/lib/import-export/handle-events';
import { keepSqliteIntegrationUpdated } from 'cli/lib/sqlite-integration';
import { untildify } from 'cli/lib/utils';
import { Logger, LoggerError } from 'cli/logger';
import { StudioArgv } from 'cli/types';
const logger = new Logger< LoggerAction >();
export function exportEventHandler( { event, data }: ImportExportEventData ): void {
switch ( event ) {
case ExportEvents.EXPORT_START:
logger.reportStart( LoggerAction.EXPORT_SITE, __( 'Starting export…' ) );
break;
case ExportEvents.BACKUP_CREATE_START:
logger.reportStart( LoggerAction.CREATE_BACKUP, __( 'Creating backup file…' ) );
break;
case ExportEvents.WP_CONTENT_EXPORT_START:
logger.reportStart( LoggerAction.EXPORT_WP_CONTENT, __( 'Traversing WordPress content…' ) );
break;
case ExportEvents.WP_CONTENT_EXPORT_COMPLETE:
logger.reportSuccess( __( 'WordPress content traversed' ) );
break;
case ExportEvents.DATABASE_EXPORT_START:
logger.reportStart( LoggerAction.EXPORT_DATABASE, __( 'Exporting database…' ) );
break;
case ExportEvents.DATABASE_EXPORT_COMPLETE:
logger.reportSuccess( __( 'Database exported' ) );
break;
case ExportEvents.BACKUP_CREATE_PROGRESS: {
const progressData = data as BackupCreateProgressEventData;
const processed = progressData?.progress?.entries?.processed;
if ( processed != null ) {
logger.reportProgress(
sprintf(
_n( 'Backing up file… (%d processed)', 'Backing up files… (%d processed)', processed ),
processed
)
);
}
break;
}
case ExportEvents.BACKUP_CREATE_COMPLETE:
logger.reportSuccess( __( 'Backup file created' ) );
break;
case ExportEvents.CONFIG_EXPORT_START:
logger.reportStart( LoggerAction.EXPORT_CONFIG, __( 'Exporting configuration…' ) );
break;
case ExportEvents.CONFIG_EXPORT_COMPLETE:
logger.reportSuccess( __( 'Configuration exported' ) );
break;
case ExportEvents.EXPORT_COMPLETE:
logger.reportSuccess( __( 'Site exported successfully' ) );
break;
case ExportEvents.EXPORT_ERROR:
throw new LoggerError( __( 'Export failed' ), data instanceof Error ? data : undefined );
}
}
export async function runCommand(
siteFolder: string,
exportPath: string,
includeOnly?: 'content' | 'db'
): Promise< void > {
try {
logger.reportStart( LoggerAction.START_DAEMON, __( 'Starting process daemon…' ) );
await connectToDaemon();
logger.reportSuccess( __( 'Process daemon started' ) );
logger.reportStart( LoggerAction.LOAD_SITES, __( 'Loading site…' ) );
const site = await getSiteByFolder( siteFolder );
logger.reportSuccess( __( 'Site loaded' ) );
logger.reportStart(
LoggerAction.INSTALL_SQLITE,
__( 'Setting up SQLite integration, if needed…' )
);
await keepSqliteIntegrationUpdated( siteFolder );
logger.reportSuccess( __( 'SQLite integration configured as needed' ) );
const includes: ExportOptions[ 'includes' ] = { database: true, wpContent: true };
if ( includeOnly === 'content' ) {
includes.database = false;
} else if ( includeOnly === 'db' ) {
includes.wpContent = false;
}
const isExported = await exportBackup(
{
site,
backupFile: exportPath,
phpVersion: DEFAULT_PHP_VERSION,
includes,
},
exportEventHandler
);
if ( ! isExported ) {
throw new LoggerError( __( 'No suitable exporter found for the provided backup file' ) );
}
} finally {
await disconnectFromDaemon();
}
}
export const registerCommand = ( yargs: StudioArgv ) => {
return yargs.command( {
command: 'export <export-file>',
describe: __( 'Export site to a backup file' ),
builder: ( yargs ) => {
return yargs
.positional( 'export-file', {
type: 'string',
normalize: true,
demandOption: true,
description: __( 'Path to the export file. Can be a .zip, .tar.gz or .sql file.' ),
coerce: ( value ) => {
return path.resolve( untildify( value ) );
},
} )
.option( 'only', {
type: 'string',
choices: [ 'content', 'db' ] as const,
description: __( 'Export only the content or the database' ),
} );
},
handler: async ( argv ) => {
try {
if ( argv.only === 'content' && argv.exportFile.endsWith( '.sql' ) ) {
throw new LoggerError(
__(
'Invalid export file extension. Must be .zip or .tar.gz when exporting site content.'
)
);
}
if (
! argv.exportFile.endsWith( '.sql' ) &&
! argv.exportFile.endsWith( '.zip' ) &&
! argv.exportFile.endsWith( '.tar.gz' )
) {
throw new LoggerError(
__( 'Invalid export file extension. Must be .zip, .tar.gz or .sql.' )
);
}
await runCommand( argv.path, argv.exportFile, argv.only );
} catch ( error ) {
if ( error instanceof LoggerError ) {
logger.reportError( error );
} else {
const loggerError = new LoggerError( __( 'Failed to export site' ), error );
logger.reportError( loggerError );
}
}
},
} );
};