-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
114 changed files
with
3,737 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { log } from '@clack/prompts'; | ||
|
||
/** | ||
* Sets up the patch file for react-native-web in the patches directory | ||
* @param projectPath - Root path of the project | ||
* @param templatePath - Path to the scripts/template directory | ||
* @returns Promise with the result of the operation | ||
*/ | ||
async function setupReactNativeWebPatch( | ||
projectPath: string = process.cwd() | ||
): Promise<boolean> { | ||
try { | ||
// Define paths | ||
const templatePath = path.join( | ||
__dirname, | ||
'..', | ||
'..', | ||
'template', | ||
'nextjs', | ||
'next15' | ||
); | ||
const patchesDir = path.join(projectPath, 'patches'); | ||
const patchFileName = 'react-native-web+0.19.13.patch'; | ||
|
||
const sourcePatchPath = path.join(templatePath, patchFileName); | ||
const targetPatchPath = path.join(patchesDir, patchFileName); | ||
|
||
// Check if template patch file exists | ||
if (!fs.existsSync(sourcePatchPath)) { | ||
log.error(`Template patch file not found at: ${sourcePatchPath}`); | ||
return false; | ||
} | ||
|
||
// Create patches directory if it doesn't exist | ||
if (!fs.existsSync(patchesDir)) { | ||
fs.mkdirSync(patchesDir, { recursive: true }); | ||
} | ||
|
||
// Copy patch file if it doesn't exist or force update | ||
if (!fs.existsSync(targetPatchPath)) { | ||
fs.copyFileSync(sourcePatchPath, targetPatchPath); | ||
} else { | ||
// Compare files to see if they're different | ||
const sourceContent = fs.readFileSync(sourcePatchPath, 'utf8'); | ||
const targetContent = fs.readFileSync(targetPatchPath, 'utf8'); | ||
|
||
if (sourceContent !== targetContent) { | ||
// Backup existing file | ||
const backupPath = `${targetPatchPath}.backup`; | ||
fs.copyFileSync(targetPatchPath, backupPath); | ||
|
||
// Update patch file | ||
fs.copyFileSync(sourcePatchPath, targetPatchPath); | ||
} | ||
} | ||
|
||
return true; | ||
} catch (error) { | ||
log.error( | ||
error instanceof Error ? error.message : 'Unknown error occurred' | ||
); | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Validates the patch file setup | ||
* @param patchPath - Path to the patch file | ||
* @returns boolean indicating if patch file is valid | ||
*/ | ||
function validatePatchFile(patchPath: string): boolean { | ||
try { | ||
if (!fs.existsSync(patchPath)) { | ||
return false; | ||
} | ||
|
||
const content = fs.readFileSync(patchPath, 'utf8'); | ||
// Basic validation - check if it looks like a patch file | ||
return content.includes('diff --git') || content.includes('@@'); | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
export { setupReactNativeWebPatch, validatePatchFile }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { log } from '@clack/prompts'; | ||
import { readFileSync, existsSync } from 'fs'; | ||
import { join } from 'path'; | ||
|
||
interface PackageJson { | ||
dependencies?: { | ||
next?: string; | ||
[key: string]: string | undefined; | ||
}; | ||
devDependencies?: { | ||
next?: string; | ||
[key: string]: string | undefined; | ||
}; | ||
} | ||
|
||
interface VersionResult { | ||
version: string; | ||
majorVersion: number; | ||
isNextjs15OrHigher: boolean; | ||
error?: string; | ||
} | ||
|
||
/** | ||
* Cleans version string by removing special characters (^, ~, >, <, =, v) | ||
* @param version - Raw version string from package.json | ||
*/ | ||
const cleanVersionString = (version: string): string => { | ||
return version.replace(/[^\d.]/g, ''); | ||
}; | ||
|
||
/** | ||
* Gets the Next.js major version from package.json | ||
* @param projectPath - Path to the project root directory | ||
* @returns Promise containing version information and status | ||
*/ | ||
async function getNextjsVersion( | ||
projectPath: string = process.cwd() | ||
): Promise<VersionResult> { | ||
try { | ||
const packageJsonPath = join(projectPath, 'package.json'); | ||
|
||
// Check if package.json exists | ||
if (!existsSync(packageJsonPath)) { | ||
return { | ||
version: '', | ||
majorVersion: 0, | ||
isNextjs15OrHigher: false, | ||
error: 'package.json not found', | ||
}; | ||
} | ||
|
||
// Read and parse package.json | ||
const packageJson: PackageJson = JSON.parse( | ||
readFileSync(packageJsonPath, 'utf8') | ||
); | ||
|
||
// Check both dependencies and devDependencies for next | ||
const nextVersion = | ||
packageJson.dependencies?.next || packageJson.devDependencies?.next; | ||
|
||
if (!nextVersion) { | ||
return { | ||
version: '', | ||
majorVersion: 0, | ||
isNextjs15OrHigher: false, | ||
error: 'Next.js not found in dependencies', | ||
}; | ||
} | ||
|
||
// Clean the version string and get major version | ||
const cleanVersion = cleanVersionString(nextVersion); | ||
const majorVersion = parseInt(cleanVersion.split('.')[0], 10); | ||
|
||
// Validate that majorVersion is a number | ||
if (isNaN(majorVersion)) { | ||
return { | ||
version: cleanVersion, | ||
majorVersion: 0, | ||
isNextjs15OrHigher: false, | ||
error: 'Invalid version format', | ||
}; | ||
} | ||
|
||
return { | ||
version: cleanVersion, | ||
majorVersion, | ||
isNextjs15OrHigher: majorVersion >= 15, | ||
}; | ||
} catch (error) { | ||
return { | ||
version: '', | ||
majorVersion: 0, | ||
isNextjs15OrHigher: false, | ||
error: error instanceof Error ? error.message : 'Unknown error occurred', | ||
}; | ||
} | ||
} | ||
|
||
export { getNextjsVersion, VersionResult, PackageJson }; | ||
|
||
// Example usage with types: | ||
|
||
export async function checkNextVersion(): Promise<boolean | undefined> { | ||
try { | ||
const result: VersionResult = await getNextjsVersion(); | ||
|
||
if (result.error) { | ||
log.error(`Error: ${result.error}`); | ||
return false; | ||
} | ||
|
||
return result.isNextjs15OrHigher; | ||
} catch (error) { | ||
log.error(`Unexpected error: ${error}`); | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/gluestack-cli/src/util/init/addReactNativeWebPatch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { log, spinner } from '@clack/prompts'; | ||
import { setupReactNativeWebPatch } from '../add-patch-file'; | ||
import { execSync } from 'child_process'; | ||
import { updatePackageJson } from './modify-package-json'; | ||
|
||
export async function addReactNativeWebPatch() { | ||
const s = spinner(); | ||
s.start('⏳ Adding react-native-web patch'); | ||
const isPatchAdded = await setupReactNativeWebPatch(); | ||
if (!isPatchAdded) { | ||
log.error('Failed to add react-native-web patch'); | ||
process.exit(1); | ||
} | ||
updatePackageJson(process.cwd()); | ||
s.stop(`\x1b[32mReact-native-web patch added.\x1b[0m`); | ||
} |
Oops, something went wrong.