Skip to content

Commit

Permalink
Complete type annotations of lib/svgo-node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jdufresne committed Jan 4, 2024
1 parent 51c59f5 commit 55f32c6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
24 changes: 23 additions & 1 deletion lib/svgo-node.js
Expand Up @@ -4,10 +4,18 @@ import { pathToFileURL } from 'url';
import path from 'path';
import { optimize as optimizeAgnostic } from './svgo.js';

/**
* @typedef {import('./svgo.js').Config} Config
*/

/**
* @param {string} configFile
* @returns Config
*/
const importConfig = async (configFile) => {
// dynamic import expects file url instead of path and may fail
// when windows path is provided
const imported = await import(pathToFileURL(configFile));
const imported = await import(pathToFileURL(configFile).toString());
const config = imported.default;

if (config == null || typeof config !== 'object' || Array.isArray(config)) {
Expand All @@ -16,6 +24,10 @@ const importConfig = async (configFile) => {
return config;
};

/**
* @param {string} file
* @returns Promise<boolean>
*/
const isFile = async (file) => {
try {
const stats = await fs.promises.stat(file);
Expand All @@ -25,6 +37,11 @@ const isFile = async (file) => {
}
};

/**
* @param {string | null} configFile
* @param {string} [cwd]
* @returns Promis<Config>
*/
export const loadConfig = async (configFile, cwd = process.cwd()) => {
if (configFile != null) {
if (path.isAbsolute(configFile)) {
Expand Down Expand Up @@ -56,6 +73,11 @@ export const loadConfig = async (configFile, cwd = process.cwd()) => {
}
};

/**
* @param {string} input
* @param {Config} [config]
* @returns Output
*/
export const optimize = (input, config) => {
if (config == null) {
config = {};
Expand Down
14 changes: 0 additions & 14 deletions lib/svgo.d.ts
Expand Up @@ -53,17 +53,3 @@ type Output = {

/** The core of SVGO */
export declare function optimize(input: string, config?: Config): Output;

/**
* If you write a tool on top of svgo you might need a way to load svgo config.
*
* You can also specify relative or absolute path and customize current working directory.
*/
export declare function loadConfig(
configFile: string,
cwd?: string,
): Promise<Config>;
export declare function loadConfig(
configFile?: null,
cwd?: string,
): Promise<Config | null>;
1 change: 0 additions & 1 deletion tsconfig.json
Expand Up @@ -11,7 +11,6 @@
},
"include": ["lib/**/*.js", "plugins/**/*", "test/cli/**/*"],
"exclude": [
"lib/svgo-node.js",
"lib/svgo-node.test.js",
"lib/svgo.js",
"lib/builtin.js",
Expand Down

0 comments on commit 55f32c6

Please sign in to comment.