Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow XDG specifications and windows recognized environment variables #452

Open
shubham-cpp opened this issue Nov 29, 2024 · 0 comments
Open

Comments

@shubham-cpp
Copy link

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';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant