@@ -64,19 +64,19 @@ function handleStreamClose(command: string, stdErrWritten: boolean, defer: Q.Def
6464
6565/**
6666 * Uses sftp to copy a file to remote machine
67- * @param src
68- * @param dest
69- * @param sftpConfig
67+ * @param { string } absolutePath - Data source for data to copy to the remote server.
68+ * @param { string } remotePath - Path to the remote file to be created on the server.
69+ * @param { SftpClient.ConnectOptions } sftpConfig
7070 * @returns {Promise<string> }
7171 */
72- export async function copyScriptToRemoteMachine ( src : string , dest : string , sftpConfig : SftpClient . ConnectOptions ) : Promise < string > {
72+ export async function copyScriptToRemoteMachine ( absolutePath : string , remotePath : string , sftpConfig : SftpClient . ConnectOptions ) : Promise < string > {
7373 const defer = Q . defer < string > ( ) ;
7474 const sftpClient = new SftpClient ( ) ;
7575
7676 try {
7777 await sftpClient . connect ( sftpConfig ) ;
78- await sftpClient . put ( src , dest ) ;
79- tl . debug ( `Copied script file to remote machine at: ${ dest } ` ) ;
78+ await sftpClient . put ( absolutePath , remotePath ) ;
79+ tl . debug ( `Copied script file to remote machine at: ${ remotePath } ` ) ;
8080 defer . resolve ( ) ;
8181 } catch ( err ) {
8282 defer . reject ( tl . loc ( 'RemoteCopyFailed' , err ) ) ;
@@ -203,3 +203,30 @@ export interface ScpConfig {
203203 /** For an encrypted private key, this is the passphrase used to decrypt it. */
204204 passphrase ?: string ;
205205}
206+
207+ /**
208+ * This function generates a new file with *_unix extension on the remote host
209+ * which contains the same file but without Windows CR LF
210+ * @param {ssh2.Client } sshClientConnection - ssh client instance
211+ * @param {RemoteCommandOptions } remoteCmdOptions
212+ * @param {string } remoteInputFilePath - remote path to target file
213+ * @throws will throw an error if command execution fails on remote host
214+ * @return {string } - path to the generated file
215+ */
216+ export async function clearFileFromWindowsCRLF ( sshClientConnection : ssh2 . Client , remoteCmdOptions : RemoteCommandOptions , remoteInputFilePath : string ) : Promise < string > {
217+ const remoteOutputFilePath = `${ remoteInputFilePath } ._unix` ;
218+ const removeLineEndingsCmd = `tr -d \'\\015\' <${ remoteInputFilePath } > ${ remoteOutputFilePath } ` ;
219+
220+ console . log ( removeLineEndingsCmd ) ;
221+
222+ try {
223+ tl . debug ( `Removing Windows CR LF from ${ remoteInputFilePath } ` ) ;
224+ await runCommandOnRemoteMachine ( removeLineEndingsCmd , sshClientConnection , remoteCmdOptions ) ;
225+ } catch ( error ) {
226+ throw new Error ( error ) ;
227+ }
228+
229+ tl . debug ( `Path to generated file = ${ remoteOutputFilePath } ` ) ;
230+
231+ return remoteOutputFilePath ;
232+ }
0 commit comments