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

feat: optimize commands for Bun #218

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function login({
} = {}) {
cy.then(() => ({ email })).as("user");
cy.exec(
`npx ts-node --require tsconfig-paths/register ./cypress/support/create-user.ts "${email}"`,
`npx ts-node -r tsconfig-paths/register ./cypress/support/create-user.ts "${email}"`,
).then(({ stdout }) => {
const cookieValue = stdout
.replace(/.*<cookie>(?<cookieValue>.*)<\/cookie>.*/s, "$<cookieValue>")
Expand All @@ -76,7 +76,7 @@ function cleanupUser({ email }: { email?: string } = {}) {

function deleteUserByEmail(email: string) {
cy.exec(
`npx ts-node --require tsconfig-paths/register ./cypress/support/delete-user.ts "${email}"`,
`npx ts-node -r tsconfig-paths/register ./cypress/support/delete-user.ts "${email}"`,
);
cy.clearCookie("__session");
}
Expand Down
2 changes: 1 addition & 1 deletion cypress/support/create-user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Use this to create a new user and login with that user
// Simply call this with:
// npx ts-node --require tsconfig-paths/register ./cypress/support/create-user.ts [email protected]
// npx ts-node -r tsconfig-paths/register ./cypress/support/create-user.ts [email protected]
// and it will log out the cookie value you can use to interact with the server
// as that new user.

Expand Down
2 changes: 1 addition & 1 deletion cypress/support/delete-user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Use this to delete a user by their email
// Simply call this with:
// npx ts-node --require tsconfig-paths/register ./cypress/support/delete-user.ts [email protected]
// npx ts-node -r tsconfig-paths/register ./cypress/support/delete-user.ts [email protected]
// and that user will get deleted

import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@
"node": ">=18.0.0"
},
"prisma": {
"seed": "ts-node --require tsconfig-paths/register prisma/seed.ts"
"seed": "ts-node -r tsconfig-paths/register prisma/seed.ts"
}
}
30 changes: 27 additions & 3 deletions remix.init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const cleanupCypressFiles = ({ fileEntries, packageManager }) =>
fileEntries.flatMap(([filePath, content]) => {
const newContent = content.replace(
new RegExp("npx ts-node", "g"),
`${packageManager.exec} ts-node`,
packageManager.name === "bun" ? "bun" : `${packageManager.exec} ts-node`,
);

return [fs.writeFile(filePath, newContent)];
Expand All @@ -27,11 +27,13 @@ const getPackageManagerCommand = (packageManager) =>
bun: () => ({
exec: "bunx",
lockfile: "bun.lockb",
name: "bun",
run: (script, args) => `bun run ${script} ${args || ""}`,
}),
npm: () => ({
exec: "npx",
lockfile: "package-lock.json",
name: "npm",
run: (script, args) => `npm run ${script} ${args ? `-- ${args}` : ""}`,
}),
pnpm: () => {
Expand All @@ -42,6 +44,7 @@ const getPackageManagerCommand = (packageManager) =>
return {
exec: useExec ? "pnpm exec" : "pnpx",
lockfile: "pnpm-lock.yaml",
name: "pnpm",
run: (script, args) =>
includeDoubleDashBeforeArgs
? `pnpm run ${script} ${args ? `-- ${args}` : ""}`
Expand All @@ -51,6 +54,7 @@ const getPackageManagerCommand = (packageManager) =>
yarn: () => ({
exec: "yarn",
lockfile: "yarn.lock",
name: "yarn",
run: (script, args) => `yarn ${script} ${args || ""}`,
}),
})[packageManager]();
Expand All @@ -61,8 +65,17 @@ const getPackageManagerVersion = (packageManager) =>

const getRandomString = (length) => crypto.randomBytes(length).toString("hex");

const updatePackageJson = ({ APP_NAME, packageJson }) => {
const removeUnusedDependencies = (dependencies, unusedDependencies) =>
Object.fromEntries(
Object.entries(dependencies).filter(
([key]) => !unusedDependencies.includes(key),
),
);

const updatePackageJson = ({ APP_NAME, packageJson, packageManager }) => {
const {
devDependencies,
prisma: { seed: prismaSeed, ...prisma },
scripts: {
// eslint-disable-next-line no-unused-vars
"format:repo": _repoFormatScript,
Expand All @@ -72,6 +85,17 @@ const updatePackageJson = ({ APP_NAME, packageJson }) => {

packageJson.update({
name: APP_NAME,
devDependencies:
packageManager.name === "bun"
? removeUnusedDependencies(devDependencies, ["ts-node"])
: devDependencies,
prisma: {
...prisma,
seed:
packageManager.name === "bun"
? prismaSeed.replace("ts-node", "bun")
: prismaSeed,
},
scripts,
});
};
Expand Down Expand Up @@ -154,7 +178,7 @@ const main = async ({ packageManager, rootDirectory }) => {
)
: dockerfile;

updatePackageJson({ APP_NAME, packageJson });
updatePackageJson({ APP_NAME, packageJson, packageManager: pm });

await Promise.all([
fs.writeFile(FLY_TOML_PATH, toml.stringify(prodToml)),
Expand Down