Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: init now expects templates to have react-native version set #2422

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 0 additions & 85 deletions packages/cli/src/commands/init/__tests__/editTemplate.test.ts
Original file line number Diff line number Diff line change
@@ -10,8 +10,6 @@ import {
replacePlaceholderWithPackageName,
validatePackageName,
replaceNameInUTF8File,
updateDependencies,
normalizeReactNativeDeps,
} from '../editTemplate';
import semver from 'semver';

@@ -202,67 +200,6 @@ describe('changePlaceholderInTemplate', () => {
);
});

const samplePackageJson: string = `{
"name": "HelloWorld",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"lint": "eslint .",
"start": "react-native start",
"test": "jest"
},
"dependencies": {
"react": "19.0.0-rc-fb9a90fa48-20240614",
"react-native": "1000.0.0"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@babel/preset-env": "^7.20.0",
"@babel/runtime": "^7.20.0",
"@react-native/babel-preset": "0.75.0-main",
"@react-native/eslint-config": "0.75.0-main",
"@react-native/metro-config": "0.75.0-main",
"@react-native/typescript-config": "0.75.0-main",
"@types/react": "^18.2.6",
"@types/react-test-renderer": "^18.0.0",
"babel-jest": "^29.6.3",
"eslint": "^8.19.0",
"jest": "^29.6.3",
"prettier": "2.8.8",
"react-test-renderer": "19.0.0-rc-fb9a90fa48-20240614",
"typescript": "5.0.4"
},
"engines": {
"node": ">=18"
}
}`;

describe('updateDependencies', () => {
beforeEach(() => {
jest.spyOn(process, 'cwd').mockImplementation(() => testPath);
jest.spyOn(fs, 'writeFileSync');
jest.spyOn(fs, 'readFileSync').mockImplementation(() => samplePackageJson);
});

afterEach(() => {
jest.restoreAllMocks();
});

it('updates react-native', () => {
updateDependencies({
dependencies: {
'react-native': '0.75.0',
},
});
expect(fs.writeFileSync as jest.Mock).toHaveBeenCalledWith(
expect.anything(),
samplePackageJson.replace('1000.0.0', '0.75.0'),
);
});
});

describe('replacePlaceholderWithPackageName', () => {
beforeEach(() => {
jest.spyOn(process, 'cwd').mockImplementation(() => testPath);
@@ -432,25 +369,3 @@ describe('replaceNameInUTF8File', () => {
expect(fsWriteFileSpy).toHaveBeenCalledTimes(0);
});
});

describe('normalizeReactNativeDeps', () => {
it('returns only @react-native/* dependencies updated to a specific version', () => {
const devDependencies = {
'@babel/core': '^7.20.0',
'@react-native/babel-preset': '0.75.0-main',
'@react-native/eslint-config': '0.75.0-main',
'@react-native/metro-config': '0.75.0-main',
'@react-native/typescript-config': '0.75.0-main',
'@types/react': '^18.2.6',
'@types/react-test-renderer': '^18.0.0',
eslint: '^8.19.0',
'react-test-renderer': '19.0.0-rc-fb9a90fa48-20240614',
};
expect(normalizeReactNativeDeps(devDependencies, '0.75.0')).toMatchObject({
'@react-native/babel-preset': '0.75.0',
'@react-native/eslint-config': '0.75.0',
'@react-native/metro-config': '0.75.0',
'@react-native/typescript-config': '0.75.0',
});
});
});
60 changes: 0 additions & 60 deletions packages/cli/src/commands/init/editTemplate.ts
Original file line number Diff line number Diff line change
@@ -15,15 +15,6 @@ interface PlaceholderConfig {
packageName?: string;
}

interface NpmPackageDependencies {
dependencies?: {
[name: string]: string;
};
devDependencies?: {
[name: string]: string;
};
}

/**
TODO: This is a default placeholder for title in react-native template.
We should get rid of this once custom templates adapt `placeholderTitle` in their configurations.
@@ -235,35 +226,6 @@ export async function replacePlaceholderWithPackageName({
}
}

export function updateDependencies(update: NpmPackageDependencies) {
logger.debug('Updating package.json dependencies:');
const pkgJsonPath = path.join(process.cwd(), 'package.json');

const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8'));

for (const [pkg, value] of Object.entries(update.dependencies ?? {})) {
const old = pkgJson.dependencies[pkg];
if (old) {
logger.debug(`${pkg}: ${old}${value}`);
} else {
logger.debug(`${pkg}: ${value}`);
}
pkgJson.dependencies[pkg] = value;
}

for (const [pkg, value] of Object.entries(update.devDependencies ?? {})) {
const old = pkgJson.devDependencies[pkg];
if (old) {
logger.debug(`${pkg}: ${old}${value}`);
} else {
logger.debug(`${pkg}: ${value}`);
}
pkgJson.devDependencies[pkg] = value;
}

fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, null, 2));
}

export async function changePlaceholderInTemplate({
projectName,
placeholderName,
@@ -307,25 +269,3 @@ export async function changePlaceholderInTemplate({
}
}
}

type Packages = {
[pkgs: string]: string;
};

const REACT_NATIVE_SCOPE = '@react-native/';

/**
* Packages that are scoped under @react-native need a consistent version
*/
export function normalizeReactNativeDeps(
deps: Packages | undefined,
version: string,
): Packages {
const updated: Packages = {};
for (const key of Object.keys(deps ?? {}).filter((pkg) =>
pkg.startsWith(REACT_NATIVE_SCOPE),
)) {
updated[key] = version;
}
return updated;
}
Loading