Skip to content

Commit

Permalink
🐛 Allow multiple bin name for a product
Browse files Browse the repository at this point in the history
Also enhance error message in case wa can't find bin path

See #59
  • Loading branch information
bchatard committed Sep 16, 2019
1 parent 282623c commit 60bf6ca
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
28 changes: 24 additions & 4 deletions src/product.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const alfy = require("alfy");
const fs = require("fs");
const path = require("path");
const execa = require("execa");
const which = require("which");

const knownProducts = require("./products.json");
Expand Down Expand Up @@ -79,8 +78,29 @@ const getPreferencePath = product => {
};

const getApplicationPath = product => {
const bin = product.bin;
product.binPath = which.sync(bin, { nothrow: true });
let bins = product.bin;
if (!Array.isArray(bins)) {
bins = [bins];
}
let binPath = null;
for (const bin of bins) {
binPath = which.sync(bin, { nothrow: true });
// is not null and path length is greater than bin length (weird case)
if (binPath !== null && binPath.length > bin.length) {
break;
}
}

if (!binPath) {
throw new Error(
`Unable to find bin for ${product.key}. Search for bin named: ${bins.join(
", "
)}`
);
}

product.binPath = binPath;

const binContent = fs.readFileSync(product.binPath, { encoding: "UTF-8" });

// Toolbox case
Expand All @@ -107,7 +127,7 @@ const getApplicationPath = product => {

const get = () => {
let product = getProduct();
const cacheKey = `product.${product.bin}`;
const cacheKey = `product.${product.key}`;
const cachedProduct = alfy.cache.get(cacheKey);
if (!cachedProduct) {
product = applyCustomisation(product);
Expand Down
6 changes: 3 additions & 3 deletions src/products.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
},
"PyCharmPro": {
"preferences": "PyCharm",
"bin": "pycharm"
"bin": ["pycharm", "charm"]
},
"PyCharmCE": {
"preferences": "PyCharmCE",
"bin": "pycharm"
"bin": ["pycharm", "charm"]
},
"PyCharmEdu": {
"preferences": "PyCharmEdu",
"bin": "pycharm"
"bin": ["pycharm", "charm"]
},
"Rider": {
"preferences": "Rider",
Expand Down
2 changes: 1 addition & 1 deletion src/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const buildItem = (product, projectPath) => {
};

const getItems = product => {
const cacheKey = `projects.${product.bin}`;
const cacheKey = `projects.${product.key}`;
const cachedProjects = alfy.cache.get(cacheKey);
if (!cachedProjects) {
const projects = [];
Expand Down

0 comments on commit 60bf6ca

Please sign in to comment.