Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

1.3 KB
Raw Blame
import { existsSync, statSync } from 'node:fs';
import { join, relative, sep } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';

export class Path {
  static dir(url: string) {
    return new Path(fileURLToPath(url)).join('..');
  }

  get value() {
    return this.path;
  }

  get relativePath() {
    return './' + this.path.slice(ProjectRoot.path.length).replace(/\\/g, '/');
  }

  constructor(private readonly path: string) {}

  join(...paths: string[]) {
    return new Path(join(this.path, ...paths));
  }

  parent() {
    return this.join('..');
  }

  toPosixString() {
    if (sep === '\\') {
      return this.path.replaceAll('\\', '/');
    }

    return this.path;
  }

  toString() {
    return this.path;
  }

  exists() {
    return existsSync(this.path);
  }

  stats() {
    return statSync(this.path);
  }

  isFile() {
    return this.exists() && this.stats().isFile();
  }

  isDirectory() {
    return this.exists() && this.stats().isDirectory();
  }

  toFileUrl() {
    return pathToFileURL(this.path);
  }

  relative(to: string) {
    const re = relative(this.value, to);
    if (sep === '\\') {
      return re.replaceAll('\\', '/');
    }

    return re;
  }
}

export const ProjectRoot = Path.dir(import.meta.url).join('../../../');