Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

omni-file

Package Overview
Dependencies
Maintainers
0
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

omni-file

Map file extensions to languages using GitHub Linguist data (Always up to date)

  • 1.1.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
160
increased by1677.78%
Maintainers
0
Weekly downloads
 
Created
Source

omni-file

npm package Last Update License

Omni-file is a comprehensive file information extractor that provides language, icon, type, and MIME information for any file based on its name or extension. With over 1000 icons, support for more than 700 programming languages, and only 79.4 kB gzipped, it's both powerful and lightweight!

It leverages GitHub Linguist data and Material Icon Theme for up-to-date and accurate file type detection and icons. The data is regularly updated to ensure the latest language and icon information.

Table of Contents

Features

  • Lightweight: only 79.4 kB gzipped
  • Over 1000 icons covering a wide range of file types, extensions, and folder names, using Material Icon Theme
  • Accurate language detection for over 700 programming languages, using GitHub Linguist
  • Comprehensive file information including type, color, aliases, and more
  • Smart extraction of filename or extension
  • Always up-to-date through automated processes to ensure the latest data

Installation

Install omni-file from npm:

npm install omni-file
pnpm add omni-file
yarn add omni-file

API

Core functions:

  • getLanguage(filename: string): Language
  • getIcon(filename: string, options?: IconOptions): string

Utility functions:

  • getBaseFilenameFromRelativePath(filePath: string): string
  • getExtensionsFromRelativePath(filePath: string): string[]

Type definitions:

  • LanguageData: Detailed information about a programming language

  • LanguageWithIconsData: Extends LanguageData with icon information

  • IconOptions: Options for icon retrieval (isFolder, isExpanded, isLight)

  • LanguagesJSON: A record of language names to LanguageData objects

  • LanguagesWithIconsJSON: A record of language names to LanguageWithIconsData objects

  • ExtensionMapJSON: Maps file extensions to language names

  • FileNamesMapJSON: Maps specific filenames to language names

  • IconsJSON: Defines icon associations for files and folders

Raw data:

  • languages: A map of all languages with icons
  • extensionMap: Maps file extensions to language names
  • fileNamesMap: Maps specific filenames to language names
  • icons: Default icon set (dark theme)
  • iconsLight: Light theme icon set (has less icons then dark theme)

Examples

Example with language

import { getLanguage } from "omni-file";

console.log(getLanguage("folder/example.ts"));

Output:

{
  "type": "programming",
  "color": "#3178c6",
  "aliases": ["ts"],
  "interpreters": ["deno", "ts-node", "tsx"],
  "extensions": [".ts", ".cts", ".mts"],
  "tm_scope": "source.ts",
  "ace_mode": "typescript",
  "codemirror_mode": "javascript",
  "codemirror_mime_type": "application/typescript",
  "language_id": 378,
  "name": "TypeScript",
  "icons": ["typescript", "javascript"]
}

Note that you can also get icons from getLanguage by using getLanguage(filename)?.icons but the answer may be less accurate. (folder icons are not included this way)

Examples of getIcon

import { getIcon } from "omni-file";

console.log(getIcon("example.js")); // Output: 'javascript'
console.log(getIcon("styles.css")); // Output: 'css'
console.log(getIcon("data.json")); // Output: 'json'
console.log(getIcon("package.json")); // Output: 'nodejs'
console.log(getIcon("Dockerfile")); // Output: 'docker'
console.log(getIcon(".gitignore")); // Output: 'git'
console.log(getIcon("src", { isFolder: true })); // Output: 'folder-src'
console.log(getIcon("node_modules", { isFolder: true })); // Output: 'folder-node'
console.log(getIcon("tests", { isFolder: true, isExpanded: true })); // Output: 'folder-test-open'
console.log(getIcon("vercel.json", { isLight: true })); // Output: 'vercel_light'
console.log(getIcon("example.js", { isLight: true })); // Output: 'javascript_light'
console.log(getIcon("example.test.js")); // Output: 'test-js'
console.log(getIcon("component.spec.ts")); // Output: 'test-ts'
console.log(getIcon(path.join("src", "components", "Button.tsx"))); // Output: 'react_ts'
console.log(getIcon(path.join("tests", "unit", "utils.test.js"))); // Output: 'test-js'
console.log(getIcon(".eslintrc.json")); // Output: 'eslint'
console.log(getIcon(".prettierrc")); // Output: 'prettier'
console.log(getIcon("tsconfig.json")); // Output: 'tsconfig'
console.log(getIcon("README.md")); // Output: 'readme'
console.log(getIcon("readme.md")); // Output: 'readme'
console.log(getIcon("DockerFile")); // Output: 'docker'
console.log(getIcon("dockerfile")); // Output: 'docker'

Example implementation of getIcon

import { getIcon } from "omni-file";

const icon = getIcon("folder/example.ts"); // "typescript"
const svgUrl = `node_modules/omni-file/icons/${icon}.svg`;

<img src={svgUrl} alt={icon} />;

Output:

TypeScript

Example using raw data

import { languages, icons } from "omni-file";

console.log(languages["JavaScript"].extensions); // -> [ ".js",".cjs",".es",".jsm", ... ]
console.log(icons.fileExtensions["js"]); // -> "javascript"

Icon Themes

Omni-file provides two icon sets:

  1. Dark Theme: Default icon set for dark-themed environments.
  2. Light Theme: Optimized icon set for light-themed environments. Note that this set has fewer icons then the dark theme but the function will fallback to the dark theme if the light themed icon is not found.

You can switch between these themes based on your application's needs:

import { getIcon, IconTheme } from "omni-file";

const darkIcon = getIcon("example.ts", { isLight: false });
const lightIcon = getIcon("example.ts", { isLight: true });

Development

To set up the project for development:

  1. Clone the repository
  2. Install dependencies:
    pnpm install
    
  3. Run the data update script:
    pnpm run update-data
    
  4. Build the project:
    pnpm run build
    
  5. Run tests:
    pnpm test
    

package.json scripts

  • build: Builds the project using tsup
  • build:watch: Watches for changes and rebuilds
  • test: Runs the test suite
  • test:coverage: Runs tests with coverage reporting
  • update-data: Updates the Linguist data
  • postinstall: Automatically updates data after installation

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Keywords

FAQs

Package last updated on 04 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc