-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Find and copy snippet from Cacher libraries (#1)
- Bootstraps workflow with Alfy - Adds JS code to pull data from authenticated Cacher endpoint - Adds fuzzy search using fuse.js - Adds README docs on installation and usage
- Loading branch information
Showing
12 changed files
with
1,182 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Editor configuration, see http://editorconfig.org | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 4 | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
max_line_length = off | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,10 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
# IDE | ||
.idea | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# Typescript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
# NPM | ||
node_modules | ||
|
||
# Ignored directories | ||
build | ||
scripts | ||
*.alfredworkflow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,53 @@ | ||
# alfred-cacher | ||
Alfred workflow for finding a Cacher snippet. | ||
> Alfred Workflow for finding a snippet from your [Cacher](https://www.cacher.io) library. | ||
![Demo](/media/demo.gif "Demo") | ||
|
||
## Prerequisites | ||
|
||
- [Alfred 3 for Mac](https://www.alfredapp.com/) | ||
|
||
- [Node.js](https://nodejs.org/en/download/) | ||
|
||
- You are registered as a user at [cacher.io](https://www.cacher.io). Cacher is a code snippet organizer for | ||
software developers. [Sign up for a free account](https://www.cacher.io). | ||
|
||
## Installation | ||
|
||
### Step 1 | ||
|
||
Run the following shell command from a Terminal window. | ||
|
||
```bash | ||
npm install --global @cacherapp/alfred-cacher | ||
``` | ||
|
||
### Step 2 | ||
|
||
1. Open Cacher and click on the "Apps" icon in the top bar. | ||
2. Note the API Key and Token in the footer. | ||
|
||
![Get API Key and Token](/media/get-key-token.gif "Get API Key and Token") | ||
|
||
### Step 3 | ||
|
||
1. Open Alfred Preferences. | ||
2. Click on the "Workflows" tab and find Cacher. | ||
3. Click on the "Configure workflow and variables" button. | ||
4. Add the following under Workflow Environment Variables: | ||
- `CACHER_API_KEY`: `[API Key from Step 2]` | ||
- `CACHER_API_TOKEN`: `[API Token from Step 2]` | ||
5. Click "Save" | ||
|
||
![Set Environment Variables](/media/alfred-env-vars.gif "Set Environment Variables") | ||
|
||
## Usage | ||
|
||
In Alfred, type `snip [keywords]` and wait for results to return. Select a snippet and press <kbd>Enter</kbd> to copy | ||
its contents to the clipboard. | ||
|
||
## Libraries Used | ||
|
||
- [alfy](https://github.com/sindresorhus/alfy) - Create Alfred Workflows using Node | ||
- [Fuse.js](https://github.com/krisk/Fuse) - Lightweight fuzzy-search | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const alfy = require('alfy'); | ||
const Fuse = require('fuse.js'); | ||
|
||
const CACHER_API_HOST = process.env['CACHER_API_HOST'] || 'https://api.cacher.io'; | ||
const API_KEY = process.env['CACHER_API_KEY']; | ||
const API_TOKEN = process.env['CACHER_API_TOKEN']; | ||
|
||
const CACHE_MAX_AGE = 1000 * 5; | ||
|
||
const FETCH_OPTIONS = { | ||
headers: { | ||
'X-Api-Key': API_KEY, | ||
'X-Api-Token': API_TOKEN | ||
}, | ||
maxAge: CACHE_MAX_AGE | ||
}; | ||
|
||
if (!API_KEY || !API_TOKEN) { | ||
alfy.error('Set "CACHER_API_KEY" and "CACHER_API_TOKEN" workflow variables.'); | ||
return; | ||
} | ||
|
||
alfy.fetch( | ||
`${CACHER_API_HOST}/integrations/show_all`, | ||
FETCH_OPTIONS | ||
).then(data => { | ||
let teamSnippets = data.teams.reduce((allSnippets, team) => { | ||
return allSnippets.concat(team.library.snippets); | ||
}, []); | ||
|
||
let allSnippets = data.personalLibrary.snippets.concat(teamSnippets); | ||
|
||
let options = { | ||
threshold: 0.6, | ||
location: 0, | ||
distance: 100, | ||
maxPatternLength: 30, | ||
minMatchCharLength: 1, | ||
shouldSort: true, | ||
keys: [ | ||
{ | ||
name: 'title', | ||
weight: 0.9 | ||
}, | ||
{ | ||
name: 'description', | ||
weight: 0.1 | ||
} | ||
] | ||
}; | ||
|
||
let fuse = new Fuse(allSnippets, options); | ||
const items = fuse.search(alfy.input).map(x => { | ||
let firstFile = x.files[0]; | ||
let firstFileContent = firstFile.content; | ||
|
||
return { | ||
title: x.title, | ||
subtitle: firstFileContent, | ||
text: { | ||
copy: firstFileContent, | ||
largetype: x.description | ||
}, | ||
arg: firstFileContent | ||
}; | ||
}); | ||
|
||
alfy.output(items); | ||
}); |
Oops, something went wrong.