Skip to content

Commit

Permalink
feat: treats folder and link not found exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
callmeteus committed Dec 5, 2024
1 parent 5960596 commit 3cbcf0c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/core/commands/mklink.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as fs from "fs";
import { glob } from "glob";
import * as path from "path";
import * as fs from "fs";
import { logger } from "../Logger";
import { Yarn } from "../../helpers/Yarn";
import { logger } from "../Logger";

/**
* Creates multiple links for a given pattern.
Expand Down Expand Up @@ -30,6 +30,12 @@ export const mklink = async(link: {
ignore: "node_modules/**"
});

// If there's no possible project paths
if (!possibleProjectPaths.length) {
logger.error("found no folders matching the pattern \"%s\"", link.pattern);
return;
}

for (const projectPath of possibleProjectPaths) {
const packageJsonPath = path.resolve(projectPath, "package.json");

Expand Down
7 changes: 4 additions & 3 deletions src/core/commands/resetGlobalLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { Yarn } from "../../helpers/Yarn";
import { logger } from "../Logger";

/**
* Resets global links.
* @param opts Any options to be passed to the resetter.
*/
* Resets global links.
* This operation will unlink all globally linked packages.
* @param opts Any options to be passed to the resetter.
*/
export const resetGlobalLinks = async (opts?: {
/**
* Will only delete broken symlinks.
Expand Down
15 changes: 14 additions & 1 deletion src/helpers/Yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { execSync } from "child_process";
import * as fs from "fs";
import * as path from "path";

import { ExternalYarn, execYarn as invokeYarn } from "./ExternalYarn";
import { logger } from "../core/Logger";
import { ExternalYarn, execYarn as invokeYarn } from "./ExternalYarn";

export interface IYarnExecOptions {
cmd: string;
Expand Down Expand Up @@ -275,6 +275,19 @@ export class Yarn {
}) {
const linksPath = await this.getGlobalLinksPath();

// Throw if the path doesn't exist
if (!fs.existsSync(linksPath)) {
const err = new Error("The global links path wasn't found.\nThis could mean that no links were created yet.\nYou can create a link by running `nayr link` in the package folder.") as Error & {
code: string;
path: string;
};

err.code = "ENOENT";
err.path = linksPath;

throw err;
}

const rootItems = await fs.promises.readdir(linksPath, {
recursive: false
});
Expand Down

0 comments on commit 3cbcf0c

Please sign in to comment.