Skip to content

Commit

Permalink
Find and copy snippet from Cacher libraries (#1)
Browse files Browse the repository at this point in the history
- 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
jookyboi authored Aug 25, 2017
1 parent b07cacd commit 718583d
Show file tree
Hide file tree
Showing 12 changed files with 1,182 additions and 59 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
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
65 changes: 8 additions & 57 deletions .gitignore
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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Cacher
Copyright (c) 2017 Cacher by Penguin Labs, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
53 changes: 52 additions & 1 deletion README.md
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

Binary file added 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 index.js
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);
});
Loading

0 comments on commit 718583d

Please sign in to comment.