Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(adaptation-project): Add Fragment enhancement #1183

Merged
merged 228 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
228 commits
Select commit Hold shift + click to select a range
57055d0
initial crude implementation
tobiasqueck Aug 10, 2023
38c06e8
Empty-Commit
tobiasqueck Aug 11, 2023
aeb5974
merge from main
tobiasqueck Aug 11, 2023
155e0b8
typescript support for rta coding
tobiasqueck Aug 11, 2023
4825515
removed odd change
tobiasqueck Aug 11, 2023
e461d8f
removed hardcoded file hosting
tobiasqueck Aug 11, 2023
de1e458
enhanced example to also use a submodule
tobiasqueck Aug 14, 2023
26ad8b0
changing folder and export name
tobiasqueck Aug 14, 2023
57fa294
feat: added fragment class to test the src-rta folder
nikmace Aug 14, 2023
a5d0e22
feat: added new api endpoint to get fragments from workspace
nikmace Aug 14, 2023
0f9ef40
Merge branch 'feat/1149/enable-enhancements' into feat/1149/fragment-…
nikmace Aug 14, 2023
57d14bb
enable ui5 types
tobiasqueck Aug 14, 2023
8a53bff
refactor: put code into new folder
nikmace Aug 14, 2023
ecf0dae
Merge branch 'feat/1149/enable-enhancements'
nikmace Aug 14, 2023
2f13f73
enable ui5 transpiling
tobiasqueck Aug 14, 2023
18e1228
Merge branch 'feat/1149/enable-enhancements'
nikmace Aug 14, 2023
b187492
fix: script path issue
tobiasqueck Aug 14, 2023
f8f2f01
lock file
tobiasqueck Aug 14, 2023
ce3106d
Merge branch 'main' into feat/1149/enable-enhancements
tobiasqueck Aug 14, 2023
a75c853
config again
tobiasqueck Aug 14, 2023
ddfb3a5
Merge branch 'feat/1149/enable-enhancements' into feat/1149/fragment-…
nikmace Aug 14, 2023
c49b323
linting
tobiasqueck Aug 14, 2023
807a60c
Merge branch 'feat/1149/enable-enhancements'
nikmace Aug 14, 2023
c8d8dc8
win issue
tobiasqueck Aug 14, 2023
7e5ab76
chore: added --glob flag to rimraf dist
nikmace Aug 14, 2023
8307e3e
Merge branch 'feat/1149/enable-enhancements' into feat/1149/fragment-…
nikmace Aug 14, 2023
1aa47e3
fix tests
tobiasqueck Aug 14, 2023
e430098
added info to readme
tobiasqueck Aug 14, 2023
34ef8b1
fix: typo
tobiasqueck Aug 14, 2023
fbb2253
feat: add ADP API;
tobiasqueck Aug 14, 2023
2f3cb9f
feat: enhance typings
nikmace Aug 14, 2023
224c5a3
feat: added helper functions to utility class
nikmace Aug 14, 2023
fcfa439
feat: separated code into classes
nikmace Aug 14, 2023
d1b21ee
feat: changed initialization of new context item
nikmace Aug 14, 2023
e75b1be
linting errors
tobiasqueck Aug 14, 2023
53c9836
fix: tests
tobiasqueck Aug 14, 2023
4c7fab5
Merge branch 'feat/1149/enable-enhancements' into feat/1149/fragment-…
nikmace Aug 14, 2023
82a6003
refactor: rollback to original method
nikmace Aug 14, 2023
4d191ec
added enum example
tobiasqueck Aug 14, 2023
56245da
Linting auto fix commit
github-actions[bot] Aug 14, 2023
bc020e3
feat: added an api handler to client requests
nikmace Aug 14, 2023
ecdb79a
fixed auto-completion in VSCode
tobiasqueck Aug 14, 2023
4bf65f5
Merge branch 'feat/1149/enable-enhancements' of https://github.com/SA…
tobiasqueck Aug 14, 2023
9cdb48c
feat: change sap imports
nikmace Aug 14, 2023
66fe43b
fix
tobiasqueck Aug 14, 2023
66e0740
Merge branch 'feat/1149/enable-enhancements'
nikmace Aug 14, 2023
2a5e050
fix: tests again
tobiasqueck Aug 14, 2023
a1632f1
chore: added new package and script for copying files
nikmace Aug 15, 2023
ad2ba90
feat: added templates for fragment and controller
nikmace Aug 15, 2023
7217bd6
feat: added enums for server code
nikmace Aug 15, 2023
4d07fc3
feat: split router endpoints into separate functions
nikmace Aug 15, 2023
d417406
feat: added class for handling api requests
nikmace Aug 15, 2023
4439322
feat: added class that handles requests from client
nikmace Aug 15, 2023
540e7bb
refactor: sorted imports, cleanup
nikmace Aug 15, 2023
b15f19a
chore: added packages for sanitization and rate limiting
nikmace Aug 15, 2023
ad88ed1
fix: rate limiting for routes
nikmace Aug 15, 2023
ba1d7c8
fix: sanitize strings to prevent accessing alt sources
nikmace Aug 15, 2023
906d91a
chore: added node cache package
nikmace Aug 15, 2023
bf67f53
feat: added manifest descr interface
nikmace Aug 15, 2023
2b2f436
feat: add caching for new route
nikmace Aug 15, 2023
97a071c
feat: add caching and new route handler
nikmace Aug 15, 2023
74d5f28
feat: added new route to client, fixed json bug
nikmace Aug 15, 2023
1711baa
feat: add api request handler
nikmace Aug 15, 2023
ecbf4b7
Merge branch 'main' into feat/1149/enable-enhancements
tobiasqueck Aug 15, 2023
8287c49
removed wrong entry
tobiasqueck Aug 15, 2023
a899267
Merge branch 'feat/1149/enable-enhancements' of https://github.com/SA…
tobiasqueck Aug 15, 2023
62912d5
refactor: made templates into two folders, fixed paths
nikmace Aug 16, 2023
2d7481a
enhanced the types module with SAPUI5 types and required enhancements
tobiasqueck Aug 16, 2023
ebc4b05
moved sapui5 types and enhancements into central types module and use…
tobiasqueck Aug 16, 2023
2414eff
Merge branch 'main' into feat/1149/enable-enhancements
tobiasqueck Aug 16, 2023
28b3ebd
Linting auto fix commit
github-actions[bot] Aug 16, 2023
9b3f5bd
feat: implement search for fragments with change file
nikmace Aug 16, 2023
71f8a8a
feat: improve typings
nikmace Aug 16, 2023
da8e480
refactor: hungarian notations, add typings
nikmace Aug 16, 2023
1a71510
refactor: remove and add missing types for any
nikmace Aug 16, 2023
1991c38
fix: build issues
tobiasqueck Aug 16, 2023
52a5b2e
fix the build
tobiasqueck Aug 16, 2023
9ce59c6
Linting auto fix commit
github-actions[bot] Aug 16, 2023
4663ff8
Merge branch 'main' into feat/1149/enable-enhancements
tobiasqueck Aug 16, 2023
0151ebb
fix tell jest to ignore ui5 files
tobiasqueck Aug 16, 2023
7b14d02
fix: lint errors, jsdocs
nikmace Aug 16, 2023
251cc8b
Merge branch 'feat/1149/enable-enhancements'
nikmace Aug 16, 2023
834c9e3
fix: some major sonar issues
nikmace Aug 16, 2023
f9e4613
fix: some sonar code smells
nikmace Aug 16, 2023
718725f
type fixes
tobiasqueck Aug 16, 2023
4f70b3c
Merge branch 'feat/1149/fragment-controller-enhancement' of https://g…
tobiasqueck Aug 16, 2023
908639d
merging type fixes
tobiasqueck Aug 16, 2023
1ecfd33
Merge branch 'feat/1149/enable-enhancements' into feat/1149/fragment-…
tobiasqueck Aug 16, 2023
fbe93c4
Linting auto fix commit
github-actions[bot] Aug 16, 2023
4db5180
Linting auto fix commit
github-actions[bot] Aug 16, 2023
4401a07
fix: eslint parsing error for windows
nikmace Aug 16, 2023
6723bd4
chore: removed root and parser from eslintrc
nikmace Aug 16, 2023
14175ce
chore: :fire: removed express-rate-limit package since the server run…
nikmace Aug 16, 2023
b8b42a0
fix: some sonar major issues
nikmace Aug 16, 2023
48a9f41
refactor: change templates paths, remove script
nikmace Aug 16, 2023
8a08a83
fix: sonar issues, refactor fragment dialog code
nikmace Aug 17, 2023
a81630e
fix: some eslint/ts errors
nikmace Aug 17, 2023
f252811
fix: regex backtracking issue
nikmace Aug 17, 2023
68510fb
fix: typings for promise returning function
nikmace Aug 17, 2023
8abb0ba
refactor: change manfiest descr route logic
nikmace Aug 17, 2023
2e958a8
set browserlist to remove polyfills from generated ui5 code
tobiasqueck Aug 17, 2023
d989023
refactor: remove the class, export indiv functions
nikmace Aug 17, 2023
b36678e
merge from main
tobiasqueck Aug 17, 2023
e3c003c
merge from main;
tobiasqueck Aug 17, 2023
40bad4d
merge from main
tobiasqueck Aug 17, 2023
2d8011a
refactor: reduce cognitive complexity of a function
nikmace Aug 17, 2023
96c381c
Merge branch 'feat/1149/fragment-controller-enhancement' of https://g…
nikmace Aug 17, 2023
3496fcb
revert wrong comma
tobiasqueck Aug 17, 2023
42d1619
revert wrong comma
tobiasqueck Aug 17, 2023
549eb65
fix: some sonar code smells
nikmace Aug 18, 2023
e14197d
fix: congnitive complexity
nikmace Aug 18, 2023
b0b4393
feat: change dialog init and refactor code
nikmace Aug 19, 2023
79521f5
refactor: change folder name for all dialogs
nikmace Aug 19, 2023
8ff83bb
fix: lint errors, sonar bug
nikmace Aug 19, 2023
474c83a
refactor: change status codes to enum
nikmace Aug 19, 2023
12b3ac4
fix: sanitize user provided input
nikmace Aug 21, 2023
734c5a2
refactor: remove unused code for filtering fragments with change file
nikmace Aug 21, 2023
a6b8f06
enhanced the readme
tobiasqueck Aug 21, 2023
f18541b
Merge branch 'main' into feat/1149/enable-enhancements
tobiasqueck Aug 21, 2023
9d4a549
chore: added --glob flag to rimraf dist
nikmace Aug 14, 2023
07acb63
refactor: made templates into two folders, fixed paths
nikmace Aug 16, 2023
939c407
Merge branch 'feat/1149/enable-enhancements' into feat/1149/fragment-…
tobiasqueck Aug 21, 2023
d139095
feat: add folders for fragments and controllers
nikmace Aug 21, 2023
1fdddca
refactor: remove node-cache module
nikmace Aug 21, 2023
edc1413
feat: add requested changes
nikmace Aug 21, 2023
608ea1e
Merge branch 'feat/1149/fragment-controller-enhancement' of https://g…
nikmace Aug 21, 2023
a1802d4
fix: controller loading
tobiasqueck Aug 21, 2023
417293a
suggestion
tobiasqueck Aug 21, 2023
2963d94
added test for new functionality
tobiasqueck Aug 21, 2023
3bf5642
setup for jest testing of client sources
tobiasqueck Aug 21, 2023
bff0350
turn-off contradicting rule
tobiasqueck Aug 21, 2023
10ea593
Merge branch 'main' into feat/1149/enable-enhancements
tobiasqueck Aug 22, 2023
1f321a2
Merge branch 'main' into feat/1149/enable-enhancements
tobiasqueck Aug 22, 2023
5501382
feat: add controller and change fragment structure
nikmace Aug 22, 2023
af84909
refactor: change initializing of a dialog
nikmace Aug 22, 2023
b5c8c35
feat: add controller specific methods
nikmace Aug 22, 2023
3216dc1
feat: add base interface for all dialogs
nikmace Aug 22, 2023
fde83cf
feat: move code to controller and cleanup
nikmace Aug 22, 2023
6ab2b4a
feat: add rta border class to xml fragment
nikmace Aug 22, 2023
31e615e
Merge branch 'feat/1149/enable-enhancements' into feat/1149/fragment-…
nikmace Aug 23, 2023
b06e555
test: fixed existing tests
nikmace Aug 23, 2023
0e1fbb0
refactor: reorder imports, add missing jsdocs
nikmace Aug 24, 2023
6dd2548
refactor: doc strings
nikmace Aug 24, 2023
7695fda
refactor: change method name
nikmace Aug 24, 2023
0d34f9f
feat: start adding tests
nikmace Aug 28, 2023
f6004ae
fix: mocking of fs
tobiasqueck Aug 28, 2023
a3a9f0b
real fix
tobiasqueck Aug 28, 2023
efe63f0
fix: post test mock
nikmace Aug 28, 2023
c1efcf9
more specific rule ignore
tobiasqueck Aug 29, 2023
2ba64bb
merge from main
tobiasqueck Aug 29, 2023
e62442f
merge from main
tobiasqueck Aug 29, 2023
483a852
feat: add some tests
nikmace Aug 29, 2023
7cd2174
Merge branch 'feat/1149/fragment-controller-enhancement' of https://g…
nikmace Aug 29, 2023
2fe8e49
fix: add requested changes
nikmace Aug 29, 2023
2937d00
Merge branch 'main' into feat/1149/enable-enhancements
tobiasqueck Aug 29, 2023
4c86194
feat: add more test suites
nikmace Aug 29, 2023
5bedea8
feat: add more tests
nikmace Aug 29, 2023
a11379e
refactor: comment out broken test
nikmace Aug 29, 2023
a9a6cb8
feat: add api handler tests
nikmace Aug 29, 2023
00c9bc1
merge from main
tobiasqueck Aug 30, 2023
4de1566
test: add example test for controller
nikmace Aug 30, 2023
c92882d
review feedback: supertest
tobiasqueck Aug 31, 2023
5872397
merge from main
tobiasqueck Aug 31, 2023
f090494
merge from main
tobiasqueck Aug 31, 2023
c56ee27
forgetful me
tobiasqueck Aug 31, 2023
653b29d
merge from main
tobiasqueck Sep 1, 2023
a39183e
merge from main
tobiasqueck Sep 1, 2023
de4ed5c
mocked JSONModel
tobiasqueck Sep 1, 2023
73dcfc5
fix: mock
tobiasqueck Sep 1, 2023
970912c
test: added controller tests
nikmace Sep 4, 2023
78b80e2
merge branch
nikmace Sep 4, 2023
1ca84df
fix: mock
tobiasqueck Sep 5, 2023
d6efe16
refactor: fragments map
nikmace Sep 5, 2023
86df68d
fix: last controller test
nikmace Sep 5, 2023
199ef11
merge from main
tobiasqueck Sep 7, 2023
61150ed
test: add missing expect statements
nikmace Sep 7, 2023
5c231b7
simplify enablement
tobiasqueck Sep 7, 2023
fea46a9
further cleanup
tobiasqueck Sep 7, 2023
495cec8
fix: tests
tobiasqueck Sep 7, 2023
c1ae534
merge from main
tobiasqueck Sep 7, 2023
ba11921
forgot the lockfile
tobiasqueck Sep 7, 2023
4337743
moved unit test
tobiasqueck Sep 7, 2023
66e8e63
last fixes after merge
tobiasqueck Sep 7, 2023
7ea7062
more concise mocking
tobiasqueck Sep 8, 2023
8dd62ad
use more concise mocking
tobiasqueck Sep 8, 2023
cd22058
global sap mocking
tobiasqueck Sep 8, 2023
f134bbc
use global sap
tobiasqueck Sep 8, 2023
768a304
fix: eslint errors
nikmace Sep 8, 2023
a22e2c1
refactor: change xml fragment
nikmace Sep 8, 2023
e0ed1c3
refactor: remove some ts-ignores
nikmace Sep 8, 2023
d2a73c6
fix: ui bug in dialog
nikmace Sep 11, 2023
75b3aa1
fix: remove classes for add fragment xml file
nikmace Sep 11, 2023
04552ec
merge from main
tobiasqueck Sep 12, 2023
75819a4
fixed merge errors
tobiasqueck Sep 12, 2023
a2429a7
not needed
tobiasqueck Sep 12, 2023
67216fb
Merge branch 'main' into feat/1149/fragment-controller-enhancement
tobiasqueck Sep 12, 2023
4cfe0d9
chore: added changeset
nikmace Sep 12, 2023
42b2b06
fix: add requested changes
nikmace Sep 12, 2023
baeb1b1
refactor: remove bcp reference
nikmace Sep 12, 2023
45221ac
refactor: remove all doc references in code
nikmace Sep 12, 2023
75aef42
simplified tests
tobiasqueck Sep 13, 2023
19e12a1
merge from main
tobiasqueck Sep 14, 2023
b7975d6
refactor: remove unused code
nikmace Sep 15, 2023
00d40b2
fix: tests after refactor
nikmace Sep 15, 2023
b005b56
refactor: remove unnecessary assertion
nikmace Sep 15, 2023
e0a55f9
feat: change fragment instantiation
nikmace Sep 15, 2023
4a2829c
refactor: remove file that does not belong to this PR
nikmace Sep 18, 2023
adf5c96
refactor: change initialization of controller and view
nikmace Sep 18, 2023
60916e4
fix: existing tests
nikmace Sep 18, 2023
f13674d
refactor: switch statement to nested ifs
nikmace Sep 18, 2023
b823e65
refactor: move name check block inside else statement
nikmace Sep 18, 2023
eb941f5
replace any
tobiasqueck Sep 18, 2023
58cb812
refactor: remove code that is not used anywhere
nikmace Sep 18, 2023
6d75924
refactor: change typing
nikmace Sep 18, 2023
07333f4
refactor: change typings for get source method
nikmace Sep 18, 2023
3fa3077
removing any
tobiasqueck Sep 18, 2023
4fbcef9
Merge branch 'main' into feat/1149/fragment-controller-enhancement
tobiasqueck Sep 18, 2023
154422d
typos
tobiasqueck Sep 18, 2023
c71dac8
fix: linting error after type fix
tobiasqueck Sep 18, 2023
d2a918d
fix: sonar code smells
nikmace Sep 18, 2023
1e3e4f2
test: add missing tests
nikmace Sep 19, 2023
87f51b8
chore: fix changeset message typo
nikmace Sep 19, 2023
06fa460
test: improve tests
nikmace Sep 19, 2023
c6c44d0
refactor: remove unused code, fix validation bug
nikmace Sep 19, 2023
ab49348
test: add missing suite
nikmace Sep 19, 2023
827b499
Merge branch 'main' into feat/1149/fragment-controller-enhancement
nikmace Sep 20, 2023
f129653
Merge branch 'main' into feat/1149/fragment-controller-enhancement
tobiasqueck Sep 20, 2023
bb106ca
Merge branch 'main' into feat/1149/fragment-controller-enhancement
nikmace Sep 20, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/loud-insects-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@sap-ux-private/preview-middleware-client': minor
'@sap-ux/preview-middleware': minor
'@sap-ux/adp-tooling': minor
---

