From cc297b018a75a769ca45b2d90e424e562b06cc32 Mon Sep 17 00:00:00 2001 From: "Jon Thysell (JAUNTY)" Date: Thu, 9 Nov 2023 09:38:34 -0800 Subject: [PATCH 1/2] Fabric: Add `windows` script to new cpp-app template Adds the functionality for apps created with the new cpp-app template to run `yarn windows`, as an alias for `react-native run-windows`, similar to the old template. To do this, I've created a new `templateUtils` module for shared template config code, with a new `updateProjectPackageJson()` method to make it easy for templates to inject items into a project's package.json file (in this case, a new `scripts` entry). --- vnext/templates/cpp-app/template.config.js | 9 ++++++- vnext/templates/templateUtils.js | 29 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 vnext/templates/templateUtils.js 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}; From 827cc4b5831a8de52bde34bcf082b384a4358093 Mon Sep 17 00:00:00 2001 From: "Jon Thysell (JAUNTY)" Date: Thu, 9 Nov 2023 13:55:23 -0800 Subject: [PATCH 2/2] Change files --- ...ative-windows-aeaa392d-12c1-43af-8980-02dfd8e6d4f3.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 change/react-native-windows-aeaa392d-12c1-43af-8980-02dfd8e6d4f3.json 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" +}