Skip to content

Commit

Permalink
fix(repack-init): monorepo & version range support (#405)
Browse files Browse the repository at this point in the history
* feat: add monorepo support to repack-init

* chore: update function docs

* chore: add changeset

* fix: deal with carets and tilde in react-native version
  • Loading branch information
jbroma authored Jul 27, 2023
1 parent 43f467a commit cbbb5b4
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-fireants-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack-init": patch
---

Fix bad behaviour of repack-init when run inside of a monorepo
1 change: 1 addition & 0 deletions packages/init/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"lint": "eslint . --ext .ts"
},
"dependencies": {
"@manypkg/find-root": "^2.2.1",
"chalk": "^5.2.0",
"dedent": "^0.7.0",
"detect-package-manager": "^2.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/init/src/tasks/addDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const dependencies = [
/**
* Installs dependencies required by Re.Pack using the specified package manager
*
* @param packageManager yarn or npm
* @param packageManager yarn, npm or pnpm
*/
export default async function addDependencies(packageManager: PM) {
let installCommand: string;
Expand Down
5 changes: 3 additions & 2 deletions packages/init/src/tasks/checkPackageManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { detect, PM } from 'detect-package-manager';

import { findRoot } from '@manypkg/find-root';
import logger from '../utils/logger.js';

/**
Expand All @@ -9,7 +9,8 @@ import logger from '../utils/logger.js';
* @returns package manager name (one of 'npm', 'yarn', 'pnpm')
*/
export default async function checkPackageManager(cwd: string): Promise<PM> {
const packageManager = await detect({ cwd });
const { rootDir } = await findRoot(cwd);
const packageManager = await detect({ cwd: rootDir });
logger.info(`Using ${packageManager} as package manager`);

return packageManager;
Expand Down
14 changes: 9 additions & 5 deletions packages/init/src/tasks/checkReactNative.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import fs from 'fs';
import path from 'path';
import semver from 'semver';
import semver, { SemVer } from 'semver';

import logger from '../utils/logger.js';

/**
* Checks whether react-native is installed and returns it's version
* Checks whether React-Native is installed and returns it's version
*
* @param cwd
* @param cwd current working directory
* @returns React-Native version
*/
export default function checkReactNative(rootDir: string): string {
export default function checkReactNative(rootDir: string): SemVer {
const packageJsonPath = path.join(rootDir, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));

Expand All @@ -19,7 +19,11 @@ export default function checkReactNative(rootDir: string): string {
throw new Error('React-Native not found in package.json');
}

const version = packageJson.dependencies['react-native'];
const version = semver.coerce(packageJson.dependencies['react-native']);

if (!version) {
throw new Error('Failed to parse React-Native version');
}

logger.info(`Found React-Native@${version} in package.json`);

Expand Down
8 changes: 7 additions & 1 deletion packages/init/src/tasks/createWebpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ function adjustEntryFilename(template: string, entry: string) {

return template.replace(/entry\s=.*,/, `entry = '${entry}',`);
}

/**
* Adds webpack.config file to the project
*
* @param cwd current working directory
* @param templateType mjs or cjs
* @param entry name of the entry file for the application
*/
export default async function createWebpackConfig(
cwd: string,
templateType: 'mjs' | 'cjs',
Expand Down
4 changes: 2 additions & 2 deletions packages/init/src/tasks/modifyAndroid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import semver from 'semver';
import semver, { SemVer } from 'semver';
import dedent from 'dedent';

import logger from '../utils/logger.js';
Expand Down Expand Up @@ -95,7 +95,7 @@ function modifyNewConfig(config: string): string {
* @param cwd path for the root directory of the project
* @param reactNativeVersion version of react-native in project
*/
export default function modifyAndroid(cwd: string, reactNativeVersion: string) {
export default function modifyAndroid(cwd: string, reactNativeVersion: SemVer) {
const buildGradlePath = path.join(cwd, 'android', 'app', 'build.gradle');
const config = fs.readFileSync(buildGradlePath, 'utf-8');

Expand Down
31 changes: 31 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4879,6 +4879,7 @@ __metadata:
resolution: "@callstack/repack-init@workspace:packages/init"
dependencies:
"@callstack/eslint-config": ^13.0.2
"@manypkg/find-root": ^2.2.1
chalk: ^5.2.0
dedent: ^0.7.0
detect-package-manager: ^2.0.1
Expand Down Expand Up @@ -6616,6 +6617,17 @@ __metadata:
languageName: node
linkType: hard

"@manypkg/find-root@npm:^2.2.1":
version: 2.2.1
resolution: "@manypkg/find-root@npm:2.2.1"
dependencies:
"@manypkg/tools": ^1.1.0
find-up: ^4.1.0
fs-extra: ^8.1.0
checksum: 7f31120c3ef0711f6e65857ea232fb4f7caf4db388080e738f82d979d7f72572dae60519c8fcd45c431a8e94095b6628257fbf7373b34401359def00687bab13
languageName: node
linkType: hard

"@manypkg/get-packages@npm:^1.1.3":
version: 1.1.3
resolution: "@manypkg/get-packages@npm:1.1.3"
Expand All @@ -6630,6 +6642,18 @@ __metadata:
languageName: node
linkType: hard

"@manypkg/tools@npm:^1.1.0":
version: 1.1.0
resolution: "@manypkg/tools@npm:1.1.0"
dependencies:
fs-extra: ^8.1.0
globby: ^11.0.0
jju: ^1.4.0
read-yaml-file: ^1.1.0
checksum: f0228435c0b12b4dec59a57c683f1431a3a0dea71640c73b231de27742ccaf5bb2f48d425184bdf330d24e940cb9ea7ecfc8b4ed5d3d6031f8132e251e728e80
languageName: node
linkType: hard

"@mapbox/node-pre-gyp@npm:^1.0.5":
version: 1.0.9
resolution: "@mapbox/node-pre-gyp@npm:1.0.9"
Expand Down Expand Up @@ -21340,6 +21364,13 @@ __metadata:
languageName: node
linkType: hard

"jju@npm:^1.4.0":
version: 1.4.0
resolution: "jju@npm:1.4.0"
checksum: 3790481bd2b7827dd6336e6e3dc2dcc6d425679ba7ebde7b679f61dceb4457ea0cda330972494de608571f4973c6dfb5f70fab6f3c5037dbab19ac449a60424f
languageName: node
linkType: hard

"joi@npm:^17.2.1":
version: 17.4.0
resolution: "joi@npm:17.4.0"
Expand Down

0 comments on commit cbbb5b4

Please sign in to comment.