Skip to content

Commit ce91f07

Browse files
committed
feat: Allow multiline commands
1 parent b8cb876 commit ce91f07

File tree

6 files changed

+31
-25
lines changed

6 files changed

+31
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codify-plugin-lib",
3-
"version": "1.0.182-beta26",
3+
"version": "1.0.182-beta27",
44
"description": "Library plugin library",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

src/pty/background-pty.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,19 @@ export class BackgroundPty implements IPty {
3333
this.initialize();
3434
}
3535

36-
async spawn(cmd: string, options?: SpawnOptions): Promise<SpawnResult> {
36+
async spawn(cmd: string | string[], options?: SpawnOptions): Promise<SpawnResult> {
3737
const spawnResult = await this.spawnSafe(cmd, options);
3838

3939
if (spawnResult.status !== 'success') {
40-
throw new SpawnError(cmd, spawnResult.exitCode, spawnResult.data);
40+
throw new SpawnError(Array.isArray(cmd) ? cmd.join(' ') : cmd, spawnResult.exitCode, spawnResult.data);
4141
}
4242

4343
return spawnResult;
4444
}
4545

46-
async spawnSafe(cmd: string, options?: SpawnOptions): Promise<SpawnResult> {
46+
async spawnSafe(cmd: string | string[], options?: SpawnOptions): Promise<SpawnResult> {
47+
cmd = Array.isArray(cmd) ? cmd.join('\\\n') : cmd;
48+
4749
// cid is command id
4850
const cid = nanoid(10);
4951
debugLog(cid);
@@ -104,7 +106,7 @@ export class BackgroundPty implements IPty {
104106
}
105107
});
106108

107-
console.log(`Running command ${cmd}${options?.cwd ? ` (cwd: ${options.cwd})` : ''}`)
109+
console.log(`Running command: ${cmd}${options?.cwd ? ` (cwd: ${options.cwd})` : ''}`)
108110
this.basePty.write(`${command}\r`);
109111

110112
}));
@@ -130,19 +132,6 @@ export class BackgroundPty implements IPty {
130132
let outputBuffer = '';
131133

132134
return new Promise(resolve => {
133-
// zsh-specific commands
134-
// switch (Utils.getShell()) {
135-
// case Shell.ZSH: {
136-
// this.basePty.write('setopt HIST_NO_STORE;\n');
137-
// break;
138-
// }
139-
//
140-
// default: {
141-
// this.basePty.write('export HISTIGNORE=\'history*\';\n');
142-
// break;
143-
// }
144-
// }
145-
146135
this.basePty.write(' unset PS1;\n');
147136
this.basePty.write(' unset PS0;\n')
148137
this.basePty.write(' echo setup complete\\"\n')

src/pty/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ export class SpawnError extends Error {
5050
}
5151

5252
export interface IPty {
53-
spawn(cmd: string, options?: SpawnOptions): Promise<SpawnResult>
53+
spawn(cmd: string | string[], options?: SpawnOptions): Promise<SpawnResult>
5454

55-
spawnSafe(cmd: string, options?: SpawnOptions): Promise<SpawnResult>
55+
spawnSafe(cmd: string | string[], options?: SpawnOptions): Promise<SpawnResult>
5656

5757
kill(): Promise<{ exitCode: number, signal?: number | undefined }>
5858
}

src/pty/seqeuntial-pty.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ const validateSudoRequestResponse = ajv.compile(CommandRequestResponseDataSchema
2323
* without a tty (or even a stdin) attached so interactive commands will not work.
2424
*/
2525
export class SequentialPty implements IPty {
26-
async spawn(cmd: string, options?: SpawnOptions): Promise<SpawnResult> {
26+
async spawn(cmd: string | string[], options?: SpawnOptions): Promise<SpawnResult> {
2727
const spawnResult = await this.spawnSafe(cmd, options);
2828

2929
if (spawnResult.status !== 'success') {
30-
throw new SpawnError(cmd, spawnResult.exitCode, spawnResult.data);
30+
throw new SpawnError(Array.isArray(cmd) ? cmd.join('\n') : cmd, spawnResult.exitCode, spawnResult.data);
3131
}
3232

3333
return spawnResult;
3434
}
3535

36-
async spawnSafe(cmd: string, options?: SpawnOptions): Promise<SpawnResult> {
36+
async spawnSafe(cmd: string | string[], options?: SpawnOptions): Promise<SpawnResult> {
37+
cmd = Array.isArray(cmd) ? cmd.join(' ') : cmd;
38+
3739
if (cmd.includes('sudo')) {
3840
throw new Error('Do not directly use sudo. Use the option { requiresRoot: true } instead')
3941
}
@@ -43,7 +45,7 @@ export class SequentialPty implements IPty {
4345
return this.externalSpawn(cmd, options);
4446
}
4547

46-
console.log(`Running command: ${cmd}` + (options?.cwd ? `(${options?.cwd})` : ''))
48+
console.log(`Running command: ${Array.isArray(cmd) ? cmd.join('\\\n') : cmd}` + (options?.cwd ? `(${options?.cwd})` : ''))
4749

4850
return new Promise((resolve) => {
4951
const output: string[] = [];

src/pty/sequential-pty.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ describe('SequentialPty tests', () => {
5151
})
5252
});
5353

54+
55+
it('Can use multi-line commands', async () => {
56+
const pty = new SequentialPty();
57+
58+
const resultSuccess = await pty.spawnSafe([
59+
'pwd',
60+
'&& ls',
61+
], { cwd: '/tmp' });
62+
expect(resultSuccess).toMatchObject({
63+
status: 'success',
64+
exitCode: 0,
65+
})
66+
});
67+
68+
5469
it('It can launch a command in interactive mode', async () => {
5570
const originalSend = process.send;
5671
process.send = (req: IpcMessageV2) => {

src/utils/functions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function tildify(pathWithTilde: string) {
3636
}
3737

3838
export function resolvePathWithVariables(pathWithVariables: string) {
39-
return pathWithVariables.replaceAll(/\$([A-Z_]+[A-Z0-9_]*)|\${([A-Z0-9_]*)}/ig, (_, a, b) => process.env[a || b])
39+
return pathWithVariables.replace(/\$([A-Z_]+[A-Z0-9_]*)|\${([A-Z0-9_]*)}/ig, (_, a, b) => process.env[a || b]!)
4040
}
4141

4242
export function addVariablesToPath(pathWithoutVariables: string) {

0 commit comments

Comments
 (0)