Skip to content

Commit

Permalink
Fix missing cache package and clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeboer committed Feb 5, 2020
1 parent be8c41b commit fb97581
Show file tree
Hide file tree
Showing 21 changed files with 1,141 additions and 1,264 deletions.
99 changes: 99 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
module.exports = {
"env": {
"es6": true,
"node": true
},
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig.json",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint",
"prefer-arrow"
],
"rules": {
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/ban-types": "error",
"@typescript-eslint/class-name-casing": "error",
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/no-empty-function": "error",
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-parameter-properties": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-for-of": "error",
"@typescript-eslint/prefer-function-type": "error",
"@typescript-eslint/prefer-namespace-keyword": "error",
"@typescript-eslint/triple-slash-reference": "error",
"@typescript-eslint/unified-signatures": "error",
"camelcase": "off",
"complexity": "off",
"constructor-super": "off",
"dot-notation": "error",
"eqeqeq": [
"error",
"smart"
],
"guard-for-in": "error",
"id-blacklist": "off",
"id-match": "off",
"import/order": "off",
"max-classes-per-file": [
"error",
1
],
"max-len": [
"warn",
{
"code": 120
}
],
"new-parens": "error",
"no-bitwise": "error",
"no-caller": "error",
"no-cond-assign": "error",
"no-console": "off",
"no-debugger": "error",
"no-empty": "error",
"no-eval": "error",
"no-fallthrough": "off",
"no-invalid-this": "off",
"no-new-wrappers": "error",
"no-shadow": [
"error",
{
"hoist": "all"
}
],
"no-throw-literal": "error",
"no-trailing-spaces": "error",
"no-undef-init": "error",
"no-underscore-dangle": "off",
"no-unsafe-finally": "error",
"no-unused-expressions": "error",
"no-unused-labels": "error",
"no-var": "error",
"object-shorthand": "error",
"one-var": [
"error",
"never"
],
"prefer-arrow/prefer-arrow-functions": "error",
"prefer-const": "error",
"radix": "error",
"spaced-comment": "error",
"use-isnan": "error",
"valid-typeof": "off",
"quotes": ["error", "single", { "avoidEscape": true }]
}
};
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
*.ai -diff
*.svg -diff
/*.js -diff
36 changes: 19 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alfred-hugo",
"version": "2.0.4",
"version": "2.0.5",
"description": "Alfred workflow bindings for NodeJS",
"author": "Maarten de Boer <maarten@cloudstek.nl> (https://cloudstek.nl)",
"contributors": [
Expand Down Expand Up @@ -32,25 +32,29 @@
"@types/fs-extra": "^8.0.0",
"@types/glob": "^7.1.1",
"@types/mockdate": "^2.0.0",
"@types/node": "^13.1.2",
"@types/semver": "^6.0.0",
"@types/node": "^13.7.0",
"@types/semver": "^7.1.0",
"@types/sinon": "^7.0.12",
"ava": "^2.0.0",
"@typescript-eslint/eslint-plugin": "^2.19.0",
"@typescript-eslint/parser": "^2.19.0",
"ava": "^3.2.0",
"bplist-creator": "^0.0.8",
"coveralls": "^3.0.4",
"del-cli": "^3.0.0",
"eslint": "^6.8.0",
"eslint-plugin-prefer-arrow": "^1.1.7",
"mockdate": "^2.0.2",
"nock": "^11.7.0",
"nock": "^11.7.2",
"npm-run-all": "^4.1.5",
"nyc": "^15.0.0",
"sinon": "^8.0.2",
"tslint": "^5.18.0",
"typescript": "^3.4.5"
"sinon": "^8.1.1",
"tslint": "^6.0.0",
"typescript": "^3.7.5"
},
"dependencies": {
"@cloudstek/cache": "^1.0.1",
"@cloudstek/cache": "^1.0.5",
"@types/node-notifier": "^5.4.0",
"axios": "^0.19.0",
"axios": "^0.19.2",
"bplist-parser": "^0.2.0",
"del": "^5.0.0",
"fs-extra": "^8.0.1",
Expand All @@ -60,7 +64,7 @@
"node-notifier": "^6.0.0",
"plist": "^3.0.1",
"read-pkg-up": "^7.0.1",
"semver": "^7.1.1",
"semver": "^7.1.2",
"strict-event-emitter-types": "^2.0.0",
"untildify": "^4.0.0"
},
Expand All @@ -78,15 +82,13 @@
"test": "npm-run-all -p clean:coverage && tsc && nyc ava",
"test:ci": "npm-run-all -p clean:* && tsc && nyc -s ava && nyc report --reporter=text-lcov | coveralls",
"lint": "npm-run-all -l -p lint:*",
"lint:src": "tslint src/**/*.ts",
"lint:test": "tslint test/**/*.ts"
"lint:src": "eslint src/**/*.ts",
"lint:test": "eslint test/**/*.ts"
},
"ava": {
"files": [
"build/test/**/*"
],
"helpers": [
"build/test/helpers/**/*"
"build/test/**/*",
"!build/test/helpers/**/*"
]
}
}
2 changes: 1 addition & 1 deletion src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Action {
return action;
}

