Skip to content

Commit

Permalink
refactor: avoid 2 calls to write package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Jul 4, 2024
1 parent 5a089f1 commit fb34620
Showing 1 changed file with 29 additions and 36 deletions.
65 changes: 29 additions & 36 deletions packages/create-react-native-library/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,14 +793,13 @@ async function create(_argv: yargs.Arguments<any>) {
}
}

const rootPackageJson = await fs.readJson(path.join(folder, 'package.json'));

if (example !== 'none') {
// Set `react` and `react-native` versions of root `package.json` from example `package.json`
const examplePackageJson = await fs.readJSON(
path.join(folder, 'example', 'package.json')
);
const rootPackageJson = await fs.readJSON(
path.join(folder, 'package.json')
);

if (!rootPackageJson.devDependencies) {
rootPackageJson.devDependencies = {};
Expand All @@ -810,34 +809,6 @@ async function create(_argv: yargs.Arguments<any>) {
examplePackageJson.dependencies.react;
rootPackageJson.devDependencies['react-native'] =
examplePackageJson.dependencies['react-native'];

await fs.writeJSON(path.join(folder, 'package.json'), rootPackageJson, {
spaces: 2,
});
}

if (!local) {
let isInGitRepo = false;

try {
isInGitRepo =
(await spawn('git', ['rev-parse', '--is-inside-work-tree'])) === 'true';
} catch (e) {
// Ignore error
}

if (!isInGitRepo) {
try {
await spawn('git', ['init'], { cwd: folder });
await spawn('git', ['branch', '-M', 'main'], { cwd: folder });
await spawn('git', ['add', '.'], { cwd: folder });
await spawn('git', ['commit', '-m', 'chore: initial commit'], {
cwd: folder,
});
} catch (e) {
// Ignore error
}
}
}

// Some of the passed args can already be derived from the generated package.json file.
Expand All @@ -864,16 +835,38 @@ async function create(_argv: yargs.Arguments<any>) {
([answer]) => !ignoredAnswers.includes(answer)
)
);

libraryMetadata.version = version;
rootPackageJson['create-react-native-library'] = libraryMetadata;

const libraryPackageJson = await fs.readJson(
path.join(folder, 'package.json')
);
libraryPackageJson['create-react-native-library'] = libraryMetadata;
await fs.writeJson(path.join(folder, 'package.json'), libraryPackageJson, {
await fs.writeJson(path.join(folder, 'package.json'), rootPackageJson, {
spaces: 2,
});

if (!local) {
let isInGitRepo = false;

try {
isInGitRepo =
(await spawn('git', ['rev-parse', '--is-inside-work-tree'])) === 'true';
} catch (e) {
// Ignore error
}

if (!isInGitRepo) {
try {
await spawn('git', ['init'], { cwd: folder });
await spawn('git', ['branch', '-M', 'main'], { cwd: folder });
await spawn('git', ['add', '.'], { cwd: folder });
await spawn('git', ['commit', '-m', 'chore: initial commit'], {
cwd: folder,
});
} catch (e) {
// Ignore error
}
}
}

spinner.succeed(
`Project created successfully at ${kleur.yellow(
path.relative(process.cwd(), folder)
Expand Down

0 comments on commit fb34620

Please sign in to comment.