Enhancing the preview-middleware with new functionality such as adding an XML Fragment (creating "addXML" change).
2 changes: 1 addition & 1 deletion packages/adp-tooling/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test/fixtures
dist
dist
15 changes: 10 additions & 5 deletions packages/adp-tooling/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# `@sap-ux/adp-tooling`

Module containing different tooling modules helpful when working with SAP UI5 adaptation projects.
A module containing different tooling modules helpful when working with SAP UI5 adaptation projects.

## preview
## Submodules

### preview
The submodule preview contains functionality allowing to preview adaptation projects. It is not a standalone UI5 middleware but designed to be integrated into the `@sap-ux/preview-middleware.`.

## writer
### writer
The submodule writer contains functionality to generate the core project structure of an SAP UI5 adaptation project. It is not a standalone generator but designed to be integrated into `@sap-ux/create` or any kind of yeoman generator.

## base
The submodule contains functionality required in different scenarios, e.g. prompting for generation or when initializing the preview.
### base
The submodule contains functionality required in different scenarios, e.g. prompting for generation or when initializing the preview.

## Templates
The templates folder contains ejs templates to be used for the generation of new adaptation projects as well as to generate artifacts in existing adaptation projects.
2 changes: 1 addition & 1 deletion packages/adp-tooling/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const config = require('../../jest.base');
module.exports = config;
module.exports = config;
1 change: 1 addition & 0 deletions packages/adp-tooling/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@sap-ux/logger": "workspace:*",
"@sap-ux/system-access": "workspace:*",
"@sap-ux/ui5-config": "workspace:*",
"sanitize-filename": "1.6.3",
"ejs": "3.1.9",
"mem-fs": "2.1.0",
"mem-fs-editor": "9.4.0",
Expand Down
41 changes: 36 additions & 5 deletions packages/adp-tooling/src/preview/adp-preview.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import type { ToolsLogger } from '@sap-ux/logger';
import express from 'express';
import { ZipFile } from 'yazl';
import type { AdpPreviewConfig, DescriptorVariant } from '../types';
import type { NextFunction, Request, Response } from 'express';
import type { MergedAppDescriptor } from '@sap-ux/axios-extension';
import type { ReaderCollection } from '@ui5/fs';
import type { MiddlewareUtils } from '@ui5/server';
import type { NextFunction, Request, Response, Router, RequestHandler } from 'express';

