Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions cds-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ cds.build?.register?.('process-validation', ProcessValidationPlugin);
// Register import handler for: cds import --from process
// @ts-expect-error: import does not exist on cds type
cds.import ??= {};
//@ts-expect-error: cds type does not exist
cds.import.options ??= {};
//@ts-expect-error: cds type does not exist
cds.import.options.process = { no_copy: true, as: 'cds', config: 'kind=rest' };
// until above is not released for cds-dk, use:
// cds-tsx import --from process ./workflows/eu12.bpm-horizon-walkme.sdshipmentprocessor.shipmentHandler.json --force --config kind=rest --no-copy --as cds
// @ts-expect-error: process does not exist on cds.import type
cds.import.from ??= {};
// @ts-expect-error: from does not exist on cds.import type
Expand Down
49 changes: 0 additions & 49 deletions lib/processImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ async function fetchAndSaveProcessDefinition(processName: string): Promise<Fetch
await fs.promises.mkdir(path.dirname(outputPath), { recursive: true });
await fs.promises.writeFile(outputPath, JSON.stringify(processHeader, null, 2), 'utf8');

const serviceName = `${projectId}.${capitalize(processHeader.identifier)}Service`;
await addServiceToPackageJson(serviceName, `srv/external/${processName}`);

return { filePath: outputPath, processHeader };
}

Expand Down Expand Up @@ -111,33 +108,9 @@ async function generateCsnModel(jsonFilePath: string): Promise<csn.CsnModel> {
const processHeader = loadProcessHeader(jsonFilePath);
const csnModel = buildCsnModel(processHeader);

// Register service in package.json for local imports too
const serviceName = `${processHeader.projectId}.${capitalize(processHeader.identifier)}Service`;
const modelPath = getModelPathFromFilePath(jsonFilePath);
await addServiceToPackageJson(serviceName, modelPath);

return csnModel;
}

/**
* Convert absolute/relative file path to model path for package.json
* e.g., "./srv/external/foo.json" -> "srv/external/foo"
* "/abs/path/srv/external/foo.json" -> "srv/external/foo"
*/
function getModelPathFromFilePath(filePath: string): string {
// Resolve to absolute, then make relative to cds.root
const absolutePath = path.resolve(filePath);
let relativePath = path.relative(cds.root, absolutePath);

// Remove .json extension
if (relativePath.endsWith('.json')) {
relativePath = relativePath.slice(0, -5);
}

// Normalize path separators
return relativePath.replace(/\\/g, '/');
}

function loadProcessHeader(filePath: string): ProcessHeader {
const content = fs.readFileSync(path.resolve(filePath), 'utf-8');
const header = JSON.parse(content) as ProcessHeader;
Expand Down Expand Up @@ -531,28 +504,6 @@ function ensureObjectSchema(schema?: JsonSchema): JsonSchema {
return { type: 'object', properties: {}, required: [] };
}

// ============================================================================
// PACKAGE.JSON UPDATE
// ============================================================================

async function addServiceToPackageJson(serviceName: string, modelPath: string): Promise<void> {
const packagePath = path.join(cds.root, 'package.json');

try {
const content = await fs.promises.readFile(packagePath, 'utf8');
const pkg = JSON.parse(content);

pkg.cds ??= {};
pkg.cds.requires ??= {};
pkg.cds.requires[serviceName] = { kind: 'external', model: modelPath };

await fs.promises.writeFile(packagePath, JSON.stringify(pkg, null, 2) + '\n', 'utf8');
LOG.debug(`Added ${serviceName} to package.json`);
} catch (error) {
LOG.warn(`Could not update package.json: ${error}`);
}
}

// ============================================================================
// UTILITIES
// ============================================================================
Expand Down
4 changes: 2 additions & 2 deletions tests/bookshop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
}
},
"eu12.bpm-horizon-walkme.sdshipmentprocessor.ShipmentHandlerService": {
"kind": "external",
"kind": "rest",
"model": "srv/external/eu12.bpm-horizon-walkme.sdshipmentprocessor.shipmentHandler"
}
}
},
"imports": {
"#cds-models/*": "./@cds-models/*/index.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* checksum : 599270b792f9fe49fe94c62583c09a3e */
/* checksum : 80b8cb79ed30de92aa919cc984a784f6 */
namespace eu12.![bpm-horizon-walkme].sdshipmentprocessor;

/** DO NOT EDIT. THIS IS A GENERATED SERVICE THAT WILL BE OVERRIDDEN ON NEXT IMPORT. */
Expand Down Expand Up @@ -42,13 +42,20 @@ service ShipmentHandlerService {
shipmentProcessResultOutput : ShipmentProcessResult;
};

type ProcessAttributes { };
type ProcessAttribute {
id : String not null;
label : String not null;
value : String;
type : String not null;
};

type ProcessAttributes : many ProcessAttribute;

type ProcessInstance {
definitionId : String;
definitionVersion : String;
id : String;
status: String;
status : String;
startedAt : String;
startedBy : String;
};
Expand Down
Loading