Skip to content

Commit

Permalink
Merge pull request #36 from olegshulyakov/bugfix/243-error-execa
Browse files Browse the repository at this point in the history
jely2002#243 Fix setting chmod
  • Loading branch information
olegshulyakov authored Jun 18, 2022
2 parents b1aa8f9 + 5e98085 commit 9a24334
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
55 changes: 30 additions & 25 deletions modules/Filepaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class Filepaths {
this.unpackedPrefix = path.join(path.dirname(this.appPath), "app.asar.unpacked");
this.packedPrefix = this.appPath;
this.ffmpeg = this.app.isPackaged ? path.join(this.unpackedPrefix, "binaries") : "binaries";
this.ytdl = this.app.isPackaged ? path.join(this.unpackedPrefix, "binaries/yt-dlp.exe") : "binaries/yt-dlp.exe";
this.ytdl = path.join(this.ffmpeg, "yt-dlp.exe");
this.icon = this.app.isPackaged ? path.join(this.packedPrefix, "renderer/img/icon.png") : "renderer/img/icon.png";
this.settings = this.app.isPackaged ? path.join(this.unpackedPrefix, "userSettings") : "userSettings";
this.taskList = this.app.isPackaged ? path.join(this.unpackedPrefix, "taskList") : "taskList";
this.ytdlVersion = this.app.isPackaged ? path.join(this.unpackedPrefix, "binaries/ytdlVersion") :"binaries/ytdlVersion";
this.ffmpegVersion = this.app.isPackaged ? path.join(this.unpackedPrefix, "binaries/ffmpegVersion") :"binaries/ffmpegVersion";
this.ytdlVersion = path.join(this.ffmpeg, "ytdlVersion");
this.ffmpegVersion = path.join(this.ffmpeg, "ffmpegVersion");
break;
case "win32app": {
const appDir = path.basename(path.join(this.appPath, "../../..")).replace(/_(.*)_/g, "_");
Expand All @@ -31,12 +31,12 @@ class Filepaths {
this.packedPrefix = this.appPath;
await this.createFolder(this.persistentPath);
this.ffmpeg = this.binaryPath;
this.ytdl = path.join(this.binaryPath, "yt-dlp.exe");
this.ytdl = path.join(this.ffmpeg, "yt-dlp.exe");
this.icon = path.join(this.packedPrefix, "renderer/img/icon.png");
this.settings = path.join(this.binaryPath, "userSettings");
this.taskList = path.join(this.binaryPath, "taskList");
this.ytdlVersion = path.join(this.binaryPath, "ytdlVersion");
this.ffmpegVersion = path.join(this.binaryPath, "ffmpegVersion");
this.settings = path.join(this.ffmpeg, "userSettings");
this.taskList = path.join(this.ffmpeg, "taskList");
this.ytdlVersion = path.join(this.ffmpeg, "ytdlVersion");
this.ffmpegVersion = path.join(this.ffmpeg, "ffmpegVersion");
break;
}
case "win32portable":
Expand All @@ -45,37 +45,37 @@ class Filepaths {
this.packedPrefix = this.appPath;
await this.createPortableFolder();
this.ffmpeg = this.persistentPath;
this.ytdl = path.join(this.persistentPath, "yt-dlp.exe");
this.ytdl = path.join(this.ffmpeg, "yt-dlp.exe");
this.icon = path.join(this.packedPrefix, "renderer/img/icon.png");
this.settings = path.join(this.persistentPath, "userSettings");
this.taskList = path.join(this.persistentPath, "taskList");
this.ytdlVersion = path.join(this.persistentPath, "ytdlVersion");
this.ffmpegVersion = path.join(this.persistentPath, "ffmpegVersion");
this.settings = path.join(this.ffmpeg, "userSettings");
this.taskList = path.join(this.ffmpeg, "taskList");
this.ytdlVersion = path.join(this.ffmpeg, "ytdlVersion");
this.ffmpegVersion = path.join(this.ffmpeg, "ffmpegVersion");
break;
case "darwin":
this.packedPrefix = this.appPath;
this.unpackedPrefix = this.appPath + ".unpacked";
this.ffmpeg = this.app.isPackaged ? path.join(this.unpackedPrefix, "binaries") : "binaries";
this.ytdl = this.app.isPackaged ? path.join(this.unpackedPrefix, "binaries/yt-dlp_macos") : "binaries/yt-dlp_macos";
this.ytdl = path.join(this.ffmpeg, "yt-dlp_macos");
this.icon = this.app.isPackaged ? path.join(this.packedPrefix, "renderer/img/icon.png") : "renderer/img/icon.png";
this.settings = this.app.isPackaged ? path.join(this.unpackedPrefix, "userSettings") : "userSettings";
this.taskList = this.app.isPackaged ? path.join(this.unpackedPrefix, "taskList") : "taskList";
this.ytdlVersion = this.app.isPackaged ? path.join(this.unpackedPrefix, "binaries/ytdlVersion") :"binaries/ytdlVersion";
this.ffmpegVersion = this.app.isPackaged ? path.join(this.unpackedPrefix, "binaries/ffmpegVersion") :"binaries/ffmpegVersion";
this.ytdlVersion = path.join(this.ffmpeg, "ytdlVersion");
this.ffmpegVersion = path.join(this.ffmpeg, "ffmpegVersion");
this.setPermissions()
break;
case "linux":
this.persistentPath = path.join(this.app.getPath('home'), ".youtube-dl-gui");
this.packedPrefix = this.appPath;
this.unpackedPrefix = this.appPath + ".unpacked";
if(this.app.isPackaged) await this.createFolder(this.persistentPath);
this.ytdl = this.app.isPackaged ? path.join(this.persistentPath, "yt-dlp") : "binaries/yt-dlp";
this.ffmpeg = this.app.isPackaged ? this.persistentPath : "binaries";
this.ytdl = path.join(this.ffmpeg, "yt-dlp");
this.icon = this.app.isPackaged ? path.join(this.packedPrefix, "renderer/img/icon.png") : "renderer/img/icon.png";
this.settings = this.app.isPackaged ? path.join(this.persistentPath, "userSettings") : "userSettings";
this.taskList = this.app.isPackaged ? path.join(this.persistentPath, "taskList") : "taskList";
this.ytdlVersion = this.app.isPackaged ? path.join(this.persistentPath, "ytdlVersion") :"binaries/ytdlVersion";
this.ffmpegVersion = this.app.isPackaged ? path.join(this.persistentPath, "ffmpegVersion") :"binaries/ffmpegVersion";
this.ytdlVersion = path.join(this.ffmpeg, "ytdlVersion");
this.ffmpegVersion = path.join(this.ffmpeg, "ffmpegVersion");
this.setPermissions()
break;
}
Expand Down Expand Up @@ -116,12 +116,17 @@ class Filepaths {
}

setPermissions() {
fs.readdirSync(this.ffmpeg).forEach(file => {
if (file === "userSettings" || file === "ytdlVersion" || file === "taskList" || file === "ffmpegVersion") return;
fs.chmod(path.join(this.ffmpeg, file), 0o755, (err) => {
if(err) console.error(err);
});
});
if (!fs.existsSync(this.ffmpeg)) {
fs.mkdirSync(this.ffmpeg, {recursive: true, mode: 0o744});
}
for (const file of fs.readdirSync(this.ffmpeg)) {
if (file === "userSettings" || file === "ytdlVersion" || file === "taskList" || file === "ffmpegVersion") continue;
try {
fs.chmodSync(path.join(this.ffmpeg, file), 0o755);
} catch (err) {
console.error(`Cannot set permissions for ${file}. Error: ${err}`);
}
}
}

async createPortableFolder() {
Expand Down
14 changes: 9 additions & 5 deletions tests/Filepaths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jest.mock('mkdirp');

beforeEach(() => {
jest.clearAllMocks();
fs.chmod = jest.fn();
fs.chmodSync = jest.fn();
jest.doMock('mkdirp', () => {
const originalModule = jest.requireActual('mkdirp')
return {
Expand All @@ -24,11 +24,12 @@ describe('set executable permissions', () => {
instance.ffmpeg = "ffmpeg/path/";
const ytdlp = "yt-dlp.exe";
const taskList = "taskList";
fs.existsSync = jest.fn(v => instance.ffmpeg === v);
fs.readdirSync = jest.fn().mockReturnValue([ytdlp, taskList]);
instance.setPermissions();
expect(fs.chmod).toBeCalledTimes(1);
expect(fs.chmod.mock.calls[0]).toContain(path.join(instance.ffmpeg, ytdlp));
expect(fs.chmod.mock.calls[0]).toContain(493);
expect(fs.chmodSync).toBeCalledTimes(1);
expect(fs.chmodSync.mock.calls[0]).toContain(path.join(instance.ffmpeg, ytdlp));
expect(fs.chmodSync.mock.calls[0]).toContain(493);
});
});

Expand Down Expand Up @@ -56,7 +57,9 @@ describe('generate filepaths', () => {
const joinSpy = jest.spyOn(path, 'join').mockReturnValue("path");
jest.spyOn(instance, 'removeLeftOver').mockImplementation(() => Promise.resolve());
await instance.generateFilepaths();
if(platform === "linux" || platform === "win32") expect(joinSpy).toBeCalledTimes(1);
if(platform === "linux") expect(joinSpy).toBeCalledTimes(4);
else if(platform === "darwin") expect(joinSpy).toBeCalledTimes(3);
else if(platform === "win32") expect(joinSpy).toBeCalledTimes(4);
else expect(joinSpy).not.toBeCalled();
joinSpy.mockRestore();
}
Expand Down Expand Up @@ -128,5 +131,6 @@ function instanceBuilder(packaged, portable) {
getPath: jest.fn(() => "path/to/downloads"),
getAppPath: jest.fn(() => portable ? "\\AppData\\Local\\Temp\\" : "path/to/application")
}
fs.existsSync = jest.fn(v => app.getPath() === v);
return new Filepaths(app);
}

0 comments on commit 9a24334

Please sign in to comment.