Skip to content

feat(get): set cacheDir on another volume #1023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"axios": "^1.6.7",
"cli-progress": "^3.12.0",
"compressing": "^1.10.0",
"fs-extra": "^11.2.0",
"glob": "^10.3.10",
"node-gyp": "^10.0.1",
"plist": "^3.1.0",
Expand Down
6 changes: 5 additions & 1 deletion src/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ const getNodeHeaders = async (options) => {
}

const createSymlinks = async (options) => {
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");
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")
// Allow resolve cacheDir from another directory for prevent crash
if (!fs.lstatSync(frameworksPath).isDirectory()) {
frameworksPath = path.resolve(options.cacheDir, `nwjs${options.flavor === "sdk" ? "-sdk" : ""}-v${options.version}-${options.platform}-${options.arch}`, "nwjs.app", "Contents", "Frameworks", "nwjs Framework.framework")
}
const symlinks = [
path.join(frameworksPath, "Helpers"),
path.join(frameworksPath, "Libraries"),
Expand Down
53 changes: 48 additions & 5 deletions src/get/decompress.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import stream from "node:stream";

import tar from "tar";
import yauzl from "yauzl-promise";
import {ensureSymlink} from "fs-extra";

/**
* Decompresses a file at `filePath` to `cacheDir` directory.
Expand Down Expand Up @@ -32,19 +33,61 @@ export default async function decompress(filePath, cacheDir) {
* @return {Promise<void>}
*/
export async function unzip(zippedFile, cacheDir) {
await unzipInternal(zippedFile, cacheDir, false).then(() => {
unzipInternal(zippedFile, cacheDir, true);
})
}

/**
* Method for unzip with symlink in theoretical
*
* @async
* @function
* @param unzipSymlink
* @param {string} zippedFile - file path to .zip file
* @param {string} cacheDir - directory to unzip in
* @param {boolean} unzipSymlink - Using or not symlink
* @return {Promise<void>}
*/
async function unzipInternal(zippedFile, cacheDir, unzipSymlink ) {
const zip = await yauzl.open(zippedFile);

let entry = await zip.readEntry();

while (entry !== null) {
// console.log(entry)
let entryPathAbs = path.join(cacheDir, entry.filename);

// Create the directory beforehand to prevent `ENOENT: no such file or directory` errors.
await fs.promises.mkdir(path.dirname(entryPathAbs), { recursive: true });

await fs.promises.mkdir(path.dirname(entryPathAbs), {recursive: true});
const readStream = await entry.openReadStream();
const writeStream = fs.createWriteStream(entryPathAbs);
await stream.promises.pipeline(readStream, writeStream);

try {
if (!unzipSymlink) {
// Regular method and silent error at this point
const writeStream = fs.createWriteStream(entryPathAbs);
await stream.promises.pipeline(readStream, writeStream);
} else {
// Need check before if file is a symlink or not at this point
const pathContent = await fs.promises.lstat(entryPathAbs);

if (pathContent.isSymbolicLink()) {
const chunks = [];
readStream.on('data', (chunk) => chunks.push(chunk));
await stream.promises.finished(readStream);
// need fetch value of current symlink here
const linkTarget = Buffer.concat(chunks).toString('utf8').trim();
await ensureSymlink(entryPathAbs, path.join(path.dirname(entryPathAbs), linkTarget));
}else{
// Regular method and silent error at this point
const writeStream = fs.createWriteStream(entryPathAbs);
await stream.promises.pipeline(readStream, writeStream);
}
}
} catch (error) {
if (unzipSymlink) {
console.error(error);
}
}

entry = await zip.readEntry();
}
Expand Down
7 changes: 3 additions & 4 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,11 @@ const replaceFfmpeg = async (platform, nwDir) => {
*
* @async
* @function
*
*
* @param {object} options - glob file options
* @param {string | string[]} options.srcDir - app src dir
* @param {boolean} options.glob - glob flag
* @return {Promise<string[]>} - Returns array of file paths

*/
async function globFiles({
srcDir,
Expand Down Expand Up @@ -449,7 +448,7 @@ export const validate = async (options, releaseInfo) => {
*
* @async
* @function
*
*
* @param {"chromedriver"} type - NW specific file or directory
* @param {object} options - nwbuild options
* @return {string} - Path to chromedriver
Expand All @@ -470,7 +469,7 @@ async function getPath(type, options) {

/**
*
* @param {string} filePath - File path to check existence of
* @param {string} filePath - File path to check existence of
* @return {Promise<boolean>} `true` if exists, otherwise `false`
*/
async function fileExists(filePath) {
Expand Down