Skip to content

Commit

Permalink
Merge pull request #2138 from gluestack/fix/nw-patch
Browse files Browse the repository at this point in the history
Fix/nw patch
  • Loading branch information
akash3gtm authored May 10, 2024
2 parents a966825 + d648434 commit 0114e8e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
2 changes: 1 addition & 1 deletion packages/nativewind/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
24 changes: 24 additions & 0 deletions packages/nativewind/utils/scripts/modify-package-json.js
Original file line number Diff line number Diff line change
@@ -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 };
67 changes: 29 additions & 38 deletions packages/nativewind/utils/scripts/post-install-script.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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();

0 comments on commit 0114e8e

Please sign in to comment.