Skip to content

Commit

Permalink
Check if hyperlayout plugin is installed
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
timolins committed Nov 15, 2016
1 parent 9673ede commit 7785623
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
art
assets
36 changes: 30 additions & 6 deletions bin/hyperlayout
Original file line number Diff line number Diff line change
@@ -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 = () => {
Expand All @@ -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')
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand All @@ -27,7 +27,6 @@
"xo": "^0.17.0"
},
"dependencies": {
"debug": "^2.3.2",
"is-hyper": "^0.2.0"
"debug": "^2.3.2"
}
}

0 comments on commit 7785623

Please sign in to comment.