Skip to content

Commit

Permalink
Merge pull request #102 from basehub-ai/jb/make-dotbasehub-be-the-out…
Browse files Browse the repository at this point in the history
…put-by-default

default to .basehub
  • Loading branch information
julianbenegas authored May 19, 2024
2 parents 0ac701f + f912c74 commit 27eab62
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ yarn-error.log*

.env
.vercel

# BaseHub
.basehub
38 changes: 38 additions & 0 deletions packages/basehub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
# basehub

## 5.0.0

### Major Changes

- 335ae9a:

## Context

Before this version, the default output path of the generated code `basehub` yielded was within the basehub node_module itself. This allowed a great starting point, as developers could instantly `import { basehub } from 'basehub'` and it would just work.

But, generating the SDK and placing it in `node_modules` was problematic, because the TS server is generally not watching `node_modules`. Because of this, users were required to "Restart TS Server" every time they run the SDK generator. Although this is not a big deal once you get accustomed to it, it's very problematic for folks that don't know about this trick, and about the reason of why this doesn't work. So, in other words, this was shit.

Because of this, we introduced the `--output` arg. With this argument, the developer could specify a custom output. One that their TS server would watch and therefore would know about when it changed. This worked great.

After we introduced and started using `--watch`, the path forward became clear: it was preferrable to run `basehub --output .basehub` by default rather than start fast but then need to transition into a custom output because the default flow just wouldn't scale well.

## Breaking Change

So this version introduces a single, potentially breaking, change: we now output to `.basehub` by default. This means, if you have a regular Next.js App, you'll now see a `.basehub` directory next to your `.next`, `node_modules`, etc.

### Will I need to change all my imports?

No. This release includes a clever (hacky, maybe) trick: when you run `basehub`, we'll automatically alias the package that's stored in `node_modules` to target the new output path, and so the `import {} from 'basehub'` should work out of the box.

### Does .basehub need to be .gitignored?

Yes. And we'll try to automatically do that for you if we find your .gitignore file.

***

Please let us know if you encounter any issues.

## 5.0.0-canary.0

### Major Changes

- Default to outputting stuff into .basehub and aliasing basehub to it.

## 4.0.17

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/basehub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "basehub",
"description": "The first AI-native content hub.",
"author": "JB <[email protected]>",
"version": "4.0.17",
"version": "5.0.0",
"license": "MIT",
"repository": "basehub-ai/basehub",
"bugs": "https://github.com/basehub-ai/basehub/issues",
Expand Down
91 changes: 86 additions & 5 deletions packages/basehub/src/bin/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,18 @@ export const main = async (
);
}

const pathArgs = output
? [output]
: ["node_modules", "basehub", "dist", "generated-client"]; // default output path
const isCustomOutput = !!output;
let shouldAppendToGitIgnore = "";
let pathArgs: string[] = [];
if (output === "node_modules") {
// old default
pathArgs = ["node_modules", "basehub", "dist", "generated-client"];
} else if (output) {
pathArgs = [output];
} else {
// default
pathArgs = [".basehub"];
shouldAppendToGitIgnore = ".basehub";
}

const basehubOutputPath = path.resolve(process.cwd(), ...pathArgs);

Expand Down Expand Up @@ -188,7 +196,7 @@ export const main = async (

appendGeneratedCodeBanner(basehubOutputPath, args["--banner"]);

if (isCustomOutput) {
if (output !== "node_modules") {
// alias react-rich-text and other packages to the generated client for better import experience
["react-rich-text", "api-transaction", "react-search"].map(
(pathsToAlias) => {
Expand All @@ -203,6 +211,79 @@ export const main = async (
);
}
);

// override index.js and index.d.ts to point to the generated client
const indexJsPath = path.join(basehubModulePath, "index.js");
const indexDtsPath = path.join(basehubModulePath, "index.d.ts");
const reactPumpIndexJsPath = path.join(
basehubModulePath,
"react-pump.js"
);
const reactPumpIndexDtsPath = path.join(
basehubModulePath,
"react-pump.d.ts"
);
fs.writeFileSync(
indexJsPath,
`module.exports = require("${path.relative(
basehubModulePath,
generatedMainExportPath
)}");`
);
fs.writeFileSync(
indexDtsPath,
`export * from "${path.relative(
basehubModulePath,
generatedMainExportPath
)}";`
);
fs.writeFileSync(
reactPumpIndexJsPath,
`module.exports = require("${path.relative(
basehubModulePath,
path.join(reactPumpOutDir, "index.js")
)}");`
);
fs.writeFileSync(
reactPumpIndexDtsPath,
`export * from "${path.relative(
basehubModulePath,
path.join(reactPumpOutDir, "index.d.ts")
)}";`
);
}

if (shouldAppendToGitIgnore) {
function findClosestGitignore(startDir: string) {
let currentDir = startDir;
let gitignorePath = null;

while (currentDir !== "/") {
const potentialGitignore = path.join(currentDir, ".gitignore");
if (fs.existsSync(potentialGitignore)) {
gitignorePath = potentialGitignore;
break;
}
currentDir = path.dirname(currentDir);
}

return gitignorePath;
}

const gitIgnorePath = findClosestGitignore(process.cwd());
if (gitIgnorePath && fs.existsSync(gitIgnorePath)) {
const gitIgnoreContents = fs.readFileSync(gitIgnorePath, "utf-8");
if (!gitIgnoreContents.includes(shouldAppendToGitIgnore)) {
fs.appendFileSync(
gitIgnorePath,
`\n\n# BaseHub\n${shouldAppendToGitIgnore}`
);
logIfNotSilent(
silent,
`🤫 Added "${shouldAppendToGitIgnore}" .gitignore`
);
}
}
}

logIfNotSilent(silent, "🪄 Generated `basehub` client");
Expand Down
14 changes: 14 additions & 0 deletions playground/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# playground

## 0.0.88

### Patch Changes

- Updated dependencies [335ae9a]
- [email protected]

## 0.0.88-canary.0

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.87

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions playground/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "playground",
"private": true,
"version": "0.0.87",
"version": "0.0.88",
"scripts": {
"dev": "basehub dev & next dev",
"build": "next build",
"build": "basehub & next build",
"build:analyze": "cross-env ANALYZE=true pnpm build",
"start": "next start",
"lint": "next lint",
Expand Down
2 changes: 1 addition & 1 deletion playground/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Search } from "./search";

export default function HomePage() {
export default async function HomePage() {
return (
<main className="">
<Search />
Expand Down

0 comments on commit 27eab62

Please sign in to comment.