react.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. import { Recipe } from "..";
  5. import { join } from "path";
  6. //@ts-ignore
  7. import scaffe from "scaffe";
  8. import { shell } from "../shell";
  9. const afterCra = async (
  10. cwd: string,
  11. appName: string,
  12. typescript: boolean = false
  13. ) => {
  14. const templateDir = join(
  15. __dirname,
  16. `../src/templates/react/${typescript ? "react-ts" : "react"}`
  17. );
  18. try {
  19. await scaffe.generate(templateDir, join(cwd, appName), {
  20. overwrite: true,
  21. });
  22. } catch (err) {
  23. console.log(err);
  24. }
  25. };
  26. const reactjs: Recipe = {
  27. descriptiveName: "React.js",
  28. shortName: "reactjs",
  29. configUpdate: ({ cfg, packageManager }) => ({
  30. ...cfg,
  31. distDir: `../build`,
  32. devPath: "http://localhost:3000",
  33. beforeDevCommand: `${packageManager === "yarn" ? "yarn" : "npm run"} start`,
  34. beforeBuildCommand: `${
  35. packageManager === "yarn" ? "yarn" : "npm run"
  36. } build`,
  37. }),
  38. extraNpmDevDependencies: [],
  39. extraNpmDependencies: [],
  40. preInit: async ({ cwd, cfg, packageManager }) => {
  41. // CRA creates the folder for you
  42. if (packageManager === "yarn") {
  43. await shell("yarn", ["create", "react-app", `${cfg.appName}`], {
  44. cwd,
  45. });
  46. } else {
  47. await shell("npx", ["create-react-app", `${cfg.appName}`, "--use-npm"], {
  48. cwd,
  49. });
  50. }
  51. await afterCra(cwd, cfg.appName);
  52. },
  53. postInit: async ({ packageManager }) => {
  54. console.log(`
  55. Your installation completed.
  56. To start, run ${packageManager === "yarn" ? "yarn" : "npm run"} tauri dev
  57. `);
  58. },
  59. };
  60. const reactts: Recipe = {
  61. ...reactjs,
  62. descriptiveName: "React with Typescript",
  63. shortName: "reactts",
  64. extraNpmDependencies: [],
  65. preInit: async ({ cwd, cfg, packageManager }) => {
  66. // CRA creates the folder for you
  67. if (packageManager === "yarn") {
  68. await shell(
  69. "yarn",
  70. ["create", "react-app", "--template", "typescript", `${cfg.appName}`],
  71. {
  72. cwd,
  73. }
  74. );
  75. } else {
  76. await shell(
  77. "npx",
  78. [
  79. "create-react-app",
  80. `${cfg.appName}`,
  81. "--use-npm",
  82. "--template",
  83. "typescript",
  84. ],
  85. {
  86. cwd,
  87. }
  88. );
  89. }
  90. await afterCra(cwd, cfg.appName, true);
  91. },
  92. postInit: async ({ packageManager }) => {
  93. console.log(`
  94. Your installation completed.
  95. To start, run ${packageManager === "yarn" ? "yarn" : "npm run"} tauri dev
  96. `);
  97. },
  98. };
  99. export { reactjs, reactts };