@gengjiawen/os-init 1.25.0 → 1.26.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/README.md +13 -5
- package/bin/bin.js +34 -11
- package/build/all-agents.js +14 -3
- package/build/clash.d.ts +8 -0
- package/build/{mihomo.js → clash.js} +35 -30
- package/build/claude-code.js +21 -4
- package/build/index.d.ts +2 -1
- package/build/index.js +6 -4
- package/build/setup-env.d.ts +5 -0
- package/build/setup-env.js +132 -0
- package/build/utils.d.ts +3 -0
- package/build/utils.js +41 -5
- package/libs/all-agents.test.ts +14 -5
- package/libs/all-agents.ts +21 -4
- package/libs/{mihomo.test.ts → clash.test.ts} +29 -21
- package/libs/{mihomo.ts → clash.ts} +42 -37
- package/libs/claude-code.test.ts +47 -3
- package/libs/claude-code.ts +29 -5
- package/libs/index.ts +5 -2
- package/libs/setup-env.test.ts +85 -0
- package/libs/setup-env.ts +172 -0
- package/libs/utils.ts +51 -8
- package/macOS-init.md +10 -31
- package/package.json +4 -10
- package/build/mihomo.d.ts +0 -8
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import * as fs from 'fs'
|
|
2
|
+
import * as os from 'os'
|
|
3
|
+
import * as path from 'path'
|
|
4
|
+
import { setupEnv } from './setup-env'
|
|
5
|
+
|
|
6
|
+
describe('setupEnv', () => {
|
|
7
|
+
let tempHome: string
|
|
8
|
+
let homedirSpy: jest.SpiedFunction<typeof os.homedir>
|
|
9
|
+
let platformDescriptor: PropertyDescriptor | undefined
|
|
10
|
+
|
|
11
|
+
function mockPlatform(platform: NodeJS.Platform): void {
|
|
12
|
+
Object.defineProperty(process, 'platform', {
|
|
13
|
+
value: platform,
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'os-init-env-'))
|
|
19
|
+
homedirSpy = jest.spyOn(os, 'homedir').mockReturnValue(tempHome)
|
|
20
|
+
platformDescriptor = Object.getOwnPropertyDescriptor(process, 'platform')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
homedirSpy.mockRestore()
|
|
25
|
+
if (platformDescriptor) {
|
|
26
|
+
Object.defineProperty(process, 'platform', platformDescriptor)
|
|
27
|
+
}
|
|
28
|
+
fs.rmSync(tempHome, { recursive: true, force: true })
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
test('writes environment block to bashrc on Linux', () => {
|
|
32
|
+
mockPlatform('linux')
|
|
33
|
+
|
|
34
|
+
const result = setupEnv()
|
|
35
|
+
const bashrcPath = path.join(tempHome, '.bashrc')
|
|
36
|
+
|
|
37
|
+
expect(result).toEqual({
|
|
38
|
+
bashrcPath,
|
|
39
|
+
changed: true,
|
|
40
|
+
})
|
|
41
|
+
expect(fs.readFileSync(bashrcPath, 'utf-8')).toContain('export PNPM_HOME=')
|
|
42
|
+
expect(fs.existsSync(path.join(tempHome, '.bash_profile'))).toBe(false)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test('creates macOS bash profile that sources bashrc', () => {
|
|
46
|
+
mockPlatform('darwin')
|
|
47
|
+
|
|
48
|
+
const result = setupEnv()
|
|
49
|
+
const bashProfilePath = path.join(tempHome, '.bash_profile')
|
|
50
|
+
|
|
51
|
+
expect(result).toEqual({
|
|
52
|
+
bashrcPath: path.join(tempHome, '.bashrc'),
|
|
53
|
+
bashProfilePath,
|
|
54
|
+
changed: true,
|
|
55
|
+
})
|
|
56
|
+
expect(fs.readFileSync(bashProfilePath, 'utf-8')).toContain(
|
|
57
|
+
'. "$HOME/.bashrc"'
|
|
58
|
+
)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('does not duplicate an existing macOS bashrc source line', () => {
|
|
62
|
+
mockPlatform('darwin')
|
|
63
|
+
const bashProfilePath = path.join(tempHome, '.bash_profile')
|
|
64
|
+
const bashProfileContent = 'source ~/.bashrc\n'
|
|
65
|
+
fs.writeFileSync(bashProfilePath, bashProfileContent)
|
|
66
|
+
|
|
67
|
+
const result = setupEnv()
|
|
68
|
+
|
|
69
|
+
expect(result.bashProfilePath).toBe(bashProfilePath)
|
|
70
|
+
expect(fs.readFileSync(bashProfilePath, 'utf-8')).toBe(bashProfileContent)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test('second macOS run reports unchanged files', () => {
|
|
74
|
+
mockPlatform('darwin')
|
|
75
|
+
|
|
76
|
+
setupEnv()
|
|
77
|
+
const result = setupEnv()
|
|
78
|
+
|
|
79
|
+
expect(result).toEqual({
|
|
80
|
+
bashrcPath: path.join(tempHome, '.bashrc'),
|
|
81
|
+
bashProfilePath: path.join(tempHome, '.bash_profile'),
|
|
82
|
+
changed: false,
|
|
83
|
+
})
|
|
84
|
+
})
|
|
85
|
+
})
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import * as fs from 'fs'
|
|
2
|
+
import * as path from 'path'
|
|
3
|
+
import * as os from 'os'
|
|
4
|
+
|
|
5
|
+
const MARKER_START = '# ===== os-init setup-env - START ====='
|
|
6
|
+
const MARKER_END = '# ===== os-init setup-env - END ====='
|
|
7
|
+
const BASH_PROFILE_MARKER_START =
|
|
8
|
+
'# ===== os-init setup-env bash_profile - START ====='
|
|
9
|
+
const BASH_PROFILE_MARKER_END =
|
|
10
|
+
'# ===== os-init setup-env bash_profile - END ====='
|
|
11
|
+
|
|
12
|
+
function getBrewPrefix(): string {
|
|
13
|
+
if (process.platform === 'darwin') {
|
|
14
|
+
if (fs.existsSync('/opt/homebrew/bin/brew')) {
|
|
15
|
+
return '/opt/homebrew'
|
|
16
|
+
}
|
|
17
|
+
return '/usr/local'
|
|
18
|
+
}
|
|
19
|
+
return '/home/linuxbrew/.linuxbrew'
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getPnpmHome(): string {
|
|
23
|
+
const home = os.homedir()
|
|
24
|
+
if (process.platform === 'darwin') {
|
|
25
|
+
return path.join(home, 'Library', 'pnpm')
|
|
26
|
+
}
|
|
27
|
+
return path.join(home, '.pnpm')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function generateBashrcContent(): string {
|
|
31
|
+
const brewPrefix = getBrewPrefix()
|
|
32
|
+
const home = os.homedir()
|
|
33
|
+
const pnpmHome = getPnpmHome()
|
|
34
|
+
const cargoHome = path.join(home, '.cargo')
|
|
35
|
+
const rustupHome = path.join(home, '.rustup')
|
|
36
|
+
|
|
37
|
+
const pathEntries = [
|
|
38
|
+
`${brewPrefix}/bin`,
|
|
39
|
+
`${brewPrefix}/sbin`,
|
|
40
|
+
pnpmHome,
|
|
41
|
+
`${cargoHome}/bin`,
|
|
42
|
+
`${home}/.jsvu/bin`,
|
|
43
|
+
`${home}/.local/bin`,
|
|
44
|
+
`${home}/.yarn/bin`,
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
if (process.platform === 'darwin') {
|
|
48
|
+
pathEntries.push(`${rustupHome}/toolchains/stable-aarch64-apple-darwin/bin`)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return `${MARKER_START}
|
|
52
|
+
export PNPM_HOME="${pnpmHome}"
|
|
53
|
+
export RUSTUP_HOME="${rustupHome}"
|
|
54
|
+
export CARGO_HOME="${cargoHome}"
|
|
55
|
+
export PATH="${pathEntries.join(':')}:$PATH"
|
|
56
|
+
${MARKER_END}
|
|
57
|
+
`
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function generateBashProfileContent(): string {
|
|
61
|
+
return `${BASH_PROFILE_MARKER_START}
|
|
62
|
+
if [ -f "$HOME/.bashrc" ]; then
|
|
63
|
+
. "$HOME/.bashrc"
|
|
64
|
+
fi
|
|
65
|
+
${BASH_PROFILE_MARKER_END}
|
|
66
|
+
`
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function stripLeadingNewline(content: string): string {
|
|
70
|
+
if (content.startsWith('\r\n')) {
|
|
71
|
+
return content.slice(2)
|
|
72
|
+
}
|
|
73
|
+
if (content.startsWith('\n')) {
|
|
74
|
+
return content.slice(1)
|
|
75
|
+
}
|
|
76
|
+
return content
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function bashProfileSourcesBashrc(content: string): boolean {
|
|
80
|
+
return content
|
|
81
|
+
.split(/\r?\n/)
|
|
82
|
+
.some((line) =>
|
|
83
|
+
/^\s*(?:source|\.)\s+(?:"\$HOME\/\.bashrc"|'\$HOME\/\.bashrc'|\$HOME\/\.bashrc|~\/\.bashrc)(?:\s|$)/.test(
|
|
84
|
+
line
|
|
85
|
+
)
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function ensureMacBashProfileSourcesBashrc(home: string): {
|
|
90
|
+
bashProfilePath: string
|
|
91
|
+
changed: boolean
|
|
92
|
+
} {
|
|
93
|
+
const bashProfilePath = path.join(home, '.bash_profile')
|
|
94
|
+
let currentContent = ''
|
|
95
|
+
|
|
96
|
+
if (fs.existsSync(bashProfilePath)) {
|
|
97
|
+
currentContent = fs.readFileSync(bashProfilePath, 'utf-8')
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (
|
|
101
|
+
currentContent.includes(BASH_PROFILE_MARKER_START) ||
|
|
102
|
+
bashProfileSourcesBashrc(currentContent)
|
|
103
|
+
) {
|
|
104
|
+
return { bashProfilePath, changed: false }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const newBlock =
|
|
108
|
+
(currentContent && !currentContent.endsWith('\n') ? '\n' : '') +
|
|
109
|
+
generateBashProfileContent()
|
|
110
|
+
fs.appendFileSync(bashProfilePath, newBlock)
|
|
111
|
+
|
|
112
|
+
return { bashProfilePath, changed: true }
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function setupEnv(): {
|
|
116
|
+
bashrcPath: string
|
|
117
|
+
bashProfilePath?: string
|
|
118
|
+
changed: boolean
|
|
119
|
+
} {
|
|
120
|
+
const home = os.homedir()
|
|
121
|
+
const bashrcPath = path.join(home, '.bashrc')
|
|
122
|
+
|
|
123
|
+
let currentContent = ''
|
|
124
|
+
if (fs.existsSync(bashrcPath)) {
|
|
125
|
+
currentContent = fs.readFileSync(bashrcPath, 'utf-8')
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (currentContent.includes(MARKER_START)) {
|
|
129
|
+
// Replace existing block
|
|
130
|
+
const startIdx = currentContent.indexOf(MARKER_START)
|
|
131
|
+
const endIdx = currentContent.indexOf(MARKER_END, startIdx)
|
|
132
|
+
if (endIdx !== -1) {
|
|
133
|
+
const before = currentContent.slice(0, startIdx)
|
|
134
|
+
const after = stripLeadingNewline(
|
|
135
|
+
currentContent.slice(endIdx + MARKER_END.length)
|
|
136
|
+
)
|
|
137
|
+
const newBlock = generateBashrcContent()
|
|
138
|
+
const nextContent = before + newBlock + after
|
|
139
|
+
const bashrcChanged = nextContent !== currentContent
|
|
140
|
+
if (bashrcChanged) {
|
|
141
|
+
fs.writeFileSync(bashrcPath, nextContent)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (process.platform === 'darwin') {
|
|
145
|
+
const profileResult = ensureMacBashProfileSourcesBashrc(home)
|
|
146
|
+
return {
|
|
147
|
+
bashrcPath,
|
|
148
|
+
bashProfilePath: profileResult.bashProfilePath,
|
|
149
|
+
changed: bashrcChanged || profileResult.changed,
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return { bashrcPath, changed: bashrcChanged }
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Append new block
|
|
157
|
+
const newBlock =
|
|
158
|
+
(currentContent && !currentContent.endsWith('\n') ? '\n' : '') +
|
|
159
|
+
generateBashrcContent()
|
|
160
|
+
fs.appendFileSync(bashrcPath, newBlock)
|
|
161
|
+
|
|
162
|
+
if (process.platform === 'darwin') {
|
|
163
|
+
const profileResult = ensureMacBashProfileSourcesBashrc(home)
|
|
164
|
+
return {
|
|
165
|
+
bashrcPath,
|
|
166
|
+
bashProfilePath: profileResult.bashProfilePath,
|
|
167
|
+
changed: true,
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return { bashrcPath, changed: true }
|
|
172
|
+
}
|
package/libs/utils.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import * as fs from 'fs'
|
|
2
|
+
import * as path from 'path'
|
|
2
3
|
import { execa } from 'execa'
|
|
4
|
+
import * as yaml from 'yaml'
|
|
3
5
|
|
|
4
6
|
export const PNPM_INSTALL_ENV = {
|
|
5
7
|
PNPM_CONFIG_ENABLE_PRE_POST_SCRIPTS: 'true',
|
|
@@ -10,17 +12,58 @@ export function ensureDir(dirPath: string): void {
|
|
|
10
12
|
fs.mkdirSync(dirPath, { recursive: true })
|
|
11
13
|
}
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
export async function getCommandVersion(
|
|
16
|
+
command: string
|
|
17
|
+
): Promise<string | null> {
|
|
15
18
|
try {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
stdio: 'ignore',
|
|
19
|
+
const { failed, stdout } = await execa(command, ['--version'], {
|
|
20
|
+
stdio: 'pipe',
|
|
19
21
|
reject: false,
|
|
20
22
|
})
|
|
21
|
-
return
|
|
23
|
+
return failed ? null : stdout.trim()
|
|
22
24
|
} catch (error) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
return null
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function pnpmSupportsAllowBuild(version: string): boolean {
|
|
30
|
+
const major = Number.parseInt(version, 10)
|
|
31
|
+
return Number.isInteger(major) && major >= 10
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
35
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Persistently allow a package's pnpm install/build script in the global workspace. */
|
|
39
|
+
export async function allowPnpmGlobalBuild(packageName: string): Promise<void> {
|
|
40
|
+
const { stdout } = await execa('pnpm', ['root', '-g'])
|
|
41
|
+
const globalRoot = stdout.trim()
|
|
42
|
+
|
|
43
|
+
if (globalRoot.length === 0) {
|
|
44
|
+
throw new Error('Unable to locate pnpm global root.')
|
|
25
45
|
}
|
|
46
|
+
|
|
47
|
+
ensureDir(globalRoot)
|
|
48
|
+
|
|
49
|
+
const workspacePath = path.join(globalRoot, 'pnpm-workspace.yaml')
|
|
50
|
+
const source = fs.existsSync(workspacePath)
|
|
51
|
+
? fs.readFileSync(workspacePath, 'utf8')
|
|
52
|
+
: ''
|
|
53
|
+
const parsed = source.trim().length > 0 ? yaml.parse(source) : {}
|
|
54
|
+
const workspace = isRecord(parsed) ? parsed : {}
|
|
55
|
+
const existingAllowBuilds = workspace.allowBuilds
|
|
56
|
+
const allowBuilds = isRecord(existingAllowBuilds)
|
|
57
|
+
? { ...existingAllowBuilds }
|
|
58
|
+
: {}
|
|
59
|
+
|
|
60
|
+
allowBuilds[packageName] = true
|
|
61
|
+
workspace.allowBuilds = allowBuilds
|
|
62
|
+
|
|
63
|
+
fs.writeFileSync(workspacePath, yaml.stringify(workspace))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Check if a command exists */
|
|
67
|
+
export async function commandExists(command: string): Promise<boolean> {
|
|
68
|
+
return (await getCommandVersion(command)) !== null
|
|
26
69
|
}
|
package/macOS-init.md
CHANGED
|
@@ -12,51 +12,30 @@ sudo /usr/sbin/DevToolsSecurity -enable
|
|
|
12
12
|
```bash
|
|
13
13
|
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
|
|
14
14
|
brew install git
|
|
15
|
-
brew install node
|
|
16
15
|
brew install fish
|
|
17
16
|
brew install go
|
|
17
|
+
brew install n
|
|
18
18
|
brew install rustup-init
|
|
19
|
-
brew install
|
|
19
|
+
brew install cursor
|
|
20
20
|
brew install google-chrome
|
|
21
21
|
brew install jetbrains-toolbox
|
|
22
22
|
brew install omnidisksweeper
|
|
23
23
|
brew install git-lfs
|
|
24
24
|
brew install jordanbaird-ice
|
|
25
|
+
brew install raycast
|
|
26
|
+
sudo n lts
|
|
27
|
+
rustup-init -y
|
|
25
28
|
sudo git lfs install --system
|
|
26
|
-
npm i -g pnpm yarn && pnpm setup
|
|
29
|
+
npm i -g pnpm yarn && pnpm setup && SHELL=fish pnpm setup && source ~/.bashrc
|
|
30
|
+
pnpx @gengjiawen/os-init set-fish
|
|
31
|
+
pnpx @gengjiawen/os-init disable-ts-extension
|
|
32
|
+
pnpx @gengjiawen/os-init setup-env
|
|
27
33
|
pnpm i -g vite 7zip-bin-full
|
|
28
34
|
sudo ln -s "$(NODE_PATH="$(pnpm root -g)" node -p "require('7zip-bin-full').path7z")" /usr/local/bin/7z
|
|
29
35
|
```
|
|
30
36
|
|
|
31
|
-
|
|
32
|
-
Fish config
|
|
33
|
-
```
|
|
34
|
-
egrep "^export " ~/.bashrc | while read e
|
|
35
|
-
set var (echo $e | sed -E "s/^export ([A-Za-z_0-9]+)=(.*)\$/\1/")
|
|
36
|
-
set value (echo $e | sed -E "s/^export ([A-Za-z_0-9]+)=(.*)\$/\2/")
|
|
37
|
-
|
|
38
|
-
# remove surrounding quotes if existing
|
|
39
|
-
set value (echo $value | sed -E "s/^\"(.*)\"\$/\1/")
|
|
40
|
-
|
|
41
|
-
if test $var = "PATH"
|
|
42
|
-
# replace ":" by spaces. this is how PATH looks for Fish
|
|
43
|
-
set value (echo $value | sed -E "s/:/ /g")
|
|
44
|
-
|
|
45
|
-
# use eval because we need to expand the value
|
|
46
|
-
eval set -xg $var $value
|
|
47
|
-
|
|
48
|
-
continue
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
# evaluate variables. we can use eval because we most likely just used "$var"
|
|
52
|
-
set value (eval echo $value)
|
|
53
|
-
|
|
54
|
-
#echo "set -xg '$var' '$value' (via '$e')"
|
|
55
|
-
set -xg $var $value
|
|
56
|
-
end
|
|
57
|
-
```
|
|
58
|
-
|
|
59
37
|
make homebrew path first
|
|
38
|
+
|
|
60
39
|
```bash
|
|
61
40
|
fish_add_path /opt/homebrew/opt
|
|
62
41
|
```
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gengjiawen/os-init",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.26.1",
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"bin": {
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"execa": "^8.0.1",
|
|
25
25
|
"ip": "^2.0.1",
|
|
26
26
|
"jsonc-parser": "^3.3.1",
|
|
27
|
-
"yaml": "^2.
|
|
27
|
+
"yaml": "^2.9.0"
|
|
28
28
|
},
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"access": "public"
|
|
@@ -43,20 +43,14 @@
|
|
|
43
43
|
"cpy-cli": "^6.0.0",
|
|
44
44
|
"husky": "9.1.6",
|
|
45
45
|
"jest": "30.3.0",
|
|
46
|
-
"lint-staged": "^15.
|
|
46
|
+
"lint-staged": "^15.5.2",
|
|
47
47
|
"nodemon": "3.1.4",
|
|
48
48
|
"prettier": "3.5.3",
|
|
49
49
|
"rimraf": "6.1.3",
|
|
50
50
|
"ts-jest": "29.4.9",
|
|
51
|
-
"ts-node": "^10.9.
|
|
51
|
+
"ts-node": "^10.9.2",
|
|
52
52
|
"typescript": "5.6.3"
|
|
53
53
|
},
|
|
54
|
-
"pnpm": {
|
|
55
|
-
"overrides": {
|
|
56
|
-
"minimatch@3.1.2": "3.1.3",
|
|
57
|
-
"minimatch@9.0.5": "9.0.9"
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
54
|
"husky": {
|
|
61
55
|
"hooks": {
|
|
62
56
|
"pre-commit": "lint-staged"
|
package/build/mihomo.d.ts
DELETED