@salesforce/templates
Version:
Salesforce JS library for templates
47 lines • 2.26 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/*
* 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
*/
const mime_types_1 = require("mime-types");
const path = require("path");
const i18n_1 = require("../i18n");
const utils_1 = require("../utils");
const sfdxGenerator_1 = require("./sfdxGenerator");
const EXTENSION_TEMPLATES = ['js', 'css', 'json', 'txt'];
class StaticResourceGenerator extends sfdxGenerator_1.SfdxGenerator {
constructor(args, options) {
super(args, options);
this.sourceRootWithPartialPath('staticresource');
}
validateOptions() {
utils_1.CreateUtil.checkInputs(this.options.resourcename);
if (!(0, mime_types_1.extension)(this.options.contenttype.toLowerCase())) {
throw new Error(i18n_1.nls.localize('InvalidMimeType'));
}
}
writing() {
const { resourcename, contenttype } = this.options;
const ext = (0, mime_types_1.extension)(contenttype);
if (ext && EXTENSION_TEMPLATES.includes(ext)) {
// For types that we have default file, write that (js, css, txt, json)
this.fs.copyTpl(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
this.fs.copyTpl(this.templatePath('_gitkeep'), this.destinationPath(path.join(this.outputdir, resourcename, '.gitkeep')), {});
}
else {
// For all other mime types write a generic .resource file
this.fs.copyTpl(this.templatePath('empty.resource'), this.destinationPath(path.join(this.outputdir, `${resourcename}.resource`)), {});
}
this.fs.copyTpl(this.templatePath('_staticresource.resource-meta.xml'), this.destinationPath(path.join(this.outputdir, `${resourcename}.resource-meta.xml`)), {
contentType: contenttype,
});
}
}
exports.default = StaticResourceGenerator;
//# sourceMappingURL=staticResourceGenerator.js.map