Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/windows fix test #2

Merged
merged 5 commits into from
Sep 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
"latest"
]
}
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,5 @@ local.properties
.sass-cache/
*.css.map


### Custom ###
.jsdoc-preview-doc/*
!.jsdoc-preview-doc/.keep
TODO.md
21 changes: 12 additions & 9 deletions lib/index.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
view = require './view'
renderer = require './renderer'

grammars = [
'source.js'
'source.javascript'
'source.es6'
'souce.coffee'
'source.litcoffee'
]

isView = (v) -> v instanceof view

module.exports =
Expand Down Expand Up @@ -34,17 +42,12 @@ module.exports =
new view(state)

toggle: ->
if isView(atom.workspace.getActivePaneItem())
atom.workspace.destroyActivePaneItem()
return
if isView atom.workspace.getActivePaneItem()
return atom.workspace.destroyActivePaneItem()

editor = atom.workspace.getActiveTextEditor()

return unless editor? and editor.getGrammar().scopeName in [
'source.js',
'source.javascript',
'source.es6'
]
return unless editor? and editor.getGrammar().scopeName in grammars

@addPreviewForEditor(editor) unless @removePreviewForEditor editor

Expand All @@ -54,7 +57,7 @@ module.exports =
uri = @uriForEditor(editor)
previewPane = atom.workspace.paneForURI(uri)
if previewPane?
previewPane.destroyItem(previewPane.itemForURI(uri))
previewPane.destroyItem previewPane.itemForURI uri
true
else
false
Expand Down
43 changes: 24 additions & 19 deletions lib/renderer.coffee
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
path = require 'path'
exec = require('child_process').execSync
jsdoc = require 'jsdoc-api'
fs = require 'fs'

tempfile = require 'tempfile'

util = require './util'

slicer = if util.isWin() then '\\' else '/'
packagePath = path.dirname __dirname

exports.toDOMFragment = (text = '', filePath, grammar, callback) ->
getConfigFilePath = () -> atom.config.get('jsdoc-preview.configFilePath') or path.join packagePath, 'conf.json'
createTempDir = () -> tempFile = tempfile().split(slicer).slice(0, -1).join slicer

module.exports =
toDOMFragment: (filePath, callback = () -> null) ->
tempDir = createTempDir()
el = document.createElement 'div'
domFragment = document.createElement 'div'

# TODO This causes performance issues
exec "rm -rf #{packagePath}/.jsdoc-preview-doc/*"
exec "touch #{packagePath}/.jsdoc-preview-doc/.keep"

try
exec "#{packagePath}/node_modules/.bin/jsdoc #{filePath} -c #{getConfig()} -d #{packagePath}/.jsdoc-preview-doc"

file = fs.readFileSync "#{packagePath}/.jsdoc-preview-doc/index.html"
# NOTE: In the future, you can probably use jsdoc2md
jsdoc.renderSync
files: filePath
destination: createTempDir()
configure: getConfigFilePath()
file = fs.readFileSync path.join tempDir, 'index.html'
catch e
return callback e, null

el.innerHTML = file

nav = el.querySelector 'nav'

[].forEach.call nav.querySelectorAll('ul li a'), (link) ->
href = link.getAttribute('href').replace /#.*$/, ''
domFragment.appendChild htmlFileToDOMFragment "#{packagePath}/.jsdoc-preview-doc/#{href}"

unless domFragment.innerHTML
domFragment.innerHTML = '<h2>No JSDocs to Preview</h2>'
domFragment.appendChild htmlFileToDOMFragment path.join tempDir, href

callback null, domFragment.innerHTML

getConfig = () ->
config = atom.config.get 'jsdoc-preview.configFilePath'
config = "#{packagePath}/conf.json" if config is 'conf.json'
domFragment.innerHTML = '<h2>No JSDocs to Preview</h2>' unless domFragment.innerHTML

config
callback null, domFragment.innerHTML
_getConfigFilePath: getConfigFilePath
_createTempDir: createTempDir

htmlFileToDOMFragment = (filePath) ->
el = document.createElement 'div'
Expand All @@ -55,4 +60,4 @@ htmlFileToDOMFragment = (filePath) ->
else
el.innerHTML = child.outerHTML + el.innerHTML

el
el
2 changes: 2 additions & 0 deletions lib/util.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports =
isWin: () -> /^win/.test process.platform
21 changes: 7 additions & 14 deletions lib/view.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
path = require 'path'

{Emitter, Disposable, CompositeDisposable, File} = require 'atom'
{$$$, ScrollView} = require 'atom-space-pen-views'
{ Emitter, Disposable, CompositeDisposable, File } = require 'atom'
{ $$$, ScrollView } = require 'atom-space-pen-views'

renderer = require './renderer'

Expand Down Expand Up @@ -104,7 +104,7 @@ module.exports = class extends ScrollView

renderJSDoc: ->
@showLoading() unless @loaded
@getJSDocSource().then (source) => @renderJSDocText(source) if source?
@getJSDocSource().then (source) => @renderJSDocText()

getJSDocSource: ->
if @file?.getPath()
Expand All @@ -114,8 +114,8 @@ module.exports = class extends ScrollView
else
Promise.resolve null

renderJSDocText: (text) ->
renderer.toDOMFragment text, @getPath(), @getGrammar(), (error, domFragment) =>
renderJSDocText: () ->
renderer.toDOMFragment @getPath(), (error, domFragment) =>
if error
@showError error
else
Expand All @@ -132,13 +132,7 @@ module.exports = class extends ScrollView
"#{@editor.getTitle()} Preview"
else "JSDoc Preview"

getIconName: -> "jsdoc"

getURI: ->
if @file?
"jsdoc-preview://#{@getPath()}"
else
"jsdoc-preview://editor/#{@editorId}"
getURI: -> if @file then "jsdoc-preview://#{@getPath()}" else "jsdoc-preview://editor/#{@editorId}"

getPath: ->
if @file?
Expand All @@ -157,5 +151,4 @@ module.exports = class extends ScrollView

showLoading: ->
@loading = true
@html $$$ ->
@div class: 'jsdoc-spinner', 'Loading JSDoc\u2026'
@html $$$ -> @div class: 'jsdoc-spinner', 'Loading JSDoc\u2026'
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsdoc-preview",
"version": "1.1.0",
"version": "1.1.1",
"main": "./lib/index",
"description": "Open a rendered version of the JSDoc docstrings in the current editor with `ctrl-shift-d`.",
"repository": {
Expand All @@ -16,7 +16,11 @@
},
"dependencies": {
"atom-space-pen-views": "^2.0.0",
"jsdoc": "^3.4.0"
"babel-preset-latest": "^6.14.0",
"jsdoc": "^3.4.0",
"jsdoc-api": "^2.0.2",
"jsdoc-to-markdown": "^1.3.7",
"tempfile": "^1.1.1"
},
"configSchema": {
"liveUpdate": {
Expand All @@ -26,7 +30,7 @@
},
"configFilePath": {
"type": "string",
"default": "conf.json",
"default": "",
"description": "Defines the path to the configuration file JSDoc uses to render JSDocs"
},
"customStyleSheets": {
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions spec/fixture/jsdoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @name foo
* @desc Some arbitrary description
* @param [string=''] foo Is an arbitrated string param
* @param [number=0] bar Is an arbitrated numeric param
* @returns string 'baz'
* @since 6/23/16
*/
function foo(foo = '', bar = 0,) {
return 'baz';
}
69 changes: 69 additions & 0 deletions spec/lib/renderer-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const path = require('path');
const fs = require('fs');

