hyper/cli/api.ts

140 lines
3.9 KiB
TypeScript
Raw Normal View History

// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable @typescript-eslint/no-unsafe-return */
2019-12-14 02:29:48 -09:00
import fs from 'fs';
import os from 'os';
2023-07-25 09:30:19 -08:00
import path from 'path';
2019-12-14 02:29:48 -09:00
import got from 'got';
import registryUrlModule from 'registry-url';
2023-07-25 09:30:19 -08:00
2019-12-14 02:29:48 -09:00
const registryUrl = registryUrlModule();
2019-05-08 13:18:21 -08:00
// If the user defines XDG_CONFIG_HOME they definitely want their config there,
// otherwise use the home directory in linux/mac and userdata in windows
2022-12-25 20:01:04 -09:00
const applicationDirectory = process.env.XDG_CONFIG_HOME
? path.join(process.env.XDG_CONFIG_HOME, 'Hyper')
: process.platform === 'win32'
2023-11-14 21:18:34 -09:00
? path.join(process.env.APPDATA!, 'Hyper')
: path.join(os.homedir(), '.config', 'Hyper');
2019-05-08 13:18:21 -08:00
2022-12-25 20:01:04 -09:00
const devConfigFileName = path.join(__dirname, `../hyper.json`);
2019-12-14 02:29:48 -09:00
const fileName =
process.env.NODE_ENV !== 'production' && fs.existsSync(devConfigFileName)
? devConfigFileName
2022-12-25 20:01:04 -09:00
: path.join(applicationDirectory, 'hyper.json');
/**
* We need to make sure the file reading and parsing is lazy so that failure to
* statically analyze the hyper configuration isn't fatal for all kinds of
* subcommands. We can use memoization to make reading and parsing lazy.
*/
2019-12-14 02:29:48 -09:00
function memoize<T extends (...args: any[]) => any>(fn: T): T {
let hasResult = false;
2019-12-14 02:29:48 -09:00
let result: any;
2023-06-25 04:27:42 -08:00
return ((...args: Parameters<T>) => {
if (!hasResult) {
result = fn(...args);
hasResult = true;
}
return result;
2019-12-14 02:29:48 -09:00
}) as T;
}
const getFileContents = memoize(() => {
2022-12-25 20:01:04 -09:00
return fs.readFileSync(fileName, 'utf8');
});
2022-12-25 20:01:04 -09:00
const getParsedFile = memoize(() => JSON.parse(getFileContents()));
2020-07-04 09:50:46 -08:00
2022-12-25 20:01:04 -09:00
const getPluginsByKey = (key: string): any[] => getParsedFile()[key] || [];
const getPlugins = memoize(() => {
return getPluginsByKey('plugins');
});
const getLocalPlugins = memoize(() => {
return getPluginsByKey('localPlugins');
});
function exists() {
return getFileContents() !== undefined;
}
2019-12-14 02:29:48 -09:00
function isInstalled(plugin: string, locally?: boolean) {
2020-07-04 09:50:46 -08:00
const array = locally ? getLocalPlugins() : getPlugins();
if (array && Array.isArray(array)) {
2022-12-25 20:01:04 -09:00
return array.includes(plugin);
}
return false;
}
2022-12-25 20:01:04 -09:00
function save(config: any) {
return fs.writeFileSync(fileName, JSON.stringify(config, null, 2), 'utf8');
}
2019-12-14 02:29:48 -09:00
function getPackageName(plugin: string) {
const isScoped = plugin[0] === '@';
const nameWithoutVersion = plugin.split('#')[0];
if (isScoped) {
2019-12-14 02:29:48 -09:00
return `@${nameWithoutVersion.split('@')[1].replace('/', '%2f')}`;
}
return nameWithoutVersion.split('@')[0];
}
2019-12-14 02:29:48 -09:00
function existsOnNpm(plugin: string) {
const name = getPackageName(plugin);
2022-02-06 22:58:24 -09:00
return got
.get<any>(registryUrl + name.toLowerCase(), {timeout: {request: 10000}, responseType: 'json'})
.then((res) => {
if (!res.body.versions) {
return Promise.reject(res);
} else {
return res;
}
});
2019-12-14 02:29:48 -09:00
}
function install(plugin: string, locally?: boolean) {
2020-07-04 09:50:46 -08:00
const array = locally ? getLocalPlugins() : getPlugins();
return existsOnNpm(plugin)
2019-12-14 02:29:48 -09:00
.catch((err: any) => {
const {statusCode} = err;
if (statusCode && (statusCode === 404 || statusCode === 200)) {
return Promise.reject(`${plugin} not found on npm`);
}
return Promise.reject(`${err.message}\nPlugin check failed. Check your internet connection or retry later.`);
})
.then(() => {
if (isInstalled(plugin, locally)) {
return Promise.reject(`${plugin} is already installed`);
}
2022-12-25 20:01:04 -09:00
const config = getParsedFile();
config[locally ? 'localPlugins' : 'plugins'] = [...array, plugin];
save(config);
});
}
2022-12-25 20:01:04 -09:00
async function uninstall(plugin: string) {
if (!isInstalled(plugin)) {
return Promise.reject(`${plugin} is not installed`);
}
2022-12-25 20:01:04 -09:00
const config = getParsedFile();
config.plugins = getPlugins().filter((p) => p !== plugin);
save(config);
}
function list() {
2020-07-04 09:50:46 -08:00
if (getPlugins().length > 0) {
2022-12-25 20:01:04 -09:00
return getPlugins().join('\n');
}
return false;
}
2019-12-14 02:29:48 -09:00
export const configPath = fileName;
export {exists, existsOnNpm, isInstalled, install, uninstall, list};