Skip to content

Commit be8052f

Browse files
committed
feat: Added utils for managing different environments. Updated spawn to handle additional shells
1 parent 1e3f1e9 commit be8052f

File tree

10 files changed

+2995
-1232
lines changed

10 files changed

+2995
-1232
lines changed

package-lock.json

Lines changed: 2806 additions & 1211 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codify-plugin-lib",
3-
"version": "1.0.180",
3+
"version": "1.0.181",
44
"description": "Library plugin library",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",
@@ -10,42 +10,47 @@
1010
"posttest": "tsc",
1111
"prepublishOnly": "tsc"
1212
},
13+
"bin": {
14+
"codify-deploy": "./dist/bin/deploy-plugin.js"
15+
},
1316
"keywords": [],
1417
"author": "",
1518
"license": "ISC",
1619
"dependencies": {
20+
"@homebridge/node-pty-prebuilt-multiarch": "^0.13.1",
21+
"@npmcli/promise-spawn": "^7.0.1",
1722
"ajv": "^8.12.0",
1823
"ajv-formats": "^2.1.1",
24+
"clean-deep": "^3.4.0",
1925
"codify-schemas": "1.0.83",
20-
"@npmcli/promise-spawn": "^7.0.1",
21-
"@homebridge/node-pty-prebuilt-multiarch": "^0.12.0-beta.5",
22-
"uuid": "^10.0.0",
2326
"lodash.isequal": "^4.5.0",
2427
"nanoid": "^5.0.9",
2528
"strip-ansi": "^7.1.0",
26-
"clean-deep": "^3.4.0"
29+
"uuid": "^10.0.0"
2730
},
2831
"devDependencies": {
32+
"@apidevtools/json-schema-ref-parser": "^11.7.2",
2933
"@oclif/prettier-config": "^0.2.1",
3034
"@oclif/test": "^3",
31-
"@types/npmcli__promise-spawn": "^6.0.3",
35+
"@types/lodash.isequal": "^4.5.8",
3236
"@types/node": "^20",
37+
"@types/npmcli__promise-spawn": "^6.0.3",
3338
"@types/semver": "^7.5.4",
3439
"@types/sinon": "^17.0.3",
3540
"@types/uuid": "^10.0.0",
36-
"@types/lodash.isequal": "^4.5.8",
3741
"chai-as-promised": "^7.1.1",
38-
"vitest": "^3.0.5",
39-
"vitest-mock-extended": "^1.3.1",
40-
"sinon": "^17.0.1",
4142
"eslint": "^8.51.0",
4243
"eslint-config-oclif": "^5",
4344
"eslint-config-oclif-typescript": "^3",
45+
"eslint-config-prettier": "^9.0.0",
46+
"merge-json-schemas": "^1.0.0",
4447
"shx": "^0.3.3",
48+
"sinon": "^17.0.1",
4549
"ts-node": "^10.9.1",
4650
"tsc-watch": "^6.0.4",
4751
"typescript": "^5",
48-
"eslint-config-prettier": "^9.0.0"
52+
"vitest": "^3.0.5",
53+
"vitest-mock-extended": "^1.3.1"
4954
},
5055
"engines": {
5156
"node": ">=18.0.0"

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export * from './resource/parsed-resource-settings.js';
1212
export * from './resource/resource.js'
1313
export * from './resource/resource-settings.js'
1414
export * from './stateful-parameter/stateful-parameter.js'
15-
export * from './utils/utils.js'
15+
export * from './utils/index.js'
1616

1717
export async function runPlugin(plugin: Plugin) {
1818
const messageHandler = new MessageHandler(plugin);

src/plugin/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { getPty } from '../pty/index.js';
2424
import { Resource } from '../resource/resource.js';
2525
import { ResourceController } from '../resource/resource-controller.js';
2626
import { ptyLocalStorage } from '../utils/pty-local-storage.js';
27-
import { VerbosityLevel } from '../utils/utils.js';
27+
import { VerbosityLevel } from '../utils/internal-utils.js';
2828

2929
export class Plugin {
3030
planStorage: Map<string, Plan<any>>;

src/pty/background-pty.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import * as fs from 'node:fs/promises';
66
import stripAnsi from 'strip-ansi';
77

88
import { debugLog } from '../utils/debug.js';
9-
import { VerbosityLevel } from '../utils/utils.js';
9+
import { Shell, Utils } from '../utils/index.js';
10+
import { VerbosityLevel } from '../utils/internal-utils.js';
1011
import { IPty, SpawnError, SpawnOptions, SpawnResult } from './index.js';
1112
import { PromiseQueue } from './promise-queue.js';
1213

@@ -19,7 +20,7 @@ EventEmitter.defaultMaxListeners = 1000;
1920
* without a tty (or even a stdin) attached so interactive commands will not work.
2021
*/
2122
export class BackgroundPty implements IPty {
22-
private basePty = pty.spawn('zsh', ['-i'], {
23+
private basePty = pty.spawn(this.getDefaultShell(), ['-i'], {
2324
env: process.env, name: nanoid(6),
2425
handleFlowControl: true
2526
});
@@ -127,7 +128,19 @@ export class BackgroundPty implements IPty {
127128
let outputBuffer = '';
128129

129130
return new Promise(resolve => {
130-
this.basePty.write('setopt hist_ignore_space;\n');
131+
// zsh-specific commands
132+
switch (Utils.getShell()) {
133+
case Shell.ZSH: {
134+
this.basePty.write('setopt HIST_NO_STORE;\n');
135+
break;
136+
}
137+
138+
default: {
139+
this.basePty.write('export HISTIGNORE=\'history*\';\n');
140+
break;
141+
}
142+
}
143+
131144
this.basePty.write(' unset PS1;\n');
132145
this.basePty.write(' unset PS0;\n')
133146
this.basePty.write(' echo setup complete\\"\n')
@@ -142,4 +155,8 @@ export class BackgroundPty implements IPty {
142155
})
143156
})
144157
}
158+
159+
private getDefaultShell(): string {
160+
return process.env.SHELL!;
161+
}
145162
}

src/resource/resource-controller.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { CreatePlan, DestroyPlan, ModifyPlan } from '../plan/plan-types.js';
77
import { ParameterChange } from '../plan/change-set.js';
88
import { ResourceController } from './resource-controller.js';
99
import { TestConfig, testPlan, TestResource, TestStatefulParameter } from '../utils/test-utils.test.js';
10-
import { tildify, untildify } from '../utils/utils.js';
10+
import { tildify, untildify } from '../utils/internal-utils.js';
1111
import { ArrayStatefulParameter, StatefulParameter } from '../stateful-parameter/stateful-parameter.js';
1212
import { Plan } from '../plan/plan.js';
1313
import os from 'node:os';

src/resource/resource-settings.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import isObjectsEqual from 'lodash.isequal'
44
import path from 'node:path';
55

66
import { ArrayStatefulParameter, StatefulParameter } from '../stateful-parameter/stateful-parameter.js';
7-
import { addVariablesToPath, areArraysEqual, resolvePathWithVariables, tildify, untildify } from '../utils/utils.js';
7+
import {
8+
addVariablesToPath,
9+
areArraysEqual,
10+
resolvePathWithVariables,
11+
tildify,
12+
untildify
13+
} from '../utils/internal-utils.js';
814
import { RefreshContext } from './resource.js';
915

1016
export interface InputTransformation {
@@ -350,8 +356,9 @@ const ParameterEqualsDefaults: Partial<Record<ParameterSettingType, (a: unknown,
350356
transformedB = path.resolve(transformedB)
351357
}
352358

353-
const notCaseSensitive = process.platform === 'darwin';
354-
if (notCaseSensitive) {
359+
// macOS has case-insensitive filesystem by default, Linux is case-sensitive
360+
const isCaseSensitive = process.platform === 'linux';
361+
if (!isCaseSensitive) {
355362
transformedA = transformedA.toLowerCase();
356363
transformedB = transformedB.toLowerCase();
357364
}

src/utils/index.ts

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import os from 'node:os';
2+
import path from 'node:path';
3+
4+
export enum Shell {
5+
ZSH = 'zsh',
6+
BASH = 'bash',
7+
SH = 'sh',
8+
KSH = 'ksh',
9+
CSH = 'csh',
10+
FISH = 'fish',
11+
}
12+
13+
export interface SystemInfo {
14+
os: 'Darwin' | 'Linux' | 'Windows_NT';
15+
shell: Shell;
16+
}
17+
18+
export const Utils = {
19+
getUser(): string {
20+
return os.userInfo().username;
21+
},
22+
23+
getSystemInfo() {
24+
return {
25+
os: os.type(),
26+
shell: this.getShell(),
27+
}
28+
},
29+
30+
isMacOS(): boolean {
31+
return os.platform() === 'darwin';
32+
},
33+
34+
isLinux(): boolean {
35+
return os.platform() === 'linux';
36+
},
37+
38+
getShell(): Shell | undefined {
39+
const shell = process.env.SHELL || '';
40+
41+
if (shell.endsWith('bash')) {
42+
return Shell.BASH
43+
}
44+
45+
if (shell.endsWith('zsh')) {
46+
return Shell.ZSH
47+
}
48+
49+
if (shell.endsWith('sh')) {
50+
return Shell.SH
51+
}
52+
53+
if (shell.endsWith('csh')) {
54+
return Shell.CSH
55+
}
56+
57+
if (shell.endsWith('ksh')) {
58+
return Shell.KSH
59+
}
60+
61+
if (shell.endsWith('fish')) {
62+
return Shell.FISH
63+
}
64+
65+
return undefined;
66+
},
67+
68+
69+
getPrimaryShellRc(): string {
70+
return this.getShellRcFiles()[0];
71+
},
72+
73+
getShellRcFiles(): string[] {
74+
const shell = process.env.SHELL || '';
75+
const homeDir = os.homedir();
76+
77+
if (shell.endsWith('bash')) {
78+
// Linux typically uses .bashrc, macOS uses .bash_profile
79+
if (Utils.isLinux()) {
80+
return [
81+
path.join(homeDir, '.bashrc'),
82+
path.join(homeDir, '.bash_profile'),
83+
path.join(homeDir, '.profile'),
84+
];
85+
}
86+
87+
return [
88+
path.join(homeDir, '.bash_profile'),
89+
path.join(homeDir, '.bashrc'),
90+
path.join(homeDir, '.profile'),
91+
];
92+
}
93+
94+
if (shell.endsWith('zsh')) {
95+
return [
96+
path.join(homeDir, '.zshrc'),
97+
path.join(homeDir, '.zprofile'),
98+
path.join(homeDir, '.zshenv'),
99+
];
100+
}
101+
102+
if (shell.endsWith('sh')) {
103+
return [
104+
path.join(homeDir, '.profile'),
105+
]
106+
}
107+
108+
if (shell.endsWith('ksh')) {
109+
return [
110+
path.join(homeDir, '.profile'),
111+
path.join(homeDir, '.kshrc'),
112+
]
113+
}
114+
115+
if (shell.endsWith('csh')) {
116+
return [
117+
path.join(homeDir, '.cshrc'),
118+
path.join(homeDir, '.login'),
119+
path.join(homeDir, '.logout'),
120+
]
121+
}
122+
123+
if (shell.endsWith('fish')) {
124+
return [
125+
path.join(homeDir, '.config/fish/config.fish'),
126+
]
127+
}
128+
129+
// Default to bash-style files
130+
return [
131+
path.join(homeDir, '.bashrc'),
132+
path.join(homeDir, '.bash_profile'),
133+
path.join(homeDir, '.profile'),
134+
];
135+
},
136+
};
137+
138+
139+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest';
2-
import { addVariablesToPath, resolvePathWithVariables, splitUserConfig } from './utils.js';
2+
import { addVariablesToPath, resolvePathWithVariables, splitUserConfig } from './internal-utils.js';
33
import os from 'node:os';
44

55
describe('Utils tests', () => {

0 commit comments

Comments
 (0)