import type { ToolsLogger } from '@sap-ux/logger';
import type { UI5FlexLayer } from '@sap-ux/project-access';
import { createAbapServiceProvider } from '@sap-ux/system-access';
import type { MergedAppDescriptor } from '@sap-ux/axios-extension';

import RoutesHandler from './routes-handler';
import type { AdpPreviewConfig, DescriptorVariant } from '../types';

export const enum ApiRoutes {
FRAGMENT = '/adp/api/fragment',
CONTROLLER = '/adp/api/controller'
}

/**
* Create a buffer based on the given zip file object.
Expand Down Expand Up @@ -36,6 +46,10 @@ export class AdpPreview {
* Merged descriptor variant with reference app manifest
*/
private mergedDescriptor: MergedAppDescriptor;
/**
* Routes handler class to handle API requests
*/
private routesHandler: RoutesHandler;

/**
* @returns merged manifest.
Expand Down Expand Up @@ -72,13 +86,17 @@ export class AdpPreview {
*
* @param config adp config
* @param project reference to the root of the project
* @param util middleware utilities provided by the UI5 CLI
* @param logger logger instance
*/
constructor(
private readonly config: AdpPreviewConfig,
private readonly project: ReaderCollection,
private readonly util: MiddlewareUtils,
private readonly logger: ToolsLogger
) {}
) {
this.routesHandler = new RoutesHandler(project, util, logger);
}

