Skip to content

Commit 31691a6

Browse files
authored
refactor: return file as object for consumers and tests (#388)
* refactor: return file as object for consumers and tests * chore: dedupe pjson
1 parent ff352ed commit 31691a6

File tree

4 files changed

+34
-118
lines changed

4 files changed

+34
-118
lines changed

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343
"/oclif.manifest.json"
4444
],
4545
"dependencies": {
46-
"@salesforce/core": "^3.36.0",
47-
"@salesforce/kit": "^1.9.2",
48-
"@salesforce/source-deploy-retrieve": "^8.4.0",
4946
"@salesforce/core": "^3.36.1",
5047
"@salesforce/kit": "^1.9.2",
5148
"@salesforce/source-deploy-retrieve": "^8.4.0",
@@ -60,6 +57,7 @@
6057
"@salesforce/dev-scripts": "^4.3.0",
6158
"@salesforce/prettier-config": "^0.0.2",
6259
"@salesforce/ts-sinon": "^1.4.6",
60+
"@types/graceful-fs": "^4.1.6",
6361
"@types/shelljs": "^0.8.11",
6462
"@typescript-eslint/eslint-plugin": "^5.59.0",
6563
"@typescript-eslint/parser": "^5.59.0",

src/shared/functions.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,28 @@ export const chunkArray = <T>(arr: T[], size: number): T[][] =>
4949
export const ensureRelative = (filePath: string, projectPath: string): string =>
5050
isAbsolute(filePath) ? relative(projectPath, filePath) : filePath;
5151

52+
export type ParsedCustomLabels = {
53+
CustomLabels: { labels: Array<{ fullName: string }> };
54+
};
55+
5256
/**
5357
* A method to help delete custom labels from a file, or the entire file if there are no more labels
5458
*
5559
* @param filename - a path to a custom labels file
5660
* @param customLabels - an array of SourceComponents representing the custom labels to delete
61+
* @returns -json equivalent of the custom labels file's contents OR undefined if the file was deleted/not written
5762
*/
58-
export const deleteCustomLabels = (filename: string, customLabels: SourceComponent[]): Promise<void> => {
63+
export const deleteCustomLabels = async (
64+
filename: string,
65+
customLabels: SourceComponent[]
66+
): Promise<ParsedCustomLabels | undefined> => {
5967
const customLabelsToDelete = customLabels
6068
.filter((label) => label.type.id === 'customlabel')
6169
.map((change) => change.fullName);
6270

6371
// if we don't have custom labels, we don't need to do anything
6472
if (!customLabelsToDelete.length) {
65-
return Promise.resolve();
73+
return undefined;
6674
}
6775
// for custom labels, we need to remove the individual label from the xml file
6876
// so we'll parse the xml
@@ -71,9 +79,7 @@ export const deleteCustomLabels = (filename: string, customLabels: SourceCompone
7179
ignoreAttributes: false,
7280
attributeNamePrefix: '@_',
7381
});
74-
const cls = parser.parse(fs.readFileSync(filename, 'utf8')) as {
75-
CustomLabels: { labels: Array<{ fullName: string }> };
76-
};
82+
const cls = parser.parse(fs.readFileSync(filename, 'utf8')) as ParsedCustomLabels;
7783

7884
// delete the labels from the json based on their fullName's
7985
cls.CustomLabels.labels = ensureArray(cls.CustomLabels.labels).filter(
@@ -82,7 +88,8 @@ export const deleteCustomLabels = (filename: string, customLabels: SourceCompone
8288

8389
if (cls.CustomLabels.labels.length === 0) {
8490
// we've deleted everything, so let's delete the file
85-
return fs.promises.unlink(filename);
91+
await fs.promises.unlink(filename);
92+
return undefined;
8693
} else {
8794
// we need to write the file json back to xml back to the fs
8895
const builder = new XMLBuilder({
@@ -93,6 +100,7 @@ export const deleteCustomLabels = (filename: string, customLabels: SourceCompone
93100
});
94101
// and then write that json back to xml and back to the fs
95102
const xml = builder.build(cls) as string;
96-
return fs.promises.writeFile(filename, xml);
103+
await fs.promises.writeFile(filename, xml);
104+
return cls;
97105
}
98106
};

test/unit/deleteCustomLabels.test.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,20 @@ describe('deleteCustomLabels', () => {
5454
});
5555

5656
describe('deleteCustomLabels', () => {
57-
it('will delete a singular custom label from a file', () => {
57+
it('will delete a singular custom label from a file', async () => {
5858
const labels = [
5959
{
6060
type: { id: 'customlabel', name: 'CustomLabel' },
6161
fullName: 'DeleteMe',
6262
} as SourceComponent,
6363
];
6464

65-
void deleteCustomLabels('labels/CustomLabels.labels-meta.xml', labels);
66-
expect(fsWriteStub.firstCall.args[1]).to.not.include('DeleteMe');
67-
expect(fsWriteStub.firstCall.args[1]).to.include('KeepMe1');
68-
expect(fsWriteStub.firstCall.args[1]).to.include('KeepMe2');
65+
const result = await deleteCustomLabels('labels/CustomLabels.labels-meta.xml', labels);
66+
const resultLabels = result?.CustomLabels.labels.map((label) => label.fullName);
67+
expect(resultLabels).to.deep.equal(['KeepMe1', 'KeepMe2']);
6968
expect(fsReadStub.callCount).to.equal(1);
7069
});
71-
it('will delete a multiple custom labels from a file', () => {
70+
it('will delete a multiple custom labels from a file', async () => {
7271
const labels = [
7372
{
7473
type: { id: 'customlabel', name: 'CustomLabel' },
@@ -80,14 +79,13 @@ describe('deleteCustomLabels', () => {
8079
},
8180
] as SourceComponent[];
8281

83-
void deleteCustomLabels('labels/CustomLabels.labels-meta.xml', labels);
84-
expect(fsWriteStub.firstCall.args[1]).to.include('DeleteMe');
85-
expect(fsWriteStub.firstCall.args[1]).to.not.include('KeepMe1');
86-
expect(fsWriteStub.firstCall.args[1]).to.not.include('KeepMe2');
82+
const result = await deleteCustomLabels('labels/CustomLabels.labels-meta.xml', labels);
83+
const resultLabels = result?.CustomLabels.labels.map((label) => label.fullName);
84+
expect(resultLabels).to.deep.equal(['DeleteMe']);
8785
expect(fsReadStub.callCount).to.equal(1);
8886
});
8987

90-
it('will delete the file when everything is deleted', () => {
88+
it('will delete the file when everything is deleted', async () => {
9189
const labels = [
9290
{
9391
type: { id: 'customlabel', name: 'CustomLabel' },
@@ -103,12 +101,13 @@ describe('deleteCustomLabels', () => {
103101
},
104102
] as SourceComponent[];
105103

106-
void deleteCustomLabels('labels/CustomLabels.labels-meta.xml', labels);
104+
const result = await deleteCustomLabels('labels/CustomLabels.labels-meta.xml', labels);
105+
expect(result).to.equal(undefined);
107106
expect(fsUnlinkStub.callCount).to.equal(1);
108107
expect(fsReadStub.callCount).to.equal(1);
109108
});
110109

111-
it('will delete the file when only a single label is present and deleted', () => {
110+
it('will delete the file when only a single label is present and deleted', async () => {
112111
fsReadStub.returns(
113112
'<?xml version="1.0" encoding="UTF-8"?>\n' +
114113
'<CustomLabels xmlns="http://soap.sforce.com/2006/04/metadata">\n' +
@@ -128,20 +127,22 @@ describe('deleteCustomLabels', () => {
128127
},
129128
] as SourceComponent[];
130129

131-
void deleteCustomLabels('labels/CustomLabels.labels-meta.xml', labels);
130+
const result = await deleteCustomLabels('labels/CustomLabels.labels-meta.xml', labels);
131+
expect(result).to.equal(undefined);
132132
expect(fsUnlinkStub.callCount).to.equal(1);
133133
expect(fsReadStub.callCount).to.equal(1);
134134
});
135135

136-
it('no custom labels, quick exit and do nothing', () => {
136+
it('no custom labels, quick exit and do nothing', async () => {
137137
const labels = [
138138
{
139139
type: { id: 'apexclass', name: 'ApexClass' },
140140
fullName: 'DeleteMe',
141141
},
142142
] as SourceComponent[];
143143

144-
void deleteCustomLabels('labels/CustomLabels.labels-meta.xml', labels);
144+
const result = await deleteCustomLabels('labels/CustomLabels.labels-meta.xml', labels);
145+
expect(result).to.equal(undefined);
145146
expect(fsUnlinkStub.callCount).to.equal(0);
146147
expect(fsWriteStub.callCount).to.equal(0);
147148
expect(fsReadStub.callCount).to.equal(0);

yarn.lock

+1-92
Original file line numberDiff line numberDiff line change
@@ -890,30 +890,7 @@
890890
strip-ansi "6.0.1"
891891
ts-retry-promise "^0.7.0"
892892

893-
"@salesforce/core@^3.34.6":
894-
version "3.34.7"
895-
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-3.34.7.tgz#445efe5c76fbab53e6c891563aa9b0dc5dd24179"
896-
integrity sha512-C4zyXzLAV5ITMChC8dCP+6Kk3t5vloyP2eXpqBOw96OzF5OaCiN5/TayNN8YJl64pvFFny7FgAQPKk7omFXNSA==
897-
dependencies:
898-
"@salesforce/bunyan" "^2.0.0"
899-
"@salesforce/kit" "^1.9.2"
900-
"@salesforce/schemas" "^1.5.1"
901-
"@salesforce/ts-types" "^1.7.2"
902-
"@types/graceful-fs" "^4.1.6"
903-
"@types/semver" "^7.3.13"
904-
ajv "^8.12.0"
905-
archiver "^5.3.0"
906-
change-case "^4.1.2"
907-
debug "^3.2.7"
908-
faye "^1.4.0"
909-
form-data "^4.0.0"
910-
graceful-fs "^4.2.11"
911-
js2xmlparser "^4.0.1"
912-
jsforce "^2.0.0-beta.21"
913-
jsonwebtoken "9.0.0"
914-
ts-retry-promise "^0.7.0"
915-
916-
"@salesforce/core@^3.34.8", "@salesforce/core@^3.36.0", "@salesforce/core@^3.36.1":
893+
"@salesforce/core@^3.34.6", "@salesforce/core@^3.34.8", "@salesforce/core@^3.36.0", "@salesforce/core@^3.36.1":
917894
version "3.36.1"
918895
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-3.36.1.tgz#053a5e1079b9749b62e461e6ac3e630b5689694a"
919896
integrity sha512-kcjyr9bj35nnL8Bqv8U39xeho3CrZYXJiS/X5X1eEHVNZLd9zckrmKrh1V7z8ElCFpsJrewT989SJsdvi9kE8w==
@@ -934,48 +911,6 @@
934911
jsonwebtoken "9.0.0"
935912
ts-retry-promise "^0.7.0"
936913

937-
"@salesforce/core@^3.34.8":
938-
version "3.36.1"
939-
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-3.36.1.tgz#053a5e1079b9749b62e461e6ac3e630b5689694a"
940-
integrity sha512-kcjyr9bj35nnL8Bqv8U39xeho3CrZYXJiS/X5X1eEHVNZLd9zckrmKrh1V7z8ElCFpsJrewT989SJsdvi9kE8w==
941-
dependencies:
942-
"@salesforce/bunyan" "^2.0.0"
943-
"@salesforce/kit" "^1.9.2"
944-
"@salesforce/schemas" "^1.5.1"
945-
"@salesforce/ts-types" "^1.7.2"
946-
"@types/semver" "^7.3.13"
947-
ajv "^8.12.0"
948-
archiver "^5.3.0"
949-
change-case "^4.1.2"
950-
debug "^3.2.7"
951-
faye "^1.4.0"
952-
form-data "^4.0.0"
953-
js2xmlparser "^4.0.1"
954-
jsforce "^2.0.0-beta.21"
955-
jsonwebtoken "9.0.0"
956-
ts-retry-promise "^0.7.0"
957-
958-
"@salesforce/core@^3.36.0":
959-
version "3.36.0"
960-
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-3.36.0.tgz#cbff147d888eee0b921e368c1fdc1a1a3c2eacab"
961-
integrity sha512-LOeSJgozf5B1S8C98/K3afewRsathfEm+HrXxaFXJjITFfIhxqcIDB5xC1cw0VikfRUlq7dQocmVsvezcK0Ghw==
962-
dependencies:
963-
"@salesforce/bunyan" "^2.0.0"
964-
"@salesforce/kit" "^1.9.2"
965-
"@salesforce/schemas" "^1.5.1"
966-
"@salesforce/ts-types" "^1.7.2"
967-
"@types/semver" "^7.3.13"
968-
ajv "^8.12.0"
969-
archiver "^5.3.0"
970-
change-case "^4.1.2"
971-
debug "^3.2.7"
972-
faye "^1.4.0"
973-
form-data "^4.0.0"
974-
js2xmlparser "^4.0.1"
975-
jsforce "^2.0.0-beta.21"
976-
jsonwebtoken "9.0.0"
977-
ts-retry-promise "^0.7.0"
978-
979914
"@salesforce/dev-config@^3.0.0", "@salesforce/dev-config@^3.1.0":
980915
version "3.1.0"
981916
resolved "https://registry.yarnpkg.com/@salesforce/dev-config/-/dev-config-3.1.0.tgz#8eb5b35860ff60d1c1dc3fd9329b01a28475d5b9"
@@ -4617,32 +4552,6 @@ jsforce@^2.0.0-beta.21:
46174552
strip-ansi "^6.0.0"
46184553
xml2js "^0.5.0"
46194554

4620-
jsforce@^2.0.0-beta.21:
4621-
version "2.0.0-beta.21"
4622-
resolved "https://registry.yarnpkg.com/jsforce/-/jsforce-2.0.0-beta.21.tgz#04c94d762d2536bf1af3062d5cca206656f5b12b"
4623-
integrity sha512-74GUF/96vYBNZo3SUccXtt4CmfvZ0iqTSc0Z3OB940Ec7oU6coOAGhlCZ+XprXaHOMMhXMXrZQ1PCd16yjIA7A==
4624-
dependencies:
4625-
"@babel/runtime" "^7.12.5"
4626-
"@babel/runtime-corejs3" "^7.12.5"
4627-
"@types/node" "^12.19.9"
4628-
abort-controller "^3.0.0"
4629-
base64url "^3.0.1"
4630-
commander "^4.0.1"
4631-
core-js "^3.6.4"
4632-
csv-parse "^4.8.2"
4633-
csv-stringify "^5.3.4"
4634-
faye "^1.4.0"
4635-
form-data "^4.0.0"
4636-
fs-extra "^8.1.0"
4637-
https-proxy-agent "^5.0.0"
4638-
inquirer "^7.0.0"
4639-
multistream "^3.1.0"
4640-
node-fetch "^2.6.1"
4641-
open "^7.0.0"
4642-
regenerator-runtime "^0.13.3"
4643-
strip-ansi "^6.0.0"
4644-
xml2js "^0.5.0"
4645-
46464555
json-buffer@3.0.1:
46474556
version "3.0.1"
46484557
resolved "https://registry.yarnpkg.com/json-buffer/-/json-buffer-3.0.1.tgz#9338802a30d3b6605fbe0613e094008ca8c05a13"

0 commit comments

Comments
 (0)