create-dage-backend-cli 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env node
  2. const { program } = require("commander");
  3. const fs = require("fs");
  4. const path = require("path");
  5. const clc = require("cli-color");
  6. const { promisify } = require("util");
  7. const cliProgress = require("cli-progress");
  8. const ncp = promisify(require("ncp").ncp);
  9. const { exec, execSync } = require("child_process");
  10. const pkg = require("./package.json");
  11. process.stdout.setEncoding("utf8");
  12. program
  13. .version(pkg.version)
  14. .arguments("<projectName>")
  15. .action(async (projectName) => {
  16. const projectDir = path.join(process.cwd(), projectName);
  17. if (fs.existsSync(projectDir)) {
  18. console.log(clc.red("当前目录下已存在" + projectName));
  19. return;
  20. }
  21. fs.mkdirSync(projectDir);
  22. const templateDir = path.join(__dirname, "template");
  23. const exclusions = ["node_modules", "pnpm-lock.yaml"];
  24. const files = await promisify(fs.readdir)(templateDir);
  25. const progressBar = new cliProgress.SingleBar({
  26. format: "Progress [{bar}] {percentage}% | {value}/{total}",
  27. barCompleteChar: "\u001b[42m \u001b[0m",
  28. barIncompleteChar: "\u001b[40m \u001b[0m",
  29. hideCursor: true,
  30. });
  31. progressBar.start(files.length, 0);
  32. for (const file of files) {
  33. if (!exclusions.includes(file)) {
  34. const srcPath = path.join(templateDir, file);
  35. const destPath = path.join(projectDir, file);
  36. await ncp(srcPath, destPath);
  37. if (file === "package.json") {
  38. const packageJsonPath = path.join(destPath);
  39. const packageJson = JSON.parse(
  40. fs.readFileSync(packageJsonPath, "utf8")
  41. );
  42. packageJson.name = projectName;
  43. packageJson.version = pkg.version;
  44. Object.keys(packageJson.dependencies).forEach((key) => {
  45. if (key.startsWith("@dage")) {
  46. packageJson.dependencies[key] = "^" + getPackageVersion(key);
  47. }
  48. });
  49. fs.writeFileSync(
  50. packageJsonPath,
  51. JSON.stringify(packageJson, null, 2)
  52. );
  53. }
  54. }
  55. progressBar.increment();
  56. }
  57. progressBar.stop();
  58. console.log(clc.blue("=====执行初始化====="));
  59. const installCommand =
  60. "pnpm i --registry http://192.168.20.245:4873/ || yarn --registry http://192.168.20.245:4873/ || npm i --registry http://192.168.20.245:4873/";
  61. try {
  62. await new Promise((resolve, reject) => {
  63. const childProcess = exec(
  64. installCommand,
  65. { cwd: projectDir },
  66. (error) => {
  67. if (error) {
  68. reject(error);
  69. } else {
  70. resolve();
  71. }
  72. }
  73. );
  74. childProcess.stdout.on("data", (data) => {
  75. console.log(data.trim());
  76. });
  77. });
  78. console.log(clc.green("=====初始化完成====="));
  79. } catch (error) {
  80. console.log(
  81. clc.red(`Error executing installation command: ${error.message}`)
  82. );
  83. }
  84. });
  85. program.parse(process.argv);
  86. function getPackageVersion(pkgName) {
  87. let version = "1.0.0";
  88. try {
  89. const componentsVersion = execSync(
  90. "npm info " + pkgName + " version --registry http://192.168.20.245:4873"
  91. );
  92. version = componentsVersion.toString().trim();
  93. } catch (err) {
  94. console.log(clc.red("获取 " + pkgName + " 版本号失败"));
  95. }
  96. return version;
  97. }