@capawesome/cli
Version:
The Capawesome Cloud Command Line Interface (CLI) to manage Live Updates and more.
97 lines (96 loc) • 3.54 kB
JavaScript
import fs from 'fs';
import pathModule from 'path';
import { fileExistsAtPath } from './file.js';
/**
* Find the Capacitor config file in the current working directory.
* Looks for capacitor.config.json or capacitor.config.ts.
*
* @returns The path to the config file, or undefined if not found.
*/
export const findCapacitorConfigPath = async () => {
const cwd = process.cwd();
const jsonPath = pathModule.join(cwd, 'capacitor.config.json');
const tsPath = pathModule.join(cwd, 'capacitor.config.ts');
if (await fileExistsAtPath(jsonPath)) {
return jsonPath;
}
if (await fileExistsAtPath(tsPath)) {
return tsPath;
}
return undefined;
};
/**
* Read and parse the Capacitor config file.
*
* @param configPath The path to the config file.
* @returns The parsed config object, or undefined if parsing fails.
*/
export const readCapacitorConfig = async (configPath) => {
try {
if (configPath.endsWith('.json')) {
const content = await fs.promises.readFile(configPath, 'utf-8');
return JSON.parse(content);
}
else if (configPath.endsWith('.ts')) {
// For TypeScript config, parse as text and extract values
const content = await fs.promises.readFile(configPath, 'utf-8');
// Extract webDir using regex
const webDirMatch = content.match(/webDir:\s*['"]([^'"]+)['"]/);
const appIdMatch = content.match(/LiveUpdate:\s*{[^}]*appId:\s*['"]([^'"]+)['"]/s);
const publicKeyMatch = content.match(/LiveUpdate:\s*{[^}]*publicKey:\s*['"]([^'"]+)['"]/s);
const config = {};
if (webDirMatch) {
config.webDir = webDirMatch[1];
}
if (appIdMatch || publicKeyMatch) {
config.plugins = {
LiveUpdate: {
...(appIdMatch && { appId: appIdMatch[1] }),
...(publicKeyMatch && { publicKey: publicKeyMatch[1] }),
},
};
}
return Object.keys(config).length > 0 ? config : undefined;
}
}
catch (error) {
// Return undefined if parsing fails
}
return undefined;
};
/**
* Get the webDir from the Capacitor config.
* Returns the absolute path to the webDir.
*
* @param configPath The path to the config file.
* @returns The absolute path to the webDir, or undefined if not found.
*/
export const getWebDirFromConfig = async (configPath) => {
const config = await readCapacitorConfig(configPath);
if (config?.webDir) {
// Resolve the webDir relative to the config file location
const configDir = pathModule.dirname(configPath);
return pathModule.resolve(configDir, config.webDir);
}
return undefined;
};
/**
* Get the LiveUpdate appId from the Capacitor config.
*
* @param configPath The path to the config file.
* @returns The appId, or undefined if not found.
*/
export const getLiveUpdatePluginAppIdFromConfig = async (configPath) => {
const config = await readCapacitorConfig(configPath);
return config?.plugins?.LiveUpdate?.appId;
};
/**
* Get the LiveUpdate publicKey from the Capacitor config.
*
* @param configPath The path to the config file.
* @returns The publicKey, or undefined if not found.
*/
export const getLiveUpdatePluginPublicKeyFromConfig = async (configPath) => {
const config = await readCapacitorConfig(configPath);
return config?.plugins?.LiveUpdate?.publicKey;
};