Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete type annotations of svgo-node.js & coa.js #1914

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>;