UNPKG

@lightweightform/theme-common

Version:

Common utilities for Lightweightform themes

143 lines (142 loc) 5.17 kB
import { Path } from '@angular-devkit/core'; import { Tree } from '@angular-devkit/schematics'; import ts from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript'; import { defaultBinding } from './ts-common'; /** * Information about a given schema. */ export interface SchemaInfo { /** * Information about the schema's file. */ fileInfo: SchemaFileInfo; /** * Storage path representing the schema. */ storagePath: string; /** * Type of schema (record or table). */ type: 'record' | 'table'; /** * TS node expression where the schema is declared. */ node: ts.CallExpression; /** * Object used to set the fields of the schema (for record schemas only). */ recordObject?: ts.ObjectLiteralExpression; } /** * Information about a given schema file. */ export interface SchemaFileInfo { /** * Schema's source file. */ source: ts.SourceFile; /** * File path where the schema is declared. */ filePath: Path; /** * Name with which `recordSchema` is exported in the source. `undefined` if it * isn't exported. */ recordSchemaName?: string; /** * Name with which `tableSchema` is exported in the source. `undefined` if it * isn't exported. */ tableSchemaName?: string; } /** * Class used to create a map of how schemas are distributed across files within * a user's project, in order to properly determine the relationship between a * newly created form in a schematic and its parent record. It allows users to * follow a directory-based hierarchical approach or a more liberal approach * when creating new forms or collections of forms. Note that this class builds * a "static" map of the schemas and will not be updated when schematics change * said schemas. */ export declare class SchemaExplorer { private host; /** * Name with which the root schema has been exported (or the default export * symbol). */ rootSchemaName: string | typeof defaultBinding; /** * Map from the storage path to the schema's info. */ private storagePathToSchemaInfo; /** * Map from a directory to the list of schema file paths in said directory * (sorted by those closest to the root schema). */ private dirToSchemaFilePaths; /** * Map from a file path to the list of schema informations in that file * (sorted by those closest to the root schema). */ private filePathToSchemaInfos; /** * Map from file path to its schema file information. */ private filePathToFileInfo; /** * Creates a new schema explorer. * @param host Source tree. * @param moduleDir Module directory (where the root schema file should be * found). */ constructor(host: Tree, moduleDir: Path); /** * Returns the schema info for a given storage path. * @param storagePath Storage path for which to fetch schema information. * @returns Schema info for the given storage path. */ getSchemaInfoFromStoragePath(storagePath: string): SchemaInfo | undefined; /** * Returns the list of schema file paths in the given directory, sorted by * those with schemas closest to the root schema. * @param dir Directory for which to fetch schema file paths. * @returns List of schema file paths in the given directory. */ getSchemaFilePathsFromDir(dir: Path): Path[]; /** * Returns all schema infos associated with the given file path, sorted by * those closest to the root schema. * @param filePath File path from which to fetch associated schemas. * @returns List of schema infos associated with the given file path. */ getSchemaInfoFromFilePath(filePath: Path): SchemaInfo[]; /** * Recursively builds info on all schemas from a given schema's info. * @param schemaInfo Info of the schema from which to build all schema's info. */ private buildAllSchemaInfo; /** * Obtains the schema information of the given node (which may be an * identifier [possibly imported] or the schema definition itself). * @param fileInfo File information of the node in question. * @param storagePath Storage path for the node in question. * @param node Node for which to fetch schema information. * @returns Schema information of the given node, `undefined` when the node * does not represent a record or table schema. */ private getSchemaInfo; /** * Returns the declaration exporting a file's schema. * @param source TS source code. * @returns Exported schema declaration. */ private getRootSchemaInfo; /** * Get the schema file information of a given file path (caching the obtained * objects). * @param filePath File path for which to get the information. * @returns Schema file information of a given path. */ private getSchemaFileInfo; }