forked from microsoft/react-native-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[0.74] Improve new project name(space) validation and cleaning
Backport PR microsoft#13566 to 0.74. ## Description This PR moves all of the logic concerning validating and cleaning project names when creating new projects (via the `init-windows` CLI command or the soon to be deprecated `react-native-windows-init` command) into one shared location with unit tests. It also updates the cleaning logic to handle package scopes (i.e. `@org/package`) and better apply cleaning to packages with hyphens (i.e. `my-package`). Going forward, when creating (new or old arch) projects with `init-windows`, the flow will still to adhere the following rules: 1. Verify that a string specified with `--name` or `--namespace` is valid, or else error out. 2. If an item isn't specified, do our best to determine the value from `package.json`, etc., and clean that value if necessary. When using `react-native-windows-init`, (which still only lets you specify the `--namespace`, and always figures out name from `package.json`, etc.), the flow will be mostly the same as before, where both name and namespace will be cleaned automatically if invalid, rather than erroring out. (However the consolidated cleaning logic should mean an improvement when using tools other than the RN CLI for creating your initial RN project). ### Type of Change - Bug fix (non-breaking change which fixes an issue) ### Why The rules for what constitutes a "valid" name for a RN project has changed over the years with each of the different tools that are used to create RN projects. This PR is an attempt to both broaden the number of supported new project tools while also ensuring RNW still produces usable native code. Closes microsoft#13558 Closes microsoft#13426 ### What Moved all name(space) logic into a new `nameHelpers` module, exposed them to existing callers, and created unit tests. ## Screenshots N/A ## Testing Add new tests for nameHelpers. ## Changelog Should this change be included in the release notes: _yes_ [0.74] Improve new project name(space) validation and cleaning
- Loading branch information
1 parent
a48562b
commit d918b03
Showing
7 changed files
with
179 additions
and
28 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@react-native-windows-cli-db04fef2-0cc5-477b-ac9a-1aa9f4e4c2ee.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "[0.74] Improve new project name(space) validation and cleaning", | ||
"packageName": "@react-native-windows/cli", | ||
"email": "jthysell@microsoft.com", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@react-native-windows-telemetry-ca4bf582-eec5-4ee4-8830-19a6d937a87b.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "[0.74] Improve new project name(space) validation and cleaning", | ||
"packageName": "@react-native-windows/telemetry", | ||
"email": "jthysell@microsoft.com", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/@react-native-windows/cli/src/utils/nameHelpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT License. | ||
* @format | ||
*/ | ||
|
||
import _ from 'lodash'; | ||
|
||
function pascalCase(str: string): string { | ||
const camelCase = _.camelCase(str); | ||
return camelCase[0].toUpperCase() + camelCase.substr(1); | ||
} | ||
|
||
export function isValidProjectName(name: string): boolean { | ||
if (name.match(/^[a-z][a-z0-9]*$/gi)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
export function cleanName(str: string): string { | ||
str = str.replace('@', ''); // Remove '@' from package scope names | ||
str = str.slice(str.lastIndexOf('/') + 1); // Remove package scope | ||
str = pascalCase(str); // Convert to PascalCase | ||
return str; | ||
} | ||
|
||
export function isValidProjectNamespace(namespace: string): boolean { | ||
if ( | ||
namespace | ||
.split(/[.]+/) | ||
.map(isValidProjectName) | ||
.every(x => x) | ||
) { | ||
// Validate that every part of the namespace is a valid project name | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
export function cleanNamespace(str: string): string { | ||
return str.split(/[.:]+/).map(cleanName).join('.'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters