-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow to set the config path with --config
Addressing #132
- Loading branch information
Showing
6 changed files
with
64 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { access, writeFile } from 'fs/promises' | ||
import read from 'read' | ||
|
||
export async function checkConfigFileExistance (configFilePath) { | ||
try { | ||
await access(configFilePath) | ||
} catch (err) { | ||
if (err.code === 'ENOENT') { | ||
await offerToCreateMissingConfigFile(configFilePath) | ||
} else { | ||
throw err | ||
} | ||
} | ||
} | ||
|
||
async function offerToCreateMissingConfigFile (configFilePath) { | ||
try { | ||
const res = await read({ prompt: `Config file not found at ${configFilePath}. Create? y/N` }) | ||
if (res.trim() === 'y') { | ||
await writeFile(configFilePath, '{}', { mode: 0o600 }) | ||
} else { | ||
process.exit(1) | ||
} | ||
} catch (err) { | ||
if (err.message === 'canceled') process.exit(1) | ||
else throw err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,27 @@ | ||
import { accessSync, writeFileSync } from 'node:fs' | ||
import { join, resolve } from '#lib/platform_agnostic_path' | ||
import configFolderFactory from '../get_folder_path.js' | ||
import path from '../path.js' | ||
|
||
const configFolder = configFolderFactory('config') | ||
import { checkConfigFileExistance } from './check_config_file_existance.js' | ||
|
||
export let configFilePath | ||
|
||
if (configFolder != null) { | ||
configFilePath = path.join(configFolder, 'config.json') | ||
const configFolder = configFolderFactory('config') | ||
|
||
try { | ||
accessSync(configFilePath) | ||
} catch (err) { | ||
// Initialize if it doesn't exist | ||
writeFileSync(configFilePath, '{}', { mode: 0o600 }) | ||
// Arguments parsing needs to be done manually as relying on lib/program | ||
// would be a circular dependecy | ||
const args = process.argv.slice(2) | ||
const configArg = args.find(arg => arg.startsWith('--config')) | ||
if (configArg) { | ||
let configFilePathFromArgs | ||
if (configArg.split('=')[1]) { | ||
configFilePathFromArgs = configArg.split('=')[1] | ||
} else { | ||
configFilePathFromArgs = args[args.indexOf(configArg) + 1] | ||
} | ||
configFilePath = resolve(process.cwd(), configFilePathFromArgs) | ||
} else if (configFolder != null) { | ||
configFilePath = join(configFolder, 'config.json') | ||
} else { | ||
configFilePath = null | ||
} | ||
|
||
if (configFilePath) await checkConfigFileExistance(configFilePath) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import path from 'node:path' | ||
|
||
const platformAgnosticPath = process.platform === 'win32' ? path.win32 : path | ||
|
||
export const resolve = platformAgnosticPath.resolve | ||
export const join = platformAgnosticPath.join |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters