UNPKG

europa-build

Version:

Tool for generating and maintaining Europa plugins and presets

147 lines 6.9 kB
"use strict"; /* * Copyright (C) 2022 neocotic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.CommonScriptProvider = void 0; const del = require("del"); const promises_1 = require("fs/promises"); const path_1 = require("path"); const PackageInfo_1 = require("../../PackageInfo"); const _logger = Symbol(); /** * An abstract {@link ScriptProvider} that provides common logic across the majority of implementations. */ class CommonScriptProvider { /** * Creates an instance of {@link CommonScriptProvider} using the `options` provided. * * @param options - The options to be used. */ constructor(options) { this[_logger] = options.parentLogger.child({ name: this.getLoggerName() }); } async afterScript(directoryPath) { // Do nothing by default } /** * Copies the specified bundled config file to the directory provided. * * @param directoryPath - The path of the directory to which the bundled config file is to be copied. * @param fileName - The name of the bundled config file to be copied. * @throws If an error occurs while attempting to copy the bundled config file. */ async copyBundledConfigFile(directoryPath, fileName) { const sourceFilePath = await this.getBundledConfigFilePath(fileName); const targetFilePath = (0, path_1.resolve)(directoryPath, fileName); this.getLogger().debug(`Copying '${sourceFilePath}' file to '${targetFilePath}'`); await (0, promises_1.copyFile)(sourceFilePath, targetFilePath); } /** * Copies the specified bundled config files to the directory provided. * * @param directoryPath - The path of the directory to which the bundled config files are to be copied. * @param fileNames - The names of the bundled config files to be copied. * @throws If an error occurs while attempting to copy any bundled config file. */ async copyBundledConfigFiles(directoryPath, fileNames) { for (const fileName of fileNames) { await this.copyBundledConfigFile(directoryPath, fileName); } } /** * Deletes the subdirectories at the specified path within the directory provided. * * @param directoryPath - The path of the directory to which `subdirectoryPaths` are resolved. * @param subdirectoryPaths - The paths of the subdirectories to be deleted relative to `directoryPath` * @throws If an error occurs while attempting to delete any subdirectory. */ async deleteDirectories(directoryPath, subdirectoryPaths) { const targetDirectoryPaths = subdirectoryPaths.map((subdirectoryPath) => (0, path_1.resolve)(directoryPath, subdirectoryPath)); this.getLogger().debug(`Deleting directories: [${targetDirectoryPaths}]`); await del(targetDirectoryPaths); } /** * Deletes the subdirectory at the specified path within the directory provided. * * @param directoryPath - The path of the directory to which `subdirectoryPath` is resolved. * @param subdirectoryPath - The path of the subdirectory to be deleted relative to `directoryPath` * @throws If an error occurs while attempting to delete the subdirectory. */ async deleteDirectory(directoryPath, subdirectoryPath) { const targetDirectoryPath = (0, path_1.resolve)(directoryPath, subdirectoryPath); this.getLogger().debug(`Deleting directory: '${targetDirectoryPath}'`); await del(targetDirectoryPath); } /** * Deletes the file at the specified path within the directory provided. * * @param directoryPath - The path of the directory to which `filePath` is resolved. * @param filePath - The path of the file to be deleted relative to `directoryPath` * @throws If an error occurs while attempting to delete the file. */ async deleteFile(directoryPath, filePath) { const targetFilePath = (0, path_1.resolve)(directoryPath, filePath); this.getLogger().debug(`Deleting file: '${targetFilePath}'`); await del(targetFilePath); } /** * Deletes the files at the specified paths within the directory provided. * * @param directoryPath - The path of the directory to which `filePaths` are resolved. * @param filePaths - The paths of the files to be deleted relative to `directoryPath` * @throws If an error occurs while attempting to delete any file. */ async deleteFiles(directoryPath, filePaths) { const targetFilePaths = filePaths.map((filePath) => (0, path_1.resolve)(directoryPath, filePath)); this.getLogger().debug(`Deleting files: [${targetFilePaths}]`); await del(targetFilePaths); } /** * Returns the absolute path of the bundled config file with the specified name. * * @param fileName - The name of the bundled config file to be resolved. * @return The absolute path of the bundled config file. */ getBundledConfigFilePath(fileName) { return this.getBundledPath('bundled-config', fileName); } /** * Returns the absolute path of the bundled file/directory at the specified paths. * * @param filePaths - The paths of the bundled file/directory to be resolved. * @return The absolute path of the bundled file/directory. */ async getBundledPath(...filePaths) { const packageInfo = await PackageInfo_1.PackageInfo.getSingleton(); return (0, path_1.resolve)(packageInfo.directoryPath, ...filePaths); } /** * Returns the logger for this {@link CommonScriptProvider}. * * @return The logger. */ getLogger() { return this[_logger]; } } exports.CommonScriptProvider = CommonScriptProvider; //# sourceMappingURL=CommonScriptProvider.js.map