Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@eclipse-che/devfile-converter

Package Overview
Dependencies
Maintainers
5
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@eclipse-che/devfile-converter - npm Package Compare versions

Comparing version 0.0.1-ba9d381 to 0.0.1-ca41630

3

lib/converter/devfile-converter.d.ts

@@ -28,2 +28,5 @@ import { V220DevfileCommands, V220DevfileComponents, V220DevfileComponentsItemsContainerEndpoints, V220DevfileComponentsItemsContainerEnv, V220DevfileComponentsItemsContainerVolumeMounts, V220DevfileProjects, V220DevfileProjectsItemsGit } from '@devfile/api';

devfileV1toDevfileV2(devfileV1: che.workspace.devfile.Devfile): Promise<Devfile>;
fixDuplicatedEndpoints(devfileV2: Devfile): Promise<void>;
fixInvalidVolumeName(devfileV2: Devfile): Promise<void>;
processVolumesFromDevfileV2(devfileV2: Devfile): Promise<void>;
processPluginsAndEditorsFromDevfileV2(devfileV2: Devfile, devfileV1: che.workspace.devfile.Devfile, externalContentAccess?: (filename: string) => Promise<string>): Promise<void>;

@@ -30,0 +33,0 @@ findFirstProjectPath(devfileV1: che.workspace.devfile.Devfile): string | undefined;

@@ -83,2 +83,4 @@ "use strict";

volume.name = volumeV1.name;
// volume names should not use _
volume.name = volume.name.replace(/_/g, '-');
}

