diff --git a/packages/nativewind/utils/package.json b/packages/nativewind/utils/package.json index e8f8c99150..d596b5b5fe 100644 --- a/packages/nativewind/utils/package.json +++ b/packages/nativewind/utils/package.json @@ -17,7 +17,7 @@ "main": "index.js", "module": "index.js", "types": "index.d.ts", - "version": "1.0.15-alpha.0", + "version": "1.0.19", "react-native": "src/index", "source": "src/index", "scripts": { diff --git a/packages/nativewind/utils/scripts/modify-package-json.js b/packages/nativewind/utils/scripts/modify-package-json.js new file mode 100644 index 0000000000..29a9185fb9 --- /dev/null +++ b/packages/nativewind/utils/scripts/modify-package-json.js @@ -0,0 +1,24 @@ +const path = require('path'); +const fs = require('fs'); + +const updatePackageJson = (fileDir) => { + const filePath = path.join(fileDir, 'package.json'); + const data = require(filePath); + + if (!data?.scripts?.postinstall?.includes('patch-package')) { + const command = data?.scripts?.postinstall + ? 'patch-package && ' + data?.scripts?.postinstall + : 'patch-package'; + + const newData = { + ...data, + scripts: { ...data?.scripts, postinstall: command }, + }; + + fs.writeFile(filePath, JSON.stringify(newData, null, 2), (err) => { + if (err) throw err; + }); + } +}; + +module.exports = { updatePackageJson }; diff --git a/packages/nativewind/utils/scripts/post-install-script.js b/packages/nativewind/utils/scripts/post-install-script.js index 32d46aba69..7a7d0669c9 100644 --- a/packages/nativewind/utils/scripts/post-install-script.js +++ b/packages/nativewind/utils/scripts/post-install-script.js @@ -1,23 +1,26 @@ const fs = require('fs'); const path = require('path'); var finder = require('./find-package-json'); -const { spawnSync } = require('child_process'); +const { exec } = require('child_process'); const findWorkspaceRoot = require('find-yarn-workspace-root'); +const { updatePackageJson } = require('./modify-package-json'); const processPath = process.cwd(); const workspaceRoot = findWorkspaceRoot(processPath); const f = finder(path.join(processPath, '..')); -const userDirectory = f.next().filename.replace('package.json', ''); +const filename = f.next().filename; +const userDirectory = filename.replace('package.json', ''); + function CopyDirectory(src, dest) { if (!fs.existsSync(dest)) { fs.mkdirSync(dest); } - let entries = fs.readdirSync(src, { withFileTypes: true }); + const entries = fs.readdirSync(src, { withFileTypes: true }); - for (let entry of entries) { - let srcPath = path.join(src, entry.name); - let destPath = path.join(dest, entry.name); + for (const entry of entries) { + const srcPath = path.join(src, entry.name); + const destPath = path.join(dest, entry.name); if (entry.isDirectory()) { CopyDirectory(srcPath, destPath); @@ -27,42 +30,30 @@ function CopyDirectory(src, dest) { } } -function main() { - CopyDirectory( - path.join(processPath, 'scripts', 'patches'), - path.join(userDirectory, 'patches') - ); +function installPatch(cwd) { // use npm if user is using npm or yarn if user is using yarn - try { - const packageManager = fs.existsSync(path.join(userDirectory, 'yarn.lock')) - ? 'yarn' - : 'npm run'; - - spawnSync(packageManager, ['patch-package'], { - cwd: userDirectory, - stdio: 'inherit', - }); - } catch (error) {} + const packageManager = fs.existsSync(path.join(cwd, 'yarn.lock')) + ? 'yarn' + : 'npx'; + + const command = packageManager + ' patch-package'; + exec(command, { + cwd: cwd, + stdio: 'inherit', + }); +} +function main() { + let rootDir = userDirectory; if (workspaceRoot && workspaceRoot !== userDirectory) { - CopyDirectory( - path.join(processPath, 'scripts', 'patches'), - path.join(workspaceRoot, 'patches') - ); - - try { - const packageManager = fs.existsSync( - path.join(workspaceRoot, 'yarn.lock') - ) - ? 'yarn' - : 'npm run'; - - spawnSync(packageManager, ['patch-package'], { - cwd: workspaceRoot, - stdio: 'inherit', - }); - } catch (error) {} + rootDir = workspaceRoot; } + CopyDirectory( + path.join(processPath, 'scripts', 'patches'), + path.join(rootDir, 'patches') + ); + installPatch(rootDir); + updatePackageJson(rootDir); } main();