Skip to content

Commit

Permalink
feat: ✨implement icon paths support interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
zenkiet committed Nov 14, 2024
1 parent cc837a5 commit a69a1dc
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 15 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,18 @@ optimize-icons
The tool will prompt you for:

- Output path (default: dist/browser)
- Icons path (default: dist/browser/icons)
- Verbose mode (yes/no)

### Options

| Option | Alias | Description |
| --------------- | ----- | ------------------------------------------------ |
| `--output-path` | `-o` | Specify the output directory for optimized icons |
| `--verbose` | `-v` | Enable verbose output |
| `--version` | - | Show version number |
| `--help` | - | Show help |
| Option | Alias | Description |
| --------------- | ----- | ----------------------------------------------------- |
| `--output-path` | `-o` | Specify the output dist directory for optimized icons |
| `--icons-path` | `-i` | Specify the icons directory for optimized icons |
| `--verbose` | `-v` | Enable verbose output |
| `--version` | - | Show version number |
| `--help` | - | Show help |

## Programmatic Usage

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "optimize-icons-cli",
"version": "1.1.2",
"version": "1.1.3",
"description": "CLI tool for optimizing icons",
"main": "dist/cli.js",
"types": "dist/types/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src/command/command-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ export class CommandManager {
private setupCommands(): void {
this.program
.option('-o, --output-path <path>', 'Output path containing the built files')
.option('-i, --icons-path <path>', 'Path to icons directory')
.option('-v, --verbose', 'Show verbose output')
.addHelpText(
'after',
`
Examples:
$ optimize-icons -o dist/browser
$ optimize-icons -o dist/browser -v`
$ optimize-icons -o dist/browser -i dist/browser/icons -v`
);
}

Expand Down
17 changes: 15 additions & 2 deletions src/command/optimize-command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import inquirer from 'inquirer';
import { OUTPUT_DIR, VERBOSE_DEFAULT } from '../constants';
import { ICONS_DIR, OUTPUT_DIR, VERBOSE_DEFAULT } from '../constants';
import IconOptimizer from '../core/icon-optimize';
import { OptimizeIconsOptions } from '../core/types';
import { BaseCommand } from './base-command';
Expand All @@ -13,7 +13,19 @@ export class OptimizeCommand implements BaseCommand {
type: 'input',
name: 'outputPath',
message: 'Enter output path:',
default: 'dist/browser',
default: OUTPUT_DIR,
validate: (input) => {
if (input.trim().length === 0) {
return 'Output path is required';
}
return true;
},
},
{
type: 'input',
name: 'iconsPath',
message: 'Enter icons directory path:',
default: ICONS_DIR,
validate: (input) => {
if (input.trim().length === 0) {
return 'Output path is required';
Expand All @@ -37,6 +49,7 @@ export class OptimizeCommand implements BaseCommand {
this.options.outputPath || this.options.verbose
? {
outputPath: this.options.outputPath || OUTPUT_DIR,
iconsPath: this.options.iconsPath || ICONS_DIR,
verbose: this.options.verbose || VERBOSE_DEFAULT,
}
: await this.promptUserInput();
Expand Down
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const OUTPUT_DIR = 'dist/browser';
export const ICONS_DIR = OUTPUT_DIR + '/icons';
export const VERBOSE_DEFAULT = false;
10 changes: 8 additions & 2 deletions src/core/icon-optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ export class IconOptimizer {
* Process all SVG icon files
*/
private async processIconFiles(): Promise<void> {
const svgFiles = await glob(`${this.options.outputPath}/icons/*.svg`);
const iconPath = this.options.iconsPath || path.join(this.options.outputPath, 'icons');
const svgFiles = await glob(`${iconPath}/*.svg`);

if (svgFiles.length === 0) {
this.logger.warning(`No SVG files found in ${iconPath}`);
return;
}

for (const file of svgFiles) {
await this.optimizeSvgFile(file);
Expand All @@ -75,7 +81,7 @@ export class IconOptimizer {
const content = await fs.readFile(filePath, 'utf8');
const fileName = path.basename(filePath);

this.logger.log(`Processing ${fileName}...`);
this.logger.log(`\nProcessing ${fileName}...`);

const { optimizedContent, removedCount, totalCount } = await this.processSvgContent(content);

Expand Down
1 change: 1 addition & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface OptimizeIconsOptions {
outputPath: string;
iconsPath?: string;
verbose?: boolean;
}

Expand Down

0 comments on commit a69a1dc

Please sign in to comment.