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

Commit

Permalink
Merge pull request #51 from atom/wl-decaf
Browse files Browse the repository at this point in the history
Decaffeinate
  • Loading branch information
Wliu authored Nov 15, 2017
2 parents 45535e0 + 145dedf commit 42b25da
Show file tree
Hide file tree
Showing 10 changed files with 569 additions and 468 deletions.
1 change: 0 additions & 1 deletion .coffeelintignore

This file was deleted.

37 changes: 0 additions & 37 deletions coffeelint.json

This file was deleted.

8 changes: 0 additions & 8 deletions lib/main.coffee

This file was deleted.

11 changes: 11 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const PackageGeneratorView = require('./package-generator-view')

module.exports = {
activate () {
this.view = new PackageGeneratorView()
},

deactivate () {
if (this.view) this.view.destroy()
}
}
129 changes: 0 additions & 129 deletions lib/package-generator-view.coffee

This file was deleted.

150 changes: 150 additions & 0 deletions lib/package-generator-view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
const path = require('path')
const _ = require('underscore-plus')
const {TextEditor, BufferedProcess, CompositeDisposable, Disposable} = require('atom')
const fs = require('fs-plus')

module.exports =
class PackageGeneratorView {
constructor () {
this.disposables = new CompositeDisposable()

this.element = document.createElement('div')
this.element.classList.add('package-generator')

this.miniEditor = new TextEditor({mini: true})
this.element.appendChild(this.miniEditor.element)

this.error = document.createElement('div')
this.error.classList.add('error')
this.element.appendChild(this.error)

this.message = document.createElement('div')
this.message.classList.add('message')
this.element.appendChild(this.message)

this.disposables.add(atom.commands.add('atom-workspace', {
'package-generator:generate-package': () => this.attach('package'),
'package-generator:generate-syntax-theme': () => this.attach('theme')
}))

const blurHandler = () => this.close()
this.miniEditor.element.addEventListener('blur', blurHandler)
this.disposables.add(new Disposable(() => this.miniEditor.element.removeEventListener('blur', blurHandler)))
this.disposables.add(atom.commands.add(this.element, {
'core:confirm': () => this.confirm(),
'core:cancel': () => this.close()
}))
}

destroy () {
if (this.panel != null) this.panel.destroy()
this.disposables.dispose()
}

attach (mode) {
this.mode = mode
if (this.panel == null) this.panel = atom.workspace.addModalPanel({item: this, visible: false})
this.previouslyFocusedElement = document.activeElement
this.panel.show()
this.message.textContent = `Enter ${this.mode} path`
if (this.isInPackageMode()) {
this.setPathText('my-package')
} else {
this.setPathText('my-theme-syntax', [0, 8])
}
this.miniEditor.element.focus()
}

setPathText (placeholderName, rangeToSelect) {
if (rangeToSelect == null) rangeToSelect = [0, placeholderName.length]
const packagesDirectory = this.getPackagesDirectory()
this.miniEditor.setText(path.join(packagesDirectory, placeholderName))
const pathLength = this.miniEditor.getText().length
const endOfDirectoryIndex = pathLength - placeholderName.length
this.miniEditor.setSelectedBufferRange([[0, endOfDirectoryIndex + rangeToSelect[0]], [0, endOfDirectoryIndex + rangeToSelect[1]]])
}

close () {
if (!this.panel.isVisible()) return
this.panel.hide()
if (this.previouslyFocusedElement != null) this.previouslyFocusedElement.focus()
}

confirm () {
if (this.validPackagePath()) {
this.createPackageFiles(() => {
const packagePath = this.getPackagePath()
atom.open({pathsToOpen: [packagePath]})
this.close()
})
}
}

getPackagePath () {
const packagePath = fs.normalize(this.miniEditor.getText().trim())
const packageName = _.dasherize(path.basename(packagePath))
return path.join(path.dirname(packagePath), packageName)
}

getPackagesDirectory () {
return process.env.ATOM_REPOS_HOME || atom.config.get('core.projectHome') || path.join(fs.getHomeDirectory(), 'github')
}

validPackagePath () {
if (fs.existsSync(this.getPackagePath())) {
this.error.textContent = `Path already exists at '${this.getPackagePath()}'`
this.error.style.display = 'block'
return false
} else {
return true
}
}

getInitOptions (packagePath) {
const options = [`--${this.mode}`, packagePath]
if (this.isInPackageMode()) {
return [...options, '--syntax', atom.config.get('package-generator.packageSyntax')]
} else {
return options
}
}

initPackage (packagePath, callback) {
const command = ['init'].concat(this.getInitOptions(packagePath))
this.runCommand(atom.packages.getApmPath(), command, callback)
}

linkPackage (packagePath, callback) {
const args = ['link']
if (atom.config.get('package-generator.createInDevMode')) args.push('--dev')
args.push(packagePath.toString())

this.runCommand(atom.packages.getApmPath(), args, callback)
}

isInPackageMode () {
return this.mode === 'package'
}

isStoredInDotAtom (packagePath) {
const packagesPath = path.join(atom.getConfigDirPath(), 'packages', path.sep)
if (packagePath.indexOf(packagesPath) === 0) return true

const devPackagesPath = path.join(atom.getConfigDirPath(), 'dev', 'packages', path.sep)
return packagePath.indexOf(devPackagesPath) === 0
}

createPackageFiles (callback) {
const packagePath = this.getPackagePath()

if (this.isStoredInDotAtom(packagePath)) {
this.initPackage(packagePath, callback)
} else {
this.initPackage(packagePath, () => this.linkPackage(packagePath, callback))
}
}

runCommand (command, args, exit) {
BufferedProcess({command, args, exit})
}
}
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"atom": "*"
},
"devDependencies": {
"coffeelint": "^1.9.7"
"standard": "^10.0.3"
},
"configSchema": {
"createInDevMode": {
Expand All @@ -37,5 +37,16 @@
],
"description": "The syntax to generate packages with."
}
},
"standard": {
"env": {
"atomtest": true,
"browser": true,
"jasmine": true,
"node": true
},
"globals": [
"atom"
]
}
}
Loading

0 comments on commit 42b25da

Please sign in to comment.