From 77856232976522dbe7fa8a11fc7ee00ac010b040 Mon Sep 17 00:00:00 2001 From: Timo Lins Date: Tue, 15 Nov 2016 13:09:57 +0100 Subject: [PATCH] Check if hyperlayout plugin is installed Closes #5 --- .npmignore | 1 + bin/hyperlayout | 36 ++++++++++++++++++++++++++++++------ package.json | 5 ++--- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/.npmignore b/.npmignore index 5f7602f..c148744 100644 --- a/.npmignore +++ b/.npmignore @@ -1 +1,2 @@ art +assets diff --git a/bin/hyperlayout b/bin/hyperlayout index 06b1fe9..d4d3117 100755 --- a/bin/hyperlayout +++ b/bin/hyperlayout @@ -1,10 +1,29 @@ #!/usr/bin/env node -const fs = require('fs') -const isHyper = require('is-hyper')() +const {readFileSync} = require('fs') +const {homedir} = require('os') +const {resolve} = require('path') +const vm = require('vm') const debug = require('debug')('hyperlayout') +const env = process.env + // Read json file -const readJson = (dir, file) => JSON.parse(fs.readFileSync(`${dir}/${file}`, 'utf8')) +const readJson = (dir, file) => JSON.parse(readFileSync(`${dir}/${file}`, 'utf8')) + +const isPluginInstalled = () => { + const path = resolve(homedir(), '.hyper.js') + try { + const hyperConfig = readFileSync(path, 'utf8') + const script = new vm.Script(hyperConfig) + const module = {} + script.runInNewContext({module}) + const {plugins, localPlugins} = module.exports + const pluginArray = plugins.concat(localPlugins) + return pluginArray.includes('hyperlayout') + } catch (err) { + console.log(err) + } +} // Get config file by reading `.hyperlayout` and `package.json` const getConfig = () => { @@ -31,7 +50,7 @@ const getConfig = () => { } } try { - const subConfig = readJson(process.env.HOME, '.hyperlayout') + const subConfig = readJson(homedir(), '.hyperlayout') config = config || subConfig } catch (err) { debug('No .hyperlayout found in home directory') @@ -61,17 +80,22 @@ const getConfig = () => { if (config && !config.layout) { console.log(`Can't find layout inside config`) - return + return 'error' } return config } function start() { - if (!isHyper) { + if (env.TERM_PROGRAM !== 'Hyper' && env.TERM_PROGRAM !== 'HyperTerm' && !env.HYPERLAYOUT) { console.log('You need to use Hyper.app to run this command') return } + if (!isPluginInstalled() && !env.HYPERLAYOUT) { + console.log(`Can't find hyperlayout plugin in ~/.hyper.js.\n\You can install it with: $ hpm install hyperlayout`) + return + } + const data = { config: getConfig(), cwd: process.cwd() diff --git a/package.json b/package.json index b1604e5..a3a5ce6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hyperlayout", - "version": "0.0.1", + "version": "0.0.2", "description": "Layout presets for Hyper.app", "repository": "timolins/hyperlayout", "author": "Timo Lins ", @@ -27,7 +27,6 @@ "xo": "^0.17.0" }, "dependencies": { - "debug": "^2.3.2", - "is-hyper": "^0.2.0" + "debug": "^2.3.2" } }