From eb58f53db25170010cc664f7206d8c4a4d83abb4 Mon Sep 17 00:00:00 2001 From: Freddy L Date: Mon, 25 Sep 2023 21:58:35 -0400 Subject: [PATCH] refactor: changed entry to mjs --- ...oints.script.ts => entrypoints.script.mts} | 2 +- packages/animelist-auth-next/package.json | 2 +- .../entrypoints.script.mts | 4 +- ...oints.script.ts => entrypoints.script.mts} | 2 +- packages/animelist-auth/package.json | 2 +- scripts/cleanUpEntrypoints.ts | 2 +- ...entrypoints.module.mts => entrypoints.mts} | 0 scripts/entrypoints.ts | 91 ------------------- 8 files changed, 7 insertions(+), 98 deletions(-) rename packages/animelist-auth-next/{entrypoints.script.ts => entrypoints.script.mts} (70%) rename packages/animelist-auth/{entrypoints.script.ts => entrypoints.script.mts} (70%) rename scripts/{entrypoints.module.mts => entrypoints.mts} (100%) delete mode 100644 scripts/entrypoints.ts diff --git a/packages/animelist-auth-next/entrypoints.script.ts b/packages/animelist-auth-next/entrypoints.script.mts similarity index 70% rename from packages/animelist-auth-next/entrypoints.script.ts rename to packages/animelist-auth-next/entrypoints.script.mts index 61fd503..049659b 100644 --- a/packages/animelist-auth-next/entrypoints.script.ts +++ b/packages/animelist-auth-next/entrypoints.script.mts @@ -1,4 +1,4 @@ -import { generateEntrypoints } from "../../scripts/entrypoints.js"; +import { generateEntrypoints } from "../../scripts/entrypoints.mjs"; import path from "path"; import { fileURLToPath } from "url"; diff --git a/packages/animelist-auth-next/package.json b/packages/animelist-auth-next/package.json index 7bed2f2..423d958 100644 --- a/packages/animelist-auth-next/package.json +++ b/packages/animelist-auth-next/package.json @@ -8,7 +8,7 @@ "scripts": { "typecheck": "tsc --noEmit", "lint": "eslint ./src", - "entrypoints": "tsx entrypoints.script.ts", + "entrypoints": "tsx entrypoints.script.mts", "build:types": "tsc --emitDeclarationOnly", "build:lib": "tsx esbuild.mts", "build": "rimraf dist && pnpm build:types && pnpm build:lib && pnpm entrypoints" diff --git a/packages/animelist-auth-sveltekit/entrypoints.script.mts b/packages/animelist-auth-sveltekit/entrypoints.script.mts index a397d42..049659b 100644 --- a/packages/animelist-auth-sveltekit/entrypoints.script.mts +++ b/packages/animelist-auth-sveltekit/entrypoints.script.mts @@ -1,6 +1,6 @@ -import { generateEntrypoints } from "../../scripts/entrypoints.module.mjs"; +import { generateEntrypoints } from "../../scripts/entrypoints.mjs"; import path from "path"; import { fileURLToPath } from "url"; const packageDir = path.dirname(fileURLToPath(import.meta.url)); -generateEntrypoints({ packageDir, format: 'esm' }) \ No newline at end of file +generateEntrypoints({ packageDir }) \ No newline at end of file diff --git a/packages/animelist-auth/entrypoints.script.ts b/packages/animelist-auth/entrypoints.script.mts similarity index 70% rename from packages/animelist-auth/entrypoints.script.ts rename to packages/animelist-auth/entrypoints.script.mts index 61fd503..049659b 100644 --- a/packages/animelist-auth/entrypoints.script.ts +++ b/packages/animelist-auth/entrypoints.script.mts @@ -1,4 +1,4 @@ -import { generateEntrypoints } from "../../scripts/entrypoints.js"; +import { generateEntrypoints } from "../../scripts/entrypoints.mjs"; import path from "path"; import { fileURLToPath } from "url"; diff --git a/packages/animelist-auth/package.json b/packages/animelist-auth/package.json index 73e5c74..f232073 100644 --- a/packages/animelist-auth/package.json +++ b/packages/animelist-auth/package.json @@ -10,7 +10,7 @@ "test": "vitest run", "test:watch": "vitest", "lint": "eslint ./src", - "entrypoints": "tsx entrypoints.script.ts", + "entrypoints": "tsx entrypoints.script.mts", "build:types": "tsc --emitDeclarationOnly", "build:lib": "tsx esbuild.mts", "build": "rimraf dist && pnpm build:types && pnpm build:lib && pnpm entrypoints" diff --git a/scripts/cleanUpEntrypoints.ts b/scripts/cleanUpEntrypoints.ts index 085dc76..e9a857d 100644 --- a/scripts/cleanUpEntrypoints.ts +++ b/scripts/cleanUpEntrypoints.ts @@ -1,7 +1,7 @@ import * as glob from "glob"; import fse from 'fs-extra'; import path from "path"; -import { ENTRYPOINT_MARKER } from "./entrypoints.js"; +import { ENTRYPOINT_MARKER } from "./entrypoints.mjs"; function main() { console.log("Removing entry points...") diff --git a/scripts/entrypoints.module.mts b/scripts/entrypoints.mts similarity index 100% rename from scripts/entrypoints.module.mts rename to scripts/entrypoints.mts diff --git a/scripts/entrypoints.ts b/scripts/entrypoints.ts deleted file mode 100644 index 4709229..0000000 --- a/scripts/entrypoints.ts +++ /dev/null @@ -1,91 +0,0 @@ -import path from 'path'; -import fse from 'fs-extra'; -import * as glob from 'glob'; - -export const ENTRYPOINT_MARKER = ".entrypoint"; - -// eslint-disable-next-line @typescript-eslint/no-unused-vars -function writeFileSync(filePath: string, contents: string) { - const dir = path.dirname(filePath); - fse.ensureDirSync(dir); - - fse.writeFileSync(filePath, contents) -} - -type GenerateEntryPointsOptions = { - packageDir: string; - additionalInputs?: string[]; -} - -export function generateEntrypoints({ packageDir, additionalInputs }: GenerateEntryPointsOptions) { - const srcDir = path.resolve(packageDir, 'src'); - - if (!fse.existsSync(srcDir)) { - throw new Error(`'src' directory does not exist on ${srcDir}`); - } - - cleanUpEntrypoints(packageDir); - - const additionalFiles = (additionalInputs || []).map(file => path.resolve(packageDir, file)); - - const indexFiles = [ - ...glob.sync(`${srcDir}/**/index.ts`), - ...additionalFiles - ]; - - for (const indexFile of indexFiles) { - const relativePath = path.dirname(path.relative(srcDir, indexFile)); - - // Not need to include the files in the root, those are already included - if (relativePath === ".") { - continue; - } - - const importDepth = relativePath.split(path.sep).length || 1; - const resolvedImport = path.join( - ...Array(importDepth).fill('..'), - "dist", - relativePath, - ) - - const resolvedModuleExport = path.join(resolvedImport, "index.js").replace(/\\/g, '/'); - const resolvedTypesExport = path.join(resolvedImport, "index.d.ts").replace(/\\/g, '/'); - const commonJsExport = `module.exports = require("${resolvedModuleExport}")`; - const esmExport = `export * from "${resolvedModuleExport}"`.replace(".js", ".mjs"); - const typesExport = `export * from "${resolvedTypesExport}"` - - const commonJsEntryFilePath = path.join(packageDir, relativePath, "index.js"); - const esmEntryFilePath = path.join(packageDir, relativePath, "index.mjs"); - const typeEntryPointFilePath = path.join(packageDir, relativePath, "index.d.ts"); - const gitignoreFilePath = path.join(packageDir, relativePath, ".gitignore"); - const entryPointMarkerFilePath = path.join(packageDir, relativePath, ".entrypoint"); - - writeFileSync(commonJsEntryFilePath, commonJsExport); - writeFileSync(esmEntryFilePath, esmExport); - writeFileSync(typeEntryPointFilePath, typesExport); - writeFileSync(gitignoreFilePath, "*"); - writeFileSync(entryPointMarkerFilePath, new Date().toUTCString()); - - console.log({ - commonJsExport, - commonJsEntryFilePath, - esmExport, - esmEntryFilePath, - typesExport, - typeEntryPointFilePath - }) - } -} - -export function cleanUpEntrypoints(packageDir: string) { - const entryPointMarkers = glob.sync(`${packageDir}/**/${ENTRYPOINT_MARKER}`); - - for (const entryPointFile of entryPointMarkers) { - if (!fse.existsSync(entryPointFile)) { - continue; - } - - const dir = path.dirname(entryPointFile); - fse.removeSync(dir); - } -} \ No newline at end of file