@stencila/jesta
Version:
Stencila plugin for executable documents using JavaScript
64 lines (63 loc) • 2.73 kB
JavaScript
;
/**
* Utility module with functions that return the path to operating
* system specific directories.
*
* Whilst there are packages that provide similar functions, we
* generally prefer to roll our own for each language to make
* sure that there is consistency in their implementation.
*
* Much of these implementations is based on
* https://github.com/sindresorhus/env-paths/blob/master/index.js
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cache = exports.plugin = void 0;
const fs_1 = __importDefault(require("fs"));
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const homedir = os_1.default.homedir();
/**
* Get a Stencila plugin directory.
*
* The `plugins` directory is used to store the manifests and other
* data of Stencila plugins.
*/
function plugin(name, ensure = true) {
const dir = path_1.default.join((() => {
var _a, _b;
const { env, platform } = process;
if (platform === 'darwin')
return path_1.default.join(homedir, 'Library', 'Preferences', 'Stencila', 'Plugins');
if (platform === 'win32')
return path_1.default.join((_a = env.APPDATA) !== null && _a !== void 0 ? _a : path_1.default.join(homedir, 'AppData', 'Roaming'), 'Stencila', 'Config', 'Plugins');
return path_1.default.join((_b = env.XDG_CONFIG_HOME) !== null && _b !== void 0 ? _b : path_1.default.join(homedir, '.config'), 'stencila', 'plugins');
})(), name);
if (ensure && !fs_1.default.existsSync(dir))
fs_1.default.mkdirSync(dir, { recursive: true });
return dir;
}
exports.plugin = plugin;
/**
* Get the Stencila cache directory.
*
* The `cache` directory is used to store cached content that may
* be used across applications, plugins and machine restarts.
*/
function cache(ensure = true) {
const dir = (() => {
var _a, _b;
const { env, platform } = process;
if (platform === 'darwin')
return path_1.default.join(homedir, 'Library', 'Caches', 'Stencila');
if (platform === 'win32')
return path_1.default.join((_a = env.LOCALAPPDATA) !== null && _a !== void 0 ? _a : path_1.default.join(homedir, 'AppData', 'Local'), 'Stencila', 'Cache');
return path_1.default.join((_b = env.XDG_CACHE_HOME) !== null && _b !== void 0 ? _b : path_1.default.join(homedir, '.cache'), 'stencila');
})();
if (ensure && !fs_1.default.existsSync(dir))
fs_1.default.mkdirSync(dir, { recursive: true });
return dir;
}
exports.cache = cache;