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 Apr 26, 2022
1 parent c8fc6a0 commit 027fef5
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 778 deletions.
98 changes: 70 additions & 28 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": "^3.14.1",
"typescript": "^4.6.3",
"vite": "^2.9.6",
"vite-jsconfig-paths": "^2.0.1",
"vite-tsconfig-paths": "^3.4.1",
"vitest": "^0.10.0"
},
Expand Down
93 changes: 71 additions & 22 deletions remix.init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,56 @@ 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 cleanupJSConfig = (jsConfig, jsConfigPath) => {
const newJSConfig =
JSON.stringify({ ...jsConfig, include: ["**/*.js", "**/*.jsx"] }, null, 2) +
"\n";

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)
)
);

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.rm(VITEST_CONFIG_NAME),
fs.rename(
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 REPLACER = "indie-stack-template";

Expand All @@ -32,15 +62,17 @@ 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, 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"),
fs.readFile(JSCONFIG_PATH, "utf-8"),
PackageJson.load(rootDirectory),
fs.rm(path.join(rootDirectory, ".github/ISSUE_TEMPLATE"), {
recursive: true,
}),
fs.rm(path.join(rootDirectory, ".github/PULL_REQUEST_TEMPLATE.md")),
fs.rm(path.join(rootDirectory, "remix.env.d.ts")),
]);

const newEnv = env.replace(
Expand All @@ -56,19 +88,36 @@ 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 } = packageJson.content;
packageJson.update({
name: APP_NAME,
devDependencies: isTypeScript
? removeUnusedDependencies(devDependencies, ["vite-jsconfig-paths"])
: removeUnusedDependencies(devDependencies, ["vite-tsconfig-paths"]),
prisma: isTypeScript ? prisma : { seed: "node prisma/seed.js" },
scripts: isTypeScript
? scripts
: {
...scripts,
typecheck: undefined,
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(...cleanupJSConfig(jsConfig, JSCONFIG_PATH));

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

await Promise.all(fileOperationPromises);

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

Expand Down

0 comments on commit 027fef5

Please sign in to comment.