Skip to content

Commit

Permalink
[INTERNAL] TypeScript: Add typings to BuildContext, Specification, Pr…
Browse files Browse the repository at this point in the history
…ojectGraph
  • Loading branch information
RandomByte committed Aug 28, 2024
1 parent c3495f7 commit 717c015
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 86 deletions.
27 changes: 23 additions & 4 deletions src/build/helpers/BuildContext.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
import type ProjectGraph from "../../graph/ProjectGraph.js";
import type Project from "../../specifications/Project.js";
import ProjectBuildContext from "./ProjectBuildContext.js";
import OutputStyleEnum from "./ProjectBuilderOutputStyle.js";
import type * as taskRepositoryModule from "@ui5/builder/internal/taskRepository";

interface BuildConfig {
selfContained: boolean;
cssVariables: boolean;
jsdoc: boolean;
createBuildManifest: boolean;
outputStyle: typeof OutputStyleEnum[keyof typeof OutputStyleEnum];
includedTasks: string[];
excludedTasks: string[];
}

/**
* Context of a build process
*
*/
class BuildContext {
constructor(graph, taskRepository, { // buildConfig
_graph: ProjectGraph;
_buildConfig: BuildConfig;
_taskRepository: typeof taskRepositoryModule;
_options: {cssVariables: boolean};
_projectBuildContexts: ProjectBuildContext[];

constructor(graph: ProjectGraph, taskRepository: typeof taskRepositoryModule, { // buildConfig
selfContained = false,
cssVariables = false,
jsdoc = false,
createBuildManifest = false,
outputStyle = OutputStyleEnum.Default,
includedTasks = [], excludedTasks = [],
} = {}) {
} = {} as Partial<BuildConfig>) {
if (!graph) {
throw new Error(`Missing parameter 'graph'`);
}
Expand Down Expand Up @@ -79,7 +98,7 @@ class BuildContext {
return this._graph.getRoot();
}

getOption(key) {
getOption(key: keyof typeof this._options) {
return this._options[key];
}

Expand All @@ -95,7 +114,7 @@ class BuildContext {
return this._graph;
}

createProjectContext({project}) {
createProjectContext({project}: {project: Project}) {
const projectBuildContext = new ProjectBuildContext({
buildContext: this,
project,
Expand Down
18 changes: 14 additions & 4 deletions src/build/helpers/ProjectBuildContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@ import ResourceTagCollection from "@ui5/fs/internal/ResourceTagCollection";
import ProjectBuildLogger from "@ui5/logger/internal/loggers/ProjectBuild";
import TaskUtil from "./TaskUtil.js";
import TaskRunner from "../TaskRunner.js";
import type BuildContext from "./BuildContext.js";
import type Project from "../../specifications/Project.js";

export type CleanupCallback = (force: boolean) => Promise<void>;

/**
* Build context of a single project. Always part of an overall
* [Build Context]{@link @ui5/project/build/helpers/BuildContext}
*
*/
class ProjectBuildContext {
constructor({buildContext, project}) {
_buildContext: BuildContext;
_project: Project;
_log: ProjectBuildLogger;
_queues: {cleanup: CleanupCallback[]};
_resourceTagCollection: ResourceTagCollection;

constructor({buildContext, project}: {buildContext: BuildContext; project: Project}) {
if (!buildContext) {
throw new Error(`Missing parameter 'buildContext'`);
}
Expand Down Expand Up @@ -37,15 +47,15 @@ class ProjectBuildContext {
return this._project === this._buildContext.getRootProject();
}

getOption(key) {
getOption(key: keyof typeof BuildContext.prototype._options) {
return this._buildContext.getOption(key);
}

registerCleanupTask(callback) {
registerCleanupTask(callback: CleanupCallback) {
this._queues.cleanup.push(callback);
}

async executeCleanupTasks(force) {
async executeCleanupTasks(force: boolean) {
await Promise.all(this._queues.cleanup.map((callback) => {
return callback(force);
}));
Expand Down
12 changes: 7 additions & 5 deletions src/build/helpers/TaskUtil.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {ResourceInterface} from "@ui5/fs/Resource";
import {
createReaderCollection,
createReaderCollectionPrioritized,
Expand All @@ -16,7 +17,6 @@ import {
* The set of available functions on that interface depends on the specification
* version defined for the extension.
*
* @alias @ui5/project/build/helpers/TaskUtil
* @hideconstructor
*/
class TaskUtil {
Expand Down Expand Up @@ -47,7 +47,9 @@ class TaskUtil {
* @param parameters
* @param parameters.projectBuildContext ProjectBuildContext
*/
constructor({projectBuildContext}: object) {
STANDARD_TAGS: object;

constructor({projectBuildContext}) {
this._projectBuildContext = projectBuildContext;
/**
*/
Expand Down Expand Up @@ -79,7 +81,7 @@ class TaskUtil {
* [STANDARD_TAGS]{@link @ui5/project/build/helpers/TaskUtil#STANDARD_TAGS} are allowed
* @param [value] Tag value. Must be primitive
*/
public setTag(resource, tag: string, value?: string | boolean | integer) {
public setTag(resource: ResourceInterface, tag: string, value?: string | boolean | number) {
if (typeof resource === "string") {
throw new Error("Deprecated parameter: " +
"Since UI5 Tooling 3.0, #setTag requires a resource instance. Strings are no longer accepted");
Expand All @@ -101,7 +103,7 @@ class TaskUtil {
* @returns Tag value for the given resource.
* <code>undefined</code> if no value is available
*/
public getTag(resource, tag: string) {
public getTag(resource: ResourceInterface, tag: string) {
if (typeof resource === "string") {
throw new Error("Deprecated parameter: " +
"Since UI5 Tooling 3.0, #getTag requires a resource instance. Strings are no longer accepted");
Expand All @@ -121,7 +123,7 @@ class TaskUtil {
* @param resource Resource-instance the tag should be cleared for
* @param tag Tag
*/
public clearTag(resource, tag: string) {
public clearTag(resource: ResourceInterface, tag: string) {
if (typeof resource === "string") {
throw new Error("Deprecated parameter: " +
"Since UI5 Tooling 3.0, #clearTag requires a resource instance. Strings are no longer accepted");
Expand Down
2 changes: 1 addition & 1 deletion src/build/helpers/createBuildManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function getSortedTags(project) {
* @param buildConfig
* @param taskRepository
*/
export default async function (project, buildConfig, taskRepository) {
export default async function (project, buildConfig, taskRepository: typeof import("@ui5/builder/internal/taskRepository")) {
if (!project) {
throw new Error(`Missing parameter 'project'`);
}
Expand Down
Loading

0 comments on commit 717c015

Please sign in to comment.