import { readFileSync } from 'graceful-fs'; import { join, normalize, resolve } from 'path'; import { wchDeliveryUrlFromApiUrl } from './wchtools'; function _readPackageConfig(aPackageDir: string): any { // read the config const filename = join(aPackageDir, 'package.json'); // read try { return JSON.parse(readFileSync(filename, 'utf-8')).config || {}; } catch (e) { return {}; } } function _getDataDir(aPackageDir: string, aConfig: any): string | undefined { // check if we have a configured application name return (aConfig && aConfig.data) ? normalize(join(aPackageDir, aConfig.data)) : undefined; } function _getDefaultDataDir(aPackageDir: string): string | undefined { // check if we have a configured application name return normalize(join(aPackageDir, 'data')); } /** * Returns the directory of the application, typcially './src' * * @param aPackageDir package directory * @param aConfig config directory */ function _getAppDir(aPackageDir: string, aConfig: any): string | undefined { // check if we have a configured application name return (aConfig && aConfig.src) ? normalize(join(aPackageDir, aConfig.src)) : normalize(join(aPackageDir, 'src')); } /** * Returns the directory of the application, typcially './src' * * @param aPackageDir package directory * @param aConfig config directory */ function _getDefaultAppDir(aPackageDir: string): string { return normalize(join(aPackageDir, 'src')); } function _getAppName(aPackageDir: string, aConfig: any): string | undefined { // check if we have a configured application name return (aConfig && aConfig.name) ? aConfig.name : undefined; } function _getContextRoot(aPackageDir: string, aConfig: any): string | undefined { // check if we have a configured application name return (aConfig && aConfig.root) ? aConfig.root : undefined; } // trim slashes at the beginning and the end const TRIM_REGEXP = /^[\/]+|[\/]+$/g; /** * Trims leading and trailing slashes * * @param aAppName the app name * @return the trimmed slashes */ function _trimSlashes(aAppName?: string): string { return aAppName ? aAppName.replace(TRIM_REGEXP, '') : ''; } function _createUrl(aAppName: string, aApiBaseUrl: string): string { // build const deliveryUrl = wchDeliveryUrlFromApiUrl(aApiBaseUrl); const trimmedName = _trimSlashes(aAppName); // build the base URL return trimmedName ? `${deliveryUrl}${encodeURIComponent(trimmedName)}/` : deliveryUrl; } function _getIndexDirFromContextRoot(aContextRoot: string, aDataDir: string): string { // split the root into path elements return resolve(normalize(join(aDataDir, 'assets', ...aContextRoot.split('/').map(decodeURIComponent)))); } export { _readPackageConfig as configReadPackageConfig, _getDataDir as configGetDataDir, _getAppDir as configGetAppDir, _getDefaultDataDir as configGetDefaultDataDir, _getDefaultAppDir as configGetDefaultAppDir, _getAppName as configGetAppName, _getContextRoot as configGetContextRoot, _createUrl as configGetBaseUrl, _trimSlashes as configTrimSlashes, _getIndexDirFromContextRoot as configIndexDirFromContextRoot };