1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-10-26 18:18:46 +03:00

refactor(server): os helper to TypeScript

This commit is contained in:
Divlo 2022-09-20 21:34:56 +02:00
parent 247167202d
commit a28eede95b
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
7 changed files with 51 additions and 48 deletions

View File

@ -1,7 +1,7 @@
import execa from 'execa'
import { log } from '@/helpers/log'
import os from '@/helpers/os'
import { getOSInformation } from '@/helpers/os'
/**
* Check OS environment
@ -10,7 +10,7 @@ export default () =>
new Promise(async (resolve, reject) => {
log.info('Checking OS environment...')
const info = os.get()
const info = getOSInformation()
if (info.type === 'windows') {
log.error('Voice offline mode is not available on Windows')

View File

@ -1,7 +1,7 @@
import { command } from 'execa'
import { log } from '@/helpers/log'
import os from '@/helpers/os'
import { getOSInformation } from '@/helpers/os'
/**
* Setup offline hotword detection
@ -10,7 +10,7 @@ export default () =>
new Promise(async (resolve, reject) => {
log.info('Setting up offline hotword detection...')
const info = os.get()
const info = getOSInformation()
let pkgm = 'apt-get install'
if (info.type === 'macos') {
pkgm = 'brew'

View File

@ -2,7 +2,7 @@ import { command } from 'execa'
import fs from 'fs'
import { log } from '@/helpers/log'
import os from '@/helpers/os'
import { getOSInformation } from '@/helpers/os'
/**
* Setup offline speech-to-text
@ -16,7 +16,7 @@ export default () =>
// check this repo for updates: https://github.com/coqui-ai/STT-models/tree/main/english/coqui
const coquiModelVersion = '1.0.0'
let downloader = 'wget'
if (os.get().type === 'macos') {
if (getOSInformation().type === 'macos') {
downloader = 'curl -L -O'
}

View File

@ -2,7 +2,7 @@ import { command } from 'execa'
import fs from 'fs'
import { log } from '@/helpers/log'
import os from '@/helpers/os'
import { getOSInformation, getNumberOfCPUCores } from '@/helpers/os'
/**
* Setup offline text-to-speech
@ -14,11 +14,11 @@ export default () =>
const destFliteFolder = 'bin/flite'
const tmpDir = 'scripts/tmp'
let makeCores = ''
if (os.cpus().length > 2) {
makeCores = `-j ${os.cpus().length - 2}`
if (getNumberOfCPUCores() > 2) {
makeCores = `-j ${getNumberOfCPUCores() - 2}`
}
let downloader = 'wget'
if (os.get().type === 'macos') {
if (getOSInformation().type === 'macos') {
downloader = 'curl -L -O'
}

View File

@ -1,31 +0,0 @@
import o from 'os'
const os = {}
/**
* Returns information about your OS
*/
os.get = () => {
let type = 'unknown'
let name = ''
if (o.type().indexOf('Windows') !== -1) {
type = 'windows'
name = 'Windows'
} else if (o.type() === 'Darwin') {
type = 'macos'
name = 'macOS'
} else if (o.type() === 'Linux') {
type = 'linux'
name = 'Linux'
}
return { type, name }
}
/**
* Returns the number of cores on your machine
*/
os.cpus = () => o.cpus()
export default os

35
server/src/helpers/os.ts Normal file
View File

@ -0,0 +1,35 @@
import nodeOS from 'node:os'
export type GetOSType = 'windows' | 'macos' | 'linux' | 'unknown'
export type GetOSName = 'Windows' | 'macOS' | 'Linux' | 'Unknown'
export interface GetOSInformation {
type: GetOSType
name: GetOSName
}
/**
* Returns information about your OS
*/
export const getOSInformation = (): GetOSInformation => {
let type: GetOSType = 'unknown'
let name: GetOSName = 'Unknown'
if (nodeOS.type().indexOf('Windows') !== -1) {
type = 'windows'
name = 'Windows'
} else if (nodeOS.type() === 'Darwin') {
type = 'macos'
name = 'macOS'
} else if (nodeOS.type() === 'Linux') {
type = 'linux'
name = 'Linux'
}
return { type, name }
}
/**
* Returns the number of cores on your machine
*/
export const getNumberOfCPUCores = (): number => {
return nodeOS.cpus().length
}

View File

@ -1,9 +1,9 @@
import os from '@/helpers/os'
import { getOSInformation, getNumberOfCPUCores } from '@/helpers/os'
describe('OS helper', () => {
describe('get()', () => {
test('returns information about the OS', () => {
const info = os.get()
const info = getOSInformation()
expect(info.type).toBeOneOf(['windows', 'linux', 'macos'])
expect(info.name).toBeOneOf(['Windows', 'Linux', 'macOS'])
@ -14,7 +14,7 @@ describe('OS helper', () => {
const o = jest.requireActual('os')
o.type = jest.fn(() => 'Windows_NT')
expect(os.get()).toEqual({ name: 'Windows', type: 'windows' })
expect(getOSInformation()).toEqual({ name: 'Windows', type: 'windows' })
})
test('returns information for Linux', () => {
@ -22,7 +22,7 @@ describe('OS helper', () => {
const o = jest.requireActual('os')
o.type = jest.fn(() => 'Linux')
expect(os.get()).toEqual({ name: 'Linux', type: 'linux' })
expect(getOSInformation()).toEqual({ name: 'Linux', type: 'linux' })
})
test('returns information for macOS', () => {
@ -30,14 +30,13 @@ describe('OS helper', () => {
const o = jest.requireActual('os')
o.type = jest.fn(() => 'Darwin')
expect(os.get()).toEqual({ name: 'macOS', type: 'macos' })
expect(getOSInformation()).toEqual({ name: 'macOS', type: 'macos' })
})
})
describe('cpus()', () => {
test('returns the number of cores on the machine', () => {
expect(os.cpus()).toBeArray()
expect(os.cpus()[0]).toContainKeys(['model', 'speed', 'times'])
expect(typeof getNumberOfCPUCores()).toBe('number')
})
})
})