Skip to content

Commit

Permalink
add skip resaving option to load function
Browse files Browse the repository at this point in the history
  • Loading branch information
bpatrik committed Apr 12, 2024
1 parent b856b37 commit 1ecf5a2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typeconfig",
"version": "2.2.13",
"version": "2.2.14",
"description": "Config loader for Typescript",
"main": "common.js",
"scripts": {
Expand Down
12 changes: 6 additions & 6 deletions src/decorators/class/ConfigClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ export function ConfigClass<C, TAGS = { [key: string]: any }>(options: ConfigCla
return this.__loadJSONObject(config, true);
}

loadSync(pathOverride?: string): void {
loadSync(opt: { pathOverride?: string, preventSaving?: boolean }): void {

const configPath = pathOverride || options.configPath;
const configPath = opt.pathOverride || options.configPath;

if (debugMode === true && configPath) {
console.log('[Typeconfig] Loading config. Path: ' + configPath);
Expand Down Expand Up @@ -192,7 +192,7 @@ export function ConfigClass<C, TAGS = { [key: string]: any }>(options: ConfigCla
console.error(e);
}
}
if ((options.saveIfNotExist === true && exists === false) || shouldSave) {
if (!opt.preventSaving && ((options.saveIfNotExist === true && exists === false) || shouldSave)) {
this.saveSync(configPath);
if (options.exitOnConfig === true) {
process.exit(0);
Expand All @@ -214,8 +214,8 @@ export function ConfigClass<C, TAGS = { [key: string]: any }>(options: ConfigCla
}


async load(pathOverride?: string): Promise<any> {
const configPath = pathOverride || options.configPath;
async load(opt: { pathOverride?: string, preventSaving?: boolean }): Promise<any> {
const configPath = opt.pathOverride || options.configPath;
await this.__loadDefaults(configPath);

if (configPath) {
Expand Down Expand Up @@ -260,7 +260,7 @@ export function ConfigClass<C, TAGS = { [key: string]: any }>(options: ConfigCla
console.error(e);
}
}
if ((options.saveIfNotExist === true && exists === false) || shouldSave) {
if (!opt.preventSaving && ((options.saveIfNotExist === true && exists === false) || shouldSave)) {
await this.save(configPath);
if (options.exitOnConfig === true) {
process.exit(0);
Expand Down
10 changes: 6 additions & 4 deletions src/decorators/class/IConfigClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,17 @@ export interface IConfigClassPrivate<TAGS> extends IConfigClassPrivateBase<TAGS>
export interface IConfigClass<TAGS = { [key: string]: any }> extends IConfigClassBase<TAGS> {
/**
* Loads the config from file, cli and ENV synchronously. Does not reinit the object from class
* @param pathOverride - overrides the config path
* @param opt.pathOverride - overrides the config path
* @param opt.preventSaving - prevents auto save if file does not exist
*/
loadSync(pathOverride?: string): void;
loadSync(opt: { pathOverride?: string, preventSaving?: boolean }): void;

/**
* Loads the config from file, cli and ENV asynchronously. Does not reinit the object from class
* @param pathOverride - overrides the config path
* @param opt.pathOverride - overrides the config path
* @param opt.preventSaving - prevents auto save if file does not exist
*/
load(pathOverride?: string): Promise<any>;
load(opt: { pathOverride?: string, preventSaving?: boolean }): Promise<any>;

save(pathOverride?: string): Promise<any>;

Expand Down

0 comments on commit 1ecf5a2

Please sign in to comment.