-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathadd.ts
93 lines (77 loc) · 3.58 KB
/
add.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
93
import { flags, SfdxCommand } from '@salesforce/command';
import chalk from 'chalk';
import { getExisting } from '../../../shared/getExisting';
import { writeJSONasXML } from '../../../shared/JSONXMLtools';
import { setupArray } from '../../../shared/setupArray';
import { CustomLabel } from '../../../shared/typeDefs';
import fs = require('fs-extra');
import sw = require('stopword');
export default class LabelAdd extends SfdxCommand {
public static description = "create a remote site setting in the local source. Push it when you're done";
public static examples = [
`sfdx shane:label:add -t "This is some Text"
// create a custom label with the displayed text and all the defaults
`
];
protected static flagsConfig = {
text: flags.string({ required: true, char: 't', description: 'the text you want to turn into a label' }),
bundle: flags.string({ description: 'label bundle when you want to organize them more', default: 'CustomLabels' }),
name: flags.string({ char: 'n', description: 'api name for your label' }),
description: flags.string({ char: 'd', description: 'description for your label' }),
protected: flags.boolean({ description: 'mark as protected (packaged, subscribers cannot change the label' }),
categories: flags.array({ description: 'categories to add to your custom label' }),
language: flags.string({ char: 'l', description: 'language code', default: 'en_US' }),
target: flags.directory({
char: 't',
default: 'force-app/main/default',
description: "where to create the folder (if it doesn't exist already) and file...defaults to force-app/main/default"
})
};
protected static requiresProject = true;
public async run(): Promise<any> {
const labelsFolder = `${this.flags.target}/labels`;
const targetFilename = `${labelsFolder}/${this.flags.bundle}.labels-meta.xml`;
await fs.ensureDir(labelsFolder);
let existing = await getExisting(targetFilename, 'CustomLabels', {
'@': {
xmlns: 'http://soap.sforce.com/2006/04/metadata'
},
labels: []
});
existing = setupArray(existing, 'labels');
const newLabel: CustomLabel = {
fullName:
this.flags.name ??
sw
.removeStopwords(this.flags.text.split(' '))
.join(' ')
.replace(/[^a-zA-Z0-9]/g, '')
.substring(0, 80),
shortDescription:
this.flags.description ??
sw
.removeStopwords(this.flags.text.split(' '))
.join(' ')
.replace(/[^a-zA-Z0-9]/g, '')
.substring(0, 80),
language: this.flags.language,
protected: this.flags.protected ?? false,
value: this.flags.text
};
if (this.flags.categories) {
newLabel.categories = this.flags.categories.join(',');
}
// verify label doesn't already exist
if (existing.labels.filter(label => label.fullName === newLabel.fullName).length > 0) {
throw new Error(`A label with the fullName ${newLabel.fullName} already exists`);
}
existing.labels.push(newLabel);
await writeJSONasXML({
path: targetFilename,
type: 'CustomLabels',
json: existing
});
this.ux.log(chalk.green(`Added ${newLabel.fullName} to ${targetFilename} in local source`));
return existing;
}
}