Skip to content

Commit 60b7883

Browse files
authored
Fabric: Add windows script to new cpp-app template (#12374)
## Description 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). ### Type of Change - New feature (non-breaking change which adds functionality) ### Why Adds a useful feature of the old templates to the new template ### What See above. ## Screenshots N/A ## Testing Verified creating a new project works. ## Changelog Should this change be included in the release notes: yes The new Fabric app template now supports `yarn windows` as an alias to `react-native run-windows`
1 parent 31fdd14 commit 60b7883

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Fabric: Add `windows` script to new cpp-app template",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

vnext/templates/cpp-app/template.config.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const util = require('util');
1515

1616
const glob = util.promisify(require('glob'));
1717

18+
const templateUtils = require('../templateUtils');
19+
1820
async function preInstall(config = {}, options = {}) {}
1921

2022
async function getFileMappings(config = {}, options = {}) {
@@ -104,7 +106,12 @@ async function getFileMappings(config = {}, options = {}) {
104106
return fileMappings;
105107
}
106108

107-
function postInstall(config = {}, options = {}) {
109+
async function postInstall(config = {}, options = {}) {
110+
// Update package.json with new scripts
111+
await templateUtils.updateProjectPackageJson(config, options, {
112+
scripts: {windows: 'react-native run-windows'},
113+
});
114+
108115
console.log(chalk.white.bold('To run your new windows app:'));
109116
console.log(chalk.white(' npx react-native run-windows'));
110117
}

vnext/templates/templateUtils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation.
3+
* Licensed under the MIT License.
4+
*
5+
* @ts check
6+
* @format
7+
*/
8+
9+
const pkgUtils = require('@react-native-windows/package-utils');
10+
11+
async function updateProjectPackageJson(config = {}, options = {}, props = {}) {
12+
const projectPath = config?.root ?? process.cwd();
13+
const projectPackage = await pkgUtils.WritableNpmPackage.fromPath(
14+
projectPath,
15+
);
16+
17+
if (!projectPackage) {
18+
throw new Error(
19+
`The directory '${projectPath}' is not the root of an npm package`,
20+
);
21+
}
22+
23+
if (options?.logging) {
24+
console.log('Modifying project package.json...');
25+
}
26+
await projectPackage.mergeProps(props);
27+
}
28+
29+
module.exports = {updateProjectPackageJson};

0 commit comments

Comments
 (0)