Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Add support for JS package generation
Browse files Browse the repository at this point in the history
Source changes:

- Add `isInPackageMode` method to minimize string comparisons of `@mode`
- Add `getInitOptions` method to facilitate differentiating options
between modes and to keep `initPackage` simple. Add `--syntax` option
here, too.

Spec changes:

- Add `packageInitCommand` helper to clean up init command test
argument duplication and to prevent it from getting even longer with
the new `--syntax` addition.
- Move `generateOutside` into more generic `generatePackage` to clean
up duplication b/w the inside/outside specs, and leverage `_.partial`
to create more readable helper functions. This is also to prevent
further duplication in the new syntax specs.
- Write specs to check `--syntax` option.
  • Loading branch information
stevenhauser authored and Steven Hauser committed Mar 6, 2016
1 parent 6c585c4 commit aeed190
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 32 deletions.
15 changes: 13 additions & 2 deletions lib/package-generator-view.coffee
Expand Up @@ -34,7 +34,7 @@ class PackageGeneratorView extends View
@previouslyFocusedElement = $(document.activeElement)
@panel.show()
@message.text("Enter #{mode} path")
if @mode is 'package'
if @isInPackageMode()
@setPathText("my-package")
else
@setPathText("my-theme-syntax", [0, 8])
Expand Down Expand Up @@ -79,8 +79,16 @@ class PackageGeneratorView extends View
else
true

getInitOptions: (packagePath) ->
options = ["--#{@mode}", packagePath]
if @isInPackageMode()
[options..., '--syntax', atom.config.get('package-generator.packageSyntax')]
else
options

initPackage: (packagePath, callback) ->
@runCommand(atom.packages.getApmPath(), ['init', "--#{@mode}", "#{packagePath}"], callback)
command = ['init', @getInitOptions(packagePath)...]
@runCommand(atom.packages.getApmPath(), command, callback)

linkPackage: (packagePath, callback) ->
args = ['link']
Expand All @@ -89,6 +97,9 @@ class PackageGeneratorView extends View

@runCommand(atom.packages.getApmPath(), args, callback)

isInPackageMode: ->
@mode is 'package'

isStoredInDotAtom: (packagePath) ->
packagesPath = path.join(atom.getConfigDirPath(), 'packages', path.sep)
return true if packagePath.indexOf(packagesPath) is 0
Expand Down
72 changes: 42 additions & 30 deletions spec/package-generator-spec.coffee
@@ -1,6 +1,7 @@
path = require 'path'
fs = require 'fs-plus'
temp = require 'temp'
_ = require 'underscore-plus'
{$} = require 'atom-space-pen-views'

describe 'Package Generator', ->
Expand Down Expand Up @@ -67,6 +68,10 @@ describe 'Package Generator', ->
describe "when a package is generated", ->
[packageName, packagePath, packageRoot] = []

packageInitCommandFor = (path, syntax) ->
syntax ||= atom.config.get('package-generator.packageSyntax')
['init', '--package', path, '--syntax', syntax]

beforeEach ->
spyOn(atom, "open")

Expand Down Expand Up @@ -94,7 +99,7 @@ describe 'Package Generator', ->

expect(apmExecute).toHaveBeenCalled()
expect(apmExecute.mostRecentCall.args[0]).toBe atom.packages.getApmPath()
expect(apmExecute.mostRecentCall.args[1]).toEqual ['init', '--package', "#{path.join(path.dirname(packagePath), "camel-case-is-for-the-birds")}"]
expect(apmExecute.mostRecentCall.args[1]).toEqual packageInitCommandFor "#{path.join(path.dirname(packagePath), "camel-case-is-for-the-birds")}"

it "normalizes the package's path", ->
packagePath = path.join("~", "the-package")
Expand All @@ -111,36 +116,40 @@ describe 'Package Generator', ->

expect(apmExecute).toHaveBeenCalled()
expect(apmExecute.mostRecentCall.args[0]).toBe atom.packages.getApmPath()
expect(apmExecute.mostRecentCall.args[1]).toEqual ['init', '--package', "#{fs.normalize(packagePath)}"]
expect(apmExecute.mostRecentCall.args[1]).toEqual packageInitCommandFor "#{fs.normalize(packagePath)}"

describe 'when creating a package', ->
[apmExecute] = []

generatePackage = (insidePackagesDirectory, callback) ->
packageGeneratorView = $(getWorkspaceView()).find(".package-generator").view()
spyOn(packageGeneratorView, 'isStoredInDotAtom').andReturn insidePackagesDirectory
expect(packageGeneratorView.hasParent()).toBeTruthy()
packageGeneratorView.miniEditor.setText(packagePath)
apmExecute = spyOn(packageGeneratorView, 'runCommand').andCallFake (command, args, exit) ->
process.nextTick -> exit()
atom.commands.dispatch(packageGeneratorView.element, "core:confirm")
waitsFor ->
atom.open.callCount is 1
runs callback