const renderer = require('../../lib/renderer.coffee');
const util = require('../../lib/util.coffee');
const { configSchema: { configFilePath: { default: defaultPath } } } = require('../../package.json');

const fixturesPath = `..${util.isWin() ? '\\' : '/'}fixture`;

describe('renderer.coffee', function() {
describe('toDOMFragment', function() {
const fixturePath = path.resolve(__dirname, fixturesPath, 'jsdoc.js');
const emptyFixturePath = path.resolve(__dirname, fixturesPath, 'empty-jsdoc.js');
const emptyHTML = '<h2>No JSDocs to Preview</h2>';
let fragment;

beforeEach(() => fragment = null);
it('should throw an error', function() {
expect(
() => renderer.toDOMFragment(path.resolve(__dirname, fixturesPath, 'fake.js'), callback)
).toThrow();
});
it('should generate an intelligible JSDoc DOM block', function() {
renderer.toDOMFragment(fixturePath, callback);
expect(fragment.length).toBeTruthy();
});
it('should generate a warning DOM block', function() {
expect(renderer.toDOMFragment(emptyFixturePath, callback)).toBe(emptyHTML);
});

function callback(e, f) {
if (e) {
throw e;
}

return (fragment = f);
}
});
describe('getConfigFilePath', function() {
let defaultConfigPath;

beforeEach(function() {
defaultConfigPath = path.resolve(process.cwd(), defaultPath);
atom.config.set('jsdoc-preview.configFilePath', defaultConfigPath);
});
xit('should throw an error if a bad path is provided');
it('should return the default config path', function() {
expect(renderer._getConfigFilePath()).toBe(defaultConfigPath);
});
it('should return a custom config path', function() {
const customConfigPath = 'foo';

atom.config.set('jsdoc-preview.configFilePath', customConfigPath);
expect(renderer._getConfigFilePath()).toEqual(customConfigPath);
});
});
describe('createTempDir', function() {
it('createTempDir', function() {
if (util.isWin()) {
expect(
renderer._createTempDir()
).toContain('\\Users\\IEUser\\AppData\\Local\\Temp');
} else {
expect(renderer._createTempDir()).toContain('/var/folders/');
expect(renderer._createTempDir()).toContain('/T');
}
});
});
});