mirror of
https://github.com/leon-ai/leon.git
synced 2025-01-04 15:12:53 +03:00
refactor(server): os helper to TypeScript
This commit is contained in:
parent
247167202d
commit
a28eede95b
@ -1,7 +1,7 @@
|
|||||||
import execa from 'execa'
|
import execa from 'execa'
|
||||||
|
|
||||||
import { log } from '@/helpers/log'
|
import { log } from '@/helpers/log'
|
||||||
import os from '@/helpers/os'
|
import { getOSInformation } from '@/helpers/os'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check OS environment
|
* Check OS environment
|
||||||
@ -10,7 +10,7 @@ export default () =>
|
|||||||
new Promise(async (resolve, reject) => {
|
new Promise(async (resolve, reject) => {
|
||||||
log.info('Checking OS environment...')
|
log.info('Checking OS environment...')
|
||||||
|
|
||||||
const info = os.get()
|
const info = getOSInformation()
|
||||||
|
|
||||||
if (info.type === 'windows') {
|
if (info.type === 'windows') {
|
||||||
log.error('Voice offline mode is not available on Windows')
|
log.error('Voice offline mode is not available on Windows')
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { command } from 'execa'
|
import { command } from 'execa'
|
||||||
|
|
||||||
import { log } from '@/helpers/log'
|
import { log } from '@/helpers/log'
|
||||||
import os from '@/helpers/os'
|
import { getOSInformation } from '@/helpers/os'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup offline hotword detection
|
* Setup offline hotword detection
|
||||||
@ -10,7 +10,7 @@ export default () =>
|
|||||||
new Promise(async (resolve, reject) => {
|
new Promise(async (resolve, reject) => {
|
||||||
log.info('Setting up offline hotword detection...')
|
log.info('Setting up offline hotword detection...')
|
||||||
|
|
||||||
const info = os.get()
|
const info = getOSInformation()
|
||||||
let pkgm = 'apt-get install'
|
let pkgm = 'apt-get install'
|
||||||
if (info.type === 'macos') {
|
if (info.type === 'macos') {
|
||||||
pkgm = 'brew'
|
pkgm = 'brew'
|
||||||
|
@ -2,7 +2,7 @@ import { command } from 'execa'
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
|
|
||||||
import { log } from '@/helpers/log'
|
import { log } from '@/helpers/log'
|
||||||
import os from '@/helpers/os'
|
import { getOSInformation } from '@/helpers/os'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup offline speech-to-text
|
* 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
|
// check this repo for updates: https://github.com/coqui-ai/STT-models/tree/main/english/coqui
|
||||||
const coquiModelVersion = '1.0.0'
|
const coquiModelVersion = '1.0.0'
|
||||||
let downloader = 'wget'
|
let downloader = 'wget'
|
||||||
if (os.get().type === 'macos') {
|
if (getOSInformation().type === 'macos') {
|
||||||
downloader = 'curl -L -O'
|
downloader = 'curl -L -O'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ import { command } from 'execa'
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
|
|
||||||
import { log } from '@/helpers/log'
|
import { log } from '@/helpers/log'
|
||||||
import os from '@/helpers/os'
|
import { getOSInformation, getNumberOfCPUCores } from '@/helpers/os'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup offline text-to-speech
|
* Setup offline text-to-speech
|
||||||
@ -14,11 +14,11 @@ export default () =>
|
|||||||
const destFliteFolder = 'bin/flite'
|
const destFliteFolder = 'bin/flite'
|
||||||
const tmpDir = 'scripts/tmp'
|
const tmpDir = 'scripts/tmp'
|
||||||
let makeCores = ''
|
let makeCores = ''
|
||||||
if (os.cpus().length > 2) {
|
if (getNumberOfCPUCores() > 2) {
|
||||||
makeCores = `-j ${os.cpus().length - 2}`
|
makeCores = `-j ${getNumberOfCPUCores() - 2}`
|
||||||
}
|
}
|
||||||
let downloader = 'wget'
|
let downloader = 'wget'
|
||||||
if (os.get().type === 'macos') {
|
if (getOSInformation().type === 'macos') {
|
||||||
downloader = 'curl -L -O'
|
downloader = 'curl -L -O'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
35
server/src/helpers/os.ts
Normal 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
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
import os from '@/helpers/os'
|
import { getOSInformation, getNumberOfCPUCores } from '@/helpers/os'
|
||||||
|
|
||||||
describe('OS helper', () => {
|
describe('OS helper', () => {
|
||||||
describe('get()', () => {
|
describe('get()', () => {
|
||||||
test('returns information about the OS', () => {
|
test('returns information about the OS', () => {
|
||||||
const info = os.get()
|
const info = getOSInformation()
|
||||||
|
|
||||||
expect(info.type).toBeOneOf(['windows', 'linux', 'macos'])
|
expect(info.type).toBeOneOf(['windows', 'linux', 'macos'])
|
||||||
expect(info.name).toBeOneOf(['Windows', 'Linux', 'macOS'])
|
expect(info.name).toBeOneOf(['Windows', 'Linux', 'macOS'])
|
||||||
@ -14,7 +14,7 @@ describe('OS helper', () => {
|
|||||||
const o = jest.requireActual('os')
|
const o = jest.requireActual('os')
|
||||||
o.type = jest.fn(() => 'Windows_NT')
|
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', () => {
|
test('returns information for Linux', () => {
|
||||||
@ -22,7 +22,7 @@ describe('OS helper', () => {
|
|||||||
const o = jest.requireActual('os')
|
const o = jest.requireActual('os')
|
||||||
o.type = jest.fn(() => 'Linux')
|
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', () => {
|
test('returns information for macOS', () => {
|
||||||
@ -30,14 +30,13 @@ describe('OS helper', () => {
|
|||||||
const o = jest.requireActual('os')
|
const o = jest.requireActual('os')
|
||||||
o.type = jest.fn(() => 'Darwin')
|
o.type = jest.fn(() => 'Darwin')
|
||||||
|
|
||||||
expect(os.get()).toEqual({ name: 'macOS', type: 'macos' })
|
expect(getOSInformation()).toEqual({ name: 'macOS', type: 'macos' })
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('cpus()', () => {
|
describe('cpus()', () => {
|
||||||
test('returns the number of cores on the machine', () => {
|
test('returns the number of cores on the machine', () => {
|
||||||
expect(os.cpus()).toBeArray()
|
expect(typeof getNumberOfCPUCores()).toBe('number')
|
||||||
expect(os.cpus()[0]).toContainKeys(['model', 'speed', 'times'])
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user