Skip to content

Commit 1758d57

Browse files
author
test2
committed
fix: add escaping and apply notes to alias resource
1 parent 6af4520 commit 1758d57

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "default",
3-
"version": "1.15.3-beta.1",
3+
"version": "1.15.3-beta.2",
44
"description": "Default plugin for Codify - provides 50+ declarative resources for managing development tools and system configuration across macOS and Linux",
55
"main": "dist/index.js",
66
"scripts": {

src/resources/shell/alias/alias-resource.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {
2+
ApplyNotes,
3+
CodifyCliSender,
24
CreatePlan,
35
DestroyPlan,
46
ExampleConfig,
@@ -96,7 +98,7 @@ export class AliasResource extends Resource<AliasConfig> {
9698
}
9799

98100
const name = aliasMatch[1].trim();
99-
const value = aliasMatch[2].trim();
101+
const value = this.unescapeAliasValue(aliasMatch[2].trim());
100102

101103
return {
102104
alias: name,
@@ -115,6 +117,8 @@ export class AliasResource extends Resource<AliasConfig> {
115117
const aliasString = this.aliasString(alias, value);
116118

117119
await FileUtils.addToStartupFile(aliasString);
120+
121+
CodifyCliSender.sendApplyNote(ApplyNotes.NEW_SHELL_REQUIRED);
118122
}
119123

120124
async modify(pc: ParameterChange<AliasConfig>, plan: ModifyPlan<AliasConfig>): Promise<void> {
@@ -143,6 +147,8 @@ export class AliasResource extends Resource<AliasConfig> {
143147
lines.splice(aliasLineNum, 1, newAlias);
144148

145149
await fs.writeFile(aliasInfo.path, lines.join('\n'), 'utf8');
150+
151+
CodifyCliSender.sendApplyNote(ApplyNotes.NEW_SHELL_REQUIRED);
146152
}
147153

148154
async destroy(plan: DestroyPlan<AliasConfig>): Promise<void> {
@@ -157,6 +163,8 @@ export class AliasResource extends Resource<AliasConfig> {
157163

158164
await FileUtils.removeLineFromFile(aliasInfo.path, aliasString);
159165
await FileUtils.removeLineFromFile(aliasInfo.path, aliasStringShort);
166+
167+
CodifyCliSender.sendApplyNote(ApplyNotes.NEW_SHELL_REQUIRED);
160168
}
161169

162170
private async findAlias(alias: string, value: string): Promise<{ path: string; contents: string; } | null> {
@@ -182,10 +190,21 @@ export class AliasResource extends Resource<AliasConfig> {
182190
}
183191

184192
private aliasString(alias: string, value: string): string {
185-
return `alias ${alias}='${value}'`
193+
return `alias ${alias}='${this.escapeAliasValue(value)}'`
186194
}
187195

188196
private aliasStringShort(alias: string, value: string): string {
189197
return `alias ${alias}=${value}`
190198
}
199+
200+
// Escapes single quotes for embedding inside a single-quoted shell string:
201+
// close the quote, insert an escaped quote, reopen the quote (POSIX ' -> '\'')
202+
private escapeAliasValue(value: string): string {
203+
return value.replace(/'/g, `'\\''`);
204+
}
205+
206+
// Reverses escapeAliasValue when parsing alias output the shell echoes back
207+
private unescapeAliasValue(value: string): string {
208+
return value.replace(/'\\''/g, `'`);
209+
}
191210
}

0 commit comments

Comments
 (0)