/**
* Fetch all required configurations from the backend and initialize all configurations.
Expand Down Expand Up @@ -131,4 +149,17 @@ export class AdpPreview {
}
}
}

/**
* Add additional APIs to the router that are required for adaptation projects only.
*
* @param router router that is to be enhanced with the API
*/
addApis(router: Router): void {
/**
* FRAGMENT Routes
*/
router.get(ApiRoutes.FRAGMENT, this.routesHandler.handleReadAllFragments as RequestHandler);
router.post(ApiRoutes.FRAGMENT, express.json(), this.routesHandler.handleWriteFragment as RequestHandler);
}
}
115 changes: 115 additions & 0 deletions packages/adp-tooling/src/preview/routes-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import * as fs from 'fs';
import * as path from 'path';
import sanitize from 'sanitize-filename';
import type { ReaderCollection } from '@ui5/fs';
import type { ToolsLogger } from '@sap-ux/logger';
import type { MiddlewareUtils } from '@ui5/server';
import type { NextFunction, Request, Response } from 'express';

import { FolderNames, TemplateFileName, HttpStatusCodes } from '../types';

interface WriteFragmentBody {
fragmentName: string;
}

/**
* @description Handles API Routes
*/
export default class RoutesHandler {
/**
* Constructor taking project as input.
*
* @param project Reference to the root of the project
* @param util middleware utilities provided by the UI5 CLI
* @param logger Logger instance
*/
constructor(
private readonly project: ReaderCollection,
private readonly util: MiddlewareUtils,
private readonly logger: ToolsLogger
) {}

/**
* Handler for reading all fragment files from the workspace.
*
* @param _ Request
* @param res Response
* @param next Next Function
*/
public handleReadAllFragments = async (_: Request, res: Response, next: NextFunction) => {
try {
const files = await this.project.byGlob('/**/changes/**/*.fragment.xml');

if (!files || files.length === 0) {
res.status(HttpStatusCodes.OK)
.contentType('application/json')
.send({ fragments: [], message: `No fragments found in the project workspace.` });
return;
}

const fragments = files.map((f) => ({
fragmentName: f.getName()
}));

res.status(HttpStatusCodes.OK)
.contentType('application/json')
.send({
fragments,
message: `${fragments.length} fragments found in the project workspace.`
});
this.logger.debug(`Read fragments ${JSON.stringify(fragments)}`);
} catch (e) {
this.logger.error(e.message);
res.status(HttpStatusCodes.INTERNAL_SERVER_ERROR).send({ message: e.message });
next(e);
}
};

/**
* Handler for writing a fragment file to the workspace.
*
* @param req Request
* @param res Response
* @param next Next Function
*/
public handleWriteFragment = async (req: Request, res: Response, next: NextFunction) => {
try {
const data = req.body as WriteFragmentBody;

const fragmentName = sanitize(data.fragmentName);

const sourcePath = this.util.getProject().getSourcePath();

if (fragmentName) {
const fullPath = path.join(sourcePath, FolderNames.Changes, FolderNames.Fragments);
const filePath = path.join(fullPath, `${fragmentName}.fragment.xml`);

if (!fs.existsSync(fullPath)) {
fs.mkdirSync(fullPath);
}

if (fs.existsSync(filePath)) {
res.status(HttpStatusCodes.CONFLICT).send(`Fragment with name "${fragmentName}" already exists`);
this.logger.debug(`XML Fragment with name "${fragmentName}" was created`);
return;
}

// Copy the template XML Fragment to the project's workspace
const fragmentTemplatePath = path.join(__dirname, '../../templates/rta', TemplateFileName.Fragment);
fs.copyFileSync(fragmentTemplatePath, filePath);

const message = 'XML Fragment created';
res.status(HttpStatusCodes.CREATED).send(message);
this.logger.debug(`XML Fragment with name "${fragmentName}" was created`);
} else {
res.status(HttpStatusCodes.BAD_REQUEST).send('Fragment name was not provided!');
this.logger.debug('Bad request. Fragment name was not provided!');
}
} catch (e) {
const sanitizedMsg = sanitize(e.message);
this.logger.error(sanitizedMsg);
res.status(HttpStatusCodes.INTERNAL_SERVER_ERROR).send(sanitizedMsg);
next(e);
}
};
}
41 changes: 41 additions & 0 deletions packages/adp-tooling/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,44 @@ export interface AdpWriterConfig {
description?: string;
};
}

