Skip to content

Commit b0174f9

Browse files
author
Ankit Saini
authored
Use multiline quotes in Powershell to simplify generated code (#679)
* Use multiline quotes in Powershell to simplify generated code * Update package list in CI before installing packages * Only unescape newlines for raw body * Fix usage of multiline strings * Remove console log
1 parent 61426cf commit b0174f9

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

codegens/powershell-restmethod/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function parseFormData (body, trim) {
8282
* @param {boolean} trim trim body option
8383
*/
8484
function parseRawBody (body, trim) {
85-
return `$body = "${sanitize(body.toString(), trim)}"\n`;
85+
return `$body = @"\n${sanitize(body.toString(), trim, false)}\n"@\n`;
8686
}
8787

8888
/**

codegens/powershell-restmethod/lib/util.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@
55
*
66
* @param {String} inputString
77
* @param {Boolean} [trim] - indicates whether to trim string or not
8+
* @param {Boolean} shouldEscapeNewLine - indicates whether to escape newline
89
* @returns {String}
910
*/
10-
function sanitize (inputString, trim) {
11+
function sanitize (inputString, trim, shouldEscapeNewLine = true) {
1112
if (typeof inputString !== 'string') {
1213
return '';
1314
}
1415
inputString = inputString
1516
.replace(/`/g, '``')
1617
.replace(/\$/g, '`$')
1718
.replace(/\\/g, '\`\\')
18-
.replace(/\"/g, '\`\"')
19-
.replace(/\n/g, '\`n');
19+
.replace(/\"/g, '\`\"');
20+
21+
if (shouldEscapeNewLine) {
22+
inputString = inputString.replace(/\n/g, '\`n');
23+
}
2024
return trim ? inputString.trim() : inputString;
2125
}
2226

codegens/powershell-restmethod/test/unit/convert.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,16 @@ describe('Powershell-restmethod converter', function () {
180180
expect.fail(null, null, error);
181181
return;
182182
}
183+
183184
const lines = snippet.split('\n');
184185
expect(lines[0]).to
185186
.eql('$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"');
186187
expect(lines[1]).to.eql('$headers.Add("Content-Type", "text/plain")');
187-
expect(lines[3]).to.eql('$body = "Hello world"');
188-
expect(lines[5]).to.eql('$response = Invoke-RestMethod \'https://mockbin.org/request\' -Method \'POST\' -Headers $headers -Body $body -TimeoutSec 10'); // eslint-disable-line max-len
189-
expect(lines[6]).to.eql('$response | ConvertTo-Json');
188+
expect(lines[3]).to.eql('$body = @"');
189+
expect(lines[4]).to.eql('Hello world');
190+
expect(lines[5]).to.eql('"@');
191+
expect(lines[7]).to.eql('$response = Invoke-RestMethod \'https://mockbin.org/request\' -Method \'POST\' -Headers $headers -Body $body -TimeoutSec 10'); // eslint-disable-line max-len
192+
expect(lines[8]).to.eql('$response | ConvertTo-Json');
190193
});
191194
});
192195
});

npm/ci-requirements.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/bash
22
set -ev; # stop on error
33

4+
sudo apt-get update
5+
46
echo "Insalling dependencies required for tests in codegens/libcurl"
57
sudo apt-get install libcurl4-gnutls-dev
68

0 commit comments

Comments
 (0)