Skip to content

Commit dd4b2f3

Browse files
committed
Add darwin implementation
1 parent 2e5d4fe commit dd4b2f3

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

packages/get-os-info/src/get-os-info.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,59 @@ describe('get-os-info', function () {
6767
});
6868
});
6969
});
70+
71+
describe('on darwin', function () {
72+
it('parses the SystemVersion.plist file', function () {
73+
const fixture = `
74+
<?xml version="1.0" encoding="UTF-8"?>
75+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
76+
<plist version="1.0">
77+
<dict>
78+
<key>BuildID</key>
79+
<string>2B3829A8-E319-11EF-8892-025514DE0AB1</string>
80+
<key>ProductBuildVersion</key>
81+
<string>24D70</string>
82+
<key>ProductCopyright</key>
83+
<string>1983-2025 Apple Inc.</string>
84+
<key>ProductName</key>
85+
<string>macOS</string>
86+
<key>ProductUserVisibleVersion</key>
87+
<string>15.3.1</string>
88+
<key>ProductVersion</key>
89+
<string>15.3.1</string>
90+
<key>iOSSupportVersion</key>
91+
<string>18.3</string>
92+
</dict>
93+
</plist>
94+
`;
95+
96+
expect(parseDarwinInfo(fixture)).to.deep.equal({
97+
os_darwin_product_name: 'macOS',
98+
os_darwin_product_version: '15.3.1',
99+
os_darwin_product_build_version: '24D70',
100+
});
101+
});
102+
103+
it('returns info from /System/Library/CoreServices/SystemVersion.plist', async function () {
104+
if (process.platform !== 'darwin') {
105+
this.skip();
106+
}
107+
108+
const systemVersionPlist = await fs.readFile(
109+
'/System/Library/CoreServices/SystemVersion.plist',
110+
'utf-8'
111+
);
112+
113+
const {
114+
os_darwin_product_name,
115+
os_darwin_product_version,
116+
os_darwin_product_build_version,
117+
} = await getOsInfo();
118+
119+
// Instead of reimplementing the parser, we simply check that the values are present in the original file
120+
expect(systemVersionPlist).contains(os_darwin_product_name);
121+
expect(systemVersionPlist).contains(os_darwin_product_version);
122+
expect(systemVersionPlist).contains(os_darwin_product_build_version);
123+
});
124+
});
70125
});

packages/get-os-info/src/get-os-info.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ type LinuxInfo = {
66
os_linux_release: string;
77
};
88

9+
type DarwinInfo = {
10+
os_darwin_product_name: string;
11+
os_darwin_product_version: string;
12+
os_darwin_product_build_version: string;
13+
};
14+
915
type OsInfo = {
1016
os_type: string;
1117
os_version: string;
1218
os_arch: string;
1319
os_release: string;
14-
} & Partial<LinuxInfo>;
20+
} & Partial<LinuxInfo> &
21+
Partial<DarwinInfo>;
1522

1623
async function getLinuxInfo(): Promise<LinuxInfo> {
1724
try {
@@ -42,12 +49,51 @@ export function parseLinuxInfo(etcRelease: string): LinuxInfo {
4249
};
4350
}
4451

52+
async function getDarwinInfo(): Promise<DarwinInfo> {
53+
try {
54+
const systemVersionPlistPath =
55+
'/System/Library/CoreServices/SystemVersion.plist';
56+
const systemVersionPlist = await fs.readFile(
57+
systemVersionPlistPath,
58+
'utf-8'
59+
);
60+
return parseDarwinInfo(systemVersionPlist);
61+
} catch (e) {
62+
return {
63+
os_darwin_product_name: 'unknown',
64+
os_darwin_product_version: 'unknown',
65+
os_darwin_product_build_version: 'unknown',
66+
};
67+
}
68+
}
69+
70+
export function parseDarwinInfo(systemVersionPlist: string): DarwinInfo {
71+
const match = systemVersionPlist.matchAll(
72+
/<key>(?<key>[^<]+)<\/key>\s*<string>(?<value>[^<]+)<\/string>/gm
73+
);
74+
75+
const {
76+
ProductName: os_darwin_product_name = 'unknown',
77+
ProductVersion: os_darwin_product_version = 'unknown',
78+
ProductBuildVersion: os_darwin_product_build_version = 'unknown',
79+
} = Object.fromEntries(
80+
Array.from(match).map((m) => [m.groups?.key, m.groups?.value])
81+
);
82+
83+
return {
84+
os_darwin_product_name,
85+
os_darwin_product_version,
86+
os_darwin_product_build_version,
87+
};
88+
}
89+
4590
export async function getOsInfo(): Promise<OsInfo> {
4691
return {
4792
os_type: os.type(),
4893
os_version: os.version(),
4994
os_arch: os.arch(),
5095
os_release: os.release(),
5196
...(process.platform === 'linux' ? await getLinuxInfo() : {}),
97+
...(process.platform === 'darwin' ? await getDarwinInfo() : {}),
5298
};
5399
}

0 commit comments

Comments
 (0)