Skip to content

Commit

Permalink
Complete type annotations of svgo-node.js & coa.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jdufresne committed Jan 7, 2024
1 parent 3e9ad5e commit d5b7728
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 93 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 | undefined} 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
4 changes: 0 additions & 4 deletions lib/svgo-node.test.js
Expand Up @@ -2,10 +2,6 @@ import os from 'os';
import path from 'path';
import { optimize, loadConfig } from './svgo-node.js';

/**
* @typedef {import('../lib/types.js').Plugin} Plugin
*/

const describeLF = os.EOL === '\r\n' ? describe.skip : describe;
const describeCRLF = os.EOL === '\r\n' ? describe : describe.skip;

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>;

0 comments on commit d5b7728

Please sign in to comment.