BREAKING(structure): modernize internal structure and support unpacking
This commit is contained in:
116
ts/mod_config/classes.tspublishconfig.ts
Normal file
116
ts/mod_config/classes.tspublishconfig.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
/**
|
||||
* Interface for tspublish.json configuration
|
||||
*/
|
||||
export interface ITsPublishJson {
|
||||
order?: number;
|
||||
unpack?: boolean;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* TsPublishConfig handles loading and parsing tspublish.json files.
|
||||
* These configuration files control module-specific settings like
|
||||
* compilation order and output unpacking behavior.
|
||||
*/
|
||||
export class TsPublishConfig {
|
||||
private folderPath: string;
|
||||
private cachedConfig: ITsPublishJson | null | undefined = undefined;
|
||||
|
||||
constructor(folderPath: string) {
|
||||
this.folderPath = folderPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the folder path this config is for
|
||||
*/
|
||||
public getFolderPath(): string {
|
||||
return this.folderPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load and parse tspublish.json from the folder
|
||||
* Returns null if file doesn't exist or is invalid
|
||||
*/
|
||||
public async load(): Promise<ITsPublishJson | null> {
|
||||
if (this.cachedConfig !== undefined) {
|
||||
return this.cachedConfig;
|
||||
}
|
||||
|
||||
try {
|
||||
const configPath = path.join(this.folderPath, 'tspublish.json');
|
||||
const content = await fs.promises.readFile(configPath, 'utf8');
|
||||
this.cachedConfig = JSON.parse(content);
|
||||
return this.cachedConfig;
|
||||
} catch {
|
||||
this.cachedConfig = null;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronously load and parse tspublish.json from the folder
|
||||
* Returns null if file doesn't exist or is invalid
|
||||
*/
|
||||
public loadSync(): ITsPublishJson | null {
|
||||
if (this.cachedConfig !== undefined) {
|
||||
return this.cachedConfig;
|
||||
}
|
||||
|
||||
try {
|
||||
const configPath = path.join(this.folderPath, 'tspublish.json');
|
||||
const content = fs.readFileSync(configPath, 'utf8');
|
||||
this.cachedConfig = JSON.parse(content);
|
||||
return this.cachedConfig;
|
||||
} catch {
|
||||
this.cachedConfig = null;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if output should be unpacked (flattened)
|
||||
* Default is true if not specified
|
||||
*/
|
||||
public get shouldUnpack(): boolean {
|
||||
const config = this.loadSync();
|
||||
if (!config || config.unpack === undefined) {
|
||||
return true; // Default to true
|
||||
}
|
||||
return config.unpack === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the compilation order for tsfolders command
|
||||
* Returns Infinity if not specified (sorted last)
|
||||
*/
|
||||
public get order(): number {
|
||||
const config = this.loadSync();
|
||||
if (!config || config.order === undefined) {
|
||||
return Infinity;
|
||||
}
|
||||
return config.order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if tspublish.json exists in the folder
|
||||
*/
|
||||
public async exists(): Promise<boolean> {
|
||||
try {
|
||||
const configPath = path.join(this.folderPath, 'tspublish.json');
|
||||
await fs.promises.access(configPath, fs.constants.F_OK);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the cached config (useful for reloading)
|
||||
*/
|
||||
public clearCache(): void {
|
||||
this.cachedConfig = undefined;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user