Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add folder/package.json entrypoints #178

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# For this repo, let's not all share our convex projects
convex.json
# Login info for npm publishing
.npmrc

# Or our local env
.env.local
Expand Down
26 changes: 26 additions & 0 deletions delete-entrypoints.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from "node:fs";
import path from "node:path";
import {
__dirname,
entryPointFiles,
entryPointFromFile,
} from "./generate-utils.mjs";

function createEntrypoints() {
for (const entryPointFile of entryPointFiles()) {
const entryPoint = entryPointFromFile(entryPointFile);
if (entryPoint === ".") continue;

const entryPointPath = path.join(__dirname, entryPoint);
const packagePath = path.join(entryPointPath, "package.json");
if (fs.existsSync(packagePath)) {
fs.rmSync(packagePath);
}
const fileName = path.parse(entryPointFile).name;
if (fileName !== "index" && fs.existsSync(entryPointPath)) {
fs.rmdirSync(entryPointPath);
}
}
}

createEntrypoints();
46 changes: 46 additions & 0 deletions generate-entrypoints.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fs from "node:fs";
import path from "node:path";
import {
__dirname,
entryPointFiles,
entryPointFromFile,
} from "./generate-utils.mjs";

function createEntrypoints() {
for (const entryPointFile of entryPointFiles()) {
const entryPoint = entryPointFromFile(entryPointFile);
if (entryPoint === ".") continue;
const entryPointPath = path.join(__dirname, entryPoint);
if (!fs.existsSync(entryPointPath)) {
// make directory
fs.mkdirSync(entryPointPath, { recursive: true });
}
const extensionless = path.join(
path.parse(entryPointFile).dir,
path.parse(entryPointFile).name,
);
const packagePath = path.join(entryPointPath, "package.json");
const parts = entryPoint.split("/");
const dist = path.join(
parts
.slice(1)
.map(() => "..")
.join("/"),
"dist",
);
fs.writeFileSync(
packagePath,
JSON.stringify(
{
type: "module",
module: path.join(dist, `${extensionless}.js`),
types: path.join(dist, `${extensionless}.d.ts`),
},
null,
2,
),
);
}
}

createEntrypoints();
46 changes: 46 additions & 0 deletions generate-exports.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
import {
__dirname,
entryPointFiles,
entryPointFromFile,
} from "./generate-utils.mjs";

function generateExport(source) {
let extensionless = path.join(
path.parse(source).dir,
path.parse(source).name,
);

return {
types: `./dist/${extensionless}.d.ts`,
default: `./dist/${extensionless}.js`,
};
}

function generateExports() {
const obj = {};
for (const entryPoint of entryPointFiles()) {
obj[entryPointFromFile(entryPoint)] = generateExport(entryPoint);
}
return obj;
}

function generatePackageExports() {
const packageJson = JSON.parse(
fs.readFileSync(path.join(__dirname, "package.json")),
);
const actual = packageJson.exports;
const expected = generateExports();
if (JSON.stringify(actual) !== JSON.stringify(expected)) {
packageJson.exports = expected;
fs.writeFileSync(
path.join(__dirname, "package.json"),
JSON.stringify(packageJson, null, 2) + "\n",
);
process.exit(1);
}
}

generatePackageExports();
42 changes: 42 additions & 0 deletions generate-utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

export const __dirname = path.join(
path.dirname(fileURLToPath(new URL(import.meta.url))),
"packages",
"convex-helpers",
);

function directoryContents(dirname) {
return fs
.readdirSync(path.join(__dirname, dirname), { recursive: true })
.filter((filename) => filename.endsWith(".ts") || filename.endsWith(".tsx"))
.filter((filename) => !filename.includes(".test"))
.filter((filename) => !filename.includes("_generated"))
.map((filename) => path.join(dirname, filename));
}

export function entryPointFiles() {
return [
"./index.ts",
"./testing.ts",
"./validators.ts",
...directoryContents("react"),
...directoryContents("server"),
];
}

export function entryPointFromFile(source) {
let entryPoint = path.join(path.parse(source).dir, path.parse(source).name);

if (path.parse(source).name === "index") {
entryPoint = path.parse(source).dir;
}

if (!entryPoint.startsWith(".")) {
entryPoint = `./${entryPoint}`;
}

return entryPoint;
}
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@
"dev:helpers": "cd packages/convex-helpers && chokidar '*.ts' 'server/**/*.ts' 'react/**/*.ts*' 'tsconfig*.json' 'package.json' -c 'npm run build' --initial",
"predev": "cd packages/convex-helpers && npm run build",
"lint": "tsc --project tsconfig.test.json",
"build": "cd packages/convex-helpers && node generate-exports.mjs && mkdir -p dist && cp -r *.ts server react ./package.json ./tsconfig.json ./README.md ../../LICENSE ./.npmignore dist/ && rm dist/server/_generated/_ignore.ts && cd dist/ && tsc",
"build": "node generate-exports.mjs && cd packages/convex-helpers && tsc",
"clean": "rm -rf packages/convex-helpers/dist",
"test": "vitest run",
"test:watch": "vitest",
"test:debug": "vitest --inspect-brk --no-file-parallelism",
"test:coverage": "vitest run --coverage --coverage.reporter=text",
"testFunctionsExistingBackend": "just convex deploy && just convex env set IS_TEST true && vitest --run convex/example.test.ts",
"testFunctions": "node backendHarness.js 'npm run testFunctionsExistingBackend'",
"arethetypeswrong": "cd packges/convex-helpers/dist && attw $(npm pack)"
"arethetypeswrong": "cd packges/convex-helpers && attw $(npm pack)",
"publish": "./publish.sh"
},
"dependencies": {
"classnames": "^2.3.2",
"convex": "^1.13.0",
"convex-helpers": "file:packages/convex-helpers/dist",
"convex-helpers": "file:packages/convex-helpers",
"hono": "^4.3.6",
"react": "^18.0.0",
"react-dom": "^18.0.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/convex-helpers/.npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules
package-lock.json
generate-exports.mjs
vitest.config.mts
*.tgz
*.tsbuildinfo
89 changes: 0 additions & 89 deletions packages/convex-helpers/generate-exports.mjs

This file was deleted.

Loading