hyper/cli/api.ts

162 lines
4.6 KiB
TypeScript
Raw Permalink 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';
import got from 'got';
import registryUrlModule from 'registry-url';
const registryUrl = registryUrlModule();
import pify from 'pify';
import * as recast from 'recast';
import path from 'path';
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
const applicationDirectory =
process.env.XDG_CONFIG_HOME !== undefined
? path.join(process.env.XDG_CONFIG_HOME, 'hyper')
: process.platform == 'win32'
2019-12-14 02:29:48 -09:00
? path.join(process.env.APPDATA!, 'Hyper')
: os.homedir();
2019-05-08 13:18:21 -08:00
const devConfigFileName = path.join(__dirname, `../.hyper.js`);
2019-12-14 02:29:48 -09:00
const fileName =
process.env.NODE_ENV !== 'production' && fs.existsSync(devConfigFileName)
? devConfigFileName
2019-05-08 13:18:21 -08:00
: path.join(applicationDirectory, '.hyper.js');
/**
* 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;
return ((...args: any[]) => {
if (!hasResult) {
result = fn(...args);
hasResult = true;
}
return result;
2019-12-14 02:29:48 -09:00
}) as T;
}
const getFileContents = memoize(() => {
try {
return fs.readFileSync(fileName, 'utf8');
} catch (_err) {
const err = _err as {code: string};
if (err.code !== 'ENOENT') {
// ENOENT === !exists()
throw err;
}
}
return null;
});
2019-12-14 02:29:48 -09:00
const getParsedFile = memoize(() => recast.parse(getFileContents()!));
2020-07-04 09:50:46 -08:00
const getProperties = memoize(
(): any[] =>
((getParsedFile()?.program?.body as any[]) || []).find(
(bodyItem) =>
bodyItem.type === 'ExpressionStatement' &&
bodyItem.expression.type === 'AssignmentExpression' &&
bodyItem.expression.left.object.name === 'module' &&
bodyItem.expression.left.property.name === 'exports' &&
bodyItem.expression.right.type === 'ObjectExpression'
)?.expression?.right?.properties || []
);
const getPluginsByKey = (key: string): any[] =>
getProperties().find((property) => property?.key?.name === key)?.value?.elements || [];
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)) {
2020-03-25 02:15:08 -08:00
return array.some((entry) => entry.value === plugin);
}
return false;
}
function save() {
return pify(fs.writeFile)(fileName, recast.print(getParsedFile()).code, '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);
2021-05-10 04:54:21 -08:00
return got.get<any>(registryUrl + name.toLowerCase(), {timeout: 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`);
}
array.push(recast.types.builders.literal(plugin));
return save();
});
}
2019-12-14 02:29:48 -09:00
function uninstall(plugin: string) {
if (!isInstalled(plugin)) {
return Promise.reject(`${plugin} is not installed`);
}
2020-07-04 09:50:46 -08:00
const index = getPlugins().findIndex((entry) => entry.value === plugin);
getPlugins().splice(index, 1);
return save();
}
function list() {
2020-07-04 09:50:46 -08:00
if (getPlugins().length > 0) {
return getPlugins()
2020-03-25 02:15:08 -08:00
.map((plugin) => plugin.value)
.join('\n');
}
return false;
}
2019-12-14 02:29:48 -09:00
export const configPath = fileName;
export {exists, existsOnNpm, isInstalled, install, uninstall, list};