@@ -310,3 +312,14 @@ if (volumeV1.containerPath) {

if (commandV1.name) {
devfileV2Command.id = commandV1.name;
// the id can't have spaces
// replace space by dash and then remove all special characters
devfileV2Command.id = commandV1.name
.replace(/\s+/g, '-')
.replace(/[^a-zA-Z-]/g, '')
.toLowerCase();
// needs to be max 63 characters
if (devfileV2Command.id.length > 63) {
devfileV2Command.id = devfileV2Command.id.substring(0, 63);
}
// trim '-' character from start or end
devfileV2Command.id = devfileV2Command.id.replace(/^\-+|\-+$/g, '');
}

@@ -316,2 +329,6 @@ if (commandV1.actions && commandV1.actions[0].type === 'exec') {

var action = commandV1.actions[0];
// label is the same as name
if (commandV1.name) {
devfileV2Command.exec.label = commandV1.name;
}
if (action.command) {

@@ -337,2 +354,5 @@ devfileV2Command.exec.commandLine = action.command;

if (commandV2.exec) {
if (commandV2.exec.label) {
devfileV1Command.name = commandV2.exec.label;
}
var devfileAction = {};

@@ -355,5 +375,12 @@ if (commandV2.exec.commandLine) {

DevfileConverter.prototype.projectV1toProjectV2 = function (projectV1) {
// the name can't have spaces
// replace space by dash and then remove all special characters
var projectName = projectV1.name
.replace(/\s+/g, '-')
// trim '-' character from start or end
.replace(/^\-+|\-+$/g, '')
.toLowerCase();
var devfileV2Project = {
attributes: {},
name: projectV1.name
name: projectName
};

@@ -438,2 +465,7 @@ if (projectV1.clonePath) {

devfileMetadataV2.attributes['metadata-name-field'] = 'generateName';
devfileMetadataV2.attributes['metadata-name-original-value'] = metadataV1.generateName;
// remove the trailing - to make it compliant with kubernetes name
if (devfileMetadataV2.name.endsWith('-')) {
devfileMetadataV2.name = devfileMetadataV2.name.slice(0, -1);
}
}

@@ -456,4 +488,5 @@ if (metadataV1.name) {

var nameField = metaDataAttributes['metadata-name-field'];
if (nameField === 'generateName') {
devfileMetadataV1.generateName = metadataV2.name;
var originalValue = metaDataAttributes['metadata-name-original-value'];
if (nameField === 'generateName' && originalValue) {
devfileMetadataV1.generateName = originalValue;
}

@@ -468,2 +501,3 @@ else if (nameField === 'name') {

delete metadataV2.attributes['metadata-name-field'];
delete metadataV2.attributes['metadata-name-original-value'];
}

@@ -599,39 +633,153 @@ }

return __generator(this, function (_b) {
devfileV2 = {
schemaVersion: '2.0.0',
metadata: this.metadataV1toMetadataV2(devfileV1.metadata),
projects: (devfileV1.projects || []).map(function (project) { return _this.projectV1toProjectV2(project); }),
components: (devfileV1.components || [])
.map(function (component) { return _this.componentV1toComponentV2(component); })
.filter(function (c) { return c; }),
commands: (devfileV1.commands || []).map(function (command) { return _this.commandV1toCommandV2(command); }).filter(function (c) { return c; })
};
devfileV2.attributes = {};
inlineCheTheiaPluginsYaml = this.inlineCheTheiaPluginsYamlFromComponentsV1(devfileV1.components);
if (inlineCheTheiaPluginsYaml) {
devfileV2.attributes[this.CHE_THEIA_PLUGINS_YAML] = inlineCheTheiaPluginsYaml;
switch (_b.label) {
case 0:
devfileV2 = {
schemaVersion: '2.1.0',
metadata: this.metadataV1toMetadataV2(devfileV1.metadata),
projects: (devfileV1.projects || []).map(function (project) { return _this.projectV1toProjectV2(project); }),
components: (devfileV1.components || [])
.map(function (component) { return _this.componentV1toComponentV2(component); })
.filter(function (c) { return c; }),
commands: (devfileV1.commands || []).map(function (command) { return _this.commandV1toCommandV2(command); }).filter(function (c) { return c; })
};
devfileV2.attributes = {};
inlineCheTheiaPluginsYaml = this.inlineCheTheiaPluginsYamlFromComponentsV1(devfileV1.components);
if (inlineCheTheiaPluginsYaml) {
devfileV2.attributes[this.CHE_THEIA_PLUGINS_YAML] = inlineCheTheiaPluginsYaml;
}
inlineVsCodeExtensionJson = this.inlineVsCodeExtensionFromComponentsV1(devfileV1.components);
if (inlineVsCodeExtensionJson) {
devfileV2.attributes[this.VSCODE_EXTENSIONS_JSON] = inlineVsCodeExtensionJson;
}
inlineCheEditorYaml = this.inlineCheEditorYamlFromComponentsV1(devfileV1.components);
if (inlineCheEditorYaml) {
devfileV2.attributes[this.CHE_EDITOR_YAML] = inlineCheEditorYaml;
}
if (devfileV1.attributes) {
Object.assign(devfileV2.metadata.attributes, {});
Object.keys(devfileV1.attributes).forEach(function (attributeName) {
devfileV2.metadata.attributes[attributeName] = devfileV1.attributes[attributeName];
});
}
launchCommand = (_a = devfileV1.commands) === null || _a === void 0 ? void 0 : _a.find(function (command) { return command.actions[0].type === 'vscode-launch'; });
if (launchCommand) {
devfileV2.attributes[this.VSCODE_LAUNCH_JSON] = launchCommand.actions[0].referenceContent;
}
// fix duplicated endpoints (same properties (name, port, etc..)
return [4 /*yield*/, this.fixDuplicatedEndpoints(devfileV2)];
case 1:
// fix duplicated endpoints (same properties (name, port, etc..)
_b.sent();
// process volumes
return [4 /*yield*/, this.fixInvalidVolumeName(devfileV2)];
case 2:
// process volumes
_b.sent();
return [4 /*yield*/, this.processVolumesFromDevfileV2(devfileV2)];
case 3:
_b.sent();
content = JSON.stringify(devfileV2);
// update devfile v1 constants
content = content.replace(/\$\(CHE_PROJECTS_ROOT\)/g, '$(PROJECTS_ROOT)');
content = content.replace(/\$\{CHE_PROJECTS_ROOT\}/g, '${PROJECTS_ROOT}');
return [2 /*return*/, JSON.parse(content)];
}
inlineVsCodeExtensionJson = this.inlineVsCodeExtensionFromComponentsV1(devfileV1.components);
if (inlineVsCodeExtensionJson) {
devfileV2.attributes[this.VSCODE_EXTENSIONS_JSON] = inlineVsCodeExtensionJson;
}
inlineCheEditorYaml = this.inlineCheEditorYamlFromComponentsV1(devfileV1.components);
if (inlineCheEditorYaml) {
devfileV2.attributes[this.CHE_EDITOR_YAML] = inlineCheEditorYaml;
}
if (devfileV1.attributes) {
Object.assign(devfileV2.metadata.attributes, {});
Object.keys(devfileV1.attributes).forEach(function (attributeName) {
devfileV2.metadata.attributes[attributeName] = devfileV1.attributes[attributeName];
});
});
};
// if some endpoints have excactly the same value, remove duplicates
DevfileConverter.prototype.fixDuplicatedEndpoints = function (devfileV2) {
return __awaiter(this, void 0, void 0, function () {
var endpoints, uniqueEndpoints, uniqueEndpointsJson, alreadyProcessedEndpoints;
return __generator(this, function (_a) {
endpoints = devfileV2.components
.filter(function (component) { return component.container; })
.map(function (component) { return component.container.endpoints; })
.flat();
uniqueEndpoints = endpoints.filter(function (value, index) {
var _value = JSON.stringify(value);
return (index ===
endpoints.findIndex(function (obj) {
return JSON.stringify(obj) === _value;
}));
});
uniqueEndpointsJson = uniqueEndpoints.map(function (endpoint) { return JSON.stringify(endpoint); });
alreadyProcessedEndpoints = [];
// ok now we'll iterate on endpoints and check if we need to replace endpoint if it's already been added
devfileV2.components
.filter(function (component) { return component.container; })
.forEach(function (component) {
var endpoints = component.container.endpoints || [];
var i = endpoints.length;
while (i--) {
var jsonEndpoint = JSON.stringify(endpoints[i]);
if (uniqueEndpointsJson.includes(jsonEndpoint)) {
// first time we see, we keep
if (!alreadyProcessedEndpoints.includes(jsonEndpoint)) {
alreadyProcessedEndpoints.push(jsonEndpoint);
}
else {
// need to remove this endpoint
endpoints.splice(i, 1);
}
}
}
});
return [2 /*return*/];
});
});
};
// if some volume component are also components, update the name of the volume component
// to be componentName-volume
DevfileConverter.prototype.fixInvalidVolumeName = function (devfileV2) {
return __awaiter(this, void 0, void 0, function () {
var mountedVolumes, mountedVolumeNames, allComponentExceptVolumeNames, invalidVolumeNames;
return __generator(this, function (_a) {
mountedVolumes = devfileV2.components
.map(function (component) { return component.container; })
.filter(function (container) { return container; })
.map(function (container) { return container.volumeMounts; })
.reduce(function (acc, volumeMounts) { return acc.concat(volumeMounts); }, [])
.filter(function (volume) { return volume; });
mountedVolumeNames = mountedVolumes.map(function (volume) { return volume.name; });
allComponentExceptVolumeNames = devfileV2.components
.filter(function (component) { return !component.volume; })
.map(function (component) { return component.name; });
invalidVolumeNames = mountedVolumeNames.filter(function (componentName) {
return allComponentExceptVolumeNames.includes(componentName);
});
// we have duplicates, need to update the volume name
mountedVolumes.forEach(function (volume) {
if (invalidVolumeNames.includes(volume.name)) {
volume.name = "".concat(volume.name, "-volume");
}
});
return [2 /*return*/];
});
});
};
// add missing volumes components when a volumeMount is defined in the devfile
DevfileConverter.prototype.processVolumesFromDevfileV2 = function (devfileV2) {
return __awaiter(this, void 0, void 0, function () {
var mountedVolumes, mountedVolumeNames, allComponentVolumeNames, missingVolumes;
return __generator(this, function (_a) {
mountedVolumes = devfileV2.components
.map(function (component) { return component.container; })
.filter(function (container) { return container; })
.map(function (container) { return container.volumeMounts; })
.reduce(function (acc, volumeMounts) { return acc.concat(volumeMounts); }, [])
.filter(function (volume) { return volume; });
mountedVolumeNames = mountedVolumes.map(function (volume) { return volume.name; });
allComponentVolumeNames = devfileV2.components
.filter(function (component) { return component.volume; })
.map(function (component) { return component.name; });
missingVolumes = Array.from(new Set(mountedVolumeNames.filter(function (volumeName) { return !allComponentVolumeNames.includes(volumeName); })));
// add missing volumes
missingVolumes.forEach(function (volumeName) {
devfileV2.components.push({
name: volumeName,
volume: {}
});
}
launchCommand = (_a = devfileV1.commands) === null || _a === void 0 ? void 0 : _a.find(function (command) { return command.actions[0].type === 'vscode-launch'; });
if (launchCommand) {
devfileV2.attributes[this.VSCODE_LAUNCH_JSON] = launchCommand.actions[0].referenceContent;
}
content = JSON.stringify(devfileV2);
// update devfile v1 constants
content = content.replace(/\$\(CHE_PROJECTS_ROOT\)/g, '$(PROJECTS_ROOT)');
content = content.replace(/\$\{CHE_PROJECTS_ROOT\}/g, '${PROJECTS_ROOT}');
return [2 /*return*/, JSON.parse(content)];
});
return [2 /*return*/];
});

@@ -699,3 +847,3 @@ });

devfileV1.components.push({
id: "".concat(recommendation.replaceAll('.', '/'), "/latest"),
id: "".concat(recommendation.replace(/\./g, '/'), "/latest"),
type: 'chePlugin'

@@ -702,0 +850,0 @@ });

{
"name": "@eclipse-che/devfile-converter",
"version": "0.0.1-ba9d381",
"version": "0.0.1-ca41630",
"description": "Convert devfile v1 to v2 or v2 to v1",

@@ -73,2 +73,10 @@ "publishConfig": {

],
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
},
"coverageDirectory": "coverage",

@@ -75,0 +83,0 @@ "modulePathIgnorePatterns": [

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc