Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
feat: existsIn, appDependsOn() + moduleDependsOn()
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Kloubert committed Feb 9, 2024
1 parent cc51f91 commit c4163b3
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 31 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log (@egomobile/documentation)

## 0.7.0

- add `appDependsOn()` and `moduleDependsOn()` function
- add `existsIn` props to `DependencyItemWithInfo` and `ISerializableDependencyItemWithInfo` based items
- fix documentation

## 0.6.0

- add optional `entities` prop to `IDependencyInformationEntity`
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@egomobile/documentation",
"version": "0.6.0",
"version": "0.7.0",
"description": "Tools for documenting (TypeScript) code.",
"main": "lib/index.js",
"engines": {
Expand Down
25 changes: 19 additions & 6 deletions src/decorators/DependsOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import type { IDependencyInformation, IParameterDependencyItem, IPropertyDependencyItem, IMethodDependencyItem, DependencyInfoResolver, DependencyInfoCollectionArg } from "../types";
import type { ClassPropKey, CreateDependsOnHelpersFunc, Nilable, Optional } from "../types/internal";
import type { IDependencyInformation, IParameterDependencyItem, IPropertyDependencyItem, IMethodDependencyItem, DependencyInfoResolver, DependencyInfoCollectionArg, IStackInfo } from "../types";
import type { ClassPropKey, CreateDependsOnHelpersFunc, Nilable, Nullable, Optional } from "../types/internal";

/**
* Adds meta for a class, property, method or parameter
Expand Down Expand Up @@ -100,6 +100,9 @@ export function DependsOn(
dependenciesOrResolver?: Nilable<DependencyInfoCollectionArg>
): any {
const createDependsOnHelpers: CreateDependsOnHelpersFunc = require("../utils/internal").createDependsOnHelpers;
const tryGetStackInfo = require("../utils/internal").tryGetStackInfo;

const stackInfo: Nullable<IStackInfo | false> = tryGetStackInfo();

const {
addItem
Expand All @@ -117,7 +120,10 @@ export function DependsOn(
"type": "method"
};

addItem(newMethodItem);
addItem({
"existsIn": stackInfo,
"item": newMethodItem
});
};

const addAsParameter = () => {
Expand All @@ -131,7 +137,10 @@ export function DependsOn(
"type": "parameter"
};

addItem(newParameterItem);
addItem({
"existsIn": stackInfo,
"item": newParameterItem
});
};

const addAsProperty = () => {
Expand All @@ -143,7 +152,10 @@ export function DependsOn(
"type": "property"
};

addItem(newPropertyItem);
addItem({
"existsIn": stackInfo,
"item": newPropertyItem
});
};

const addWith3Args = () => {
Expand Down Expand Up @@ -176,7 +188,8 @@ export function DependsOn(
classDependsOn(
target,
infoOrResolver,
dependenciesOrResolver
dependenciesOrResolver,
stackInfo
);
}
}
Expand Down
141 changes: 134 additions & 7 deletions src/functions/dependsOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,60 @@
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import type { DependencyInfoCollectionArg, DependencyInfoResolver, IClassDependencyItem, IDependencyInformation, IFunctionDependencyItem } from "../types";
import type { DependencyInfoCollectionArg, DependencyInfoResolver, IClassDependencyItem, IDependencyInformation, IFunctionDependencyItem, IModuleDependencyItem, IStackInfo } from "../types";
import type { Constructor, CreateDependsOnHelpersFunc, Func, Nilable } from "../types/internal";

/**
* Adds dependency information for current app.
*
* @example
* ```
* import {
* appDependsOn,
* getDependencies
* } from "@egomobile/documentation"
*
* appDependsOn(
* {
* // your information here...
* }
* )
*
* console.log(
* getDependencies()
* )
* ```
*
* @param {IDependencyInformation|DependencyInfoResolver} infoOrResolver The dependency information or the function that resolves it.
* @param {Nilable<DependenciesArg>} [dependenciesOrResolver] The custom collection for the dependency info items or the function that resolves it.
* @param {Nilable<IStackInfo|false>} [stackInfo] Custom stack information.
*
* @throws ReferenceError No CommonJS based main module found.
*/
export function appDependsOn(
infoOrResolver: IDependencyInformation | DependencyInfoResolver,
dependenciesOrResolver?: Nilable<DependencyInfoCollectionArg>,
stackInfo?: Nilable<IStackInfo | false>
) {
const mainModule = require.main;
if (!mainModule) {
throw new ReferenceError("no main module found");
}

const tryGetStackInfo = require("../utils/internal").tryGetStackInfo;

if (!stackInfo) {
stackInfo = tryGetStackInfo();
}

moduleDependsOn(
mainModule,
infoOrResolver,
dependenciesOrResolver,
stackInfo
);
}

/**
* Adds dependency information for a class.
*
Expand All @@ -31,7 +82,7 @@ import type { Constructor, CreateDependsOnHelpersFunc, Func, Nilable } from "../
* }
*
* classDependsOn(
* MyClass
* MyClass,
* {
* // your information here...
* }
Expand All @@ -45,14 +96,21 @@ import type { Constructor, CreateDependsOnHelpersFunc, Func, Nilable } from "../
* @param {Constructor<any>} classConstructor The class constructor.
* @param {IDependencyInformation|DependencyInfoResolver} infoOrResolver The dependency information or the function that resolves it.
* @param {Nilable<DependenciesArg>} [dependenciesOrResolver] The custom collection for the dependency info items or the function that resolves it.
* @param {Nilable<IStackInfo|false>} [stackInfo] Custom stack information.
*/
export function classDependsOn(
classConstructor: Constructor<any>,
infoOrResolver: IDependencyInformation | DependencyInfoResolver,
dependenciesOrResolver?: Nilable<DependencyInfoCollectionArg>
dependenciesOrResolver?: Nilable<DependencyInfoCollectionArg>,
stackInfo?: Nilable<IStackInfo | false>
) {
const createDependsOnHelpers: CreateDependsOnHelpersFunc =
require("../utils/internal").createDependsOnHelpers;
const tryGetStackInfo = require("../utils/internal").tryGetStackInfo;

if (!stackInfo) {
stackInfo = tryGetStackInfo();
}

const {
addItem
Expand All @@ -63,7 +121,10 @@ export function classDependsOn(
"type": "class"
};

addItem(newClassItem);
addItem({
"existsIn": stackInfo ?? null,
"item": newClassItem
});
}

/**
Expand All @@ -81,7 +142,7 @@ export function classDependsOn(
* }
*
* functionDependsOn(
* myFunction
* myFunction,
* {
* // your information here...
* }
Expand All @@ -95,14 +156,21 @@ export function classDependsOn(
* @param {Func} func The function.
* @param {IDependencyInformation|DependencyInfoResolver} infoOrResolver The dependency information or the function that resolves it.
* @param {Nilable<DependenciesArg>} [dependenciesOrResolver] The custom collection for the dependency info items or the function that resolves it.
* @param {Nilable<IStackInfo|false>} [stackInfo] Custom stack information.
*/
export function functionDependsOn(
func: Func,
infoOrResolver: IDependencyInformation | DependencyInfoResolver,
dependenciesOrResolver?: Nilable<DependencyInfoCollectionArg>
dependenciesOrResolver?: Nilable<DependencyInfoCollectionArg>,
stackInfo?: Nilable<IStackInfo | false>
) {
const createDependsOnHelpers: CreateDependsOnHelpersFunc =
require("../utils/internal").createDependsOnHelpers;
const tryGetStackInfo = require("../utils/internal").tryGetStackInfo;

if (!stackInfo) {
stackInfo = tryGetStackInfo();
}

const {
addItem
Expand All @@ -113,5 +181,64 @@ export function functionDependsOn(
"type": "function"
};

addItem(newFunctionItem);
addItem({
"existsIn": stackInfo ?? null,
"item": newFunctionItem
});
}

/**
* Adds dependency information for a module.
*
* @example
* ```
* import {
* moduleDependsOn,
* getDependencies
* } from "@egomobile/documentation"
*
* moduleDependsOn(
* require.main,
* {
* // your information here...
* }
* )
*
* console.log(
* getDependencies()
* )
* ```
*
* @param {any} module The module.
* @param {IDependencyInformation|DependencyInfoResolver} infoOrResolver The dependency information or the function that resolves it.
* @param {Nilable<DependenciesArg>} [dependenciesOrResolver] The custom collection for the dependency info items or the function that resolves it.
* @param {Nilable<IStackInfo|false>} [stackInfo] Custom stack information.
*/
export function moduleDependsOn(
module: any,
infoOrResolver: IDependencyInformation | DependencyInfoResolver,
dependenciesOrResolver?: Nilable<DependencyInfoCollectionArg>,
stackInfo?: Nilable<IStackInfo | false>
) {
const createDependsOnHelpers: CreateDependsOnHelpersFunc =
require("../utils/internal").createDependsOnHelpers;
const tryGetStackInfo = require("../utils/internal").tryGetStackInfo;

if (!stackInfo) {
stackInfo = tryGetStackInfo();
}

const {
addItem
} = createDependsOnHelpers(infoOrResolver, dependenciesOrResolver);

const newModuleItem: IModuleDependencyItem = {
module,
"type": "module"
};

addItem({
"existsIn": stackInfo ?? null,
"item": newModuleItem
});
}
43 changes: 42 additions & 1 deletion src/types/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import type { IWithClassNameProp, IWithClassOrInstanceProp, IWithEffectsProps, IWithIsStaticProp, IWithReferencesProp, IWithRemarksProp, IWithTypeProp } from ".";
import type { IStackInfo, IWithClassNameProp, IWithClassOrInstanceProp, IWithEffectsProps, IWithIsStaticProp, IWithReferencesProp, IWithRemarksProp, IWithTypeProp } from ".";
import type { ClassPropKey, Collection, Constructor, Nilable, Nullable, Optional, ReferenceValue } from "./internal";

/**
Expand Down Expand Up @@ -48,6 +48,7 @@ export type DependencyItem =
IClassDependencyItem |
IFunctionDependencyItem |
IMethodDependencyItem |
IModuleDependencyItem |
IParameterDependencyItem |
IPropertyDependencyItem;

Expand All @@ -60,6 +61,13 @@ export type DependencyItemCollectionResolver = () => Collection<DependencyItemWi
* An extension of `DependencyItem`.
*/
export type DependencyItemWithInfo = DependencyItem & {
/**
* Information about the underlying file, if available.
*
* While `null` means that there is no information, does `false` indicate that detection of this
* information failed.
*/
existsIn: Nullable<IStackInfo | false>;
/**
* The underlying information.
*/
Expand Down Expand Up @@ -172,6 +180,20 @@ export interface IMethodDependencyItem extends IDependencyItem, Partial<IWithCla
type: "method";
}

/**
* A `IDependencyItem` with information about a module and its dependencies.
*/
export interface IModuleDependencyItem extends IDependencyItem {
/**
* The module.
*/
module: any;
/**
* The type.
*/
type: "module";
}

/**
* A `IDependencyItem` with information about a paremeter of a class method and its dependencies.
*/
Expand Down Expand Up @@ -222,6 +244,10 @@ export interface ISerializableClassDependencyItemWithInfo extends ISerializableD
* A serizable version of an `IDependencyItem`.
*/
export interface ISerializableDependencyItemWithInfo {
/**
* Information about the origin.
*/
existsIn: Nullable<IStackInfo>;
/**
* The underlying information.
*/
Expand Down Expand Up @@ -256,6 +282,20 @@ export interface ISerializableMethodDependencyItemWithInfo extends ISerializable
type: "method";
}

/**
* A serizable version of an `IModuleDependencyItem`.
*/
export interface ISerializableModuleDependencyItemWithInfo extends ISerializableDependencyItemWithInfo {
/**
* The name/path of the module.
*/
name: string;
/**
* The type.
*/
type: "module";
}

/**
* A serizable version of an `IParameterDependencyItem`.
*/
Expand Down Expand Up @@ -295,5 +335,6 @@ export type SerializableDependencyItem =
ISerializableClassDependencyItemWithInfo |
ISerializableFunctionDependencyItemWithInfo |
ISerializableMethodDependencyItemWithInfo |
ISerializableModuleDependencyItemWithInfo |
ISerializableParameterDependencyItemWithInfo |
ISerializablePropertyDependencyItemWithInfo;
Loading

0 comments on commit c4163b3

Please sign in to comment.