diff --git a/.gitignore b/.gitignore index a5d66db..1e99656 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /main.js *.swp /.idea +/src/manifests diff --git a/package.json b/package.json index bcb7ea4..7ce3c08 100644 --- a/package.json +++ b/package.json @@ -6,11 +6,11 @@ "scripts": { "build": "npm run clean && npm run build:types", "build:types": "tsc --noEmit", - "build:update-manifest-paths": "node scripts/update-manifests-dist-path.js", + "build:manifests-to-ts": "node scripts/manifests-to-ts.js", "clean": "node -e \"fs.rmSync('./dist', {force: true, recursive: true})\"", "format": "prettier --write \"./src/**/*.ts\" \"./manifests/**/*.json\"", "generate-schema": "node scripts/generate-schema.js", - "prepare": "tshy && npm run build:update-manifest-paths", + "prepare": "npm run build:manifests-to-ts && tshy", "test:validate": "node scripts/validate-modules.js", "sort-manifests": "node scripts/sort-manifests.js" }, diff --git a/scripts/manifests-to-ts.js b/scripts/manifests-to-ts.js new file mode 100644 index 0000000..eda2a40 --- /dev/null +++ b/scripts/manifests-to-ts.js @@ -0,0 +1,30 @@ +import {mkdir, readFile, readdir, rm, writeFile} from 'node:fs/promises'; +import {fileURLToPath} from 'node:url'; +import {existsSync} from 'node:fs'; +import * as path from 'node:path'; + +const scriptDir = fileURLToPath(new URL('.', import.meta.url)); +const tsManifestsPath = path.resolve(scriptDir, '../src/manifests'); +const jsonManifestsPath = path.resolve(scriptDir, '../manifests'); + +if (existsSync(tsManifestsPath)) { + await rm(tsManifestsPath, {recursive: true}); +} + +await mkdir(tsManifestsPath, {recursive: true}); + +const manifests = await readdir(jsonManifestsPath, {withFileTypes: true}); + +for (const manifest of manifests) { + if (manifest.isFile() && path.extname(manifest.name) === '.json') { + const content = await readFile( + path.join(jsonManifestsPath, manifest.name), + 'utf8' + ); + + await writeFile( + path.join(tsManifestsPath, manifest.name.replace(/\.json$/, '.ts')), + `import {ManifestModule} from '../types.js';\n\nconst manifest: ManifestModule = ${content.trim()};\nexport default manifest;\n` + ); + } +} diff --git a/scripts/update-manifests-dist-path.js b/scripts/update-manifests-dist-path.js deleted file mode 100644 index 630064f..0000000 --- a/scripts/update-manifests-dist-path.js +++ /dev/null @@ -1,16 +0,0 @@ -import {readFile, writeFile} from 'node:fs/promises'; -import {fileURLToPath} from 'node:url'; -import * as path from 'node:path'; - -const scriptDir = fileURLToPath(new URL('.', import.meta.url)); -const distPath = path.resolve(scriptDir, '../dist'); -const flavours = ['esm', 'commonjs']; - -for (const flavour of flavours) { - const flavourPath = `${distPath}/${flavour}/manifests-dir.js`; - const contents = await readFile(flavourPath, 'utf8'); - await writeFile( - flavourPath, - contents.replaceAll('../manifests', '../../manifests') - ); -} diff --git a/src/main.ts b/src/main.ts index e5ff9c1..fd036f4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,16 +1,8 @@ -import {readFileSync} from 'node:fs'; import {ManifestModule} from './types.js'; -import {manifestsDir} from './manifests-dir.js'; -const nativeReplacements = JSON.parse( - readFileSync(`${manifestsDir}/native.json`, 'utf8') -) as ManifestModule; -const microUtilsReplacements = JSON.parse( - readFileSync(`${manifestsDir}/micro-utilities.json`, 'utf8') -) as ManifestModule; -const preferredReplacements = JSON.parse( - readFileSync(`${manifestsDir}/preferred.json`, 'utf8') -) as ManifestModule; +import microUtilsReplacements from './manifests/micro-utilities.js'; +import preferredReplacements from './manifests/preferred.js'; +import nativeReplacements from './manifests/native.js'; export * from './types.js'; diff --git a/src/manifests-dir-cjs.cts b/src/manifests-dir-cjs.cts deleted file mode 100644 index 370721c..0000000 --- a/src/manifests-dir-cjs.cts +++ /dev/null @@ -1,4 +0,0 @@ -import * as path from 'node:path'; - -const currentDir = __dirname; -export const manifestsDir = path.resolve(currentDir, '../manifests'); diff --git a/src/manifests-dir.ts b/src/manifests-dir.ts deleted file mode 100644 index a8ae3ed..0000000 --- a/src/manifests-dir.ts +++ /dev/null @@ -1,6 +0,0 @@ -import {fileURLToPath} from 'node:url'; -import * as path from 'node:path'; - -// @ts-ignore filthy cjs/esm polyfill hacks so we can have import.meta -const currentDir = fileURLToPath(new URL('.', import.meta.url)); -export const manifestsDir = path.resolve(currentDir, '../manifests');