@salesforce/templates
Version:
Salesforce JS library for templates
68 lines • 2.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateUtil = void 0;
/*
* Copyright (c) 2019, 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 fs = require("fs");
const path = require("path");
const i18n_1 = require("../i18n");
class CreateUtil {
static checkInputs(flagValue) {
const alphaRegExp = /^\w+$/;
if (!alphaRegExp.test(flagValue)) {
throw new Error(i18n_1.nls.localize('AlphaNumericNameError'));
}
const letterStartRegExp = /^[A-Za-z]/;
if (!letterStartRegExp.test(flagValue)) {
throw new Error(i18n_1.nls.localize('NameMustStartWithLetterError'));
}
const endUnderscore = /_$/;
if (endUnderscore.test(flagValue)) {
throw new Error(i18n_1.nls.localize('EndWithUnderscoreError'));
}
const dblUnderscore = /__/;
if (dblUnderscore.test(flagValue)) {
throw new Error(i18n_1.nls.localize('DoubleUnderscoreError'));
}
return '';
}
// TODO: switch filetype to a string instead of regex
static getCommandTemplatesForFiletype(filetype, command) {
const files = fs
.readdirSync(path.resolve(__dirname, '..', 'templates', command))
.filter((file) => filetype.test(file))
.map((file) => {
return file.split('.', 1).toString();
});
return files;
}
/** Get the names of directories that contain matching template files.
* This will look in directories under the command/subdir folder.
* @param command the command name
* @param filetype optional file name pattern to match on in the subdirectories
* @param subdir optional subdirectory under `templates/${command}`
* @return the set of template names
*/
static getCommandTemplatesInSubdirs(command, { filetype, subdir } = {}) {
let basedir = path.resolve(__dirname, '..', 'templates', command);
if (subdir) {
basedir = path.join(basedir, subdir);
}
const subdirs = fs
.readdirSync(basedir, { withFileTypes: true })
.filter((ent) => ent.isDirectory())
.map((ent) => ent.name);
if (filetype) {
return subdirs.filter((dir) => fs
.readdirSync(path.join(basedir, dir), { withFileTypes: true })
.some((ent) => ent.isFile() && filetype.test(ent.name)));
}
return subdirs;
}
}
exports.CreateUtil = CreateUtil;
//# sourceMappingURL=createUtil.js.map