Skip to content

Commit

Permalink
feat: remove all TypeScript references when isTypeScript is passed …
Browse files Browse the repository at this point in the history
…as `false` to `remix.init`
  • Loading branch information
MichaelDeBoey committed May 3, 2022
1 parent dfbb1d2 commit b73f09b
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 746 deletions.
5 changes: 3 additions & 2 deletions cypress/support/create-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
// and it will log out the cookie value you can use to interact with the server
// as that new user.

import { parse } from "cookie";
import { installGlobals } from "@remix-run/node/globals";
import { createUserSession } from "~/session.server";
import { parse } from "cookie";

import { createUser } from "~/models/user.server";
import { createUserSession } from "~/session.server";

installGlobals();

Expand Down
1 change: 1 addition & 0 deletions cypress/support/delete-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// and that user will get deleted

import { installGlobals } from "@remix-run/node/globals";

import { prisma } from "~/db.server";

installGlobals();
Expand Down
90 changes: 90 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"tsconfig-paths": "^4.0.0",
"typescript": "^4.6.4",
"vite": "^2.9.7",
"vite-jsconfig-paths": "^2.0.1",
"vite-tsconfig-paths": "^3.4.1",
"vitest": "^0.10.2"
},
Expand Down
154 changes: 132 additions & 22 deletions remix.init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,87 @@ const { execSync } = require("child_process");
const crypto = require("crypto");
const fs = require("fs/promises");
const path = require("path");
const inquirer = require("inquirer");

const toml = require("@iarna/toml");
const sort = require("sort-package-json");
const PackageJson = require("@npmcli/package-json");
const inquirer = require("inquirer");

const cleanupCypressFiles = (filesEntries) =>
filesEntries.flatMap(([filePath, content]) => {
const newContent = content
.replace(
"npx ts-node --require tsconfig-paths/register ./cypress/support/create-user.ts",
"node --require tsconfig-paths/register ./cypress/support/create-user.js"
)
.replace(
"npx ts-node --require tsconfig-paths/register ./cypress/support/delete-user.ts",
"node --require tsconfig-paths/register ./cypress/support/delete-user.js"
);

return [fs.writeFile(filePath, newContent)];
});

const cleanupJSConfig = (jsConfig, jsConfigPath) => {
const newJSConfig = jsConfig.replace(
'"include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"]',
'"include": ["**/*.js", "**/*.jsx"]'
);

return [fs.writeFile(jsConfigPath, newJSConfig)];
};

