Comparing version 0.0.8 to 0.0.9
@@ -7,8 +7,14 @@ /** | ||
declare class SvgPackager { | ||
private svgoConfigPath; | ||
/** | ||
* Constructor for SvgPackager class. | ||
* Optionally accepts configurations or dependencies. | ||
*/ | ||
constructor(svgoConfigPath: string); | ||
/** | ||
* Processes all SVG files in a given directory. | ||
* @param directory The directory containing SVG files to process. | ||
* @param inputDirectory The directory containing SVG files to process. | ||
* @param outputDirectory The directory where optimized SVGs will be output as TypeScript files. | ||
*/ | ||
processSvgFiles(directory: string, outputDirectory: string, ts_output_directory: string, json_output_directory: string): Promise<void>; | ||
processSvgFiles(inputDirectory: string, outputDirectory: string, ts_output_directory: string, json_output_directory: string): Promise<void>; | ||
/** | ||
@@ -26,2 +32,3 @@ * Reads the content of an SVG file. | ||
private sanitizeFileName; | ||
private writeFiles; | ||
/** | ||
@@ -28,0 +35,0 @@ * Optimizes SVG content using SVGO. |
"use strict"; | ||
// class/SvgPackager.ts | ||
// script/class/SvgPackager.ts | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
@@ -43,14 +43,7 @@ if (k2 === undefined) k2 = k; | ||
// ============================================================================ | ||
// import * as fs from 'fs'; | ||
var fs_extra = __importStar(require("fs-extra")); | ||
var fs_1 = require("fs"); | ||
var fs = __importStar(require("fs/promises")); | ||
var path = __importStar(require("path")); | ||
var glob = __importStar(require("glob")); | ||
var path = __importStar(require("path")); | ||
var url_1 = require("url"); | ||
var svgo_1 = __importDefault(require("svgo")); | ||
var svgo_2 = require("svgo"); | ||
// Convert the current file's URL to a file path | ||
const __filename = (0, url_1.fileURLToPath)(import.meta.url); | ||
// Derive the directory name of the current module | ||
const __dirname = path.dirname(__filename); | ||
// ============================================================================ | ||
@@ -66,11 +59,18 @@ // Classes | ||
/** | ||
* Constructor for SvgPackager class. | ||
* Optionally accepts configurations or dependencies. | ||
*/ | ||
constructor(svgoConfigPath) { | ||
this.svgoConfigPath = svgoConfigPath; | ||
} | ||
/** | ||
* Processes all SVG files in a given directory. | ||
* @param directory The directory containing SVG files to process. | ||
* @param inputDirectory The directory containing SVG files to process. | ||
* @param outputDirectory The directory where optimized SVGs will be output as TypeScript files. | ||
*/ | ||
async processSvgFiles(directory, outputDirectory, ts_output_directory, json_output_directory) { | ||
async processSvgFiles(inputDirectory, outputDirectory, ts_output_directory, json_output_directory) { | ||
const iconNames = []; | ||
try { | ||
console.log(`Processing directory: ${directory}`); | ||
const svgFiles = glob.sync(`${directory}/**/*.svg`); | ||
console.log(`Processing directory: ${inputDirectory}`); | ||
const svgFiles = glob.sync(`${inputDirectory}/**/*.svg`); | ||
for (const file of svgFiles) { | ||
@@ -82,9 +82,14 @@ console.log(`Processing file: ${file}`); | ||
const svgContent = await this.readSvgFile(file); | ||
const optimizedSvg = await this.optimizeSvg(file, svgContent); | ||
const optimizedSvg = await this.optimizeSvg(svgContent); | ||
// const optimizedSvg = await this.optimizeSvg(file, svgContent); | ||
// svgo will always add a final newline when in pretty mode | ||
const resultSvg = optimizedSvg.trim(); | ||
// Write the optimized SVG file | ||
await this.writeSvgFile(file, iconName, resultSvg, outputDirectory); | ||
await this.writeSvgFile( | ||
// file, | ||
iconName, resultSvg, outputDirectory); | ||
// Write the optimized TypeScript file | ||
await this.writeTypeScriptFile(file, iconName, resultSvg, ts_output_directory); | ||
await this.writeTypeScriptFile( | ||
// file, | ||
iconName, resultSvg, ts_output_directory); | ||
} | ||
@@ -99,2 +104,27 @@ await this.writeIconsJson(iconNames, json_output_directory); | ||
} | ||
// public async processSvgFiles(directory: string, outputDirectory: string): Promise<void> { | ||
// try { | ||
// console.log(`Processing directory: ${directory}`); | ||
// const svgFiles = await this.findSvgFiles(directory); | ||
// for (const file of svgFiles) { | ||
// const iconName = this.sanitizeFileName(path.basename(file, '.svg')); | ||
// console.log(`Processing file: ${file}`); | ||
// const svgContent = await this.readSvgFile(file); | ||
// const optimizedSvg = await this.optimizeSvg(svgContent); | ||
// await this.writeFiles(iconName, optimizedSvg, outputDirectory); | ||
// } | ||
// console.log(`Successfully processed ${svgFiles.length} SVG files.`); | ||
// } catch (error) { | ||
// console.error('Error processing SVG files:', error); | ||
// throw error; | ||
// } | ||
// } | ||
// private async findSvgFiles(directory: string): Promise<string[]> { | ||
// return new Promise((resolve, reject) => { | ||
// glob(`${directory}/**/*.svg`, (err, files) => { | ||
// if (err) reject(err); | ||
// else resolve(files); | ||
// }); | ||
// }); | ||
// } | ||
/** | ||
@@ -105,12 +135,14 @@ * Reads the content of an SVG file. | ||
*/ | ||
// private async readSvgFile(filePath: string): Promise<string> { | ||
// try { | ||
// const absolutePath = path.resolve(filePath); | ||
// const svgContent = await fs.readFile(absolutePath, 'utf8'); | ||
// return svgContent; | ||
// } catch (error) { | ||
// console.error('Error reading file:', filePath, error); | ||
// throw error; | ||
// } | ||
// } | ||
async readSvgFile(filePath) { | ||
try { | ||
const absolutePath = path.resolve(filePath); | ||
const svgContent = await fs_1.promises.readFile(absolutePath, 'utf8'); | ||
return svgContent; | ||
} | ||
catch (error) { | ||
console.error('Error reading file:', filePath, error); | ||
throw error; | ||
} | ||
return fs.readFile(filePath, 'utf8'); | ||
} | ||
@@ -122,6 +154,13 @@ /** | ||
*/ | ||
// private sanitizeFileName(fileName: string): string { | ||
// // Implement more robust sanitization logic if necessary | ||
// return fileName.replace(/[^a-zA-Z0-9_]/g, '_'); | ||
// } | ||
sanitizeFileName(fileName) { | ||
// Implement more robust sanitization logic if necessary | ||
return fileName.replace(/[^a-zA-Z0-9_]/g, '_'); | ||
} | ||
async writeFiles(iconName, svgContent, outputDirectory) { | ||
await this.writeSvgFile(iconName, svgContent, outputDirectory); | ||
await this.writeTypeScriptFile(iconName, svgContent, outputDirectory); | ||
} | ||
/** | ||
@@ -132,13 +171,24 @@ * Optimizes SVG content using SVGO. | ||
*/ | ||
async optimizeSvg(filePath, svgContent) { | ||
try { | ||
const config = await (0, svgo_2.loadConfig)(path.join(__dirname, '../config/svgo.config.js')); | ||
const result = await svgo_1.default.optimize(svgContent, { path: filePath, ...config } // Add SVGO options if needed | ||
); | ||
return result.data; | ||
} | ||
catch (error) { | ||
console.error('Error optimizing SVG:', error); | ||
throw error; | ||
} | ||
// private async optimizeSvg( | ||
// filePath: string, | ||
// svgContent: string | ||
// ): Promise<string> { | ||
// try { | ||
// const config = await loadConfig( | ||
// path.join(__dirname, '../config/svgo.config.js') | ||
// ) | ||
// const result = await SVGO.optimize( | ||
// svgContent, | ||
// { path: filePath, ...config } // Add SVGO options if needed | ||
// ); | ||
// return result.data; | ||
// } catch (error) { | ||
// console.error('Error optimizing SVG:', error); | ||
// throw error; | ||
// } | ||
// } | ||
async optimizeSvg(svgContent) { | ||
const config = await (0, svgo_2.loadConfig)(this.svgoConfigPath); | ||
const result = await svgo_1.default.optimize(svgContent, { ...config }); | ||
return result.data.trim(); | ||
} | ||
@@ -151,12 +201,21 @@ /** | ||
*/ | ||
async writeTypeScriptFile(filePath, iconName, svgContent, outputDirectory) { | ||
try { | ||
const tsContent = `export const icon_${iconName} = \`${svgContent}\`;\n`; | ||
const outputPath = path.join(outputDirectory, `${iconName}.ts`); | ||
await fs_extra.outputFile(outputPath, tsContent); | ||
} | ||
catch (error) { | ||
console.error(`Error creating TypeScript file for ${filePath}:`, error); | ||
throw error; | ||
} | ||
// private async writeTypeScriptFile( | ||
// filePath: string, | ||
// iconName: string, | ||
// svgContent: string, | ||
// outputDirectory: string | ||
// ): Promise<void> { | ||
// try { | ||
// const tsContent = `export const icon_${iconName} = \`${svgContent}\`;\n`; | ||
// const outputPath = path.join(outputDirectory, `${iconName}.ts`); | ||
// await fs.writeFile(outputPath, tsContent); | ||
// } catch (error) { | ||
// console.error(`Error creating TypeScript file for ${filePath}:`, error); | ||
// throw error; | ||
// } | ||
// } | ||
async writeTypeScriptFile(iconName, svgContent, outputDirectory) { | ||
const tsContent = `export const icon_${iconName} = \`${svgContent}\`;\n`; | ||
const outputPath = path.join(outputDirectory, `${iconName}.ts`); | ||
await fs.writeFile(outputPath, tsContent); | ||
} | ||
@@ -169,12 +228,20 @@ /** | ||
*/ | ||
async writeSvgFile(filePath, iconName, svgContent, outputDirectory) { | ||
try { | ||
const outputPath = path.join(outputDirectory, `${iconName}.svg`); | ||
await fs_extra.outputFile(outputPath, svgContent); | ||
console.log(`SVG file written successfully for ${iconName}`); | ||
} | ||
catch (error) { | ||
console.error(`Error writing SVG file for ${iconName}:`, error); | ||
throw error; | ||
} | ||
// private async writeSvgFile( | ||
// filePath: string, | ||
// iconName: string, | ||
// svgContent: string, | ||
// outputDirectory: string | ||
// ): Promise<void> { | ||
// try { | ||
// const outputPath = path.join(outputDirectory, `${iconName}.svg`); | ||
// await fs_extra.outputFile(outputPath, svgContent); | ||
// console.log(`SVG file written successfully for ${iconName}`); | ||
// } catch (error) { | ||
// console.error(`Error writing SVG file for ${iconName}:`, error); | ||
// throw error; | ||
// } | ||
// } | ||
async writeSvgFile(iconName, svgContent, outputDirectory) { | ||
const outputPath = path.join(outputDirectory, `${iconName}.svg`); | ||
await fs.writeFile(outputPath, svgContent); | ||
} | ||
@@ -194,3 +261,4 @@ /** | ||
const outputPath = path.join(outputDirectory, 'icons.json'); | ||
await fs_extra.outputFile(outputPath, jsonContent); | ||
// await fs_extra.outputFile(outputPath, jsonContent); | ||
await fs.writeFile(outputPath, jsonContent); | ||
console.log('Icons JSON file created successfully'); | ||
@@ -197,0 +265,0 @@ } |
{ | ||
"name": "pack.gl", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Package Builder.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -1,2 +0,2 @@ | ||
// class/SvgPackager.ts | ||
// script/class/SvgPackager.ts | ||
@@ -22,17 +22,9 @@ // Copyright 2023 Scape Agency BV | ||
// import * as fs from 'fs'; | ||
import * as fs_extra from 'fs-extra'; | ||
import { promises as fs } from 'fs'; | ||
import * as fs from 'fs/promises'; | ||
import * as path from 'path'; | ||
import * as glob from 'glob'; | ||
import * as path from 'path'; | ||
import { fileURLToPath } from "url"; | ||
import SVGO from 'svgo'; | ||
import { loadConfig } from 'svgo'; | ||
// Convert the current file's URL to a file path | ||
const __filename = fileURLToPath(import.meta.url); | ||
// Derive the directory name of the current module | ||
const __dirname = path.dirname(__filename); | ||
// ============================================================================ | ||
@@ -50,8 +42,14 @@ // Classes | ||
/** | ||
* Constructor for SvgPackager class. | ||
* Optionally accepts configurations or dependencies. | ||
*/ | ||
constructor(private svgoConfigPath: string) {} | ||
/** | ||
* Processes all SVG files in a given directory. | ||
* @param directory The directory containing SVG files to process. | ||
* @param inputDirectory The directory containing SVG files to process. | ||
* @param outputDirectory The directory where optimized SVGs will be output as TypeScript files. | ||
*/ | ||
public async processSvgFiles( | ||
directory: string, | ||
inputDirectory: string, | ||
outputDirectory: string, | ||
@@ -65,5 +63,5 @@ ts_output_directory: string, | ||
try { | ||
console.log(`Processing directory: ${directory}`); | ||
console.log(`Processing directory: ${inputDirectory}`); | ||
const svgFiles = glob.sync(`${directory}/**/*.svg`); | ||
const svgFiles = glob.sync(`${inputDirectory}/**/*.svg`); | ||
@@ -76,3 +74,4 @@ for (const file of svgFiles) { | ||
const svgContent = await this.readSvgFile(file); | ||
const optimizedSvg = await this.optimizeSvg(file, svgContent); | ||
const optimizedSvg = await this.optimizeSvg(svgContent); | ||
// const optimizedSvg = await this.optimizeSvg(file, svgContent); | ||
// svgo will always add a final newline when in pretty mode | ||
@@ -82,3 +81,3 @@ const resultSvg = optimizedSvg.trim() | ||
await this.writeSvgFile( | ||
file, | ||
// file, | ||
iconName, | ||
@@ -90,3 +89,3 @@ resultSvg, | ||
await this.writeTypeScriptFile( | ||
file, | ||
// file, | ||
iconName, | ||
@@ -104,3 +103,33 @@ resultSvg, | ||
} | ||
// public async processSvgFiles(directory: string, outputDirectory: string): Promise<void> { | ||
// try { | ||
// console.log(`Processing directory: ${directory}`); | ||
// const svgFiles = await this.findSvgFiles(directory); | ||
// for (const file of svgFiles) { | ||
// const iconName = this.sanitizeFileName(path.basename(file, '.svg')); | ||
// console.log(`Processing file: ${file}`); | ||
// const svgContent = await this.readSvgFile(file); | ||
// const optimizedSvg = await this.optimizeSvg(svgContent); | ||
// await this.writeFiles(iconName, optimizedSvg, outputDirectory); | ||
// } | ||
// console.log(`Successfully processed ${svgFiles.length} SVG files.`); | ||
// } catch (error) { | ||
// console.error('Error processing SVG files:', error); | ||
// throw error; | ||
// } | ||
// } | ||
// private async findSvgFiles(directory: string): Promise<string[]> { | ||
// return new Promise((resolve, reject) => { | ||
// glob(`${directory}/**/*.svg`, (err, files) => { | ||
// if (err) reject(err); | ||
// else resolve(files); | ||
// }); | ||
// }); | ||
// } | ||
/** | ||
@@ -111,13 +140,15 @@ * Reads the content of an SVG file. | ||
*/ | ||
// private async readSvgFile(filePath: string): Promise<string> { | ||
// try { | ||
// const absolutePath = path.resolve(filePath); | ||
// const svgContent = await fs.readFile(absolutePath, 'utf8'); | ||
// return svgContent; | ||
// } catch (error) { | ||
// console.error('Error reading file:', filePath, error); | ||
// throw error; | ||
// } | ||
// } | ||
private async readSvgFile(filePath: string): Promise<string> { | ||
try { | ||
const absolutePath = path.resolve(filePath); | ||
const svgContent = await fs.readFile(absolutePath, 'utf8'); | ||
return svgContent; | ||
} catch (error) { | ||
console.error('Error reading file:', filePath, error); | ||
throw error; | ||
} | ||
return fs.readFile(filePath, 'utf8'); | ||
} | ||
/** | ||
@@ -128,7 +159,15 @@ * Sanitizes a file name to be a valid TypeScript identifier. | ||
*/ | ||
// private sanitizeFileName(fileName: string): string { | ||
// // Implement more robust sanitization logic if necessary | ||
// return fileName.replace(/[^a-zA-Z0-9_]/g, '_'); | ||
// } | ||
private sanitizeFileName(fileName: string): string { | ||
// Implement more robust sanitization logic if necessary | ||
return fileName.replace(/[^a-zA-Z0-9_]/g, '_'); | ||
return fileName.replace(/[^a-zA-Z0-9_]/g, '_'); | ||
} | ||
private async writeFiles(iconName: string, svgContent: string, outputDirectory: string): Promise<void> { | ||
await this.writeSvgFile(iconName, svgContent, outputDirectory); | ||
await this.writeTypeScriptFile(iconName, svgContent, outputDirectory); | ||
} | ||
/** | ||
@@ -139,25 +178,29 @@ * Optimizes SVG content using SVGO. | ||
*/ | ||
private async optimizeSvg( | ||
filePath: string, | ||
svgContent: string | ||
): Promise<string> { | ||
// private async optimizeSvg( | ||
// filePath: string, | ||
// svgContent: string | ||
// ): Promise<string> { | ||
try { | ||
// try { | ||
const config = await loadConfig( | ||
path.join(__dirname, '../config/svgo.config.js') | ||
) | ||
// const config = await loadConfig( | ||
// path.join(__dirname, '../config/svgo.config.js') | ||
// ) | ||
const result = await SVGO.optimize( | ||
svgContent, | ||
{ path: filePath, ...config } // Add SVGO options if needed | ||
); | ||
// const result = await SVGO.optimize( | ||
// svgContent, | ||
// { path: filePath, ...config } // Add SVGO options if needed | ||
// ); | ||
return result.data; | ||
} catch (error) { | ||
console.error('Error optimizing SVG:', error); | ||
throw error; | ||
} | ||
// return result.data; | ||
// } catch (error) { | ||
// console.error('Error optimizing SVG:', error); | ||
// throw error; | ||
// } | ||
// } | ||
private async optimizeSvg(svgContent: string): Promise<string> { | ||
const config = await loadConfig(this.svgoConfigPath); | ||
const result = await SVGO.optimize(svgContent, { ...config }); | ||
return result.data.trim(); | ||
} | ||
/** | ||
@@ -169,16 +212,22 @@ * Creates a TypeScript file from SVG content. | ||
*/ | ||
private async writeTypeScriptFile( | ||
filePath: string, | ||
iconName: string, | ||
svgContent: string, | ||
outputDirectory: string | ||
): Promise<void> { | ||
try { | ||
const tsContent = `export const icon_${iconName} = \`${svgContent}\`;\n`; | ||
const outputPath = path.join(outputDirectory, `${iconName}.ts`); | ||
await fs_extra.outputFile(outputPath, tsContent); | ||
} catch (error) { | ||
console.error(`Error creating TypeScript file for ${filePath}:`, error); | ||
throw error; | ||
} | ||
// private async writeTypeScriptFile( | ||
// filePath: string, | ||
// iconName: string, | ||
// svgContent: string, | ||
// outputDirectory: string | ||
// ): Promise<void> { | ||
// try { | ||
// const tsContent = `export const icon_${iconName} = \`${svgContent}\`;\n`; | ||
// const outputPath = path.join(outputDirectory, `${iconName}.ts`); | ||
// await fs.writeFile(outputPath, tsContent); | ||
// } catch (error) { | ||
// console.error(`Error creating TypeScript file for ${filePath}:`, error); | ||
// throw error; | ||
// } | ||
// } | ||
private async writeTypeScriptFile(iconName: string, svgContent: string, outputDirectory: string): Promise<void> { | ||
const tsContent = `export const icon_${iconName} = \`${svgContent}\`;\n`; | ||
const outputPath = path.join(outputDirectory, `${iconName}.ts`); | ||
await fs.writeFile(outputPath, tsContent); | ||
} | ||
@@ -192,16 +241,20 @@ | ||
*/ | ||
private async writeSvgFile( | ||
filePath: string, | ||
iconName: string, | ||
svgContent: string, | ||
outputDirectory: string | ||
): Promise<void> { | ||
try { | ||
const outputPath = path.join(outputDirectory, `${iconName}.svg`); | ||
await fs_extra.outputFile(outputPath, svgContent); | ||
console.log(`SVG file written successfully for ${iconName}`); | ||
} catch (error) { | ||
console.error(`Error writing SVG file for ${iconName}:`, error); | ||
throw error; | ||
} | ||
// private async writeSvgFile( | ||
// filePath: string, | ||
// iconName: string, | ||
// svgContent: string, | ||
// outputDirectory: string | ||
// ): Promise<void> { | ||
// try { | ||
// const outputPath = path.join(outputDirectory, `${iconName}.svg`); | ||
// await fs_extra.outputFile(outputPath, svgContent); | ||
// console.log(`SVG file written successfully for ${iconName}`); | ||
// } catch (error) { | ||
// console.error(`Error writing SVG file for ${iconName}:`, error); | ||
// throw error; | ||
// } | ||
// } | ||
private async writeSvgFile(iconName: string, svgContent: string, outputDirectory: string): Promise<void> { | ||
const outputPath = path.join(outputDirectory, `${iconName}.svg`); | ||
await fs.writeFile(outputPath, svgContent); | ||
} | ||
@@ -226,3 +279,4 @@ | ||
const outputPath = path.join(outputDirectory, 'icons.json'); | ||
await fs_extra.outputFile(outputPath, jsonContent); | ||
// await fs_extra.outputFile(outputPath, jsonContent); | ||
await fs.writeFile(outputPath, jsonContent); | ||
console.log('Icons JSON file created successfully'); | ||
@@ -235,2 +289,4 @@ } catch (error) { | ||
} | ||
@@ -237,0 +293,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
157147
3429
12