Skip to content

Commit

Permalink
Merge branch 'release/v1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeboer committed Apr 20, 2017
2 parents 2b2f232 + fd8be8b commit 57fcb10
Show file tree
Hide file tree
Showing 26 changed files with 491 additions and 1,090 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"comments": false,
"compact": false,
"compact": true,
"plugins": [
"transform-class-properties",
"transform-runtime"
Expand Down
8 changes: 6 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"parser": "babel-eslint",
"extends": "xo",
"extends": [
"xo",
"plugin:ava/recommended"
],
"env": {
"node": true,
"browser": false
Expand All @@ -16,6 +19,7 @@
"flowtype-errors/show-errors": 2
},
"plugins": [
"flowtype-errors"
"flowtype-errors",
"ava"
]
}
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.ai -diff
*.svg -diff
/*.js -diff
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Type: `boolean`

Default: `true`

Use the `/tmp/<workflow bundle id>` directory for your workflow cache when `true`. When `false` it will use the path in the `alfred_workflow_cache` environment variable as cache directory, which is set by Alfred by default. You can override this environment variable if you like to set a custom cache directory.
Use the OS temp directory for your workflow cache when `true`, otherwise use the directory set by Alfred in the`alfred_workflow_cache` environment variable. It's recommended to leave this set at `true` unless you want your cache to survive reboots and clean it up yourself every once in a while.

### Properties

Expand All @@ -138,10 +138,13 @@ Type: `Object`

Collection of Alfred metadata that is available to the script through environment variables. See the alfred documentation for more info: https://www.alfredapp.com/help/workflows/script-environment-variables/.

When you use a custom Alfred theme, the `themeFile` property will be set to the path of the theme file (which is a JSON file). Also see [Hugo.alfredTheme](#hugo.alfredtheme).

```javascript
{
version: process.env.alfred_version,
theme: process.env.alfred_theme,
themeFile: '',
themeBackground: process.env.alfred_theme_background,
themeSelectionBackground: process.env.alfred_theme_selection_background,
themeSubtext: parseFloat(process.env.alfred_theme_subtext),
Expand All @@ -152,6 +155,12 @@ Collection of Alfred metadata that is available to the script through environmen
```
*Also see: [Hugo.workflowMeta](#hugo.workflowmeta)*

#### Hugo.alfredTheme

Type: `Object`

When a custom Alfred theme is used, this will return the contents of the theme file. Usefull if you need to render icons on-the-fly and want to know the background colour.

#### Hugo.cache

Type: [`cache-conf `](https://github.com/samverschueren/cache-conf)
Expand Down Expand Up @@ -294,9 +303,9 @@ Callback to execute when the keyword matches the first argument. The callback ta

Processing/parsing files often takes time and is only needed when the file has changed. You can cache the results for a period of time, but you'll be left with an outdated cache when the file changes.

This method makes caching the processed results a lot easier by checking whether a file has changed. If it hasn't changed, `get()` will return the cached value. If no cached result is found or the file has changed, the `changed` event will be emitted with a reference to the cache store, file contents and SHA-1 hash so you can process your file and store the results in the cache store.
This method makes caching the processed results a lot easier by checking whether a file has changed. If it hasn't changed, `get()` will return the cached value. If no cached result is found or the file has changed, the `change` event will be emitted with a reference to the cache store, file contents and SHA-1 hash so you can process your file and store the results in the cache store.

*\* It's recommended to have the `useTmpCache` option set to `true` to prevent the cache from piling up as it isn't cleaned from time to time, unlike the `/tmp` folder which is cleared after a reboot.*
*\* It's recommended to have the `useTmpCache` option set to `true` to prevent the cache from piling up as it isn't cleaned from time to time, unlike your OS temporary folder (for example `/tmp`) which is cleared after a reboot.*

##### filepath

Expand All @@ -316,6 +325,12 @@ Check out [file-cache.js.flow](file-cache.js.flow) and the [example](examples/fi

Checks for workflow package updates on either NPM or Packal when enabled. No need to call this method manually, updates will be checked automagically ​:sparkles:

#### Hugo.clearCache()

Empties the workflow cache directory but doesn't remove the directory itself.

Call `Hugo.clearCacheSync()` if you need it synchronously.

#### Hugo.filterItems(query, options)

Fuzzy filter the output buffer by search query using Fuse.js. Add items to the output buffer first and then call `filterItems` to remove the items that do not match the search query. This method alters the output buffer directly.
Expand Down
4 changes: 2 additions & 2 deletions examples/file-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ function myProcessingFunction(file) {
// Get cached file instance
let file = Hugo.cacheFile('path/to/my/file', 'myfile');

// Subscribe to changed event
file.on('changed', function (cache, file) {
// Subscribe to change event
file.on('change', function (cache, file) {
let result = myProcessingFunction(file);

cache.store(result);
Expand Down
127 changes: 2 additions & 125 deletions file-cache.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 34 additions & 11 deletions file-cache.js.flow
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// @flow

const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const mkdirp = require('mkdirp');
const EventEmitter = require('events').EventEmitter;
const fs = require('fs-promise');
const path = require('path');

/**
* FileCache
Expand Down Expand Up @@ -62,6 +61,7 @@ class FileCache extends EventEmitter {
filePath: string;
cacheName: string;
cacheDir: string;
isCleaning: boolean;

/**
* FileCache constructor
Expand All @@ -76,6 +76,7 @@ class FileCache extends EventEmitter {
this.filePath = filePath;
this.cacheName = cacheName;
this.cacheDir = cacheDir;
this.isCleaning = false;
}

/**
Expand Down Expand Up @@ -109,8 +110,7 @@ class FileCache extends EventEmitter {

// Always call callback when cache dir is not set
if (!this.cacheDir) {
console.log('No cache dir set');
this.emit('changed', eventResult, file, hash);
this.emit('change', eventResult, file, hash);
return eventResult.contents;
}

Expand All @@ -119,24 +119,47 @@ class FileCache extends EventEmitter {

// Return cached data if found
if (this._fileExists(cachePath)) {
return JSON.parse(fs.readFileSync(cachePath, 'utf8'));
return fs.readJsonSync(cachePath, {throws: false});
}

// Create cache directory if it doesn't exist
if (!this._fileExists(path.dirname(cachePath))) {
mkdirp.sync(path.dirname(cachePath));
}
fs.ensureDirSync(path.dirname(cachePath));

// Get processed data
this.emit('changed', eventResult, file, hash);
this.emit('change', eventResult, file, hash);

// Write cache
fs.writeFileSync(cachePath, JSON.stringify(eventResult.contents));
fs.writeJsonSync(cachePath, eventResult.contents);
return eventResult.contents;
}

return null;
}

/**
* Clear cache
* @async
* @return {Promise}
*/
async clearCache() {
if (!this.cacheDir) {
return;
}

return fs.emptyDir(path.join(this.cacheDir, this.cacheName));
}

/**
* Clear cache synchronously
* @return {void}
*/
clearCacheSync() {
if (!this.cacheDir) {
return;
}

return fs.emptyDirSync(path.join(this.cacheDir, this.cacheName));
}
}

module.exports = FileCache;
Loading

0 comments on commit 57fcb10

Please sign in to comment.