function escapeRegExp(string) {
const escapeRegExp = (string) =>
// $& means the whole matched string
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");

function getRandomString(length) {
return crypto.randomBytes(length).toString("hex");
}
const getRandomString = (length) => crypto.randomBytes(length).toString("hex");

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

const replaceMocksIndex = (rootDirectory) => {
const REMIX_INIT_DIR = path.join(rootDirectory, "remix.init");

const MOCKS_INDEX_NAME = "mocks.js";
const MOCKS_INDEX_PATH = path.join(rootDirectory, "mocks", "index.js");

return [fs.cp(path.join(REMIX_INIT_DIR, MOCKS_INDEX_NAME), MOCKS_INDEX_PATH)];
};

async function main({ rootDirectory }) {
const replaceVitestConfig = (rootDirectory) => {
const REMIX_INIT_DIR = path.join(rootDirectory, "remix.init");

// We renamed this during `create-remix`.
// Instead of reading this file and changing it,
// we have a copy of it in this directory already set up for JavaScript
const VITEST_CONFIG_NAME = "vitest.config.js";
const VITEST_CONFIG_PATH = path.join(rootDirectory, VITEST_CONFIG_NAME);

return [
fs.cp(path.join(REMIX_INIT_DIR, VITEST_CONFIG_NAME), VITEST_CONFIG_PATH),
];
};

async function main({ isTypeScript, rootDirectory }) {
const README_PATH = path.join(rootDirectory, "README.md");
const FLY_TOML_PATH = path.join(rootDirectory, "fly.toml");
const EXAMPLE_ENV_PATH = path.join(rootDirectory, ".env.example");
const ENV_PATH = path.join(rootDirectory, ".env");
const PACKAGE_JSON_PATH = path.join(rootDirectory, "package.json");
const JSCONFIG_PATH = path.join(rootDirectory, "jsconfig.json"); // We renamed this during `create-remix`
const CYPRESS_SUPPORT_PATH = path.join(rootDirectory, "cypress", "support");
const CYPRESS_COMMANDS_PATH = path.join(CYPRESS_SUPPORT_PATH, "commands.js"); // We renamed this during `create-remix`
const CREATE_USER_COMMAND_PATH = path.join(
CYPRESS_SUPPORT_PATH,
"create-user.js"
); // We renamed this during `create-remix`
const DELETE_USER_COMMAND_PATH = path.join(
CYPRESS_SUPPORT_PATH,
"delete-user.js"
); // We renamed this during `create-remix`

const REPLACER = "indie-stack-template";

Expand All @@ -32,11 +93,30 @@ async function main({ rootDirectory }) {
// get rid of anything that's not allowed in an app name
.replace(/[^a-zA-Z0-9-_]/g, "-");

const [prodContent, readme, env, packageJson] = await Promise.all([
const [
prodContent,
readme,
env,
jsConfig,
cypressCommands,
createUserCommand,
deleteUserCommand,
packageJson,
] = await Promise.all([
fs.readFile(FLY_TOML_PATH, "utf-8"),
fs.readFile(README_PATH, "utf-8"),
fs.readFile(EXAMPLE_ENV_PATH, "utf-8"),
fs.readFile(PACKAGE_JSON_PATH, "utf-8"),
isTypeScript ? Promise.resolve() : fs.readFile(JSCONFIG_PATH, "utf-8"),
isTypeScript
? Promise.resolve()
: fs.readFile(CYPRESS_COMMANDS_PATH, "utf-8"),
isTypeScript
? Promise.resolve()
: fs.readFile(CREATE_USER_COMMAND_PATH, "utf-8"),
isTypeScript
? Promise.resolve()
: fs.readFile(DELETE_USER_COMMAND_PATH, "utf-8"),
PackageJson.load(rootDirectory),
fs.rm(path.join(rootDirectory, ".github/ISSUE_TEMPLATE"), {
recursive: true,
}),
Expand All @@ -56,19 +136,49 @@ async function main({ rootDirectory }) {
APP_NAME
);

const newPackageJson =
JSON.stringify(
sort({ ...JSON.parse(packageJson), name: APP_NAME }),
null,
2
) + "\n";

await Promise.all([
const {
devDependencies,
prisma,
scripts: { typecheck, validate, ...scripts },
} = packageJson.content;
packageJson.update({
name: APP_NAME,
devDependencies: isTypeScript
? removeUnusedDependencies(devDependencies, ["vite-jsconfig-paths"])
: removeUnusedDependencies(devDependencies, [
"ts-node",
"vite-tsconfig-paths",
]),
prisma: isTypeScript ? prisma : { seed: "node prisma/seed.js" },
scripts: isTypeScript
? { ...scripts, typecheck, validate }
: { ...scripts, validate: 'run-p "test -- --run" lint test:e2e:run' },
});

const fileOperationPromises = [
fs.writeFile(FLY_TOML_PATH, toml.stringify(prodToml)),
fs.writeFile(README_PATH, newReadme),
fs.writeFile(ENV_PATH, newEnv),
fs.writeFile(PACKAGE_JSON_PATH, newPackageJson),
]);
packageJson.save(),
];

if (!isTypeScript) {
fileOperationPromises.push(
...cleanupCypressFiles([
[CYPRESS_COMMANDS_PATH, cypressCommands],
[CREATE_USER_COMMAND_PATH, createUserCommand],
[DELETE_USER_COMMAND_PATH, deleteUserCommand],
])
);

fileOperationPromises.push(...cleanupJSConfig(jsConfig, JSCONFIG_PATH));

fileOperationPromises.push(...replaceMocksIndex(rootDirectory));

fileOperationPromises.push(...replaceVitestConfig(rootDirectory));
}

await Promise.all(fileOperationPromises);

execSync(`npm run setup`, { stdio: "inherit", cwd: rootDirectory });

Expand Down
2 changes: 2 additions & 0 deletions remix.init/mocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require("tsconfig-paths/register");
require("./start");

0 comments on commit b73f09b

Please sign in to comment.