'use strict' ; import * as vscode from 'vscode' ; import { Reporter } from '../util/Reporter' ; import * as fs from 'fs-extra' ; import * as path from 'path' ; import * as os from 'os' ; import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter' ; import { LogType } from 'ibm-blockchain-platform-common' ; import { ExtensionUtil } from '../util/ExtensionUtil' ; export async function saveTutorial ( tutorialObject , saveAll ? , tutorialFolder ? ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; outputAdapter . log ( LogType . INFO , undefined , 'saveTutorial' ) ; try { let packageUri ; let folderPath ; let defaultPath ; let fileName ; const extensionPath = ExtensionUtil . getExtensionPath ( ) ; const tutorialBaseDir = path . join ( extensionPath , 'resources' , 'tutorials' ) ; if ( saveAll ) { folderPath = path . join ( tutorialBaseDir , 'new-tutorials' , tutorialFolder , 'pdf' ) ; defaultPath = path . join ( os . homedir ( ) , tutorialFolder ) ; packageUri = await vscode . window . showSaveDialog ( { defaultUri : vscode . Uri . file ( defaultPath ) } ) ; if ( ! packageUri ) { return ; } await fs . copy ( folderPath , packageUri . fsPath ) ; outputAdapter . log ( LogType . SUCCESS , `Downloaded all ${ tutorialFolder } to ${ packageUri . fsPath }.` ) ; } else { const pathToFolder = ( tutorialObject . file ) . substring ( 0 , tutorialObject . file . lastIndexOf ( '/' ) ) ; folderPath = path . join ( tutorialBaseDir , pathToFolder , 'pdf' ) ; const tutorialPDF = tutorialObject . title . split ( ':' ) [ 0 ] . toLocaleLowerCase ( ) ; const pdfPath = path . join ( folderPath , `${ tutorialPDF }.pdf` ) ; fileName = `${ tutorialPDF }.pdf` ; defaultPath = path . join ( os . homedir ( ) , fileName ) ; packageUri = await vscode . window . showSaveDialog ( { defaultUri : vscode . Uri . file ( defaultPath ) } ) ; if ( ! packageUri ) { return ; } await fs . copy ( pdfPath , packageUri . fsPath ) ; outputAdapter . log ( LogType . SUCCESS , `Downloaded tutorial ${ tutorialPDF }.pdf to ${ packageUri . fsPath }.` ) ; } Reporter . instance ( ) . sendTelemetryEvent ( 'saveTutorialCommand' ) ; } catch ( error ) { outputAdapter . log ( LogType . ERROR , error . message , error . toString ( ) ) ; } }