@compass-docgen/core
Version:
Core functionality for Compass
113 lines (112 loc) • 4.06 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppConfig = exports.DEF_CONFIG = void 0;
const fs = __importStar(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const normalizer_browser_1 = require("./normalizer.browser");
exports.DEF_CONFIG = {
dir: './packages',
port: 3000,
name: 'Compass',
};
class AppConfig {
static tsFileDirectory = path_1.default.dirname(__filename);
static cacheFile = `build/.compassrc.cache`;
static async bind(configLocation = '.compassrc') {
try {
if (!fs.existsSync('./build')) {
fs.mkdirSync('./build');
}
if (!(await fs.exists(configLocation))) {
configLocation = `config/${configLocation}`;
if (!(await fs.exists(configLocation))) {
return exports.DEF_CONFIG;
}
}
const rcFileContent = await fs.readFile(configLocation, 'utf8');
if (!rcFileContent) {
return exports.DEF_CONFIG;
}
const config = {
...exports.DEF_CONFIG,
...AppConfig.resolveName(JSON.parse(rcFileContent)),
};
await fs.writeFile(AppConfig.cacheFile, JSON.stringify(config));
return AppConfig;
}
catch (error) {
console.error('Error reading or parsing the RC file:', error);
throw error;
}
}
static getCacheFile() {
if (fs.existsSync(AppConfig.cacheFile)) {
return AppConfig.cacheFile;
}
else {
const basePath = `.compassrc.cache`;
if (fs.existsSync(basePath)) {
return basePath;
}
}
return AppConfig.cacheFile;
}
static async read() {
try {
const json = await fs.readFile(AppConfig.getCacheFile(), 'utf-8');
return JSON.parse(json);
}
catch (error) {
console.error('Error reading or parsing the RC file:', error);
throw error;
}
}
static readSync() {
try {
const json = fs.readFileSync(AppConfig.getCacheFile(), 'utf-8');
return JSON.parse(json);
}
catch (error) {
console.error('Error reading or parsing the RC file:', error);
throw error;
}
}
static resolveName(config) {
if (!config.name) {
if (fs.existsSync('./package.json')) {
var packageJson = fs.readFileSync('./package.json', 'utf-8');
if (packageJson) {
config.name = normalizer_browser_1.Normalizer.capitalizePackageName(JSON.parse(packageJson).name, ' ');
}
}
}
return config;
}
}
exports.AppConfig = AppConfig;