Skip to content

Commit

Permalink
chore(reveal): only support entry.client or entry.server, use flag fo…
Browse files Browse the repository at this point in the history
…r no ts; attempt to create both entries if no entry supplied

Signed-off-by: Logan McAnsh <logan@mcan.sh>
  • Loading branch information
mcansh committed Feb 10, 2023
1 parent 33d797b commit 282ce35
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
77 changes: 51 additions & 26 deletions packages/remix-dev/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export async function codemod(

let clientEntries = ["entry.client.tsx", "entry.client.js", "entry.client.jsx"];
let serverEntries = ["entry.server.tsx", "entry.server.js", "entry.server.jsx"];
let entries = [...clientEntries, ...serverEntries];
let entries = ["entry.client", "entry.server"];

// @ts-expect-error available in node 12+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/ListFormat#browser_compatibility
Expand All @@ -262,9 +262,20 @@ let listFormat = new Intl.ListFormat("en", {
type: "conjunction",
});

export async function generateEntry(entry: string, remixRoot: string) {
export async function generateEntry(
entry: string,
remixRoot: string,
useTypeScript: boolean = true
) {
let config = await readConfig(remixRoot);

// if no entry passed, attempt to create both
if (!entry) {
await generateEntry("entry.client", remixRoot, useTypeScript);
await generateEntry("entry.server", remixRoot, useTypeScript);
return;
}

if (!entries.includes(entry)) {
let entriesArray = Array.from(entries);
let list = listFormat.format(entriesArray);
Expand Down Expand Up @@ -322,17 +333,25 @@ export async function generateEntry(entry: string, remixRoot: string) {
`entry.server.${serverRuntime}.tsx`
);

let isServerEntry = entry.startsWith("entry.server.");
let isServerEntry = entry === "entry.server";

let contents = isServerEntry
? await createServerEntry(config.appDirectory, defaultEntryServer)
: await createClientEntry(config.appDirectory, defaultEntryClient);
? await createServerEntry(
config.rootDirectory,
config.appDirectory,
defaultEntryServer
)
: await createClientEntry(
config.rootDirectory,
config.appDirectory,
defaultEntryClient
);

let outputFile = path.resolve(config.appDirectory, entry);
let outputExtension = useTypeScript ? "tsx" : "jsx";
let outputEntry = `${entry}.${outputExtension}`;
let outputFile = path.resolve(config.appDirectory, outputEntry);

// 3. if entry is js/jsx, convert to js
// otherwise, copy the entry file from the defaults
if (/\.jsx?$/.test(entry)) {
if (!useTypeScript) {
let javascript = convertFileToJS(contents, {
cwd: config.rootDirectory,
filename: isServerEntry ? defaultEntryServer : defaultEntryClient,
Expand All @@ -352,32 +371,38 @@ export async function generateEntry(entry: string, remixRoot: string) {
);
}

async function checkForEntry(appDirectory: string, entries: string[]) {
for (let entryToCheck of entries) {
let entryPath = path.resolve(appDirectory, entryToCheck);
let entryExists = await fse.pathExists(entryPath);
if (entryExists) {
console.error(
colors.error(
`Entry file ${path.relative(
appDirectory,
entryToCheck
)} already exists.`
)
);
async function checkForEntry(
rootDirectory: string,
appDirectory: string,
entries: string[]
) {
for (let entry of entries) {
let entryPath = path.resolve(appDirectory, entry);
let exists = await fse.pathExists(entryPath);
if (exists) {
let relative = path.relative(rootDirectory, entryPath);
console.error(colors.error(`Entry file ${relative} already exists.`));
return process.exit(1);
}
}
}

async function createServerEntry(appDirectory: string, inputFile: string) {
await checkForEntry(appDirectory, serverEntries);
async function createServerEntry(
rootDirectory: string,
appDirectory: string,
inputFile: string
) {
await checkForEntry(rootDirectory, appDirectory, serverEntries);
let contents = await fse.readFile(inputFile, "utf-8");
return contents;
}

async function createClientEntry(appDirectory: string, inputFile: string) {
await checkForEntry(appDirectory, clientEntries);
async function createClientEntry(
rootDirectory: string,
appDirectory: string,
inputFile: string
) {
await checkForEntry(rootDirectory, appDirectory, clientEntries);
let contents = await fse.readFile(inputFile, "utf-8");
return contents;
}
9 changes: 8 additions & 1 deletion packages/remix-dev/cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ ${colors.logoBlue("R")} ${colors.logoGreen("E")} ${colors.logoYellow(
$ remix routes
$ remix routes my-app
$ remix routes --json
${colors.heading("Reveal the used entry point")}:
$ remix reveal entry.client
$ remix reveal entry.server
$ remix reveal entry.client --no-typescript
$ remix reveal entry.server --no-typescript
`;

const templateChoices = [
Expand Down Expand Up @@ -483,7 +490,7 @@ export async function run(argv: string[] = process.argv.slice(2)) {
}
case "reveal": {
// TODO: simplify getting started guide
await commands.generateEntry(input[1], input[2]);
await commands.generateEntry(input[1], input[2], flags.typescript);
break;
}
case "dev":
Expand Down

0 comments on commit 282ce35

Please sign in to comment.