export interface ManifestAppdescr {
fileName: string;
layer: string;
fileType: string;
reference: string;
id: string;
namespace: string;
version: string;
content: Content[];
}

export interface Content {
changeType: string;
content: object;
texts?: object;
}

export const enum FolderNames {
Changes = 'changes',
Fragments = 'fragments'
}

export const enum TemplateFileName {
Fragment = 'fragment.xml'
}

export const enum HttpStatusCodes {
OK = 200,
CREATED = 201,
NO_CONTENT = 204,
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
FORBIDDEN = 403,
NOT_FOUND = 404,
METHOD_NOT_ALLOWED = 405,
CONFLICT = 409,
INTERNAL_SERVER_ERROR = 500,
NOT_IMPLEMETED = 501,
SERVICE_UNAVAILABLE = 503
}
2 changes: 1 addition & 1 deletion packages/adp-tooling/src/writer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function generate(basePath: string, config: AdpWriterConfig, fs?: E
if (!fs) {
fs = create(createStorage());
}
const tmplPath = join(__dirname, '../../templates');
const tmplPath = join(__dirname, '../../templates/project');
const fullConfig = setDefaults(config);

fs.copyTpl(join(tmplPath, '**/*.*'), join(basePath), fullConfig, undefined, {
Expand Down
4 changes: 4 additions & 0 deletions packages/adp-tooling/templates/rta/fragment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- Use stable and unique IDs!-->
<core:FragmentDefinition xmlns:core='sap.ui.core' xmlns='sap.m'>
<!-- add your xml here -->
</core:FragmentDefinition>
Loading