Skip to content

Commit

Permalink
Add TypeScript typings (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
yassernasc authored Dec 16, 2024
1 parent 7a3a2fc commit 0789648
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 96 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
128 changes: 64 additions & 64 deletions .github/workflows/prebuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,74 @@ jobs:
strategy:
matrix:
include:
- os: ubuntu-22.04
platform: linux
arch: x64
- os: ubuntu-22.04-arm64
platform: linux
arch: arm64
- os: ubuntu-22.04
platform: android
arch: x64
- os: ubuntu-22.04
platform: android
arch: ia32
- os: ubuntu-22.04
platform: android
arch: arm64
- os: ubuntu-22.04
platform: android
arch: arm
- os: macos-14
platform: darwin
arch: x64
- os: macos-14
platform: darwin
arch: arm64
- os: macos-14
platform: ios
arch: arm64
- os: macos-14
platform: ios
arch: arm64
tags: -simulator
flags: --simulator
- os: macos-14
platform: ios
arch: x64
tags: -simulator
flags: --simulator
- os: windows-2022
platform: win32
arch: x64
- os: windows-2022
platform: win32
arch: arm64
- os: ubuntu-22.04
platform: linux
arch: x64
- os: ubuntu-22.04-arm64
platform: linux
arch: arm64
- os: ubuntu-22.04
platform: android
arch: x64
- os: ubuntu-22.04
platform: android
arch: ia32
- os: ubuntu-22.04
platform: android
arch: arm64
- os: ubuntu-22.04
platform: android
arch: arm
- os: macos-14
platform: darwin
arch: x64
- os: macos-14
platform: darwin
arch: arm64
- os: macos-14
platform: ios
arch: arm64
- os: macos-14
platform: ios
arch: arm64
tags: -simulator
flags: --simulator
- os: macos-14
platform: ios
arch: x64
tags: -simulator
flags: --simulator
- os: windows-2022
platform: win32
arch: x64
- os: windows-2022
platform: win32
arch: arm64
runs-on: ${{ matrix.os }}
name: ${{ matrix.platform }}-${{ matrix.arch }}${{ matrix.tags }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- run: npm install -g bare-runtime bare-make
- run: npm install
- run: bare-make generate --platform ${{ matrix.platform }} --arch ${{ matrix.arch }} ${{ matrix.flags }}
- run: bare-make build
- run: bare-make install
- uses: actions/upload-artifact@v4
with:
name: ${{ matrix.platform }}-${{ matrix.arch }}${{ matrix.tags }}
path: prebuilds/*
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- run: npm install -g bare-runtime bare-make
- run: npm install
- run: bare-make generate --platform ${{ matrix.platform }} --arch ${{ matrix.arch }} ${{ matrix.flags }}
- run: bare-make build
- run: bare-make install
- uses: actions/upload-artifact@v4
with:
name: ${{ matrix.platform }}-${{ matrix.arch }}${{ matrix.tags }}
path: prebuilds/*
merge:
runs-on: ubuntu-latest
needs: prebuild
steps:
- uses: actions/download-artifact@v4
with:
path: prebuilds
merge-multiple: true
- uses: actions/upload-artifact@v4
with:
name: prebuilds
path: prebuilds
- uses: actions/download-artifact@v4
with:
path: prebuilds
merge-multiple: true
- uses: actions/upload-artifact@v4
with:
name: prebuilds
path: prebuilds
38 changes: 19 additions & 19 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ jobs:
strategy:
matrix:
include:
- os: ubuntu-latest
platform: linux
arch: x64
- os: macos-latest
platform: darwin
arch: arm64
- os: windows-latest
platform: win32
arch: x64
- os: ubuntu-latest
platform: linux
arch: x64
- os: macos-latest
platform: darwin
arch: arm64
- os: windows-latest
platform: win32
arch: x64
runs-on: ${{ matrix.os }}
name: ${{ matrix.platform }}-${{ matrix.arch }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- run: npm install -g bare-runtime bare-make
- run: npm install
- run: bare-make generate --platform ${{ matrix.platform }} --arch ${{ matrix.arch }} --debug
- run: bare-make build
- run: bare-make install
- run: npm test
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- run: npm install -g bare-runtime bare-make
- run: npm install
- run: bare-make generate --platform ${{ matrix.platform }} --arch ${{ matrix.arch }} --debug
- run: bare-make build
- run: bare-make install
- run: npm test
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"prettier-config-standard"
93 changes: 93 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
export const constants: {
signals: Record<string, number>
errnos: Record<string, number>
}

export const EOL: '\r\n' | '\n'

export function platform(): 'android' | 'darwin' | 'ios' | 'linux' | 'win32'

export function arch(): 'arm' | 'arm64' | 'ia32' | 'x64'

export function type(): string

export function version(): string

export function release(): string

export function machine(): string

export function execPath(): string

export function pid(): number

export function ppid(): number

export function cwd(): string

export function chdir(dir: string): string

export function tmpdir(): string

export function homedir(): string

export function hostname(): string

export function kill(pid: number, signal?: string | number): void

export function endianness(): 'LE' | 'BE'

export function availableParallelism(): number

export function cpuUsage(previous?: { user: number; system: number }): {
user: number
system: number
}

export function resourceUsage(): {
userCPUTime: number
systemCPUTime: number
maxRSS: number
sharedMemorySize: number
unsharedDataSize: number
unsharedStackSize: number
minorPageFault: number
majorPageFault: number
swappedOut: number
fsRead: number
fsWrite: number
ipcSent: number
ipcReceived: number
signalsCount: number
voluntaryContextSwitches: number
involuntaryContextSwitches: number
}

export function memoryUsage(): {
rss: number
heapTotal: number
heapUsed: number
external: number
}

export function freemem(): number

export function totalmem(): number

export function uptime(): number

export function loadavg(): ArrayLike<number>

export function getProcessTitle(): string

export function setProcessTitle(title: unknown): void

export function getEnvKeys(): string[]

export function getEnv(name: string): string | undefined

export function hasEnv(name: string): boolean

export function setEnv(name: string, value: string): void

export function unsetEnv(name: string): void
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ exports.constants = constants

exports.EOL = binding.platform === 'win32' ? '\r\n' : '\n'

exports.platform = function platform () {
exports.platform = function platform() {
return binding.platform
}

exports.arch = function arch () {
exports.arch = function arch() {
return binding.arch
}

Expand All @@ -27,7 +27,7 @@ exports.tmpdir = binding.tmpdir
exports.homedir = binding.homedir
exports.hostname = binding.hostname

exports.kill = function kill (pid, signal = constants.signals.SIGTERM) {
exports.kill = function kill(pid, signal = constants.signals.SIGTERM) {
if (typeof signal === 'string') {
if (signal in constants.signals === false) {
throw errors.UNKNOWN_SIGNAL('Unknown signal: ' + signal)
Expand All @@ -39,13 +39,13 @@ exports.kill = function kill (pid, signal = constants.signals.SIGTERM) {
binding.kill(pid, signal)
}

exports.endianness = function endianness () {
exports.endianness = function endianness() {
return binding.isLittleEndian ? 'LE' : 'BE'
}

exports.availableParallelism = binding.availableParallelism

exports.cpuUsage = function cpuUsage (previous) {
exports.cpuUsage = function cpuUsage(previous) {
const current = binding.cpuUsage()

if (previous) {
Expand All @@ -67,7 +67,7 @@ exports.loadavg = binding.loadavg

exports.getProcessTitle = binding.getProcessTitle

exports.setProcessTitle = function setProcessTitle (title) {
exports.setProcessTitle = function setProcessTitle(title) {
if (typeof title !== 'string') title = title.toString()

if (title.length >= 256) {
Expand Down
8 changes: 4 additions & 4 deletions lib/errors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = class OSError extends Error {
constructor (msg, code, fn = OSError) {
constructor(msg, code, fn = OSError) {
super(`${code}: ${msg}`)
this.code = code

Expand All @@ -8,15 +8,15 @@ module.exports = class OSError extends Error {
}
}

get name () {
get name() {
return 'OSError'
}

static UNKNOWN_SIGNAL (msg) {
static UNKNOWN_SIGNAL(msg) {
return new OSError(msg, 'UNKNOWN_SIGNAL', OSError.UNKNOWN_SIGNAL)
}

static TITLE_OVERFLOW (msg) {
static TITLE_OVERFLOW(msg) {
return new OSError(msg, 'TITLE_OVERFLOW', OSError.TITLE_OVERFLOW)
}
}
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
"version": "3.3.0",
"description": "Operating system utilities for Javascript",
"exports": {
".": "./index.js",
".": {
"types": "./index.d.ts",
"default": "./index.js"
},
"./package": "./package.json",
"./constants": "./lib/constants.js",
"./errors": "./lib/errors.js"
},
"files": [
"index.js",
"index.d.ts",
"binding.c",
"binding.js",
"CMakeLists.txt",
Expand All @@ -18,7 +22,7 @@
],
"addon": true,
"scripts": {
"test": "standard && bare test.js"
"test": "prettier . --check && bare test.js"
},
"repository": {
"type": "git",
Expand All @@ -36,6 +40,7 @@
"devDependencies": {
"brittle": "^3.1.1",
"cmake-bare": "^1.1.6",
"standard": "^17.0.0"
"prettier": "^3.4.2",
"prettier-config-standard": "^7.0.0"
}
}

0 comments on commit 0789648

Please sign in to comment.