@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
271 lines • 7.83 kB
JavaScript
"use strict";
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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProjectModule = exports.Module = void 0;
exports.newModule = newModule;
exports.newProjectModule = newProjectModule;
const type_1 = require("../type");
const mount_1 = require("./mount");
const path = __importStar(require("path"));
/**
* Module value object representing a downloaded module
* TypeScript version of Go's Module struct
*/
class Module {
constructor(fs, absoluteDir, modulePath, parent = null) {
this.fs = fs;
this.absoluteDir = absoluteDir;
this.modulePath = modulePath;
this.parentModule = parent;
this.mountDirs = [];
this.metadata = null;
}
/**
* Get module owner (parent)
*/
owner() {
return this.parentModule;
}
/**
* Get module mounts
*/
mounts() {
return this.mountDirs.map(mount => mount);
}
/**
* Get module directory
*/
dir() {
return this.absoluteDir;
}
/**
* Get module path
*/
path() {
return this.modulePath;
}
/**
* Set module metadata
*/
setMetadata(metadata) {
this.metadata = metadata;
}
/**
* Get module metadata
*/
getMetadata() {
return this.metadata;
}
/**
* Apply mounts from import configuration
*/
async applyMounts(moduleImport) {
try {
let mounts = moduleImport.mounts || [];
// If no mounts specified, use default component mounts
if (mounts.length === 0) {
for (const componentFolder of type_1.ComponentFolders) {
const sourceDir = path.join(this.absoluteDir, componentFolder);
try {
const stat = await this.fs.stat(sourceDir);
if (stat.isDir()) {
mounts.push({
sourcePath: componentFolder,
targetPath: componentFolder,
});
}
}
catch (error) {
// Directory doesn't exist, skip it
}
}
}
// Convert mount configs to Mount objects
this.mountDirs = mounts.map(mountConfig => (0, mount_1.newMount)(mountConfig.sourcePath, mountConfig.targetPath));
}
catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new type_1.ModuleError(`Failed to apply mounts: ${message}`, 'MOUNT_FAILED');
}
}
/**
* Add a mount
*/
appendMount(mount) {
this.mountDirs.push(mount);
}
/**
* Remove a mount
*/
removeMount(mount) {
const index = this.mountDirs.findIndex(m => m.equals(mount));
if (index >= 0) {
this.mountDirs.splice(index, 1);
return true;
}
return false;
}
/**
* Get mount by target path
*/
getMountByTarget(targetPath) {
return this.mountDirs.find(mount => mount.target() === targetPath) || null;
}
/**
* Get mounts by component
*/
getMountsByComponent(component) {
return this.mountDirs.filter(mount => mount.component() === component);
}
/**
* Check if module is downloaded
*/
isDownloaded() {
return this.metadata?.downloadStatus === type_1.DownloadStatus.COMPLETED;
}
/**
* Check if module is being downloaded
*/
isDownloading() {
return this.metadata?.downloadStatus === type_1.DownloadStatus.DOWNLOADING;
}
/**
* Check if module download failed
*/
isDownloadFailed() {
return this.metadata?.downloadStatus === type_1.DownloadStatus.FAILED;
}
/**
* Check if directory exists
*/
async exists() {
try {
const stat = await this.fs.stat(this.absoluteDir);
return stat.isDir();
}
catch (error) {
return false;
}
}
/**
* Get module info summary
*/
getSummary() {
return {
path: this.modulePath,
dir: this.absoluteDir,
mountCount: this.mountDirs.length,
downloaded: this.isDownloaded(),
hasParent: this.parentModule !== null,
};
}
/**
* Create a copy of this module
*/
copy() {
const copy = new Module(this.fs, this.absoluteDir, this.modulePath, this.parentModule);
copy.mountDirs = this.mountDirs.map(mount => mount.copy());
copy.metadata = this.metadata ? { ...this.metadata } : null;
return copy;
}
/**
* String representation
*/
toString() {
return `Module{path: ${this.modulePath}, dir: ${this.absoluteDir}, mounts: ${this.mountDirs.length}}`;
}
}
exports.Module = Module;
/**
* Project module - special module representing the main project
*/
class ProjectModule {
constructor(module) {
this.module = module;
}
/**
* Get the underlying module
*/
getModule() {
return this.module;
}
/**
* Append mount to project module
*/
appendMount(mount) {
this.module.appendMount(mount);
}
/**
* Apply default project mounts
*/
applyDefaultMounts() {
const defaultMounts = (0, mount_1.createDefaultMounts)(type_1.ComponentFolders);
for (const mount of defaultMounts) {
this.module.appendMount(mount);
}
}
/**
* Delegate methods to underlying module
*/
owner() {
return this.module.owner();
}
mounts() {
return this.module.mounts();
}
dir() {
return this.module.dir();
}
path() {
return this.module.path();
}
}
exports.ProjectModule = ProjectModule;
/**
* Creates a new Module instance
*/
function newModule(fs, absoluteDir, modulePath, parent) {
return new Module(fs, absoluteDir, modulePath, parent || null);
}
/**
* Creates a new ProjectModule instance
*/
function newProjectModule(fs, absoluteDir, modulePath) {
const module = new Module(fs, absoluteDir, modulePath, null);
const projectModule = new ProjectModule(module);
projectModule.applyDefaultMounts();
return projectModule;
}
//# sourceMappingURL=module.js.map