remove import alias and src folder rewrite

This commit is contained in:
Marcus Schiesser
2023-10-26 16:49:26 +07:00
parent 5713716916
commit ea6010a2e3
4 changed files with 1 additions and 159 deletions
-6
View File
@@ -16,15 +16,11 @@ export async function createApp({
appPath,
packageManager,
eslint,
srcDir,
importAlias,
}: {
framework: TemplateFramework;
appPath: string;
packageManager: PackageManager;
eslint: boolean;
srcDir: boolean;
importAlias: string;
}): Promise<void> {
const template: TemplateType = "simple";
@@ -67,8 +63,6 @@ export async function createApp({
packageManager,
isOnline,
eslint,
srcDir,
importAlias,
});
if (tryGitInit(root)) {
-44
View File
@@ -179,10 +179,6 @@ async function run(): Promise<void> {
const defaults: typeof preferences = {
framework: "nextjs",
eslint: true,
app: true,
srcDir: false,
importAlias: "@/*",
customizeImportAlias: false,
};
const getPrefOrDefault = (field: string) =>
preferences[field] ?? defaults[field];
@@ -236,51 +232,11 @@ async function run(): Promise<void> {
}
}
if (typeof program.importAlias !== "string" || !program.importAlias.length) {
if (ciInfo.isCI) {
// We don't use preferences here because the default value is @/* regardless of existing preferences
program.importAlias = defaults.importAlias;
} else {
const styledImportAlias = blue("import alias");
const { customizeImportAlias } = await prompts({
onState: onPromptState,
type: "toggle",
name: "customizeImportAlias",
message: `Would you like to customize the default ${styledImportAlias} (${defaults.importAlias})?`,
initial: getPrefOrDefault("customizeImportAlias"),
active: "Yes",
inactive: "No",
});
if (!customizeImportAlias) {
// We don't use preferences here because the default value is @/* regardless of existing preferences
program.importAlias = defaults.importAlias;
} else {
const { importAlias } = await prompts({
onState: onPromptState,
type: "text",
name: "importAlias",
message: `What ${styledImportAlias} would you like configured?`,
initial: getPrefOrDefault("importAlias"),
validate: (value) =>
/.+\/\*/.test(value)
? true
: "Import alias must follow the pattern <prefix>/*",
});
program.importAlias = importAlias;
preferences.importAlias = importAlias;
}
}
}
await createApp({
framework: program.framework,
appPath: resolvedProjectPath,
packageManager,
eslint: program.eslint,
srcDir: program.srcDir,
importAlias: program.importAlias,
});
conf.set("preferences", preferences);
}
+1 -101
View File
@@ -1,29 +1,13 @@
import { copy } from "../helpers/copy";
import { install } from "../helpers/install";
import { makeDir } from "../helpers/make-dir";
import { Sema } from "async-sema";
import { async as glob } from "fast-glob";
import fs from "fs/promises";
import os from "os";
import path from "path";
import { bold, cyan } from "picocolors";
import { version } from "../package.json";
import { GetTemplateFileArgs, InstallTemplateArgs } from "./types";
/**
* Get the file path for a given file in a template, e.g. "next.config.js".
*/
export const getTemplateFile = ({
template,
framework,
file,
}: GetTemplateFileArgs): string => {
return path.join(__dirname, template, framework, file);
};
export const SRC_DIR_NAMES = ["app", "pages", "styles"];
import { InstallTemplateArgs } from "./types";
/**
* Install a LlamaIndex internal template to a given `root` directory.
@@ -36,8 +20,6 @@ export const installTemplate = async ({
template,
framework,
eslint,
srcDir,
importAlias,
}: InstallTemplateArgs) => {
console.log(bold(`Using ${packageManager}.`));
@@ -70,88 +52,6 @@ export const installTemplate = async ({
},
});
const tsconfigFile = path.join(root, "tsconfig.json");
await fs.writeFile(
tsconfigFile,
(await fs.readFile(tsconfigFile, "utf8"))
.replace(
`"@/*": ["./*"]`,
srcDir ? `"@/*": ["./src/*"]` : `"@/*": ["./*"]`,
)
.replace(`"@/*":`, `"${importAlias}":`),
);
// update import alias in any files if not using the default
if (importAlias !== "@/*") {
const files = await glob("**/*", {
cwd: root,
dot: true,
stats: false,
});
const writeSema = new Sema(8, { capacity: files.length });
await Promise.all(
files.map(async (file) => {
// We don't want to modify compiler options in [ts/js]config.json
if (file === "tsconfig.json" || file === "jsconfig.json") return;
await writeSema.acquire();
const filePath = path.join(root, file);
if ((await fs.stat(filePath)).isFile()) {
await fs.writeFile(
filePath,
(await fs.readFile(filePath, "utf8")).replace(
`@/`,
`${importAlias.replace(/\*/g, "")}`,
),
);
}
await writeSema.release();
}),
);
}
if (srcDir) {
await makeDir(path.join(root, "src"));
await Promise.all(
SRC_DIR_NAMES.map(async (file) => {
await fs
.rename(path.join(root, file), path.join(root, "src", file))
.catch((err) => {
if (err.code !== "ENOENT") {
throw err;
}
});
}),
);
const isAppTemplate = template.startsWith("app");
// Change the `Get started by editing pages/index` / `app/page` to include `src`
const indexPageFile = path.join(
"src",
isAppTemplate ? "app" : "pages",
`${isAppTemplate ? "page" : "index"}.tsx`,
);
await fs.writeFile(
indexPageFile,
(await fs.readFile(indexPageFile, "utf8")).replace(
isAppTemplate ? "app/page" : "pages/index",
isAppTemplate ? "src/app/page" : "src/pages/index",
),
);
if (framework === "nextjs") {
const tailwindConfigFile = path.join(root, "tailwind.config.ts");
await fs.writeFile(
tailwindConfigFile,
(await fs.readFile(tailwindConfigFile, "utf8")).replace(
/\.\/(\w+)\/\*\*\/\*\.\{js,ts,jsx,tsx,mdx\}/g,
"./src/$1/**/*.{js,ts,jsx,tsx,mdx}",
),
);
}
}
/**
* Update the package.json scripts.
*/
-8
View File
@@ -3,12 +3,6 @@ import { PackageManager } from "../helpers/get-pkg-manager";
export type TemplateType = "simple";
export type TemplateFramework = "nextjs" | "express";
export interface GetTemplateFileArgs {
template: TemplateType;
framework: TemplateFramework;
file: string;
}
export interface InstallTemplateArgs {
appName: string;
root: string;
@@ -17,6 +11,4 @@ export interface InstallTemplateArgs {
template: TemplateType;
framework: TemplateFramework;
eslint: boolean;
srcDir: boolean;
importAlias: string;
}