public run(args: string[]) {
public run(args: string[]): boolean {
if (args.length === 0) {
return false;
}
Expand Down
34 changes: 17 additions & 17 deletions src/bin/link.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
#!/usr/bin/env node
import path from "path";
import fs from "fs-extra";
import glob from "glob";
import plist from "plist";
import semver from "semver";
import readPkg from "read-pkg-up";
import del from "del";
import * as utils from "../utils";
import path from 'path';
import fs from 'fs-extra';
import glob from 'glob';
import plist from 'plist';
import semver from 'semver';
import readPkg from 'read-pkg-up';
import del from 'del';
import utils from '../utils';

if (process.getuid && process.getuid() === 0) {
console.error("You cannot run hugo-link as root.");
console.error('You cannot run hugo-link as root.');
process.exit(1);
}

// Get alfred ersion
const apps = glob.sync("/Applications/Alfred?( )+([0-9]).app");
const apps = glob.sync('/Applications/Alfred?( )+([0-9]).app');

for (const app of apps) {
const plistPath = path.join(app, "Contents", "Info.plist");
const plistPath = path.join(app, 'Contents', 'Info.plist');

if (!utils.fileExists(plistPath)) {
continue;
}

try {
const appInfo = plist.parse(fs.readFileSync(plistPath, { encoding: "utf8" }));
const appInfo = plist.parse(fs.readFileSync(plistPath, { encoding: 'utf8' }));
const version = appInfo.CFBundleShortVersionString;

if (!version || !semver.valid(semver.coerce(version))) {
continue;
}

const prefsDir = utils.resolveAlfredPrefs(version);
const workflowsDir = path.join(prefsDir, "workflows");
const workflowsDir = path.join(prefsDir, 'workflows');

// Read package.json
readPkg()
.then(({ packageJson: pkg, path: pkgPath }) => {
const src = path.dirname(pkgPath);
const dest = path.join(workflowsDir, pkg.name.replace("/", "-"));
const dest = path.join(workflowsDir, pkg.name.replace('/', '-'));

if (fs.pathExistsSync(dest)) {
const destStat = fs.lstatSync(dest);

// Skip link creation if destination is a directory, not a symlink
if (destStat.isDirectory()) {
console.debug("Destination is a directory, skipping.");
console.debug('Destination is a directory, skipping.');
return;
}

// Skip if destination exists but is not a directory or symlink
if (destStat.isSymbolicLink() === false) {
console.debug("Desination exists but is neither a directory or symlink, skipping.");
console.debug('Desination exists but is neither a directory or symlink, skipping.');
return;
}

// Skip link creation if already linked
if (fs.realpathSync(dest) === src) {
console.debug("Link already exists, skipping.");
console.debug('Link already exists, skipping.');
return;
}

Expand Down
30 changes: 15 additions & 15 deletions src/bin/unlink.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
#!/usr/bin/env node
import path from "path";
import fs from "fs-extra";
import glob from "glob";
import plist from "plist";
import semver from "semver";
import readPkg from "read-pkg-up";
import del from "del";
import * as utils from "../utils";
import path from 'path';
import fs from 'fs-extra';
import glob from 'glob';
import plist from 'plist';
import semver from 'semver';
import readPkg from 'read-pkg-up';
import del from 'del';
import utils from '../utils';

if (process.getuid && process.getuid() === 0) {
console.error("You cannot run hugo-unlink as root.");
console.error('You cannot run hugo-unlink as root.');
process.exit(1);
}

// Get alfred ersion
const apps = glob.sync("/Applications/Alfred?( )+([0-9]).app");
const apps = glob.sync('/Applications/Alfred?( )+([0-9]).app');

for (const app of apps) {
const plistPath = path.join(app, "Contents", "Info.plist");
const plistPath = path.join(app, 'Contents', 'Info.plist');

if (!utils.fileExists(plistPath)) {
continue;
}

try {
const appInfo = plist.parse(fs.readFileSync(plistPath, { encoding: "utf8" }));
const appInfo = plist.parse(fs.readFileSync(plistPath, { encoding: 'utf8' }));
const version = appInfo.CFBundleShortVersionString;

if (!version || !semver.valid(semver.coerce(version))) {
continue;
}

const prefsDir = utils.resolveAlfredPrefs(version);
const workflowsDir = path.join(prefsDir, "workflows");
const workflowsDir = path.join(prefsDir, 'workflows');

// Read package.json
readPkg()
.then(({ packageJson: pkg }) => {
const dest = path.join(workflowsDir, pkg.name.replace("/", "-"));
const dest = path.join(workflowsDir, pkg.name.replace('/', '-'));

// Skip if destination does not exist
if (fs.pathExistsSync(dest) === false) {
Expand All @@ -48,7 +48,7 @@ for (const app of apps) {

// Skip if destination is not a symbolic link
if (destStat.isSymbolicLink() === false) {
console.debug("Destination is not a symbolic link, skipping.");
console.debug('Destination is not a symbolic link, skipping.');
return;
}

Expand Down
24 changes: 12 additions & 12 deletions src/file-cache.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Cache } from "@cloudstek/cache";
import { ICacheOptions } from "@cloudstek/cache";
import Crypto from "crypto";
import { EventEmitter } from "events";
import fs from "fs-extra";
import { Cache } from '@cloudstek/cache';
import { ICacheOptions } from '@cloudstek/cache';
import Crypto from 'crypto';
import { EventEmitter } from 'events';
import fs from 'fs-extra';

import * as utils from "./utils";
import { FileCacheEventEmitter } from "./types";
import utils from './utils';
import { FileCacheEventEmitter } from './types';

/**
* File cache.
Expand All @@ -30,7 +30,7 @@ export class FileCache extends (EventEmitter as new() => FileCacheEventEmitter)
this.filePath = filePath;

// Initialize cache store for this file
options.name = Crypto.createHash("sha1").update(filePath).digest("hex") + ".json";
options.name = Crypto.createHash('sha1').update(filePath).digest('hex') + '.json';
options.ttl = options.ttl || false;

this.cache = new Cache(options);
Expand All @@ -42,18 +42,18 @@ export class FileCache extends (EventEmitter as new() => FileCacheEventEmitter)
* Emits the "change" event with the cache instance, file and hash of that file when the file has been changed
* or expired from the cache.
*/
public get() {
public get(): any {
if (utils.fileExists(this.filePath) === false) {
return null;
}

// Get file fstat
const stat = fs.statSync(this.filePath);

if (this.cache.has("mtime") === false || this.cache.get("mtime") !== stat.mtimeMs) {
this.emit("change", this.cache, fs.readFileSync(this.filePath, "utf8"));
if (this.cache.has('mtime') === false || this.cache.get('mtime') !== stat.mtimeMs) {
this.emit('change', this.cache, fs.readFileSync(this.filePath, 'utf8'));

this.cache.set("mtime", stat.mtimeMs);
this.cache.set('mtime', stat.mtimeMs);
this.cache.commit();

return this.cache.all();
Expand Down
Loading

0 comments on commit fb97581

Please sign in to comment.