mirror of
https://github.com/quine-global/hyper.git
synced 2026-01-15 05:08:41 -09:00
check ast for module.exports in cli
This commit is contained in:
parent
dab6e06dc1
commit
a6997591da
1 changed files with 19 additions and 19 deletions
38
cli/api.ts
38
cli/api.ts
|
|
@ -54,20 +54,20 @@ const getFileContents = memoize(() => {
|
||||||
|
|
||||||
const getParsedFile = memoize(() => recast.parse(getFileContents()!));
|
const getParsedFile = memoize(() => recast.parse(getFileContents()!));
|
||||||
|
|
||||||
const getProperties = memoize(() => ((getParsedFile()?.program?.body as any[]) || []).map((obj) => obj));
|
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) => {
|
const getPluginsByKey = (key: string): any[] =>
|
||||||
const properties = getProperties();
|
getProperties().find((property) => property?.key?.name === key)?.value?.elements || [];
|
||||||
for (let i = 0; i < properties.length; i++) {
|
|
||||||
const rightProperties = Object.values<any>(properties[i]?.expression?.right?.properties || {});
|
|
||||||
for (let j = 0; j < rightProperties.length; j++) {
|
|
||||||
const plugin = rightProperties[j];
|
|
||||||
if (plugin?.key?.name === key) {
|
|
||||||
return (plugin?.value?.elements as any[]) || [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const getPlugins = memoize(() => {
|
const getPlugins = memoize(() => {
|
||||||
return getPluginsByKey('plugins');
|
return getPluginsByKey('plugins');
|
||||||
|
|
@ -82,7 +82,7 @@ function exists() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function isInstalled(plugin: string, locally?: boolean) {
|
function isInstalled(plugin: string, locally?: boolean) {
|
||||||
const array = (locally ? getLocalPlugins() : getPlugins()) || [];
|
const array = locally ? getLocalPlugins() : getPlugins();
|
||||||
if (array && Array.isArray(array)) {
|
if (array && Array.isArray(array)) {
|
||||||
return array.some((entry) => entry.value === plugin);
|
return array.some((entry) => entry.value === plugin);
|
||||||
}
|
}
|
||||||
|
|
@ -118,7 +118,7 @@ function existsOnNpm(plugin: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function install(plugin: string, locally?: boolean) {
|
function install(plugin: string, locally?: boolean) {
|
||||||
const array = (locally ? getLocalPlugins() : getPlugins()) || [];
|
const array = locally ? getLocalPlugins() : getPlugins();
|
||||||
return existsOnNpm(plugin)
|
return existsOnNpm(plugin)
|
||||||
.catch((err: any) => {
|
.catch((err: any) => {
|
||||||
const {statusCode} = err;
|
const {statusCode} = err;
|
||||||
|
|
@ -142,14 +142,14 @@ function uninstall(plugin: string) {
|
||||||
return Promise.reject(`${plugin} is not installed`);
|
return Promise.reject(`${plugin} is not installed`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const index = getPlugins()!.findIndex((entry) => entry.value === plugin);
|
const index = getPlugins().findIndex((entry) => entry.value === plugin);
|
||||||
getPlugins()!.splice(index, 1);
|
getPlugins().splice(index, 1);
|
||||||
return save();
|
return save();
|
||||||
}
|
}
|
||||||
|
|
||||||
function list() {
|
function list() {
|
||||||
if (Array.isArray(getPlugins())) {
|
if (getPlugins().length > 0) {
|
||||||
return getPlugins()!
|
return getPlugins()
|
||||||
.map((plugin) => plugin.value)
|
.map((plugin) => plugin.value)
|
||||||
.join('\n');
|
.join('\n');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue