This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from atom/wl-decaf
Decaffeinate
- Loading branch information
Showing
10 changed files
with
569 additions
and
468 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.