npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

omni-file

v1.2.1

Published

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

Downloads

281

Readme

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

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.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 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)

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