-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove.ts
92 lines (80 loc) · 3.01 KB
/
remove.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Copyright (c) 2021, jayree
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { flags, SfdxCommand } from '@salesforce/command';
import { Messages } from '@salesforce/core';
import { AnyJson } from '@salesforce/ts-types';
import AdmZip from 'adm-zip';
import { js2Manifest, parseManifest } from '../../../utils/xml.js';
// eslint-disable-next-line no-underscore-dangle
const __filename = fileURLToPath(import.meta.url);
// eslint-disable-next-line no-underscore-dangle
const __dirname = dirname(__filename);
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@jayree/sfdx-plugin-legacy', 'removepackagedescription');
export default class RemovePackageDescription extends SfdxCommand {
// hotfix to receive only one help page
// public static hidden = true;
public static description = messages.getMessage('commandDescription');
public static examples = [
`$ sfdx jayree:packagedescription:remove --file FILENAME
`,
];
public static args = [{ name: 'file' }];
protected static flagsConfig = {
file: flags.string({
char: 'f',
description: messages.getMessage('fileFlagDescription'),
required: true,
}),
};
protected static requiresUsername = false;
protected static supportsDevhubUsername = false;
protected static requiresProject = false;
// eslint-disable-next-line @typescript-eslint/require-await
public async run(): Promise<AnyJson> {
const inputfile = this.args.file || this.flags.file;
const newZip = new AdmZip();
const zip = new AdmZip(inputfile);
const zipEntries = zip.getEntries();
let action;
let text;
zipEntries.forEach((zipEntry) => {
const fileName = zipEntry.entryName;
const fileContent = zip.readFile(fileName);
if (fileName.includes('package.xml')) {
let fileContentjs;
const fileTXTContent = zip.readAsText(fileName);
const xml = parseManifest(fileTXTContent);
if (xml.Package.description && xml.Package.description.length > 0) {
text = xml.Package.description.toString();
action = 'removed';
this.ux.log(action + ' description: ' + text);
fileContentjs = {
Package: {
$: { xmlns: 'http://soap.sforce.com/2006/04/metadata' },
types: xml.Package.types,
version: xml.Package.version,
},
};
newZip.addFile(fileName, Buffer.from(js2Manifest(fileContentjs)), '', 0o644);
} else {
action = '';
this.ux.log('no description found');
}
} else {
newZip.addFile(fileName, fileContent, '', 0o644);
}
});
if (action === 'removed') {
newZip.writeZip(inputfile);
}
// eslint-disable-next-line camelcase
return { old_description: text, task: action };
}
}