/packages/@react-native-windows/cli/src/index.ts

https://github.com/microsoft/react-native-windows · TypeScript · 93 lines · 54 code · 8 blank · 31 comment · 5 complexity · 8a982b21a96d436c8b6e6bd5cbb9aeb6 MD5 · raw file

  1. /**
  2. * Copyright (c) Microsoft Corporation.
  3. * Licensed under the MIT License.
  4. * @format
  5. */
  6. import * as fs from 'fs';
  7. import * as path from 'path';
  8. import {
  9. copyProjectTemplateAndReplace,
  10. installDependencies,
  11. } from './generator-windows';
  12. import {autoLinkCommand} from './runWindows/utils/autolink';
  13. import {runWindowsCommand} from './runWindows/runWindows';
  14. import {dependencyConfigWindows} from './config/dependencyConfig';
  15. import {projectConfigWindows} from './config/projectConfig';
  16. /**
  17. * Project generation options
  18. *
  19. * _
  20. * | |
  21. * __| | __ _ _ __ __ _ ___ _ __
  22. * / _` |/ _` | '_ \ / _` |/ _ \ '__|
  23. * | (_| | (_| | | | | (_| | __/ |
  24. * \__,_|\__,_|_| |_|\__, |\___|_|
  25. * __/ |
  26. * |___/
  27. *
  28. *
  29. * Properties on this interface must remain stable, as new versions of
  30. * react-native-windows-init may be used with local-cli dating back to 0.61.
  31. * All existing arguments must work correctly when changing this interface.
  32. */
  33. export interface GenerateOptions {
  34. overwrite: boolean;
  35. language: 'cpp' | 'cs';
  36. projectType: 'app' | 'lib';
  37. experimentalNuGetDependency: boolean;
  38. nuGetTestVersion?: string;
  39. nuGetTestFeed?: string;
  40. useWinUI3: boolean;
  41. verbose: boolean;
  42. }
  43. /**
  44. * Simple utility for running the Windows generator.
  45. *
  46. * @param projectDir root project directory (i.e. contains index.js)
  47. * @param name name of the root JS module for this app
  48. * @param ns namespace for the project
  49. * @param options command line options container
  50. */
  51. export async function generateWindows(
  52. projectDir: string,
  53. name: string,
  54. ns: string,
  55. options: GenerateOptions,
  56. ) {
  57. if (!fs.existsSync(projectDir)) {
  58. fs.mkdirSync(projectDir);
  59. }
  60. installDependencies(options);
  61. const rnwPackage = path.dirname(
  62. require.resolve('react-native-windows/package.json', {paths: [projectDir]}),
  63. );
  64. const templateRoot = path.join(rnwPackage, 'template');
  65. await copyProjectTemplateAndReplace(
  66. templateRoot,
  67. projectDir,
  68. name,
  69. ns,
  70. options,
  71. );
  72. }
  73. // Assert the interface here doesn't change for the reasons above
  74. const assertStableInterface: typeof generateWindows extends (
  75. projectDir: string,
  76. name: string,
  77. ns: string,
  78. options: GenerateOptions,
  79. ) => Promise<void>
  80. ? true
  81. : never = true;
  82. assertStableInterface;
  83. export const commands = [autoLinkCommand, runWindowsCommand];
  84. export const dependencyConfig = dependencyConfigWindows;
  85. export const projectConfig = projectConfigWindows;