UNPKG

ts-comment-remover

Version:

TypeScript file compression tool that removes comments and unnecessary whitespace using AST

83 lines 3.38 kB
import { readdir, stat } from 'node:fs/promises'; import { join, relative } from 'node:path'; import { exec } from 'node:child_process'; export const directoryExists = (path) => async () => { try { const stats = await stat(path); return stats.isDirectory(); } catch { return false; } }; const patternToRegex = (pattern) => new RegExp(pattern.replace(/\*/g, '.*')); export const createFileFilter = (includePatterns, excludePatterns) => { const includeRegexes = includePatterns.map(patternToRegex); const excludeRegexes = excludePatterns.map(patternToRegex); return (fileName) => { if (!fileName.endsWith('.ts')) return false; if (excludeRegexes.some(regex => regex.test(fileName))) return false; return includeRegexes.some(regex => regex.test(fileName)); }; }; const shouldSkipDirectory = (name) => name === 'node_modules' || name.startsWith('.') || name === 'dist'; const getFilesRecursiveIO = (dir, filter) => async () => { const entries = await readdir(dir, { withFileTypes: true }); const results = await Promise.all(entries.map(async (entry) => { const fullPath = join(dir, entry.name); if (entry.isDirectory()) { return shouldSkipDirectory(entry.name) ? [] : await getFilesRecursiveIO(fullPath, filter)(); } return filter(entry.name) ? [fullPath] : []; })); return results.flat().sort(); }; export const getTypeScriptFiles = (dir, includePatterns = ['*.ts'], excludePatterns = ['*.d.ts', '*.test.ts', '*.spec.ts']) => { const filter = createFileFilter(includePatterns, excludePatterns); return getFilesRecursiveIO(dir, filter); }; export const calculateStats = (originalSize, compressedSize) => ({ originalSize, compressedSize, ratio: originalSize > 0 ? Math.round((1 - compressedSize / originalSize) * 100) : 0, }); const getClipboardCommand = (platform) => { const commands = { darwin: 'pbcopy', win32: 'clip', linux: 'xclip -selection clipboard', }; return commands[platform] ?? 'xclip -selection clipboard'; }; export const copyToClipboard = (text) => async () => { const command = getClipboardCommand(process.platform); const child = exec(command); if (!child.stdin) throw new Error('Failed to access clipboard command stdin'); child.stdin.write(text); child.stdin.end(); await new Promise((resolve, reject) => { child.on('exit', code => { code === 0 ? resolve() : reject(new Error(`Exit code: ${code}`)); }); child.on('error', reject); }); }; export const formatFileSize = (bytes) => { const units = ['B', 'KB', 'MB', 'GB']; const index = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1); const size = (bytes / Math.pow(1024, index)).toFixed(2); return `${size} ${units[index]}`; }; export const getRelativePath = (filePath, baseDir) => relative(baseDir, filePath).replace(/\\/g, '/'); export const concat = (...arrays) => arrays.reduce((acc, arr) => [...acc, ...arr], []); export const merge = (...objects) => Object.assign({}, ...objects); export const pipe = (f, g) => (a) => g(f(a)); export const pipeAsync = (f, g) => async (a) => g(await f(a)); //# sourceMappingURL=utils.js.map