Skip to content

Commit

Permalink
fix(cli): portable config not creating folder correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
bambanah committed Oct 30, 2024
1 parent 53cd45f commit 4ee0ced
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-snakes-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"deemix-cli": minor
---

Correctly create portable config
1 change: 1 addition & 0 deletions cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config/
13 changes: 13 additions & 0 deletions cli/src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
type Settings,
} from "deemix";
import { TrackFormats, type Deezer } from "deezer-sdk";
import { configFolder } from "./main";

const listener: Listener = {
send: (key: string, data?: unknown) => {
Expand Down Expand Up @@ -35,12 +36,24 @@ export const downloadLinks = async (
{ spotify: spotifyPlugin },
listener
);

if (Array.isArray(downloadObject)) {
downloadObjects.concat(downloadObject);
} else {
downloadObjects.push(downloadObject);
}
} catch (e) {
if (e instanceof Error && e.name === "PluginNotEnabledError") {
console.warn(
`Populate Spotify app credentials to download Spotify links:\n\t${configFolder}spotify/config.json\n`
);
console.log(
"Documentation: https://developer.spotify.com/documentation/web-api/tutorials/getting-started#create-an-app"
);

continue;
}

if (e instanceof Error) {
console.error(e);
}
Expand Down
27 changes: 17 additions & 10 deletions cli/src/login.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Deezer } from "deezer-sdk";
import { existsSync, readFileSync, writeFileSync } from "fs";
import { existsSync, readFileSync, rmSync, writeFileSync } from "fs";
import readline from "node:readline/promises";
import path from "path";

Expand All @@ -8,16 +8,23 @@ export const deezerLogin = async (dz: Deezer, configFolder: string) => {

if (existsSync(arlFileLocation)) {
const arl = readFileSync(arlFileLocation).toString().trim();
await dz.loginViaArl(arl);
} else {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const arl = await rl.question("Enter your ARL: ");
await dz.loginViaArl(arl);
writeFileSync(arlFileLocation, arl);
const loggedIn = await dz.loginViaArl(arl);

if (loggedIn) {
return true;
}

// If the ARL is invalid, remove it
rmSync(arlFileLocation);
}

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const arl = await rl.question("Enter your ARL: ");
await dz.loginViaArl(arl);
writeFileSync(arlFileLocation, arl);

return dz.loggedIn;
};
15 changes: 11 additions & 4 deletions cli/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Command } from "commander";
import { loadSettings, SpotifyPlugin, utils } from "deemix";
import { Deezer, setDeezerCacheDir } from "deezer-sdk";
import path from "path";
import path, { sep } from "path";
import packageJson from "../package.json" with { type: "json" };
import { parseBitrate } from "./bitrate";
import { downloadLinks } from "./download";
Expand All @@ -17,7 +17,10 @@ program
.version(packageJson.version)
.argument("<url>", "The URL of the track or playlist")
.option("-p, --path <path>", "Downloads in the given folder")
.option("-b, --bitrate <type>", "Overrides the default bitrate selected")
.option(
"-b, --bitrate <type>",
"Overrides the default bitrate selected - 128, 320, flac"
)
.option(
"--portable",
"Creates the config folder in the same directory where the script is launched"
Expand All @@ -26,10 +29,10 @@ program

const { portable } = program.opts();

let configFolder: string;
export let configFolder: string;

if (portable) {
configFolder = path.join(process.cwd(), "config");
configFolder = path.join(process.cwd(), `config${sep}`);
} else {
configFolder = getConfigFolder();
}
Expand All @@ -53,8 +56,12 @@ const loginAndDownload = async () => {
const urls = program.args;

await downloadLinks(dz, urls, settings, spotifyPlugin);

process.exit(0);
} else {
console.error("Not logged in");

process.exit(1);
}
};

Expand Down
9 changes: 9 additions & 0 deletions deemix/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ export class DownloadError extends DeemixError {
}
}

export class PluginNotEnabledError extends DeemixError {
constructor(pluginName: string) {
const message = `${pluginName} plugin not enabled`;
super(message);

this.name = "PluginNotEnabledError";
}
}

export const ErrorMessages = {
notOnDeezer: "Track not available on Deezer!",
notEncoded: "Track not yet encoded!",
Expand Down
13 changes: 9 additions & 4 deletions deemix/src/plugins/spotify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Collection, Convertable } from "@/download-objects/Collection.js";
import { generateAlbumItem } from "@/download-objects/generateAlbumItem.js";
import { generateTrackItem } from "@/download-objects/generateTrackItem.js";
import { AlbumNotOnDeezer, InvalidID, TrackNotOnDeezer } from "@/errors.js";
import {
AlbumNotOnDeezer,
InvalidID,
PluginNotEnabledError,
TrackNotOnDeezer,
} from "@/errors.js";
import { type Settings } from "@/types/Settings.js";
import { getConfigFolder } from "@/utils/localpaths.js";
import {
Expand Down Expand Up @@ -162,7 +167,7 @@ export default class SpotifyPlugin extends BasePlugin {
}

async generatePlaylistItem(dz: Deezer, link_id: string, bitrate: number) {
if (!this.enabled) throw new Error("Spotify plugin not enabled");
if (!this.enabled) throw new PluginNotEnabledError("Spotify");

const spotifyPlaylist = await this.sp.playlists.getPlaylist(link_id);

Expand Down Expand Up @@ -217,7 +222,7 @@ export default class SpotifyPlugin extends BasePlugin {
}

async getTrack(track_id: string, spotifyTrack?: SpotifyTrack) {
if (!this.enabled) throw new Error("Spotify plugin not enabled");
if (!this.enabled) throw new PluginNotEnabledError("Spotify");

const cachedTrack = {
isrc: null,
Expand Down Expand Up @@ -247,7 +252,7 @@ export default class SpotifyPlugin extends BasePlugin {
}

async getAlbum(album_id: string, spotifyAlbum = null) {
if (!this.enabled) throw new Error("Spotify plugin not enabled");
if (!this.enabled) throw new PluginNotEnabledError("Spotify");
const cachedAlbum = {
upc: null,
data: null,
Expand Down

0 comments on commit 4ee0ced

Please sign in to comment.