Skip to content

Commit

Permalink
fix: prettify all files
Browse files Browse the repository at this point in the history
  • Loading branch information
OrJDev committed Apr 12, 2024
1 parent 59d32ce commit 4390dc9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-items-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-jd-app": patch
---

fix: prettify all files
16 changes: 8 additions & 8 deletions src/helpers/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { IExpectedPackages } from "./packages";
import type { TInstallers } from "~types";

export default async (
ctx: ICtx
ctx: ICtx,
): Promise<[Record<string, string>, IExpectedPackages, IEnv[], string[]]> => {
let normalDeps: IExpectedPackages[0] = {};
let devModeDeps: IExpectedPackages[1] = {};
Expand Down Expand Up @@ -40,7 +40,7 @@ export default async (
scripts = { ...scripts, ...cfg.scripts };
}
if (cfg.files?.length) {
await execFiles(cfg.files, ctx, cfg.ignorePrettier);
await execFiles(cfg.files, ctx);
}
if (cfg.commands) {
if (Array.isArray(cfg.commands)) {
Expand All @@ -59,9 +59,9 @@ export default async (
(installer: { default: IInstaller }) =>
typeof installer.default === "function"
? installer.default(ctx)
: installer.default
)
)
: installer.default,
),
),
);

console.log();
Expand All @@ -85,7 +85,7 @@ export default async (

export async function getCtxWithInstallers(
ctx: IAppCtx,
curr: string[]
curr: string[],
): Promise<ICtx> {
let installers: string[] = [];
let pkgs: TInstallers[] = [];
Expand All @@ -103,12 +103,12 @@ export async function getCtxWithInstallers(
console.log(
`${chalk.green("√")} Using installers: ${validInstallers
.map((installer) => chalk.blue(installer))
.join(", ")}`
.join(", ")}`,
);
}
if (!skip) {
let optInstallers = installers.filter(
(pkg) => !validInstallers.includes(pkg)
(pkg) => !validInstallers.includes(pkg),
);
const newPkgs = (
await inquirer.prompt<{ pkgs: TInstallers[] }>({
Expand Down
24 changes: 12 additions & 12 deletions src/installers/AuthJS/files/protected.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {protected$} from "@solid-mediakit/auth"
import { protectedQuery } from "~/server/user/user.queries"
import { protected$ } from "@solid-mediakit/auth";
import { protectedQuery } from "~/server/user/user.queries";

export default protected$(session$=>{
const res = protectedQuery(()=> ({hello: 'world'}))
return (
<div>
<h1>Protected Page</h1>
<p>Session: {JSON.stringify(session$)}</p>
<p>Response: {JSON.stringify(res.data)}</p>
</div>
)
})
export default protected$((session$) => {
const res = protectedQuery(() => ({ hello: "world" }));
return (
<div>
<h1>Protected Page</h1>
<p>Session: {JSON.stringify(session$)}</p>
<p>Response: {JSON.stringify(res.data)}</p>
</div>
);
});
2 changes: 1 addition & 1 deletion src/installers/Prisma/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const config: IInstaller = (ctx) => ({
path: `${__dirname}/utils/getSchema`,
type: "exec",
to: `${ctx.userDir}/prisma/schema.prisma`,
ignorePrettier: true,
},
{
path: `${__dirname}/files/client.txt`,
Expand Down Expand Up @@ -36,7 +37,6 @@ const config: IInstaller = (ctx) => ({
normal: "@prisma/client",
}),
commands: `${ctx.pkgManager === "npm" ? "npx" : "pnpm"} prisma db push`,
ignorePrettier: true
});

export default config;
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export type IConfig = {
scripts?: Record<string, string>;
env?: IEnv[];
commands?: string | string[];
ignorePrettier?: boolean;
};

type IInstallerCB = (ctx: ICtx) => IPromiseOrType<IConfig>;
Expand All @@ -52,6 +51,9 @@ export type IEnv = {
kind: "server" | "client";
};

export type IUtil<T = undefined> = (ctx: ICtx, passed?: T) => string | Promise<string>;
export type IUtil<T = undefined> = (
ctx: ICtx,
passed?: T,
) => string | Promise<string>;

export type TInstallers = "AuthJS" | "Prisma" | "TailwindCSS" | "pRPC";
26 changes: 16 additions & 10 deletions src/utils/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,47 @@ import fs from "fs-extra";
import ora from "ora";
import type { ICtx, IFile } from "~types";
import { formatError } from "./helpers";
import prettier from "prettier"
import prettier from "prettier";

export async function execFiles(files: (IFile | undefined)[], ctx: ICtx, ignorePrettier?:boolean) {
export async function execFiles(files: (IFile | undefined)[], ctx: ICtx) {
const actualFiles = files.filter((f) => f !== undefined) as IFile[];
// `sep` files are parent files, so they should be executed first ro resolve conflicts
for (const file of actualFiles.filter((e) => e.sep)) {
await execFile(file, ctx, ignorePrettier);
await execFile(file, ctx);
}
await Promise.all(
actualFiles
.filter((e) => !e.sep)
.map(async (file) => {
await execFile(file, ctx, ignorePrettier);
})
await execFile(file, ctx);
}),
);
}

async function execFile(file: IFile, ctx: ICtx, ignorePrettier?: boolean) {
async function execFile(file: IFile, ctx: ICtx) {
if (file.type && file.type !== "copy") {
if (file.type === "exec") {
if (!file.path) {
return;
}
const method = await import(file.path);
let code = await method.default(ctx, file.pass)
if(!ignorePrettier && !file.ignorePrettier) {
let code = await method.default(ctx, file.pass);
if (!file.ignorePrettier) {
code = await prettier.format(code, {
parser: "typescript",
});
}
await fs.outputFile(file.to,code);
await fs.outputFile(file.to, code);
} else if (file.type === "delete") {
await fs.remove(file.to);
} else if (file.type === "write") {
await fs.outputFile(file.to, file.content);
let code = file.content!;
if (!file.ignorePrettier) {
code = await prettier.format(code, {
parser: "typescript",
});
}
await fs.outputFile(file.to, code);
} else if (file.type === "append") {
await fs.appendFile(file.to, file.content);
}
Expand Down

0 comments on commit 4390dc9

Please sign in to comment.