diff --git a/change/react-native-windows-aeaa392d-12c1-43af-8980-02dfd8e6d4f3.json b/change/react-native-windows-aeaa392d-12c1-43af-8980-02dfd8e6d4f3.json new file mode 100644 index 00000000000..a9e0f2c8e8c --- /dev/null +++ b/change/react-native-windows-aeaa392d-12c1-43af-8980-02dfd8e6d4f3.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Fabric: Add `windows` script to new cpp-app template", + "packageName": "react-native-windows", + "email": "jthysell@microsoft.com", + "dependentChangeType": "patch" +} diff --git a/vnext/templates/cpp-app/template.config.js b/vnext/templates/cpp-app/template.config.js index dfcd62a37dc..2e0db4ce69c 100644 --- a/vnext/templates/cpp-app/template.config.js +++ b/vnext/templates/cpp-app/template.config.js @@ -15,6 +15,8 @@ const util = require('util'); const glob = util.promisify(require('glob')); +const templateUtils = require('../templateUtils'); + async function preInstall(config = {}, options = {}) {} async function getFileMappings(config = {}, options = {}) { @@ -104,7 +106,12 @@ async function getFileMappings(config = {}, options = {}) { return fileMappings; } -function postInstall(config = {}, options = {}) { +async function postInstall(config = {}, options = {}) { + // Update package.json with new scripts + await templateUtils.updateProjectPackageJson(config, options, { + scripts: {windows: 'react-native run-windows'}, + }); + console.log(chalk.white.bold('To run your new windows app:')); console.log(chalk.white(' npx react-native run-windows')); } diff --git a/vnext/templates/templateUtils.js b/vnext/templates/templateUtils.js new file mode 100644 index 00000000000..0d6238484e8 --- /dev/null +++ b/vnext/templates/templateUtils.js @@ -0,0 +1,29 @@ +/** + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + * + * @ts check + * @format + */ + +const pkgUtils = require('@react-native-windows/package-utils'); + +async function updateProjectPackageJson(config = {}, options = {}, props = {}) { + const projectPath = config?.root ?? process.cwd(); + const projectPackage = await pkgUtils.WritableNpmPackage.fromPath( + projectPath, + ); + + if (!projectPackage) { + throw new Error( + `The directory '${projectPath}' is not the root of an npm package`, + ); + } + + if (options?.logging) { + console.log('Modifying project package.json...'); + } + await projectPackage.mergeProps(props); +} + +module.exports = {updateProjectPackageJson};