Skip to content

Commit 7e16a1e

Browse files
sayarrozele
authored andcommitted
fix(react-native-windows): Fixed adding react-native-windows to a react-native project on a non-Windows platform. (microsoft#1162)
Adding react-native-windows to a react-native project through the `react-native windows` command would fail due to a dependency on a powershell script to generate a certificate. Temporarily using the default certificate that gets outputed if the powershell New-SelfSignedCertificate command fails.
1 parent 26dcbd9 commit 7e16a1e

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

local-cli/generator-windows/index.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const fs = require('fs');
66
const path = require('path');
77
const uuid = require('uuid');
88
const yeoman = require('yeoman-generator');
9+
const username = require('username');
10+
const os = require('os');
911

1012
const REACT_NATIVE_PACKAGE_JSON_PATH = function () {
1113
return path.resolve(
@@ -43,25 +45,32 @@ module.exports = yeoman.Base.extend({
4345
writing: function () {
4446
const projectGuid = uuid.v4();
4547
const packageGuid = uuid.v4();
46-
const currentUser = childProcess.execSync('powershell $env:username')
48+
const currentUser = username.sync(); // Gets the current username depending on the platform.
4749
const templateVars = { name: this.name, ns: this.options.ns, certificateThumbprint : null, projectGuid, packageGuid, currentUser };
4850

49-
const certGenCommand = [
50-
`$cert = New-SelfSignedCertificate -KeyUsage DigitalSignature -KeyExportPolicy Exportable -Subject "CN=${currentUser}" -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3", "2.5.29.19={text}Subject Type:End Entity") -CertStoreLocation "Cert:\\CurrentUser\\My"`,
51-
`$pwd = ConvertTo-SecureString -String password -Force -AsPlainText`,
52-
`Export-PfxCertificate -Cert "cert:\\CurrentUser\\My\\$($cert.Thumbprint)" -FilePath ${path.join('windows', this.name, this.name)}_TemporaryKey.pfx -Password $pwd`,
53-
`$cert.Thumbprint`
54-
];
55-
5651
console.log(`Generating self-signed certificate...`);
57-
const certGenProcess = childProcess.spawnSync('powershell', ['-command', certGenCommand.join(';')]);
58-
59-
if (certGenProcess.status === 0) {
60-
const certGenProcessOutput = certGenProcess.stdout.toString().trim().split('\n');
61-
templateVars.certificateThumbprint = certGenProcessOutput[certGenProcessOutput.length - 1];
62-
console.log(chalk.green("Self-signed certificate generated successfully."));
52+
if (os.platform() === 'win32') {
53+
const certGenCommand = [
54+
`$cert = New-SelfSignedCertificate -KeyUsage DigitalSignature -KeyExportPolicy Exportable -Subject "CN=${currentUser}" -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3", "2.5.29.19={text}Subject Type:End Entity") -CertStoreLocation "Cert:\\CurrentUser\\My"`,
55+
`$pwd = ConvertTo-SecureString -String password -Force -AsPlainText`,
56+
`Export-PfxCertificate -Cert "cert:\\CurrentUser\\My\\$($cert.Thumbprint)" -FilePath ${path.join('windows', this.name, this.name)}_TemporaryKey.pfx -Password $pwd`,
57+
`$cert.Thumbprint`
58+
];
59+
const certGenProcess = childProcess.spawnSync('powershell', ['-command', certGenCommand.join(';')]);
60+
61+
if (certGenProcess.status === 0) {
62+
const certGenProcessOutput = certGenProcess.stdout.toString().trim().split('\n');
63+
templateVars.certificateThumbprint = certGenProcessOutput[certGenProcessOutput.length - 1];
64+
console.log(chalk.green("Self-signed certificate generated successfully."));
65+
} else {
66+
console.log(chalk.yellow('Failed to generate Self-signed certificate. Using Default Certificate. Use Visual Studio to renew it.'));
67+
this.fs.copy(
68+
this.templatePath(path.join('keys', 'MyApp_TemporaryKey.pfx')),
69+
this.destinationPath(path.join('windows', this.name, this.name + '_TemporaryKey.pfx'))
70+
);
71+
}
6372
} else {
64-
console.log(chalk.yellow('Failed to generate Self-signed certificate. Using Default Certificate. Use Visual Studio to renew it.'));
73+
console.log(chalk.yellow('Using Default Certificate. Use Visual Studio to renew it.'));
6574
this.fs.copy(
6675
this.templatePath(path.join('keys', 'MyApp_TemporaryKey.pfx')),
6776
this.destinationPath(path.join('windows', this.name, this.name + '_TemporaryKey.pfx'))

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"chalk": "^1.1.3",
4343
"glob": "^7.0.3",
4444
"shelljs": "^0.7.0",
45+
"username": "^3.0.0",
4546
"uuid": "^2.0.1",
4647
"xml-parser": "^1.2.1",
4748
"yeoman-environment": "^1.5.3",

0 commit comments

Comments
 (0)