Files
AFFiNE-Mirror/tools/utils/src/path.ts
forehalo 8519f4474a chore: cli to create self signed ca to dev with domain (#12466)
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
  - Introduced a CLI command to manage local Certificate Authority (CA) and generate SSL certificates for development domains.
  - Added example and template files for Nginx and OpenSSL configurations to support local development with SSL.
  - Provided new DNS and Nginx configuration files for enhanced local development setup.

- **Documentation**
  - Added a README with step-by-step instructions for setting up development containers and managing certificates on MacOS with OrbStack.

- **Chores**
  - Updated ignore patterns to exclude additional development files and directories.
  - Enhanced example Docker Compose files with commented service configurations and new volume definitions.
  - Removed the Elasticsearch example Docker Compose file.

- **Refactor**
  - Extended utility and command classes with new methods to support file operations and command execution.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-05-23 05:19:13 +00:00

94 lines
1.6 KiB
TypeScript

import {
existsSync,
mkdirSync,
readFileSync,
rmSync,
statSync,
writeFileSync,
} 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);
}
rm(opts: { recursive?: boolean } = {}) {
rmSync(this.path, { ...opts, force: true });
}
mkdir() {
mkdirSync(this.path, { recursive: true });
}
readAsFile() {
return readFileSync(this.path);
}
writeFile(content: Buffer | string) {
writeFileSync(this.path, content);
}
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('../../../');