Skip to content

Commit

Permalink
improve no-js and other package managers
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Jul 15, 2022
1 parent 1176673 commit f1faada
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ FROM base as deps

WORKDIR /myapp

ADD package.json package-lock.json ./
ADD package.json ./
RUN npm install --production=false

# Setup production node_modules
Expand All @@ -21,7 +21,7 @@ FROM base as production-deps
WORKDIR /myapp

COPY --from=deps /myapp/node_modules /myapp/node_modules
ADD package.json package-lock.json ./
ADD package.json ./
RUN npm prune --production

# Build the app
Expand Down
68 changes: 56 additions & 12 deletions remix.init/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { execSync } = require("child_process");
const crypto = require("crypto");
const fs = require("fs/promises");
const path = require("path");

const toml = require("@iarna/toml");
const YAML = require("yaml");
const sort = require("sort-package-json");

function escapeRegExp(string) {
Expand All @@ -14,12 +16,17 @@ function getRandomString(length) {
return crypto.randomBytes(length).toString("hex");
}

async function main({ rootDirectory }) {
async function main({ rootDirectory, packageManager, isTypeScript }) {
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 DEPLOY_YAML_PATH = path.join(
rootDirectory,
".github/workflows/deploy.yml"
);
const DOCKERFILE_PATH = path.join(rootDirectory, "Dockerfile");

const REPLACER = "blues-stack-template";

Expand All @@ -30,12 +37,15 @@ 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([
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"),
]);
const [prodContent, readme, env, packageJson, deployConfig, dockerfile] =
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").then((s) => JSON.parse(s)),
fs.readFile(DEPLOY_YAML_PATH, "utf-8").then((s) => YAML.parse(s)),
fs.readFile(DOCKERFILE_PATH, "utf-8"),
]);

const newEnv = env.replace(
/^SESSION_SECRET=.*$/m,
Expand All @@ -50,18 +60,45 @@ async function main({ rootDirectory }) {
APP_NAME
);

let saveDeploy = null;
if (!isTypeScript) {
delete packageJson.scripts.typecheck;
packageJson.scripts.validate = packageJson.scripts.validate.replace(
" typecheck",
""
);

delete deployConfig.jobs.typecheck;
deployConfig.jobs.deploy.needs = deployConfig.jobs.deploy.needs.filter(
(n) => n !== "typecheck"
);
// only write the deploy config if it's changed
saveDeploy = fs.writeFile(DEPLOY_YAML_PATH, YAML.stringify(deployConfig));
}

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

const lockfile = {
npm: "package-lock.json",
yarn: "yarn.lock",
pnpm: "pnpm-lock.yaml",
}[packageManager];

const newDockerfile = lockfile
? dockerfile.replace(
new RegExp(escapeRegExp("ADD package.json"), "g"),
`ADD package.json ${lockfile}`
)
: dockerfile;

await Promise.all([
fs.writeFile(FLY_TOML_PATH, toml.stringify(prodToml)),
fs.writeFile(README_PATH, newReadme),
fs.writeFile(ENV_PATH, newEnv),
fs.writeFile(PACKAGE_JSON_PATH, newPackageJson),
fs.writeFile(DOCKERFILE_PATH, newDockerfile),
saveDeploy,
fs.copyFile(
path.join(rootDirectory, "remix.init", "gitignore"),
path.join(rootDirectory, ".gitignore")
Expand All @@ -72,6 +109,13 @@ async function main({ rootDirectory }) {
fs.rm(path.join(rootDirectory, ".github/PULL_REQUEST_TEMPLATE.md")),
]);

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

execSync("npm run format -- --loglevel warn", {
stdio: "inherit",
cwd: rootDirectory,
});

console.log(
`
Setup is almost complete. Follow these steps to finish initialization:
Expand Down
3 changes: 2 additions & 1 deletion remix.init/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"license": "MIT",
"dependencies": {
"@iarna/toml": "^2.2.5",
"sort-package-json": "^1.57.0"
"sort-package-json": "^1.57.0",
"yaml": "^2.1.1"
}
}

0 comments on commit f1faada

Please sign in to comment.