generateOutside = _.partial generatePackage, false
generateInside = _.partial generatePackage, true

beforeEach ->
atom.commands.dispatch(getWorkspaceView(), "package-generator:generate-package")

waitsForPromise ->
activationPromise

describe "when the package is created outside of the packages directory", ->
[apmExecute] = []

generateOutside = (callback) ->
packageGeneratorView = $(getWorkspaceView()).find(".package-generator").view()
expect(packageGeneratorView.hasParent()).toBeTruthy()
packageGeneratorView.miniEditor.setText(packagePath)
apmExecute = spyOn(packageGeneratorView, 'runCommand').andCallFake (command, args, exit) ->
process.nextTick -> exit()
atom.commands.dispatch(packageGeneratorView.element, "core:confirm")
waitsFor ->
atom.open.callCount is 1

runs callback

it "calls `apm init` and `apm link`", ->
atom.config.set 'package-generator.createInDevMode', false

generateOutside ->
expect(apmExecute.argsForCall[0][0]).toBe atom.packages.getApmPath()
expect(apmExecute.argsForCall[0][1]).toEqual ['init', '--package', "#{packagePath}"]
expect(apmExecute.argsForCall[0][1]).toEqual packageInitCommandFor "#{packagePath}"
expect(apmExecute.argsForCall[1][0]).toBe atom.packages.getApmPath()
expect(apmExecute.argsForCall[1][1]).toEqual ['link', "#{packagePath}"]
expect(atom.open.argsForCall[0][0].pathsToOpen[0]).toBe packagePath
Expand All @@ -150,30 +159,33 @@ describe 'Package Generator', ->

generateOutside ->
expect(apmExecute.argsForCall[0][0]).toBe atom.packages.getApmPath()
expect(apmExecute.argsForCall[0][1]).toEqual ['init', '--package', "#{packagePath}"]
expect(apmExecute.argsForCall[0][1]).toEqual packageInitCommandFor "#{packagePath}"
expect(apmExecute.argsForCall[1][0]).toBe atom.packages.getApmPath()
expect(apmExecute.argsForCall[1][1]).toEqual ['link', '--dev', "#{packagePath}"]
expect(atom.open.argsForCall[0][0].pathsToOpen[0]).toBe packagePath

describe "when the package is created inside the packages directory", ->
it "calls `apm init`", ->
packageGeneratorView = $(getWorkspaceView()).find(".package-generator").view()
spyOn(packageGeneratorView, 'isStoredInDotAtom').andReturn true
expect(packageGeneratorView.hasParent()).toBeTruthy()
packageGeneratorView.miniEditor.setText(packagePath)
apmExecute = spyOn(packageGeneratorView, 'runCommand').andCallFake (command, args, exit) ->
process.nextTick -> exit()
atom.commands.dispatch(packageGeneratorView.element, "core:confirm")

waitsFor ->
atom.open.callCount

runs ->
generateInside ->
expect(apmExecute.argsForCall[0][0]).toBe atom.packages.getApmPath()
expect(apmExecute.argsForCall[0][1]).toEqual ['init', '--package', "#{packagePath}"]
expect(apmExecute.argsForCall[0][1]).toEqual packageInitCommandFor "#{packagePath}"
expect(atom.open.argsForCall[0][0].pathsToOpen[0]).toBe packagePath
expect(apmExecute.argsForCall[1]).toBeUndefined()

describe "when the package is a coffeescript package", ->
it "calls `apm init` with the correct syntax option", ->
atom.config.set 'package-generator.packageSyntax', 'coffeescript'
generateInside ->
expect(apmExecute.argsForCall[0][0]).toBe atom.packages.getApmPath()
expect(apmExecute.argsForCall[0][1]).toEqual packageInitCommandFor "#{packagePath}", 'coffeescript'

describe "when the package is a javascript package", ->
it "calls `apm init` with the correct syntax option", ->
atom.config.set 'package-generator.packageSyntax', 'javascript'
generateInside ->
expect(apmExecute.argsForCall[0][0]).toBe atom.packages.getApmPath()
expect(apmExecute.argsForCall[0][1]).toEqual packageInitCommandFor "#{packagePath}", 'javascript'

describe 'when creating a theme', ->
beforeEach ->
atom.commands.dispatch(getWorkspaceView(), "package-generator:generate-syntax-theme")
Expand Down

0 comments on commit aeed190

Please sign in to comment.