Skip to content

Commit

Permalink
Convert SVG to PNG on Azure & Bitbucket
Browse files Browse the repository at this point in the history
  • Loading branch information
nvuillam committed Dec 31, 2024
1 parent 871c324 commit 4317deb
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@actions/github": "^6.0.0",
"@gitbeaker/node": "^35.8.1",
"@oclif/core": "^4.0.37",
"@resvg/resvg-js": "^2.6.2",
"@salesforce/core": "^8.8.0",
"@salesforce/sf-plugins-core": "^11.3.12",
"@slack/types": "^2.14.0",
Expand Down
8 changes: 8 additions & 0 deletions src/common/gitProvider/azureDevops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CommentThreadStatus, GitPullRequest, GitPullRequestCommentThread, GitPu
import { CONSTANTS } from "../../config/index.js";
import { SfError } from "@salesforce/core";
import { prompts } from "../utils/prompts.js";
import { convertSvgToPng } from "../utils/filesUtils.js";

export class AzureDevopsProvider extends GitProviderRoot {
private azureApi: InstanceType<typeof azdev.WebApi>;
Expand Down Expand Up @@ -514,6 +515,13 @@ _Powered by [sfdx-hardis](${CONSTANTS.DOC_URL_ROOT}) from job [${azureJobName}](
}

public async uploadImage(localImagePath: string): Promise<string | null> {
if (localImagePath.endsWith(".svg")) {
const pngFileRes = convertSvgToPng(localImagePath, localImagePath.replace(".svg", ".png"));
if (pngFileRes == null) {
return null;
}
localImagePath = pngFileRes;
}
try {
// Upload the image to Azure DevOps
const imageName = path.basename(localImagePath);
Expand Down
9 changes: 9 additions & 0 deletions src/common/gitProvider/bitbucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { git, uxLog } from '../utils/index.js';
import bbPkg, { Schema } from 'bitbucket';
import { CONSTANTS } from '../../config/index.js';
import * as path from 'path';
import { convertSvgToPng } from '../utils/filesUtils.js';
const { Bitbucket } = bbPkg;

export class BitbucketProvider extends GitProviderRoot {
Expand Down Expand Up @@ -279,6 +280,14 @@ export class BitbucketProvider extends GitProviderRoot {

// Upload the image to Bitbucket
public async uploadImage(localImagePath: string): Promise<string | null> {
// Bitbucket can not handle SVG format
if (localImagePath.endsWith(".svg")) {
const pngFileRes = convertSvgToPng(localImagePath, localImagePath.replace(".svg", ".png"));
if (pngFileRes == null) {
return null;
}
localImagePath = pngFileRes;
}
try {
const imageBuffer = fs.readFileSync(localImagePath);
const imageBlob = new Blob([imageBuffer]);
Expand Down
15 changes: 15 additions & 0 deletions src/common/utils/filesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import ExcelJS from 'exceljs';
// Project Specific Utilities
import { getCurrentGitBranch, isCI, isGitRepo, uxLog } from './index.js';
import { bulkQuery, soqlQuery } from './apiUtils.js';
import { Resvg } from '@resvg/resvg-js';
import { prompts } from './prompts.js';
import { CONSTANTS, getReportDirectory } from '../../config/index.js';
import { WebSocketClient } from '../websocketClient.js';
Expand Down Expand Up @@ -758,3 +759,17 @@ async function csvToXls(csvFile: string, xslxFile: string) {
}
await workbook.xlsx.writeFile(xslxFile);
}


export function convertSvgToPng(svgPath: string, pngPath: string) {
try {
const svgContent = fs.readFileSync(svgPath, 'utf8');
const resvg = new Resvg(svgContent);
const pngBuffer = resvg.render().asPng();
fs.writeFileSync(pngPath, pngBuffer);
return pngPath;
} catch (error: any) {
uxLog(this, 'Error converting SVG to PNG:' + error.message);
return null;
}
}
78 changes: 78 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2104,6 +2104,84 @@
unbzip2-stream "^1.4.3"
yargs "^17.7.2"

"@resvg/[email protected]":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@resvg/resvg-js-android-arm-eabi/-/resvg-js-android-arm-eabi-2.6.2.tgz#e761e0b688127db64879f455178c92468a9aeabe"
integrity sha512-FrJibrAk6v29eabIPgcTUMPXiEz8ssrAk7TXxsiZzww9UTQ1Z5KAbFJs+Z0Ez+VZTYgnE5IQJqBcoSiMebtPHA==

"@resvg/[email protected]":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@resvg/resvg-js-android-arm64/-/resvg-js-android-arm64-2.6.2.tgz#b8cb564d7f6b3f37d9b43129f5dc5fe171e249e4"
integrity sha512-VcOKezEhm2VqzXpcIJoITuvUS/fcjIw5NA/w3tjzWyzmvoCdd+QXIqy3FBGulWdClvp4g+IfUemigrkLThSjAQ==

"@resvg/[email protected]":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@resvg/resvg-js-darwin-arm64/-/resvg-js-darwin-arm64-2.6.2.tgz#49bd3faeda5c49f53302d970e6e79d006de18e7d"
integrity sha512-nmok2LnAd6nLUKI16aEB9ydMC6Lidiiq2m1nEBDR1LaaP7FGs4AJ90qDraxX+CWlVuRlvNjyYJTNv8qFjtL9+A==

"@resvg/[email protected]":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@resvg/resvg-js-darwin-x64/-/resvg-js-darwin-x64-2.6.2.tgz#e1344173aa27bfb4d880ab576d1acf1c1648faca"
integrity sha512-GInyZLjgWDfsVT6+SHxQVRwNzV0AuA1uqGsOAW+0th56J7Nh6bHHKXHBWzUrihxMetcFDmQMAX1tZ1fZDYSRsw==

"@resvg/[email protected]":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@resvg/resvg-js-linux-arm-gnueabihf/-/resvg-js-linux-arm-gnueabihf-2.6.2.tgz#34c445eba45efd68f6130b2ab426d76a7424253d"
integrity sha512-YIV3u/R9zJbpqTTNwTZM5/ocWetDKGsro0SWp70eGEM9eV2MerWyBRZnQIgzU3YBnSBQ1RcxRZvY/UxwESfZIw==

"@resvg/[email protected]":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@resvg/resvg-js-linux-arm64-gnu/-/resvg-js-linux-arm64-gnu-2.6.2.tgz#30da47087dd8153182198b94fe9f8d994890dae5"
integrity sha512-zc2BlJSim7YR4FZDQ8OUoJg5holYzdiYMeobb9pJuGDidGL9KZUv7SbiD4E8oZogtYY42UZEap7dqkkYuA91pg==

"@resvg/[email protected]":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@resvg/resvg-js-linux-arm64-musl/-/resvg-js-linux-arm64-musl-2.6.2.tgz#5d75b8ff5c83103729c1ca3779987302753c50d4"
integrity sha512-3h3dLPWNgSsD4lQBJPb4f+kvdOSJHa5PjTYVsWHxLUzH4IFTJUAnmuWpw4KqyQ3NA5QCyhw4TWgxk3jRkQxEKg==

"@resvg/[email protected]":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@resvg/resvg-js-linux-x64-gnu/-/resvg-js-linux-x64-gnu-2.6.2.tgz#411abedfaee5edc57cbb7701736cecba522e26f3"
integrity sha512-IVUe+ckIerA7xMZ50duAZzwf1U7khQe2E0QpUxu5MBJNao5RqC0zwV/Zm965vw6D3gGFUl7j4m+oJjubBVoftw==

"@resvg/[email protected]":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@resvg/resvg-js-linux-x64-musl/-/resvg-js-linux-x64-musl-2.6.2.tgz#fe4984038f0372f279e3ff570b72934dd7eb2a5c"
integrity sha512-UOf83vqTzoYQO9SZ0fPl2ZIFtNIz/Rr/y+7X8XRX1ZnBYsQ/tTb+cj9TE+KHOdmlTFBxhYzVkP2lRByCzqi4jQ==

"@resvg/[email protected]":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@resvg/resvg-js-win32-arm64-msvc/-/resvg-js-win32-arm64-msvc-2.6.2.tgz#d3a053cf7ff687087a2106330c0fdaae706254d1"
integrity sha512-7C/RSgCa+7vqZ7qAbItfiaAWhyRSoD4l4BQAbVDqRRsRgY+S+hgS3in0Rxr7IorKUpGE69X48q6/nOAuTJQxeQ==

"@resvg/[email protected]":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@resvg/resvg-js-win32-ia32-msvc/-/resvg-js-win32-ia32-msvc-2.6.2.tgz#7cdda1ce29ef7209e28191d917fa5bef0624a4ad"
integrity sha512-har4aPAlvjnLcil40AC77YDIk6loMawuJwFINEM7n0pZviwMkMvjb2W5ZirsNOZY4aDbo5tLx0wNMREp5Brk+w==

"@resvg/[email protected]":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@resvg/resvg-js-win32-x64-msvc/-/resvg-js-win32-x64-msvc-2.6.2.tgz#cb0ad04525d65f3def4c8d346157a57976d5b388"
integrity sha512-ZXtYhtUr5SSaBrUDq7DiyjOFJqBVL/dOBN7N/qmi/pO0IgiWW/f/ue3nbvu9joWE5aAKDoIzy/CxsY0suwGosQ==

"@resvg/resvg-js@^2.6.2":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@resvg/resvg-js/-/resvg-js-2.6.2.tgz#3e92a907d88d879256c585347c5b21a7f3bb5b46"
integrity sha512-xBaJish5OeGmniDj9cW5PRa/PtmuVU3ziqrbr5xJj901ZDN4TosrVaNZpEiLZAxdfnhAe7uQ7QFWfjPe9d9K2Q==
optionalDependencies:
"@resvg/resvg-js-android-arm-eabi" "2.6.2"
"@resvg/resvg-js-android-arm64" "2.6.2"
"@resvg/resvg-js-darwin-arm64" "2.6.2"
"@resvg/resvg-js-darwin-x64" "2.6.2"
"@resvg/resvg-js-linux-arm-gnueabihf" "2.6.2"
"@resvg/resvg-js-linux-arm64-gnu" "2.6.2"
"@resvg/resvg-js-linux-arm64-musl" "2.6.2"
"@resvg/resvg-js-linux-x64-gnu" "2.6.2"
"@resvg/resvg-js-linux-x64-musl" "2.6.2"
"@resvg/resvg-js-win32-arm64-msvc" "2.6.2"
"@resvg/resvg-js-win32-ia32-msvc" "2.6.2"
"@resvg/resvg-js-win32-x64-msvc" "2.6.2"

"@salesforce/cli-plugins-testkit@^5.3.39":
version "5.3.39"
resolved "https://registry.yarnpkg.com/@salesforce/cli-plugins-testkit/-/cli-plugins-testkit-5.3.39.tgz#97218016503fc19194509cc603752de4f2baceae"
Expand Down

0 comments on commit 4317deb

Please sign in to comment.