Skip to content
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

Fix LaunchAgents folder creation for inexistent folder #14053

Merged
merged 3 commits into from
Aug 22, 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
2 changes: 2 additions & 0 deletions extensions/launch-agents/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Launch Agents Extension Changelog

## [Guarantee that the user launch agents folder exists] - 2024-08-22

## [Initial Version] - 2024-08-16
2 changes: 1 addition & 1 deletion extensions/launch-agents/components/LaunchAgentDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default function LaunchAgentDetails({

const deleteFile = () => {
try {
execSync(`rm ${selectedFile}`);
execSync(`rm -rf ${selectedFile}`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using the built-in feature trash here

Copy link
Contributor Author

@stevensd2m stevensd2m Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but that only accepts a file right? this change adapts so that it can also remove if it is a folder for some reason.

nvm, just saw that it states that it can be handled by folder.

nonetheless, could it be after or do u think necessary on the PR?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do it later on for sure 🙂

showToast(Toast.Style.Success, "File Deleted", `${selectedFile} has been deleted.`);
refreshList();
pop();
Expand Down
16 changes: 9 additions & 7 deletions extensions/launch-agents/components/LaunchAgentList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Action, ActionPanel, Color, Icon, List } from "@raycast/api";
import { execSync } from "child_process";
import { useEffect, useState } from "react";
import { createLaunchAgent } from "../lib/plist";
import { getFileName } from "../lib/utils";
import LaunchAgentDetails from "./LaunchAgentDetails";

Expand All @@ -13,10 +14,7 @@ const EmptyView = () => (
<Action
icon={{ source: Icon.NewDocument, tintColor: Color.Green }}
title="Create Launch Agent"
onAction={() => {
const fileName = `com.raycast.${Math.random()}`;
execSync(`touch ~/Library/LaunchAgents/${fileName}.plist`);
}}
onAction={createLaunchAgent}
/>
</ActionPanel>
}
Expand All @@ -29,11 +27,15 @@ export default function LaunchAgentList() {

const loadLaunchAgentsFiles = () => {
try {
const launchAgentsOutput = execSync("ls ~/Library/LaunchAgents/*.plist").toString();
const launchAgentsFiles = launchAgentsOutput.split("\n").filter((file) => file.trim() !== "");
const userLaunchAgentsOutput = execSync("ls ~/Library/LaunchAgents/*.plist").toString();
const launchAgentsFiles = userLaunchAgentsOutput.split("\n").filter((file) => file.trim() !== "");
setLaunchAgentsFiles(launchAgentsFiles);
} catch (error) {
console.error("Error fetching launch agents files:", error);
if (error instanceof Error && error.message.includes("No such file or directory")) {
setLaunchAgentsFiles([]);
} else {
console.error("Error fetching launch agents files:", error);
}
}
};

Expand Down
10 changes: 10 additions & 0 deletions extensions/launch-agents/lib/plist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ export const unloadLaunchAgent = (filePath: string) => {
console.error("Error unloading file:", error);
}
};

export const createLaunchAgent = () => {
const userLaunchAgentsPath = "~/Library/LaunchAgents";
const fileName = `com.raycast.${Math.random()}`;

execSync(`mkdir -p ${userLaunchAgentsPath}`);
execSync(`touch ${userLaunchAgentsPath}/${fileName}.plist`);

return fileName;
};
2 changes: 1 addition & 1 deletion extensions/launch-agents/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
"prepublishOnly": "echo \"\\n\\nIt seems like you are trying to publish the Raycast extension to npm.\\n\\nIf you did intend to publish it to npm, remove the \\`prepublishOnly\\` script and rerun \\`npm publish\\` again.\\nIf you wanted to publish it to the Raycast Store instead, use \\`npm run publish\\` instead.\\n\\n\" && exit 1",
"publish": "npx @raycast/api@latest publish"
}
}
}
5 changes: 2 additions & 3 deletions extensions/launch-agents/src/create-launch-agent.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { showToast, Toast } from "@raycast/api";
import { execSync } from "child_process";
import { createLaunchAgent } from "../lib/plist";

export default function Command() {
try {
const fileName = `com.raycast.${Math.random()}`;
execSync(`touch ~/Library/LaunchAgents/${fileName}.plist`);
const fileName = createLaunchAgent();
showToast(Toast.Style.Success, "File Created", `Name: ${fileName}.plist`);
} catch (error) {
console.error("Error creating file:", error);
Expand Down