"use strict";
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 nodeFs = require("fs");
const path = require("path");
const constants_1 = require("./constants");
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, fs = nodeFs, templatesRootPath) {
        var _a;
        const basePath = (_a = templatesRootPath !== null && templatesRootPath !== void 0 ? templatesRootPath : constants_1.dirnameTemplatesDefault) !== null && _a !== void 0 ? _a : '';
        const files = fs
            .readdirSync(path.resolve(basePath, 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 } = {}, fs = nodeFs, templatesRootPath) {
        var _a;
        const basePath = (_a = templatesRootPath !== null && templatesRootPath !== void 0 ? templatesRootPath : constants_1.dirnameTemplatesDefault) !== null && _a !== void 0 ? _a : '';
        let basedir = path.resolve(basePath, 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