better code: moved to (new) proper file

Massimo Melina committed Jan 26, 2023 at 16:43 UTC c6ca33cd42170c39824939e13688b9fdbdb8266c
3 files changed +21 -23
src/api.vfs.ts
+2 -6
@@ -11,6 +11,7 @@ import {
11 HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_SERVER_ERROR, HTTP_CONFLICT, HTTP_NOT_ACCEPTABLE
12 } from './const'
13 import { isMatch } from 'micromatch'
14 +import { getDrives } from './util-os'
15
16 type VfsAdmin = {
17 type?: string,
@@ -178,9 +179,4 @@ function pickProps(o: any, keys: string[]) {
179 if (k in o)
180 ret[k] = o[k] === null || o[k] === '' ? undefined : o[k]
181 return ret
181 -}
182 -
183 -async function getDrives() {
184 - const { stdout } = await promisify(exec)('wmic logicaldisk get name')
185 - return stdout.split('\n').slice(1).map(x => x.trim()).filter(Boolean)
186 -}
182 +}
\ No newline at end of file
src/util-files.ts
+3 -13
@@ -2,11 +2,11 @@
2
3 import fs from 'fs/promises'
4 import { wait } from './misc'
5 -import { createWriteStream, mkdirSync, watch } from 'fs'
5 +import { createWriteStream, watch } from 'fs'
6 import { basename, dirname } from 'path'
7 import glob from 'fast-glob'
8 import { IS_WINDOWS } from './const'
9 -import { execFile } from 'child_process'
9 +import { runCmd } from './util-os'
10 import { once, Readable } from 'stream'
11 // @ts-ignore
12 import unzipper from 'unzip-stream'
@@ -102,22 +102,12 @@ export async function* dirStream(path: string, deep?: number) {
102
103 async function getItemsToSkip(path: string) {
104 if (!IS_WINDOWS) return
105 - const out = await run('dir', ['/ah', '/b', path.replace(/\//g, '\\')])
105 + const out = await runCmd('dir', ['/ah', '/b', path.replace(/\//g, '\\')])
106 .catch(()=>'') // error in case of no matching file
107 return out.split('\r\n').slice(0,-1)
108 }
109 }
110
111 -export function run(cmd: string, args: string[] = []): Promise<string> {
112 - return new Promise((resolve, reject) =>
113 - execFile('cmd', ['/c', cmd, ...args], (err, stdout) => {
114 - if (err)
115 - reject(err)
116 - else
117 - resolve(stdout)
118 - }))
119 -}
120 -
111 export async function unzip(stream: Readable, cb: (path: string) => false | string) {
112 let pending: Promise<any> = Promise.resolve()
113 return new Promise(resolve =>
src/util-os.ts
+16 -4
@@ -1,10 +1,11 @@
1 -import os from 'os'
1 import { resolve } from 'path'
3 -import { execSync } from 'child_process'
2 +import { exec, execFile, execSync } from 'child_process'
3 import { try_ } from './misc'
4 +import { promisify } from 'util'
5 +import { IS_WINDOWS } from './const'
6
7 export function getFreeDiskSync(path: string) {
7 - if (os.platform() === 'win32') {
8 + if (IS_WINDOWS) {
9 const drive = resolve(path).slice(0, 2).toUpperCase()
10 const out = execSync('wmic logicaldisk get FreeSpace,name /format:list').toString().replace(/\r/g, '')
11 const one = out.split(/\n\n+/).find(x => x.includes('Name=' + drive))
@@ -23,4 +24,15 @@ export function getFreeDiskSync(path: string) {
24 const one = out.split('\n')[1]
25 const free = Number(one.split(/\s+/)[3])
26 return free * 1024
26 -}
\ No newline at end of file
27 +}
28 +
29 +export async function getDrives() {
30 + const { stdout } = await promisify(exec)('wmic logicaldisk get name')
31 + return stdout.split('\n').slice(1).map(x => x.trim()).filter(Boolean)
32 +}
33 +
34 +// execute win32 shell commands
35 +export async function runCmd(cmd: string, args: string[] = []) {
36 + const { stdout, stderr } = await promisify(execFile)('cmd', ['/c', cmd, ...args])
37 + return stderr || stdout
38 +}