UNPKG

@thelabnyc/typed-scss-modules

Version:

TypeScript type definition generator for SCSS CSS Modules

88 lines (87 loc) 3.2 kB
import path from "node:path"; import { bundleRequire } from "bundle-require"; import JoyCon from "joycon"; import { alerts } from "./core/index.js"; import { nameFormatDefault } from "./sass/index.js"; import { bannerTypeDefault, exportTypeDefault, exportTypeInterfaceDefault, exportTypeNameDefault, logLevelDefault, quoteTypeDefault, } from "./typescript/index.js"; const VALID_CONFIG_FILES = [ "typed-scss-modules.config.ts", "typed-scss-modules.config.js", "typed-scss-modules.config.cts", "typed-scss-modules.config.cjs", ]; const joycon = new JoyCon.default(); /** * Load a custom config file in the project root directory with any options for this package. * * This supports config files in the following formats and order: * - Named `config` export: `export const config = {}` * - Default export: `export default {}` * - `module.exports = {}` */ export const loadConfig = async () => { const CURRENT_WORKING_DIRECTORY = process.cwd(); const configPath = await joycon.resolve(VALID_CONFIG_FILES, CURRENT_WORKING_DIRECTORY, path.parse(CURRENT_WORKING_DIRECTORY).root); if (configPath) { try { const configModule = await bundleRequire({ filepath: configPath, }); // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const config = // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access configModule.mod.config || // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access configModule.mod.default || configModule.mod; return config; } catch (error) { alerts.error( // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `An error occurred loading the config file "${configPath}":\n${error}`); return {}; } } return {}; }; // Default values for all options that need defaults. export const DEFAULT_OPTIONS = { nameFormat: [nameFormatDefault], exportType: exportTypeDefault, exportTypeName: exportTypeNameDefault, exportTypeInterface: exportTypeInterfaceDefault, watch: false, ignoreInitial: false, listDifferent: false, ignore: [], quoteType: quoteTypeDefault, updateStaleOnly: false, logLevel: logLevelDefault, banner: bannerTypeDefault, outputFolder: null, allowArbitraryExtensions: false, }; const removedUndefinedValues = (obj) => { for (const key in obj) { if (obj[key] === undefined) { delete obj[key]; } } return obj; }; /** * Given both the CLI and config file options merge into a single options object. * * When possible, CLI options will override config file options. * * Some options are only available in the config file. For example, a custom function can't * be easily defined via the CLI so some complex options are only available in the config file. */ export const mergeOptions = (cliOptions, configOptions) => { return { ...DEFAULT_OPTIONS, ...configOptions, ...removedUndefinedValues(cliOptions), }; };