update
This commit is contained in:
63
src/fs.ts
63
src/fs.ts
@@ -1,5 +1,7 @@
|
||||
import { App, TFile, TFolder, normalizePath } from "obsidian";
|
||||
import { PROJECTS_ROOT, PROJECT_FILES } from "./const";
|
||||
import { PROJECTS_ROOT, PROJECT_FILES, TO_UPDATE_DIR } from "./const";
|
||||
|
||||
export type Zone = "ready" | "to-update";
|
||||
|
||||
export function projectsPath(): string {
|
||||
return PROJECTS_ROOT;
|
||||
@@ -9,13 +11,26 @@ export function projectPath(name: string): string {
|
||||
return normalizePath(`${PROJECTS_ROOT}/${name}`);
|
||||
}
|
||||
|
||||
export function areaPath(project: string, area: string): string {
|
||||
return normalizePath(`${PROJECTS_ROOT}/${project}/${area}`);
|
||||
export function toUpdatePath(project: string): string {
|
||||
return normalizePath(`${PROJECTS_ROOT}/${project}/${TO_UPDATE_DIR}`);
|
||||
}
|
||||
|
||||
export function featurePath(project: string, area: string, feature: string): string {
|
||||
export function zoneRootPath(project: string, zone: Zone): string {
|
||||
return zone === "ready" ? projectPath(project) : toUpdatePath(project);
|
||||
}
|
||||
|
||||
export function areaPath(project: string, area: string, zone: Zone = "ready"): string {
|
||||
return normalizePath(`${zoneRootPath(project, zone)}/${area}`);
|
||||
}
|
||||
|
||||
export function featurePath(
|
||||
project: string,
|
||||
area: string,
|
||||
feature: string,
|
||||
zone: Zone = "ready",
|
||||
): string {
|
||||
const file = feature.endsWith(".md") ? feature : `${feature}.md`;
|
||||
return normalizePath(`${PROJECTS_ROOT}/${project}/${area}/${file}`);
|
||||
return normalizePath(`${zoneRootPath(project, zone)}/${area}/${file}`);
|
||||
}
|
||||
|
||||
export async function ensureFolder(app: App, path: string): Promise<void> {
|
||||
@@ -82,8 +97,14 @@ export async function createProject(app: App, name: string): Promise<void> {
|
||||
await createArea(app, name, "story");
|
||||
}
|
||||
|
||||
export async function createArea(app: App, project: string, area: string): Promise<void> {
|
||||
await ensureFolder(app, areaPath(project, area));
|
||||
export async function createArea(
|
||||
app: App,
|
||||
project: string,
|
||||
area: string,
|
||||
zone: Zone = "to-update",
|
||||
): Promise<void> {
|
||||
if (zone === "to-update") await ensureFolder(app, toUpdatePath(project));
|
||||
await ensureFolder(app, areaPath(project, area, zone));
|
||||
}
|
||||
|
||||
export async function createFeature(
|
||||
@@ -91,21 +112,28 @@ export async function createFeature(
|
||||
project: string,
|
||||
area: string,
|
||||
feature: string,
|
||||
zone: Zone = "ready",
|
||||
): Promise<TFile> {
|
||||
return await ensureFile(app, featurePath(project, area, feature), "");
|
||||
return await ensureFile(app, featurePath(project, area, feature, zone), "");
|
||||
}
|
||||
|
||||
export function projectFeaturePath(project: string, feature: string): string {
|
||||
export function projectFeaturePath(
|
||||
project: string,
|
||||
feature: string,
|
||||
zone: Zone = "ready",
|
||||
): string {
|
||||
const file = feature.endsWith(".md") ? feature : `${feature}.md`;
|
||||
return normalizePath(`${PROJECTS_ROOT}/${project}/${file}`);
|
||||
return normalizePath(`${zoneRootPath(project, zone)}/${file}`);
|
||||
}
|
||||
|
||||
export async function createProjectFeature(
|
||||
app: App,
|
||||
project: string,
|
||||
feature: string,
|
||||
zone: Zone = "to-update",
|
||||
): Promise<TFile> {
|
||||
return await ensureFile(app, projectFeaturePath(project, feature), "");
|
||||
if (zone === "to-update") await ensureFolder(app, toUpdatePath(project));
|
||||
return await ensureFile(app, projectFeaturePath(project, feature, zone), "");
|
||||
}
|
||||
|
||||
export function isProjectRootFile(name: string): boolean {
|
||||
@@ -115,13 +143,22 @@ export function isProjectRootFile(name: string): boolean {
|
||||
export interface ProjectFileLocation {
|
||||
project: string;
|
||||
area?: string;
|
||||
zone: Zone;
|
||||
}
|
||||
|
||||
export function parseProjectFilePath(path: string): ProjectFileLocation | null {
|
||||
if (!path.endsWith(".md")) return null;
|
||||
const parts = normalizePath(path).split("/");
|
||||
if (parts[0] !== PROJECTS_ROOT) return null;
|
||||
if (parts.length === 3) return { project: parts[1] };
|
||||
if (parts.length === 4) return { project: parts[1], area: parts[2] };
|
||||
if (parts.length < 3) return null;
|
||||
const project = parts[1];
|
||||
const rest = parts.slice(2);
|
||||
let zone: Zone = "ready";
|
||||
if (rest[0] === TO_UPDATE_DIR) {
|
||||
zone = "to-update";
|
||||
rest.shift();
|
||||
}
|
||||
if (rest.length === 1) return { project, zone };
|
||||
if (rest.length === 2) return { project, area: rest[0], zone };
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user