Skip to content

Commit fb34620

Browse files
committed
refactor: avoid 2 calls to write package.json
1 parent 5a089f1 commit fb34620

File tree

1 file changed

+29
-36
lines changed
  • packages/create-react-native-library/src

1 file changed

+29
-36
lines changed

packages/create-react-native-library/src/index.ts

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -793,14 +793,13 @@ async function create(_argv: yargs.Arguments<any>) {
793793
}
794794
}
795795

796+
const rootPackageJson = await fs.readJson(path.join(folder, 'package.json'));
797+
796798
if (example !== 'none') {
797799
// Set `react` and `react-native` versions of root `package.json` from example `package.json`
798800
const examplePackageJson = await fs.readJSON(
799801
path.join(folder, 'example', 'package.json')
800802
);
801-
const rootPackageJson = await fs.readJSON(
802-
path.join(folder, 'package.json')
803-
);
804803

805804
if (!rootPackageJson.devDependencies) {
806805
rootPackageJson.devDependencies = {};
@@ -810,34 +809,6 @@ async function create(_argv: yargs.Arguments<any>) {
810809
examplePackageJson.dependencies.react;
811810
rootPackageJson.devDependencies['react-native'] =
812811
examplePackageJson.dependencies['react-native'];
813-
814-
await fs.writeJSON(path.join(folder, 'package.json'), rootPackageJson, {
815-
spaces: 2,
816-
});
817-
}
818-
819-
if (!local) {
820-
let isInGitRepo = false;
821-
822-
try {
823-
isInGitRepo =
824-
(await spawn('git', ['rev-parse', '--is-inside-work-tree'])) === 'true';
825-
} catch (e) {
826-
// Ignore error
827-
}
828-
829-
if (!isInGitRepo) {
830-
try {
831-
await spawn('git', ['init'], { cwd: folder });
832-
await spawn('git', ['branch', '-M', 'main'], { cwd: folder });
833-
await spawn('git', ['add', '.'], { cwd: folder });
834-
await spawn('git', ['commit', '-m', 'chore: initial commit'], {
835-
cwd: folder,
836-
});
837-
} catch (e) {
838-
// Ignore error
839-
}
840-
}
841812
}
842813

843814
// Some of the passed args can already be derived from the generated package.json file.
@@ -864,16 +835,38 @@ async function create(_argv: yargs.Arguments<any>) {
864835
([answer]) => !ignoredAnswers.includes(answer)
865836
)
866837
);
838+
867839
libraryMetadata.version = version;
840+
rootPackageJson['create-react-native-library'] = libraryMetadata;
868841

869-
const libraryPackageJson = await fs.readJson(
870-
path.join(folder, 'package.json')
871-
);
872-
libraryPackageJson['create-react-native-library'] = libraryMetadata;
873-
await fs.writeJson(path.join(folder, 'package.json'), libraryPackageJson, {
842+
await fs.writeJson(path.join(folder, 'package.json'), rootPackageJson, {
874843
spaces: 2,
875844
});
876845

846+
if (!local) {
847+
let isInGitRepo = false;
848+
849+
try {
850+
isInGitRepo =
851+
(await spawn('git', ['rev-parse', '--is-inside-work-tree'])) === 'true';
852+
} catch (e) {
853+
// Ignore error
854+
}
855+
856+
if (!isInGitRepo) {
857+
try {
858+
await spawn('git', ['init'], { cwd: folder });
859+
await spawn('git', ['branch', '-M', 'main'], { cwd: folder });
860+
await spawn('git', ['add', '.'], { cwd: folder });
861+
await spawn('git', ['commit', '-m', 'chore: initial commit'], {
862+
cwd: folder,
863+
});
864+
} catch (e) {
865+
// Ignore error
866+
}
867+
}
868+
}
869+
877870
spinner.succeed(
878871
`Project created successfully at ${kleur.yellow(
879872
path.relative(process.cwd(), folder)

0 commit comments

Comments
 (0)