-
Notifications
You must be signed in to change notification settings - Fork 41
/
create.ts
91 lines (78 loc) · 3.56 KB
/
create.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
import { flags, SfdxCommand } from '@salesforce/command';
import { writeJSONasXML } from '@mshanemc/plugin-helpers/dist/JSONXMLtools';
import { removeTrailingSlash } from '../../../shared/flagParsing';
import fs = require('fs-extra');
export default class StaticCreate extends SfdxCommand {
public static description = 'create a static resource locally';
public static examples = [
`sfdx shane:static:create -n myJSResource -y js
// creates /staticresources/myJSResource.js (empty file) and /staticresources/myJSResource.resource-meta.xml
`,
`sfdx shane:static:create -n myZipResource -y js -d "my description" -t myOtherDirectory/main/default
// create an empty folder (zips when pushed), the meta.xml, with a description in a non-default directory.
`
];
protected static flagsConfig = {
name: flags.string({ char: 'n', required: true, description: 'name it (Salesforce API compliant name)' }),
type: flags.string({
char: 'y',
required: true,
description: 'choose one of the following: zip, css, js, text, xml',
options: ['zip', 'css', 'js', 'text', 'xml']
}),
description: flags.string({
char: 'd',
default: 'added from sfdx plugin',
description: "optional description so you can remember why you added this and what it's for"
}),
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",
parse: input => removeTrailingSlash(input)
}),
public: flags.boolean({ char: 'p', default: false, description: 'mark the cache control public' })
// public: { type: 'boolean', char: 'p', default: false, description: 'mark the cache control public' })
};
protected static requiresProject = true;
public async run(): Promise<any> {
const staticPath = `${this.flags.target}/staticresources`;
const metaPath = `${this.flags.target}/staticresources/${this.flags.name}.resource-meta.xml`;
// make /staticresources exist if it doesn't already
if (fs.existsSync(metaPath)) {
throw new Error(`a static resource by that name already exists at ${metaPath}`);
}
await fs.ensureDir(staticPath);
if (this.flags.type === 'zip') {
await fs.mkdir(`${staticPath}/${this.flags.name}`);
} else {
await fs.writeFile(`${staticPath}/${this.flags.name}.${suffixMap.get(this.flags.type) ?? this.flags.type}`, '');
}
await writeJSONasXML({
path: metaPath,
type: 'StaticResource',
json: {
'@': {
xmlns: 'http://soap.sforce.com/2006/04/metadata'
},
cacheControl: this.flags.public ? 'Public' : 'Private',
contentType: contentTypeMap.get(this.flags.type),
description: this.flags.description,
fullName: this.flags.name
}
});
this.ux.log(
this.flags.type === 'zip'
? 'Empty Static Resource folder created locally for you to fill with good things'
: 'Empty Static Resource created locally'
);
}
}
const contentTypeMap = new Map([
['zip', 'application/zip'],
['css', 'text/css'],
['js', 'application/javascript'],
['text', 'text/plan'],
['xml', 'application/xml']
]);
const suffixMap = new Map([['text', 'txt']]);