Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ use template from local #51

Merged
merged 5 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ yarn-error.log*
# Misc
.DS_Store
*.pem

# Webstorm
.idea/
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ program
.arguments("<project-name>")
.description("Create a directory for your project files")
.option("-f, --force", "Overwrite target directory if it exists")

.option("--dev", "Use development mode")
.action((name, options) => {
createApp(name, options);
})
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/utils/createApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const getTableInfo = async () => {
};

// 模板创建主函数
export default async function createApp(matter: string, options: { force: boolean }) {
export default async function createApp(matter: string, options: { force: boolean; dev: boolean }) {
intro(chalk.green(" create-you-app "));
const rootDirectory = resolveApp(matter);

Expand All @@ -81,9 +81,8 @@ export default async function createApp(matter: string, options: { force: boolea

// 写入 .gitignore 文件
fs.writeFileSync(join(rootDirectory, ".gitignore"), createTemplateFile("gitignore"));

// 下载 npm 包解压,获取目标模板导入文件,并删除一些无用的代码文件
getNpmPackage(projectLink.get(projectType) as string, projectType, rootDirectory);
getNpmPackage(projectLink.get(projectType) as string, projectType, rootDirectory, options.dev);

// 注入 lint 规则
if (commitLint === true) {
Expand Down
34 changes: 32 additions & 2 deletions packages/core/src/utils/fileController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ora from "ora";
import fs from "fs-extra";
import tar from "tar";
import axios from "axios";
import { join } from "node:path";
import { join, resolve } from "node:path";

import { packageVersion } from "./constants";

Expand Down Expand Up @@ -50,13 +50,28 @@ export async function getNpmPackage(
packageURL: string,
packageName: string,
projectName: string,
isDev?: boolean | undefined,
): Promise<void> {
const spinner = ora(chalk.bold.cyan("Creating a project...")).start();
try {
const currentDir = resolveApp(projectName);
// 如果是dev mode,检查并使用本地模板
if (isDev) {
const root = resolve(__dirname, "../../../../apps/");
// 通过dist/index.js,找到模板文件的路径
const templateDir = resolve(
root,
"template-react-web-ts/laconic-template-react-web-ts-1.0.1.tgz",
);
const hasLocalTemplate = fs.existsSync(templateDir)
if (hasLocalTemplate) {
await getPackageFromLocal(currentDir, templateDir);
return;
}
}
const response = await axios.get(packageURL, {
responseType: "arraybuffer",
});
const currentDir = resolveApp(projectName);
const tgzPath = join(currentDir, `${packageName}-${packageVersion}.tgz`);
fs.writeFileSync(tgzPath, response.data);

Expand All @@ -75,3 +90,18 @@ export async function getNpmPackage(
process.exit(1);
}
}

export async function getPackageFromLocal(currentDir: string, targetFile: string) {
const spinner = ora(chalk.bold.cyan("Creating a project...")).start();
try {
await tar.extract({
file: targetFile,
cwd: currentDir,
});
spinner.succeed(chalk.bold.green("Project creation successful"));
} catch (error) {
spinner.fail(chalk.bold.red("Project creation failed"));
console.error("Error:", error);
process.exit(1);
}
}
Loading