Skip to content

Commit 08310df

Browse files
committed
fix(core): no indent based on first line
Signed-off-by: 90dy <90dy@proton.me>
1 parent cbac572 commit 08310df

4 files changed

Lines changed: 51 additions & 2 deletions

File tree

src/core/mod.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,11 @@ class TemplateClass<T = unknown> extends String {
338338

339339
}
340340
if (options?.indent === false) {
341-
str = str.replace(/^\s+/gm, "");
341+
// Get the first non-empty line to determine the indentation
342+
const lines = str.split("\n");
343+
const nonEmptyLine = lines.find(line => line.trim() !== "") || "";
344+
const indent = nonEmptyLine.match(/^\s+/)?.[0]?.length || 0;
345+
str = lines.map(line => line.slice(indent)).join("\n");
342346
}
343347

344348

src/core/mod_test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,10 @@ Deno.test("Template indentation works correctly", () => {
193193
assertEquals(indented.toString() !== original.toString(), true);
194194
assertEquals(noIndent.toString() !== original.toString(), true);
195195

196+
console.error("Indented Template:", indented.toString());
197+
console.error("No Indent Template:", noIndent.toString());
196198
// Test that noIndent template has no leading whitespace
197-
assertEquals(noIndent.toString().startsWith("<div>"), true);
199+
assertEquals(noIndent.toString().startsWith("\n<div>"), true);
198200
});
199201

200202
Deno.test("Custom extension template works correctly", () => {

src/gen/fixtures/test-no-indent.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
sudo mkdir -p $(dirname /etc/kubernetes/encryption-config.yaml)
3+
cat <<EOF | sudo tee /etc/kubernetes/encryption-config.yaml
4+
5+
apiVersion: apiserver.config.k8s.io/v1
6+
kind: EncryptionConfiguration
7+
resources:
8+
- resources:
9+
- secrets
10+
providers:
11+
- aescbc:
12+
keys:
13+
- name: aescbc-key-20250508
14+
- secretbox:
15+
keys:
16+
- name: secretbox-key-20250508
17+
- identity: {}
18+
19+
20+
EOF
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { sh } from '@tmpl/core'
2+
3+
export default sh`
4+
sudo mkdir -p $(dirname /etc/kubernetes/encryption-config.yaml)
5+
cat <<EOF | sudo tee /etc/kubernetes/encryption-config.yaml
6+
7+
apiVersion: apiserver.config.k8s.io/v1
8+
kind: EncryptionConfiguration
9+
resources:
10+
- resources:
11+
- secrets
12+
providers:
13+
- aescbc:
14+
keys:
15+
- name: aescbc-key-20250508
16+
- secretbox:
17+
keys:
18+
- name: secretbox-key-20250508
19+
- identity: {}
20+
21+
22+
EOF
23+
`.noindent()

0 commit comments

Comments
 (0)