2021-03-28 12:13:49 -08:00
|
|
|
// 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';
|
2018-01-09 06:05:19 -09:00
|
|
|
|
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')
|
2019-10-02 16:56:50 -08:00
|
|
|
: process.platform == 'win32'
|
2019-12-14 02:29:48 -09:00
|
|
|
? path.join(process.env.APPDATA!, 'Hyper')
|
2019-10-02 16:56:50 -08:00
|
|
|
: os.homedir();
|
2019-05-08 13:18:21 -08:00
|
|
|
|
2018-04-20 07:02:30 -08:00
|
|
|
const devConfigFileName = path.join(__dirname, `../.hyper.js`);
|
|
|
|
|
|
2019-12-14 02:29:48 -09:00
|
|
|
const fileName =
|
2018-04-20 07:02:30 -08:00
|
|
|
process.env.NODE_ENV !== 'production' && fs.existsSync(devConfigFileName)
|
|
|
|
|
? devConfigFileName
|
2019-05-08 13:18:21 -08:00
|
|
|
: path.join(applicationDirectory, '.hyper.js');
|
2019-07-30 05:40:33 -08:00
|
|
|
|
2018-01-12 22:04:57 -09:00
|
|
|
/**
|
|
|
|
|
* 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 {
|
2018-01-12 22:04:57 -09:00
|
|
|
let hasResult = false;
|
2019-12-14 02:29:48 -09:00
|
|
|
let result: any;
|
|
|
|
|
return ((...args: any[]) => {
|
2018-01-12 22:04:57 -09:00
|
|
|
if (!hasResult) {
|
|
|
|
|
result = fn(...args);
|
|
|
|
|
hasResult = true;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2019-12-14 02:29:48 -09:00
|
|
|
}) as T;
|
2018-01-09 06:05:19 -09:00
|
|
|
}
|
|
|
|
|
|
2018-01-12 22:04:57 -09:00
|
|
|
const getFileContents = memoize(() => {
|
|
|
|
|
try {
|
|
|
|
|
return fs.readFileSync(fileName, 'utf8');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err.code !== 'ENOENT') {
|
|
|
|
|
// ENOENT === !exists()
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
2019-12-14 02:29:48 -09:00
|
|
|
const getParsedFile = memoize(() => recast.parse(getFileContents()!));
|
2018-01-12 22:04:57 -09:00
|
|
|
|
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 || [];
|
2019-12-20 04:46:36 -09:00
|
|
|
|
|
|
|
|
const getPlugins = memoize(() => {
|
|
|
|
|
return getPluginsByKey('plugins');
|
2019-07-30 05:40:33 -08:00
|
|
|
});
|
2018-01-12 22:04:57 -09:00
|
|
|
|
2019-07-30 05:40:33 -08:00
|
|
|
const getLocalPlugins = memoize(() => {
|
2019-12-20 04:46:36 -09:00
|
|
|
return getPluginsByKey('localPlugins');
|
2019-07-30 05:40:33 -08:00
|
|
|
});
|
2018-01-12 22:04:57 -09:00
|
|
|
|
2018-01-09 06:05:19 -09:00
|
|
|
function exists() {
|
2018-01-12 22:04:57 -09:00
|
|
|
return getFileContents() !== undefined;
|
2018-01-09 06:05:19 -09:00
|
|
|
}
|
|
|
|
|
|
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();
|
2018-01-09 06:05:19 -09:00
|
|
|
if (array && Array.isArray(array)) {
|
2020-03-25 02:15:08 -08:00
|
|
|
return array.some((entry) => entry.value === plugin);
|
2018-01-09 06:05:19 -09:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function save() {
|
2018-01-12 22:04:57 -09:00
|
|
|
return pify(fs.writeFile)(fileName, recast.print(getParsedFile()).code, 'utf8');
|
2018-01-09 06:05:19 -09:00
|
|
|
}
|
|
|
|
|
|
2019-12-14 02:29:48 -09:00
|
|
|
function getPackageName(plugin: string) {
|
2018-05-23 13:32:38 -08:00
|
|
|
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')}`;
|
2018-05-23 13:32:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nameWithoutVersion.split('@')[0];
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-14 02:29:48 -09:00
|
|
|
function existsOnNpm(plugin: string) {
|
|
|
|
|
const name = getPackageName(plugin);
|
|
|
|
|
return got
|
|
|
|
|
.get<any>(registryUrl + name.toLowerCase(), {timeout: 10000, responseType: 'json'})
|
2020-03-25 02:15:08 -08:00
|
|
|
.then((res) => {
|
2019-12-14 02:29:48 -09:00
|
|
|
if (!res.body.versions) {
|
|
|
|
|
return Promise.reject(res);
|
|
|
|
|
} else {
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function install(plugin: string, locally?: boolean) {
|
2020-07-04 09:50:46 -08:00
|
|
|
const array = locally ? getLocalPlugins() : getPlugins();
|
2018-09-23 11:14:39 -08:00
|
|
|
return existsOnNpm(plugin)
|
2019-12-14 02:29:48 -09:00
|
|
|
.catch((err: any) => {
|
2018-09-23 11:14:39 -08:00
|
|
|
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();
|
|
|
|
|
});
|
2018-01-09 06:05:19 -09:00
|
|
|
}
|
|
|
|
|
|
2019-12-14 02:29:48 -09:00
|
|
|
function uninstall(plugin: string) {
|
2018-09-23 11:14:39 -08:00
|
|
|
if (!isInstalled(plugin)) {
|
|
|
|
|
return Promise.reject(`${plugin} is not installed`);
|
|
|
|
|
}
|
2018-01-09 06:05:19 -09:00
|
|
|
|
2020-07-04 09:50:46 -08:00
|
|
|
const index = getPlugins().findIndex((entry) => entry.value === plugin);
|
|
|
|
|
getPlugins().splice(index, 1);
|
2018-09-23 11:14:39 -08:00
|
|
|
return save();
|
2018-01-09 06:05:19 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2018-01-12 22:04:57 -09:00
|
|
|
.join('\n');
|
2018-01-09 06:05:19 -09:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-14 02:29:48 -09:00
|
|
|
export const configPath = fileName;
|
|
|
|
|
export {exists, existsOnNpm, isInstalled, install, uninstall, list};
|