11import { inject , injectable } from 'inversify' ;
2- import { Uri } from 'vscode' ;
2+ import * as path from 'path' ;
3+ import { CancellationToken , Uri } from 'vscode' ;
34import { IFileSystem } from '../../common/platform/types' ;
45import { IPythonExecutionFactory , IPythonExecutionService } from '../../common/process/types' ;
56import { reportAction } from '../progress/decorator' ;
67import { ReportableAction } from '../progress/types' ;
78import { IJupyterSubCommandExecutionService , INotebookImporter } from '../types' ;
8- import { IExport } from './types' ;
9+ import { ExportFormat , IExport } from './types' ;
910
1011@injectable ( )
1112export class ExportBase implements IExport {
@@ -18,37 +19,55 @@ export class ExportBase implements IExport {
1819 ) { }
1920
2021 // tslint:disable-next-line: no-empty
21- public async export ( _source : Uri , _target : Uri ) : Promise < void > { }
22+ public async export ( _source : Uri , _target : Uri , _token : CancellationToken ) : Promise < void > { }
2223
2324 @reportAction ( ReportableAction . PerformingExport )
24- public async executeCommand ( source : Uri , target : Uri , args : string [ ] ) : Promise < void > {
25+ public async executeCommand (
26+ source : Uri ,
27+ target : Uri ,
28+ format : ExportFormat ,
29+ token : CancellationToken
30+ ) : Promise < void > {
31+ if ( token . isCancellationRequested ) {
32+ return ;
33+ }
34+
2535 const service = await this . getExecutionService ( source ) ;
2636 if ( ! service ) {
2737 return ;
2838 }
2939
30- const oldFileExists = await this . fileSystem . fileExists ( target . fsPath ) ;
31- let oldFileTime ;
32- if ( oldFileExists ) {
33- oldFileTime = ( await this . fileSystem . stat ( target . fsPath ) ) . mtime ;
40+ if ( token . isCancellationRequested ) {
41+ return ;
3442 }
3543
44+ const tempTarget = await this . fileSystem . createTemporaryFile ( path . extname ( target . fsPath ) ) ;
45+ const args = [
46+ source . fsPath ,
47+ '--to' ,
48+ format ,
49+ '--output' ,
50+ path . basename ( tempTarget . filePath ) ,
51+ '--output-dir' ,
52+ path . dirname ( tempTarget . filePath )
53+ ] ;
3654 const result = await service . execModule ( 'jupyter' , [ 'nbconvert' ] . concat ( args ) , {
3755 throwOnStdErr : false ,
38- encoding : 'utf8'
56+ encoding : 'utf8' ,
57+ token : token
3958 } ) ;
4059
41- // Need to check if export failed, since throwOnStdErr is not an
42- // indicator of a failed export.
43- if ( ! ( await this . fileSystem . fileExists ( target . fsPath ) ) ) {
60+ if ( token . isCancellationRequested ) {
61+ tempTarget . dispose ( ) ;
62+ return ;
63+ }
64+
65+ try {
66+ await this . fileSystem . copyFile ( tempTarget . filePath , target . fsPath ) ;
67+ } catch {
4468 throw new Error ( result . stderr ) ;
45- } else if ( oldFileExists ) {
46- // If we exported to a file that already exists we need to check that
47- // this file was actually overridden during export
48- const newFileTime = ( await this . fileSystem . stat ( target . fsPath ) ) . mtime ;
49- if ( newFileTime === oldFileTime ) {
50- throw new Error ( result . stderr ) ;
51- }
69+ } finally {
70+ tempTarget . dispose ( ) ;
5271 }
5372 }
5473
0 commit comments