diff --git a/.gitignore b/.gitignore
index 1ff9c6b..9f2d90d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,4 +6,4 @@ node_modules
/index.js
/index.mjs
-/src/index.js
+/src/index.ts
diff --git a/bin/index.js b/bin/index.ts
similarity index 58%
rename from bin/index.js
rename to bin/index.ts
index 938a52c..863500e 100644
--- a/bin/index.js
+++ b/bin/index.ts
@@ -1,22 +1,30 @@
// @ts-check
-const fs = require('fs');
-const { join, dirname } = require('path');
-const DB = require('mime-db');
+///
+import { join, dirname, resolve } from 'node:path';
+import { spawnSync } from 'node:child_process';
+import * as fs from 'node:fs';
+import DB from 'mime-db';
-const input = join(__dirname, '../src/$index.js');
-const output = join(__dirname, '../src/index.js');
-const denomod = join(__dirname, '../deno/mod.ts');
+const ROOT = resolve('.');
+
+const input = join(ROOT, 'src/$index.ts');
+const denomod = join(ROOT, 'deno/mod.ts');
+
+function write(file: string, data: string) {
+ let f = resolve(ROOT, file);
+ fs.writeFileSync(f, data);
+ console.log('~> "%s" created', file);
+}
// iana > mimedb > apache > nginx
// https://github.com/jshttp/mime-types/blob/master/index.js#L156
-const scores = { nginx: 1, apache: 2, iana: 4 };
-
-/**
- * @param {string} prev
- * @param {string} next
- * @return {string}
- */
-function compare(prev, next) {
+const SOURCES = {
+ nginx: 1,
+ apache: 2,
+ iana: 4,
+};
+
+function compare(prev: string, next: string): string {
let [p1] = prev.split('/', 1);
let [p2] = next.split('/', 1);
@@ -28,8 +36,8 @@ function compare(prev, next) {
if (p2 === 'application') return next;
}
// compare sources
- let s1 = scores[DB[prev].source] || 3;
- let s2 = scores[DB[next].source] || 3;
+ let s1 = SOURCES[DB[prev].source] || 3;
+ let s2 = SOURCES[DB[next].source] || 3;
if (s1 !== s2) return s2 > s1 ? next : prev;
return prev.length >= next.length ? next : prev;
}
@@ -37,8 +45,8 @@ function compare(prev, next) {
let dict = {};
let ignore = /[/](x-|vnd\.)/;
-let mtype, arr;
-let i=0, extn, prev;
+let mtype: string, arr: string[];
+let i=0, extn: string, prev: string;
for (mtype in DB) {
if (ignore.test(mtype)) continue;
@@ -50,7 +58,7 @@ for (mtype in DB) {
extn = arr[i];
prev = dict[extn];
- if (prev) {
+ if (prev && prev !== mtype) {
let msg = `Found existing "${extn}" value:`;
msg += `\n (prev) "${prev}"`;
msg += `\n (next) "${mtype}"`;
@@ -65,10 +73,14 @@ for (mtype in DB) {
let content = fs.readFileSync(input, 'utf8').replace(
'{}', JSON.stringify(dict, null, 2)
-);
+) + '\n';
+
+let esm = content + 'export { mimes, lookup };\n';
+let cjs = content + 'exports.mimes = mimes;\nexports.lookup = lookup;\n';
-fs.writeFileSync(output, content);
-console.log('~> "src/index.js" created');
+// build exports
+write('index.mjs', esm);
+write('index.js', cjs);
let denodir = dirname(denomod);
fs.existsSync(denodir) || fs.mkdirSync(denodir);
@@ -76,20 +88,22 @@ fs.existsSync(denodir) || fs.mkdirSync(denodir);
fs.copyFileSync('readme.md', join(denodir, 'readme.md'));
console.log('\n~> "deno/readme.md" created');
-fs.writeFileSync(denomod, content.replace(
+write('deno/mod.ts', esm.replace(
'function lookup(extn) {',
'function lookup(extn: string): string | undefined {',
).replace(
'const mimes = {',
'const mimes: Record = {',
));
-console.log('~> "deno/mod.ts" created');
if (!process.env.CI) {
try {
- require('child_process').spawnSync('deno', ['fmt', denomod]);
+ spawnSync('deno', ['fmt', denomod], { cwd: ROOT });
console.log('\n~> $ deno fmt "deno/mod.ts"');
} catch (err) {
console.log('[deno]', err.stack);
}
}
+
+fs.copyFileSync(denomod, 'src/index.ts');
+console.log('\n~> "src/index.ts" created');
diff --git a/bin/package.json b/bin/package.json
index b37628c..d4df2ed 100644
--- a/bin/package.json
+++ b/bin/package.json
@@ -1,6 +1,7 @@
{
"private": true,
"devDependencies": {
+ "@types/node": "20.10.5",
"mime-db": "1.49.0"
}
}
diff --git a/deno/mod.ts b/deno/mod.ts
index 445dc6c..fe953ea 100644
--- a/deno/mod.ts
+++ b/deno/mod.ts
@@ -1,4 +1,4 @@
-export const mimes: Record = {
+const mimes: Record = {
"ez": "application/andrew-inset",
"aw": "application/applixware",
"atom": "application/atom+xml",
@@ -407,8 +407,10 @@ export const mimes: Record = {
"webm": "video/webm",
};
-export function lookup(extn: string): string | undefined {
+function lookup(extn: string): string | undefined {
let tmp = ("" + extn).trim().toLowerCase();
let idx = tmp.lastIndexOf(".");
return mimes[!~idx ? tmp : tmp.substring(++idx)];
}
+
+export { lookup, mimes };
diff --git a/package.json b/package.json
index 3204e56..4531636 100644
--- a/package.json
+++ b/package.json
@@ -29,8 +29,8 @@
"node": ">=10"
},
"scripts": {
- "build": "node bin && bundt",
- "test": "uvu -r esm test"
+ "build": "tsm bin/index.ts",
+ "test": "uvu -r tsm test"
},
"keywords": [
"mime",
@@ -38,8 +38,7 @@
"mimetype"
],
"devDependencies": {
- "bundt": "1.1.1",
- "esm": "3.2.25",
+ "tsm": "2.3.0",
"uvu": "0.5.2"
}
}
diff --git a/src/$index.js b/src/$index.ts
similarity index 70%
rename from src/$index.js
rename to src/$index.ts
index e9f7193..9a76cae 100644
--- a/src/$index.js
+++ b/src/$index.ts
@@ -1,6 +1,6 @@
-export const mimes = {};
+const mimes = {};
-export function lookup(extn) {
+function lookup(extn) {
let tmp = ('' + extn).trim().toLowerCase();
let idx = tmp.lastIndexOf('.');
return mimes[!~idx ? tmp : tmp.substring(++idx)];
diff --git a/test/index.js b/test/index.ts
similarity index 100%
rename from test/index.js
rename to test/index.ts