Skip to content

Commit

Permalink
setup
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmondok committed Jan 23, 2023
0 parents commit 4395075
Show file tree
Hide file tree
Showing 11 changed files with 1,404 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"root": true,
"env": {
"es2020": true,
"node": true
},
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "prettier"]
}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules

# misc
.DS_Store
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"printWidth": 120,
"singleQuote": false
}
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# PHP Changelog

## [Initial Version] - 2023-01-23
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# PHP

Switch PHP version with Homebrew
Binary file added assets/icon-small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"$schema": "https://www.raycast.com/schemas/extension.json",
"name": "php",
"title": "Switch PHP version with Homebrew",
"description": "Switch PHP version with Homebrew",
"icon": "icon.png",
"author": "David",
"owner": "woda",
"license": "MIT",
"commands": [
{
"name": "php",
"title": "PHP",
"description": "Switch PHP version with Homebrew",
"mode": "view",
"preferences": [
{
"name": "7.4",
"required": false,
"title": "PHP 7.4",
"type": "checkbox",
"description": "PHP 7.4 installed?"
},
{
"name": "8.0",
"required": false,
"title": "PHP 8.0",
"type": "checkbox",
"description": "PHP 8.0 installed?"
},
{
"name": "8.1",
"required": false,
"title": "PHP 8.1",
"type": "checkbox",
"description": "PHP 8.1 installed?"
},
{
"name": "8.2",
"required": false,
"title": "PHP 8.2",
"type": "checkbox",
"description": "PHP 8.2 installed?"
}
]
}
],
"dependencies": {
"@raycast/api": "^1.46.0",
"@raycast/utils": "^1.4.16"
},
"devDependencies": {
"@types/node": "18.8.3",
"@types/react": "18.0.9",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"prettier": "^2.5.1",
"typescript": "^4.4.3"
},
"scripts": {
"build": "ray build -e dist",
"dev": "ray develop",
"fix-lint": "ray lint --fix",
"lint": "ray lint",
"publish": "ray publish"
}
}
124 changes: 124 additions & 0 deletions src/php.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import {
Action,
ActionPanel,
Clipboard,
Toast,
showToast,
popToRoot,
List,
} from '@raycast/api';
import { exec } from 'child_process';
import { useMemo, useState } from 'react';
import { useExec } from "@raycast/utils";

interface Version {
name: string;
value: string;
}

const phpRegex = new RegExp('^(php[\\s@])');
const versionRegex = new RegExp('^(\\d+\\.\\d+)');

const handleError = async (message: string) => {
await Clipboard.copy(message);
void showToast({
title: 'Error',
message: 'Message copied to your clipboard',
style: Toast.Style.Failure,
});
return;
}

const linkVersion = (version: Version, unlinkVersions: string[]) => {
showToast({
title: `Linking PHP ${version.name}`,
style: Toast.Style.Animated,
});
const commands: string[] = [];
unlinkVersions.forEach((v) => {
commands.push(`brew unlink ${v}`);
});
commands.push(`brew link ${version.value}`);
const command = commands.join(' && ');
exec(command, (error, _stdout, stderr) => {
if (error) {
return handleError(error.message);
}
if (stderr) {
return handleError(stderr);
}
showToast({
title: `Successfully linked PHP ${version.name}`,
style: Toast.Style.Success,
}).then(() => {
popToRoot();
});;
});
}

export default function Command() {
const { isLoading, data } = useExec('/usr/local/bin/brew', ['list', '--versions']);
const [linkingInProgress, setLinkingInProgress] = useState(false);
const [linkedVersion, setLinkedVersion] = useState('');

const versions: Version[] = useMemo(() => {
if (isLoading) {
return [];
}
const newVersions: Version[] = [];
const entries = (data || '').split('\n');
entries.forEach((entry) => {
if (!phpRegex.test(entry)) {
return;
}
const [name, version] = entry.split(' ');
const simpleVersion = versionRegex.exec(version);
if (!simpleVersion) {
return;
}
newVersions.push({ name: simpleVersion[0], value: name });
});
return newVersions;
}, [data, isLoading]);

return <List
navigationTitle="Link PHP Version"
searchBarPlaceholder="Link PHP Version"
>
{linkingInProgress || versions.length === 0 ? (
<List.EmptyView
title={linkingInProgress ? `Linking PHP ${linkedVersion}` : 'Loading...'}
icon={{ source: "icon-small.png" }}
/>
) : (
versions.map((version) => (
<List.Item
key={version.value}
title={version.name}
actions={
<ActionPanel>
<Action
title="Link"
onAction={() => {
setLinkingInProgress(true);
setLinkedVersion(version.name);
linkVersion(
version,
versions.reduce(
(acc, v) => {
if (v.value !== version.value) {
acc.push(v.value);
}
return acc;
},
[] as string[]
)
)
}} />
</ActionPanel>
}
/>
))
)}
</List>
}
17 changes: 17 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Node 16",
"include": ["src/**/*"],
"compilerOptions": {
"lib": ["es2021"],
"module": "commonjs",
"target": "es2021",
"strict": true,
"isolatedModules": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"jsx": "react-jsx",
"resolveJsonModule": true
}
}
Loading

0 comments on commit 4395075

Please sign in to comment.