Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | /**
* Path Helper Utilities
*
* Handles path resolution for application directories and resources.
* Uses dependency injection instead of global state.
*/
import path from 'path';
import fs from 'fs-extra';
/**
* PathHelper class provides methods for resolving application paths
* Uses dependency injection for platform adapters
*/
export class PathHelper {
_lastBuildDir;
configGetter;
appInfo;
rootPath;
/**
* Create a PathHelper instance
* @param appInfo Platform adapter for app info
* @param rootPath Root path of the application
* @param configOrGetter Either a static config object or a function that returns the current config
*/
constructor(appInfo, rootPath, configOrGetter = {}) {
this.appInfo = appInfo;
this.rootPath = rootPath;
// Support both static config (for backward compatibility) and dynamic getter
this.configGetter = typeof configOrGetter === 'function'
? configOrGetter
: () => configOrGetter;
}
/**
* Get the current config (always fresh from getter)
*/
get config() {
return this.configGetter();
}
/* DIRS */
/**
* Get the root data directory for Quiqr
*/
getRoot() {
let thedir;
const config = this.config;
if (config.dataFolder && fs.existsSync(config.dataFolder)) {
thedir = config.dataFolder;
}
else {
thedir = path.join(this.appInfo.getPath('home'), 'Quiqr');
fs.ensureDirSync(thedir);
// Note: We no longer mutate config here since it may be a dynamic getter
// The caller should update the config source directly if needed
}
return thedir;
}
/**
* Get the temporary directory
*/
getTempDir() {
const dir = path.join(this.getRoot(), 'temp');
fs.ensureDirSync(dir);
return dir;
}
/**
* Get the root directory for a specific site
*/
getSiteRoot(siteKey) {
if (siteKey.trim() === '')
return null;
return path.join(this.getRoot(), 'sites', siteKey);
}
/**
* Get the current site mount path from global state
* TODO: Replace with dependency-injected value
*/
getSiteRootMountPath() {
return this.config.currentSitePath;
}
/**
* Get the default publish directory for a site
*/
getSiteDefaultPublishDir(siteKey, publishKey) {
if (siteKey.trim() === '')
return null;
const siteRoot = this.getSiteRoot(siteKey);
if (!siteRoot)
return null;
return path.join(siteRoot, 'publish', publishKey);
}
/**
* Get the publish repositories root directory
*/
getPublishReposRoot() {
return path.join(this.getRoot(), 'sitesRepos');
}
/**
* Get the binary root directory for a specific SSG type
* @param ssgType - The SSG type (e.g., 'hugo', 'eleventy')
*/
getSSGBinRoot(ssgType) {
return path.join(this.getRoot(), 'tools', `${ssgType}bin`);
}
/**
* Get the binary directory for a specific SSG version
* @param ssgType - The SSG type (e.g., 'hugo', 'eleventy')
* @param version - The version (e.g., 'v0.100.2', '2.0.1')
*/
getSSGBinDirForVer(ssgType, version) {
return path.join(this.getSSGBinRoot(ssgType), version);
}
/**
* Get the binary path for a specific SSG version
* @param ssgType - The SSG type (e.g., 'hugo', 'eleventy')
* @param version - The version (e.g., 'v0.100.2', '2.0.1')
*/
getSSGBinForVer(ssgType, version) {
// Check for custom environment variable (e.g., HUGO_PATH, ELEVENTY_PATH)
const envVarName = `${ssgType.toUpperCase()}_PATH`;
if (process.env[envVarName]) {
return process.env[envVarName];
}
const platform = process.platform.toLowerCase();
const binaryName = ssgType.toLowerCase();
const binDir = this.getSSGBinDirForVer(ssgType, version);
// For npm-based SSGs (like Eleventy), use node_modules/.bin/
if (ssgType.toLowerCase() === 'eleventy') {
const npmBinPath = path.join(binDir, 'node_modules', '.bin', binaryName);
if (platform.startsWith('win')) {
return npmBinPath + '.cmd';
}
return npmBinPath;
}
// For gem-based SSGs (like Jekyll), use wrapper scripts
if (ssgType.toLowerCase() === 'jekyll') {
if (platform.startsWith('win')) {
return path.join(binDir, 'jekyll.cmd');
}
return path.join(binDir, 'jekyll.sh');
}
// For standalone binaries (like Hugo)
if (platform.startsWith('win')) {
return path.join(binDir, `${binaryName}.exe`);
}
else {
return path.join(binDir, binaryName);
}
}
/**
* @deprecated Use getSSGBinRoot('hugo') instead
* Get the Hugo binary root directory
*/
getHugoBinRoot() {
return this.getSSGBinRoot('hugo');
}
/**
* @deprecated Use getSSGBinDirForVer('hugo', version) instead
* Get the Hugo binary directory for a specific version
*/
getHugoBinDirForVer(version) {
return this.getSSGBinDirForVer('hugo', version);
}
/**
* Get the last build directory
*/
getLastBuildDir() {
return this._lastBuildDir;
}
/**
* Set and get the build directory
*/
getBuildDir(dir) {
this._lastBuildDir = dir;
return this._lastBuildDir;
}
/**
* Get the Hugo themes directory
*/
getThemesDir() {
return path.join(this.getRoot(), 'tools', 'hugothemes');
}
/**
* Get the application resources directory based on platform and packaging status
*/
getApplicationResourcesDir(environment) {
if (environment.isPackaged) {
// On all platforms, app.getAppPath() returns the path to app.asar (a file).
// extraResources are placed in the parent directory next to app.asar.
// macOS: Quiqr.app/Contents/Resources/app.asar → Contents/Resources/
// Linux AppImage: /tmp/.mount_xxx/resources/app.asar → /tmp/.mount_xxx/resources/
// Linux RPM/DEB: /usr/lib/quiqr/resources/app.asar → /usr/lib/quiqr/resources/
// Windows: ...\resources\app.asar → ...\resources\
return path.dirname(this.rootPath);
}
else {
return path.join(this.rootPath, 'resources');
}
}
/**
* Get the workspace cache thumbs path
*/
workspaceCacheThumbsPath(workspacePath, relativePath) {
return path.join(workspacePath, '.quiqr-cache/thumbs', relativePath);
}
/**
* Get the SSH known_hosts file path
*/
getKnownHosts() {
const homeDir = this.appInfo.getPath('home');
return path.join(homeDir, '.ssh', 'known_hosts');
}
/**
* Get the site mount config path
*/
getSiteMountConfigPath(siteKey) {
const oldfile = path.join(this.getRoot(), 'config.' + siteKey + '.json');
if (fs.existsSync(oldfile)) {
return oldfile;
}
else {
return path.join(this.getRoot(), 'sites', siteKey, 'config.json');
}
}
/**
* @deprecated Use getSSGBinForVer('hugo', version) instead
* Get the Hugo binary path for a specific version
*/
getHugoBinForVer(version) {
return this.getSSGBinForVer('hugo', version);
}
/* PATH STRING CREATORS */
/**
* Generate a random path-safe string
*/
randomPathSafeString(length) {
return Math.random().toString(16).substring(2, length);
}
/* HELPERS */
/**
* Find the Hugo config file path in a Hugo root directory
* Supports both old (config.*) and new (hugo.*) naming conventions
*/
hugoConfigFilePath(hugoRootDir) {
// TODO this could be faster and less code
// let hugoConfigExp = path.join(this.workspacePath,'config.{'+formatProviderResolver.allFormatsExt().join(',')+'}');
// let hugoConfigPath = glob.sync(hugoConfigExp)[0];
let configExt;
const configBase = path.join(hugoRootDir, 'config');
const configNewBase = path.join(hugoRootDir, 'hugo');
let confVersion = 1;
// Check old-style config files
if (fs.existsSync(configBase + '.toml')) {
configExt = '.toml';
}
else if (fs.existsSync(configBase + '.json')) {
configExt = '.json';
}
else if (fs.existsSync(configBase + '.yaml')) {
configExt = '.yaml';
}
else if (fs.existsSync(configBase + '.yml')) {
configExt = '.yml';
}
// Check new-style hugo files
else if (fs.existsSync(configNewBase + '.toml')) {
configExt = '.toml';
confVersion = 2;
}
else if (fs.existsSync(configNewBase + '.json')) {
configExt = '.json';
confVersion = 2;
}
else if (fs.existsSync(configNewBase + '.yaml')) {
configExt = '.yaml';
confVersion = 2;
}
else if (fs.existsSync(configNewBase + '.yml')) {
configExt = '.yml';
confVersion = 2;
}
else {
return null;
}
if (confVersion === 1) {
return configBase + configExt;
}
else {
return configNewBase + configExt;
}
}
}
|