Skip to content

Commit e370dc7

Browse files
committed
feat: add types for cds.build
1 parent 4f90ed9 commit e370dc7

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed

apis/build.d.ts

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import { CSN } from './csn'
2+
3+
/**
4+
* WARNING: `cds.build` is only accessible during running `cds build` via the CDS SDK.
5+
* You can only use it in build contexts like registering a custom build plugin inside `cds-plugin.js`
6+
*/
7+
export namespace build {
8+
type TaskDefaults = { src?: string, dest?: string, options?: object } & Record<string, any>;
9+
type Task = { src: string, dest: string, for: string, options: object } & Record<string, any>;
10+
type Context = { options: Record<string, any>, tasks: Task[] };
11+
type FileWriter = {
12+
/**
13+
* @param {string} dest - absolute or relative file path. Relative paths will be resolved to this task's destination path.
14+
*/
15+
to: (dest: string) => Promise<void>,
16+
};
17+
type Severity = 'Info' | 'Warning' | 'Error';
18+
19+
/**
20+
* The build plugin creates the build output for a dedicated build task. It is uniquely identified
21+
* by the build task's `for` or `use` property. The `use` property represents
22+
* the fully qualified node module path of the build plugin implementation.
23+
*
24+
* The build task engine defines the following protocol. The methods are invoked in descending order:
25+
* - init() - optional
26+
* - get priority() - optional
27+
* - async clean()
28+
* - async build()
29+
*
30+
* The reflected CSN can be accessed using the async method `model()`.
31+
*/
32+
abstract class Plugin {
33+
static taskDefaults: TaskDefaults
34+
static readonly INFO: Severity
35+
static readonly WARNING: Severity
36+
static readonly ERROR: Severity
37+
38+
/**
39+
* Determines whether a task of this type will be created when cds build is executed,
40+
* @returns `true` by default
41+
*/
42+
static hasTask(): boolean;
43+
44+
/**
45+
* Returns the build task executed by this build plugin.
46+
*/
47+
get task(): Task;
48+
49+
/**
50+
* Returns a list of build and compiler messages created by this build plugin.
51+
* Supported message severities are 'Info', 'Warning', and 'Error'.
52+
*/
53+
get messages(): string[];
54+
55+
/**
56+
* Returns the list of files and folders written by this build plugin.
57+
*/
58+
get files(): string[];
59+
60+
/**
61+
* Returns the build context
62+
*/
63+
get context(): Context;
64+
65+
/**
66+
* Returns the priority of this plugin as number, where 1024 represents the default value
67+
* to ensure that custom plugins are by default executed before the built-in plugins.
68+
* The higher the priority value, the sooner the build plugin will be run.
69+
* The range of valid priority values is from -1024 to +1024.
70+
* Built-in plugins have a priority value range from 0 - 524.
71+
*/
72+
get priority(): number;
73+
74+
/**
75+
* Called by the framework immediately after this instance has been created.
76+
* The instance has already been fully initialized.
77+
*/
78+
init(): void;
79+
80+
/**
81+
* Called by the framework to create the artifacts of this build plugin.
82+
*/
83+
abstract build(): Promise<void>;
84+
85+
/**
86+
* Called by the framework immediately before 'build' to delete any output created by this build plugin.
87+
*
88+
* Note: The `BuildTaskEngine` is cleaning the common generation target folder if the build is
89+
* executed in staging mode, e.g. build.target: "gen".
90+
*/
91+
clean(): Promise<void>;
92+
93+
/**
94+
* Asynchronously write the given content to a given file path.
95+
* If the file exists the content is replaced. If the file does not exist, a new file will be created.
96+
* The file name is stored in the list of files written by this build plugin.
97+
* @param {any} data - If data is of type object the JSON-stringified version is written.
98+
* @returns {FileWriter} object to write the file asynchronously to its destination
99+
*/
100+
write(data: any): FileWriter;
101+
102+
/**
103+
* Asynchronously copies a single file or the entire directory structure from 'src' to 'dest', including subdirectories and files.
104+
*
105+
* Note: The file names are stored in the list of files written by this build plugin.
106+
*
107+
* @param {string} src The absolute or relative source path of the file or directory to copy.
108+
* Relative paths will be resolved to this task's source path.
109+
* @returns {FileWriter} object to copy the file asynchronously to its destination
110+
*/
111+
copy(src: string): FileWriter;
112+
113+
/**
114+
* Adds the given user message and severity to the list of messages issued by this build task.
115+
* <p>
116+
* User messages will be logged after CDS build has been finished based on the log-level that has been set.
117+
* By default messages with severity <em>warning</em> and <em>error</em> will be logged.
118+
* @param {string} message the message text
119+
* @param {string} severity the severity of the message, if ommitted 'Error' is used
120+
*/
121+
pushMessage(message: string, severity?: Severity): void;
122+
123+
/**
124+
* Returns a compiled CSN model according to the model paths defined by this build task.
125+
* The model includes model enhancements defined by feature toggles, if any.
126+
* @return {object} the compiled CSN model
127+
*/
128+
model(): Promise<CSN>;
129+
130+
/**
131+
* Returns a compiled base model CSN according to the model paths defined by this build task.
132+
* The base model does not include any model enhancements defined by feature toggles.
133+
* @return {CSN} the compiled base model CSN
134+
*/
135+
baseModel(): Promise<CSN>;
136+
137+
options(): { messages: string[] };
138+
139+
/**
140+
* Adds the given fully qualified file path to the list of files that are written by this build task.
141+
*/
142+
pushFile(filePath: string): void;
143+
}
144+
145+
/**
146+
* Registers plugin class with the given identifier
147+
*
148+
* @param id identifier of plugin
149+
* @param plugin Plugin implementation that inherits from `cds.build.Plugin`
150+
*/
151+
export function register(id: string, plugin: { new (): Plugin }): void;
152+
153+
/**
154+
* Executes cds build in the directory defined by cds.root.
155+
*
156+
* @param {Record<string, any>} options - command options as defined by build command.
157+
*/
158+
export function build(options: Record<string, any>): Promise<void>
159+
}

apis/facade.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './models'
55
export * from './services'
66
export * from './events'
77
export * from './utils'
8+
export * from './build'
89
export * from './cqn'
910
export * from './global'
1011
export { log, debug } from './log'
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import cds from "@sap/cds";
2+
3+
class MyPlugin extends cds.build.Plugin {
4+
static taskDefaults: cds.build.TaskDefaults = { src: "." };
5+
async build(): Promise<void> {
6+
await this.write({ content: "" }).to("my target");
7+
8+
const model = await this.model();
9+
model.definitions!["entity"].elements.ID;
10+
11+
this.pushMessage("error message");
12+
this.pushMessage("warning message", cds.build.Plugin.WARNING);
13+
this.pushMessage("info message", "Info");
14+
15+
// @ts-expect-error
16+
this.pushMessage("invalid severity", "aa");
17+
18+
this.pushFile("./path-to-file");
19+
20+
this.files[0].startsWith("");
21+
this.context.tasks.length;
22+
this.context.tasks.find(t => t.for === "nodejs");
23+
this.context.options.test === true
24+
this.messages.length
25+
26+
this.task.src;
27+
this.task.dest;
28+
}
29+
get priority(): number {
30+
return -1;
31+
}
32+
}
33+
34+
cds.build.register("custom", MyPlugin);
35+
36+
cds.build.build({})

0 commit comments

Comments
 (0)