From 8c217dc20a80dd529c3942fc3fa0fd6434333f75 Mon Sep 17 00:00:00 2001 From: tmcdos Date: Sun, 21 Apr 2024 13:40:59 +0300 Subject: [PATCH] Added better handling for the case when patch is for older version of the NPM package and the file/folder for a given chunk does not exist any more. --- index.js | 22 +++++++++++++++++++--- package.json | 2 +- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index bc19f91..f2a05d7 100644 --- a/index.js +++ b/index.js @@ -373,9 +373,15 @@ function loadFile(info, callback) } */ const oldName = path.join(curDir, 'node_modules', pathNormalize(info.index)); - if(!fs.existsSync(oldName)) fs.writeFileSync(oldName, ''); - // read the original file - fs.readFile(oldName, 'utf8', callback); + if(fs.existsSync(oldName)) + { + // read the original file + fs.readFile(oldName, 'utf8', callback); + } + else + { + callback(null, ''); // old file does not exist - i.e. it is empty + } } function pathNormalize(pathName) @@ -440,6 +446,16 @@ function waitForResults() else { chunk.success = false; + const oldName = path.join(curDir, 'node_modules', pathNormalize(chunk.chunkInfo.index)); + if(!fs.existsSync(oldName)) + { + const folder = path.dirname(oldName); + if (!fs.existsSync(folder)) + { + echo(startColor('yellowBright') + 'WARNING: Folder ' + stopColor() + startColor('redBright') + path.dirname(pathNormalize(chunk.chunkInfo.index)) + stopColor() + startColor('yellowBright') + ' does not exist - the patch is probably for older version'); + return; + } + } echo(startColor('yellowBright') + 'WARNING: ' + stopColor() + 'Chunk failed - ' + startColor('redBright') + ' either already applied or for different version' + stopColor()); } }); diff --git a/package.json b/package.json index 9309cd1..ca06d4d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "custompatch", - "version": "1.0.26", + "version": "1.0.27", "description": "Tool for patching buggy NPM packages instead of forking them", "author": "IVO GELOV", "private": false,