forked from forcedotcom/salesforcedx-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticResourceGenerator.ts
More file actions
70 lines (63 loc) · 2.13 KB
/
Copy pathstaticResourceGenerator.ts
File metadata and controls
70 lines (63 loc) · 2.13 KB
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
/*
* Copyright (c) 2020, salesforce.com, inc.
* 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 { extension } from 'mime-types';
import * as path from 'path';
import { nls } from '../i18n';
import { CreateUtil } from '../utils';
import { StaticResourceOptions } from '../utils/types';
import { BaseGenerator } from './baseGenerator';
const EXTENSION_TEMPLATES = ['js', 'css', 'json', 'txt'];
export default class StaticResourceGenerator extends BaseGenerator<StaticResourceOptions> {
public validateOptions(): void {
CreateUtil.checkInputs(this.options.resourcename);
if (!extension(this.options.contenttype.toLowerCase())) {
throw new Error(nls.localize('InvalidMimeType'));
}
}
public async generate(): Promise<void> {
const { resourcename, contenttype } = this.options;
this.sourceRootWithPartialPath('staticresource');
const ext = extension(contenttype);
if (ext && EXTENSION_TEMPLATES.includes(ext)) {
// For types that we have default file, write that (js, css, txt, json)
await this.render(
this.templatePath(`empty.${ext}`),
this.destinationPath(
path.join(this.outputdir, `${resourcename}.${ext}`)
),
{}
);
} else if (ext === 'zip') {
// For zip files, write an empty js file in a folder
await this.render(
this.templatePath('_gitkeep'),
this.destinationPath(
path.join(this.outputdir, resourcename, '.gitkeep')
),
{}
);
} else {
// For all other mime types write a generic .resource file
await this.render(
this.templatePath('empty.resource'),
this.destinationPath(
path.join(this.outputdir, `${resourcename}.resource`)
),
{}
);
}
await this.render(
this.templatePath('_staticresource.resource-meta.xml'),
this.destinationPath(
path.join(this.outputdir, `${resourcename}.resource-meta.xml`)
),
{
contentType: contenttype,
}
);
}
}