Skip to content

Commit 7c0f711

Browse files
authored
feat(get): set cacheDir on another volume (#1023)
### Description of Changes - Use both absolute and relative paths for the cache directory. - Fixes the issue on MacOS related to the existence of a cache folder that is not present in the same current directory. - Correctly unzips files in MacOs. Notes: Resolves issued caused by cache is in a directory other than the current path. Closes: #1017
1 parent 851afad commit 7c0f711

File tree

5 files changed

+90
-10
lines changed

5 files changed

+90
-10
lines changed

package-lock.json

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"axios": "^1.6.7",
6464
"cli-progress": "^3.12.0",
6565
"compressing": "^1.10.0",
66+
"fs-extra": "^11.2.0",
6667
"glob": "^10.3.10",
6768
"node-gyp": "^10.0.1",
6869
"plist": "^3.1.0",

src/get.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ const getNodeHeaders = async (options) => {
209209
}
210210

211211
const createSymlinks = async (options) => {
212-
const frameworksPath = path.join(process.cwd(), options.cacheDir, `nwjs${options.flavor === "sdk" ? "-sdk" : ""}-v${options.version}-${options.platform}-${options.arch}`, "nwjs.app", "Contents", "Frameworks", "nwjs Framework.framework");
212+
let frameworksPath = path.resolve(process.cwd(), options.cacheDir, `nwjs${options.flavor === "sdk" ? "-sdk" : ""}-v${options.version}-${options.platform}-${options.arch}`, "nwjs.app", "Contents", "Frameworks", "nwjs Framework.framework")
213+
// Allow resolve cacheDir from another directory for prevent crash
214+
if (!fs.lstatSync(frameworksPath).isDirectory()) {
215+
frameworksPath = path.resolve(options.cacheDir, `nwjs${options.flavor === "sdk" ? "-sdk" : ""}-v${options.version}-${options.platform}-${options.arch}`, "nwjs.app", "Contents", "Frameworks", "nwjs Framework.framework")
216+
}
213217
const symlinks = [
214218
path.join(frameworksPath, "Helpers"),
215219
path.join(frameworksPath, "Libraries"),

src/get/decompress.js

+48-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import stream from "node:stream";
44

55
import tar from "tar";
66
import yauzl from "yauzl-promise";
7+
import {ensureSymlink} from "fs-extra";
78

89
/**
910
* Decompresses a file at `filePath` to `cacheDir` directory.
@@ -32,19 +33,61 @@ export default async function decompress(filePath, cacheDir) {
3233
* @return {Promise<void>}
3334
*/
3435
export async function unzip(zippedFile, cacheDir) {
36+
await unzipInternal(zippedFile, cacheDir, false).then(() => {
37+
unzipInternal(zippedFile, cacheDir, true);
38+
})
39+
}
40+
41+
/**
42+
* Method for unzip with symlink in theoretical
43+
*
44+
* @async
45+
* @function
46+
* @param unzipSymlink
47+
* @param {string} zippedFile - file path to .zip file
48+
* @param {string} cacheDir - directory to unzip in
49+
* @param {boolean} unzipSymlink - Using or not symlink
50+
* @return {Promise<void>}
51+
*/
52+
async function unzipInternal(zippedFile, cacheDir, unzipSymlink ) {
3553
const zip = await yauzl.open(zippedFile);
3654

3755
let entry = await zip.readEntry();
3856

3957
while (entry !== null) {
58+
// console.log(entry)
4059
let entryPathAbs = path.join(cacheDir, entry.filename);
41-
4260
// Create the directory beforehand to prevent `ENOENT: no such file or directory` errors.
43-
await fs.promises.mkdir(path.dirname(entryPathAbs), { recursive: true });
44-
61+
await fs.promises.mkdir(path.dirname(entryPathAbs), {recursive: true});
4562
const readStream = await entry.openReadStream();
46-
const writeStream = fs.createWriteStream(entryPathAbs);
47-
await stream.promises.pipeline(readStream, writeStream);
63+
64+
try {
65+
if (!unzipSymlink) {
66+
// Regular method and silent error at this point
67+
const writeStream = fs.createWriteStream(entryPathAbs);
68+
await stream.promises.pipeline(readStream, writeStream);
69+
} else {
70+
// Need check before if file is a symlink or not at this point
71+
const pathContent = await fs.promises.lstat(entryPathAbs);
72+
73+
if (pathContent.isSymbolicLink()) {
74+
const chunks = [];
75+
readStream.on('data', (chunk) => chunks.push(chunk));
76+
await stream.promises.finished(readStream);
77+
// need fetch value of current symlink here
78+
const linkTarget = Buffer.concat(chunks).toString('utf8').trim();
79+
await ensureSymlink(entryPathAbs, path.join(path.dirname(entryPathAbs), linkTarget));
80+
}else{
81+
// Regular method and silent error at this point
82+
const writeStream = fs.createWriteStream(entryPathAbs);
83+
await stream.promises.pipeline(readStream, writeStream);
84+
}
85+
}
86+
} catch (error) {
87+
if (unzipSymlink) {
88+
console.error(error);
89+
}
90+
}
4891

4992
entry = await zip.readEntry();
5093
}

src/util.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,11 @@ const replaceFfmpeg = async (platform, nwDir) => {
162162
*
163163
* @async
164164
* @function
165-
*
165+
*
166166
* @param {object} options - glob file options
167167
* @param {string | string[]} options.srcDir - app src dir
168168
* @param {boolean} options.glob - glob flag
169169
* @return {Promise<string[]>} - Returns array of file paths
170-
171170
*/
172171
async function globFiles({
173172
srcDir,
@@ -449,7 +448,7 @@ export const validate = async (options, releaseInfo) => {
449448
*
450449
* @async
451450
* @function
452-
*
451+
*
453452
* @param {"chromedriver"} type - NW specific file or directory
454453
* @param {object} options - nwbuild options
455454
* @return {string} - Path to chromedriver
@@ -470,7 +469,7 @@ async function getPath(type, options) {
470469

471470
/**
472471
*
473-
* @param {string} filePath - File path to check existence of
472+
* @param {string} filePath - File path to check existence of
474473
* @return {Promise<boolean>} `true` if exists, otherwise `false`
475474
*/
476475
async function fileExists(filePath) {

0 commit comments

Comments
 (0)