Skip to content

Follow XDG specifications and windows recognized environment variables #452

Description

@shubham-cpp

On Linux there's a XDG Base Directory which is a specification to structures different directories created by application.

Right now srcbook is creating .srcbook in Home directory, which is not terrible but also not ideal.
Also on Windows we have APPDATA to create folders used by application (instead of USERPROFILE). You can check Recognized environment variables

I think a code like following

// file: packages/api/constants.mts
import os from 'node:os';
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'url';

// When running Srcbook as an npx executable, the cwd is not reliable.
// Commands that should be run from the root of the package, like npm scripts
// should therefore use DIST_DIR as the cwd.
const _filename = fileURLToPath(import.meta.url);
const _dirname = path.dirname(_filename);

export const HOME_DIR = (() => {
  let HOME = os.homedir();
  if (
    os.platform() === 'win32' &&
    process.env.APPDATA &&
    fs.existsSync(path.normalize(process.env.APPDATA))
  ) {
    HOME = path.normalize(process.env.APPDATA);
  } else if (os.platform() === 'linux') {
    if (process.env.XDG_CACHE_HOME && fs.existsSync(path.normalize(process.env.XDG_CACHE_HOME))) {
      HOME = path.normalize(process.env.XDG_CACHE_HOME);
    } else if (
      process.env.HOME &&
      fs.existsSync(path.normalize(path.join(os.homedir(), '.cache')))
    ) {
      HOME = path.normalize(path.join(os.homedir(), '.cache'));
    }
  }
  return HOME;
})(); /** A helper function to retrieve `APPDATA` on winddows and `XDG_CACHE_HOME` on linux*/

export const SRCBOOK_DIR = path.join(HOME_DIR, '.srcbook');
export const SRCBOOKS_DIR = path.join(SRCBOOK_DIR, 'srcbooks');
export const APPS_DIR = path.join(SRCBOOK_DIR, 'apps');
export const DIST_DIR = _dirname;
export const PROMPTS_DIR = path.join(DIST_DIR, 'prompts');
export const IS_PRODUCTION = process.env.NODE_ENV === 'production';

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions