-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add filetype detection package. (#6103)
- Loading branch information
Showing
31 changed files
with
926 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Change Log | ||
|
||
All notable changes to this project will be documented in this file. | ||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Jason Dent | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# `@cspell/filetypes` | ||
|
||
A library to help determine the type of a file. | ||
|
||
## Install | ||
|
||
```sh | ||
npm install -S @cspell/filetypes | ||
``` | ||
|
||
## Usage | ||
|
||
```ts | ||
import { findMatchingFileTypes } from '@cspell/filetypes'; | ||
|
||
console.log(findMatchingFileTypes('code.js')); // outputs: [ 'javascript' ] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"name": "@cspell/filetypes", | ||
"version": "8.13.3", | ||
"description": "Library to determine file types based upon the file name.", | ||
"keywords": [ | ||
"cspell", | ||
"filetypes" | ||
], | ||
"author": "Jason Dent <jason@streetsidesoftware.nl>", | ||
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-filetypes#readme", | ||
"license": "MIT", | ||
"type": "module", | ||
"sideEffects": false, | ||
"exports": { | ||
".": "./dist/index.js" | ||
}, | ||
"directories": { | ||
"dist": "dist" | ||
}, | ||
"typings": "dist/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"!**/*.tsbuildInfo", | ||
"!**/__mocks__", | ||
"!**/*.test.*", | ||
"!**/*.spec.*", | ||
"!**/*.map" | ||
], | ||
"scripts": { | ||
"build": "tsc -p .", | ||
"watch": "tsc -p . -w", | ||
"clean": "shx rm -rf dist temp coverage \"*.tsbuildInfo\"", | ||
"clean-build": "pnpm run clean && pnpm run build", | ||
"sort-filetypes": "pnpm build && node scripts/sortFileTypes.js && prettier --write src/definitions.ts", | ||
"coverage": "vitest run --coverage", | ||
"test-watch": "vitest", | ||
"test": "vitest run" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/streetsidesoftware/cspell.git", | ||
"directory": "packages/cspell-filetypes" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/streetsidesoftware/cspell/labels/filetype" | ||
}, | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"dependencies": {} | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/cspell-filetypes/scripts/extract-filetypes-from-package.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { readFile } from 'node:fs/promises'; | ||
|
||
async function processFiles(files) { | ||
const defs = []; | ||
|
||
for (const file of files) { | ||
const content = await readFile(file, 'utf8'); | ||
const pkg = JSON.parse(content); | ||
|
||
const languages = pkg.contributes?.languages; | ||
if (!languages?.length) { | ||
continue; | ||
} | ||
|
||
for (const def of languages) { | ||
if (!def.id) { | ||
continue; | ||
} | ||
const filenames = [...(def.filenamePatterns || []), ...(def.filenames || [])]; | ||
defs.push({ | ||
id: def.id, | ||
extensions: def.extensions || [], | ||
filenames: filenames.length ? filenames : undefined, | ||
}); | ||
} | ||
} | ||
|
||
return defs; | ||
} | ||
|
||
const packageFiles = process.argv.slice(2); | ||
|
||
const defs = await processFiles(packageFiles); | ||
|
||
console.log(JSON.stringify(defs, undefined, 2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { readFile, writeFile } from 'node:fs/promises'; | ||
|
||
import { definitions } from '../dist/definitions.js'; | ||
|
||
const urlFile = new URL('../src/definitions.ts', import.meta.url); | ||
|
||
/** | ||
* @typedef {import('../dist/types.js').FileTypeDefinition} FileTypeDefinition | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {FileTypeDefinition} a | ||
* @param {FileTypeDefinition} b | ||
*/ | ||
function compare(a, b) { | ||
if (a.format !== b.format) { | ||
return !a.format || a.format === 'Text' ? -1 : 1; | ||
} | ||
return a.id.localeCompare(b.id); | ||
} | ||
|
||
const fieldOrder = ['id', 'extensions', 'filenames', 'format', 'description', 'comment']; | ||
|
||
/** | ||
* | ||
* @param {FileTypeDefinition} def | ||
* @returns {FileTypeDefinition} | ||
*/ | ||
function normalizeDef(def) { | ||
const entries = Object.entries(def).sort(([a], [b]) => fieldOrder.indexOf(a) - fieldOrder.indexOf(b)); | ||
def = Object.fromEntries(entries); | ||
def.extensions = [...new Set(def.extensions.map((a) => (a.startsWith('.') ? a : '.' + a)).sort())]; | ||
def.filenames = def.filenames ? [...new Set(def.filenames.sort())] : undefined; | ||
return def; | ||
} | ||
|
||
const defs = dedupe(definitions).sort(compare).map(normalizeDef); | ||
|
||
async function updateFile() { | ||
const content = await readFile(urlFile, 'utf8'); | ||
const newLines = `export const definitions: FileTypeDefinitions = ${JSON.stringify(defs)};\n`; | ||
const start = content.indexOf('export const definitions:'); | ||
const end = content.indexOf('];\n', start); | ||
const output = content.slice(0, start) + newLines + content.slice(end + 3); | ||
await writeFile(urlFile, output, 'utf8'); | ||
} | ||
|
||
/** | ||
* | ||
* @param {FileTypeDefinition[]} defs | ||
* @returns {FileTypeDefinition[]} | ||
*/ | ||
function dedupe(defs) { | ||
/** @type {Map<string, FileTypeDefinition>} */ | ||
const map = new Map(); | ||
for (const def of defs) { | ||
const key = def.id; | ||
const existing = map.get(key); | ||
if (!existing) { | ||
map.set(key, def); | ||
} else { | ||
existing.extensions = [...existing.extensions, ...def.extensions]; | ||
existing.filenames = [...(existing.filenames || []), ...(def.filenames || [])]; | ||
if (!existing.filenames.length) { | ||
delete existing.filenames; | ||
} | ||
existing.format ??= def.format; | ||
existing.description ??= def.description; | ||
existing.comment ??= def.comment; | ||
} | ||
} | ||
return [...map.values()]; | ||
} | ||
|
||
updateFile(); |
Oops, something went wrong.