use ~/.hfs as default CWD in non-windows systems

Massimo Melina committed Aug 26, 2022 at 16:13 UTC a3458d3d7c3d677e6e1f6ae34f39d6c6b082fc37
4 files changed +39 -7
package.json
+1 -1
@@ -23,7 +23,7 @@
23 "build-admin": "npm run build --workspace=admin",
24 "build-shared": "npm run build --workspace=shared",
25 "build-form": "npm run build --workspace=mui-grid-form",
26 - "server-for-test": "cross-env HFS_CONFIG=tests node dist/src",
26 + "server-for-test": "node dist/src --cwd . --config tests",
27 "test": "mocha -r ts-node/register 'tests/**/*.ts'",
28 "dist-node": "cd dist && zip hfs-node.zip -r * -x *.zip *.exe hfs-* *.log logs",
29 "dist-win": "pkg . -C gzip -t node16-win && cd dist && zip hfs-windows.zip hfs.exe -r plugins",
src/config.ts
+17 -3
@@ -1,14 +1,14 @@
1 // This file is part of HFS - Copyright 2021-2022, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3 import EventEmitter from 'events'
4 -import { argv } from './const'
4 +import { APP_PATH, argv, ORIGINAL_CWD } from './const'
5 import { watchLoad } from './watchLoad'
6 import yaml from 'yaml'
7 import _ from 'lodash'
8 import { debounceAsync, same, objSameKeys, onOff, wait, with_ } from './misc'
9 -import { exists, statSync } from 'fs'
9 +import { copyFileSync, exists, existsSync, renameSync, statSync } from 'fs'
10 import { promisify } from 'util'
11 -import { join } from 'path'
11 +import { join, resolve } from 'path'
12 import events from './events'
13
14 const FILE = 'config.yaml'
@@ -22,6 +22,7 @@ cfgEvents.setMaxListeners(10_000)
22 const path = with_(argv.config || process.env.HFS_CONFIG, p => {
23 if (!p)
24 return FILE
25 + p = resolve(ORIGINAL_CWD, p)
26 try {
27 if (statSync(p).isDirectory()) // try to detect if path points to a folder, in which case we add the standard filename
28 return join(p, FILE)
@@ -30,6 +31,19 @@ const path = with_(argv.config || process.env.HFS_CONFIG, p => {
31 return p
32 })
33 console.log("config", path)
34 +const legacyPosition = join(APP_PATH, FILE)
35 +if (!existsSync(path) && existsSync(legacyPosition))
36 + try {
37 + renameSync(legacyPosition, path)
38 + console.log("moved from legacy position", legacyPosition)
39 + }
40 + catch {
41 + try { // attempt copying, in case moving the source file proves to be impractical
42 + copyFileSync(legacyPosition, path)
43 + console.log("copied from legacy position", legacyPosition)
44 + }
45 + catch {}
46 + }
47 const { save } = watchLoad(path, values => setConfig(values||{}, false), {
48 failedOnFirstAttempt(){
49 console.log("No config file, using defaults")
src/const.ts
+17
@@ -2,10 +2,14 @@
2
3 import minimist from 'minimist'
4 import * as fs from 'fs'
5 +import { homedir } from 'os'
6 import _ from 'lodash'
7 +import { mkdirSync } from 'fs'
8 +import { join, resolve } from 'path'
9
10 export const argv = minimist(process.argv.slice(2))
11 export const DEV = process.env.DEV || argv.dev ? 'DEV' : ''
12 +export const ORIGINAL_CWD = process.cwd()
13 export const HFS_STARTED = new Date()
14 export const BUILD_TIMESTAMP = ''
15 export const VERSION = ''
@@ -29,6 +33,8 @@ export const UNAUTHORIZED = 401
33
34 export const IS_WINDOWS = process.platform === 'win32'
35
36 +export const APP_PATH = resolve(__dirname + '/..')
37 +
38 // we want this to be the first stuff to be printed, then we print it in this module, that is executed at the beginning
39 if (DEV) console.clear()
40 else console.debug = ()=>{}
@@ -37,5 +43,16 @@ console.log(`License https://www.gnu.org/licenses/gpl-3.0.txt`)
43 console.log('started', HFS_STARTED.toLocaleString(), DEV)
44 console.log('version', VERSION||'-')
45 console.log('build', BUILD_TIMESTAMP||'-')
46 +if (argv.cwd)
47 + process.chdir(argv.cwd)
48 +else if (!process.argv0.endsWith('.exe')) { // still considering whether to use this behavior with Windows users, who may be less accustomed to it
49 + const dir = join(homedir(), '.hfs')
50 + try { mkdirSync(dir) }
51 + catch(e: any) {
52 + if (e.code !== 'EEXIST')
53 + console.error(e)
54 + }
55 + process.chdir(dir)
56 +}
57 console.log('cwd', process.cwd())
58
src/plugins.ts
+4 -3
@@ -3,8 +3,8 @@
3 import glob from 'fast-glob'
4 import { watchLoad } from './watchLoad'
5 import _ from 'lodash'
6 -import pathLib from 'path'
7 -import { API_VERSION, COMPATIBLE_API_VERSION, PLUGINS_PUB_URI } from './const'
6 +import pathLib, { join } from 'path'
7 +import { API_VERSION, APP_PATH, COMPATIBLE_API_VERSION, PLUGINS_PUB_URI } from './const'
8 import * as Const from './const'
9 import Koa from 'koa'
10 import { Callback, debounceAsync, Dict, getOrSet, onProcessExit, same, tryJson, wantArray, watchDir } from './misc'
@@ -198,7 +198,8 @@ export async function rescan() {
198 console.debug('scanning plugins')
199 const found = []
200 const foundDisabled: typeof availablePlugins = {}
201 - for (let f of await glob(PATH+'/*/plugin.js')) {
201 + const MASK = join(PATH, '*', 'plugin.js')
202 + for (const f of await glob([join(APP_PATH, MASK), MASK])) {
203 const id = f.split('/').slice(-2)[0]
204 if (id.endsWith(DISABLING_POSTFIX)) continue
205 if (!enablePlugins.get().includes(id)) {