Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Merge pull request #18146 from atom/migrate-dev-live-reload-package
Browse files Browse the repository at this point in the history
➡️ Migrate core package 'dev-live-reload' into ./packages
  • Loading branch information
daviwil committed Sep 28, 2018
2 parents 0d006f2 + 1904c01 commit 1db6273
Show file tree
Hide file tree
Showing 54 changed files with 1,134 additions and 6 deletions.
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -50,7 +50,7 @@
"dalek": "https://www.atom.io/api/packages/dalek/versions/0.2.2/tarball",
"dedent": "^0.7.0",
"deprecation-cop": "https://www.atom.io/api/packages/deprecation-cop/versions/0.56.9/tarball",
"dev-live-reload": "https://www.atom.io/api/packages/dev-live-reload/versions/0.48.1/tarball",
"dev-live-reload": "file:packages/dev-live-reload",
"devtron": "1.3.0",
"encoding-selector": "https://www.atom.io/api/packages/encoding-selector/versions/0.23.9/tarball",
"etch": "^0.12.6",
Expand Down Expand Up @@ -197,7 +197,7 @@
"command-palette": "0.43.5",
"dalek": "0.2.2",
"deprecation-cop": "0.56.9",
"dev-live-reload": "0.48.1",
"dev-live-reload": "file:./packages/dev-live-reload",
"encoding-selector": "0.23.9",
"exception-reporting": "file:./packages/exception-reporting",
"find-and-replace": "0.215.14",
Expand Down
3 changes: 1 addition & 2 deletions packages/README.md
Expand Up @@ -28,7 +28,7 @@ See [RFC 003](https://github.com/atom/atom/blob/master/docs/rfcs/003-consolidate
| **command-palette** | [`atom/command-palette`][command-palette] | |
| **dalek** | [`atom/dalek`][dalek] | [#17838](https://github.com/atom/atom/issues/17838) |
| **deprecation-cop** | [`atom/deprecation-cop`][deprecation-cop] | [#17839](https://github.com/atom/atom/issues/17839) |
| **dev-live-reload** | [`atom/dev-live-reload`][dev-live-reload] | [#17840](https://github.com/atom/atom/issues/17840) |
| **dev-live-reload** | [`./dev-live-reload`](dev-live-reload) | [#17840](https://github.com/atom/atom/issues/17840) |
| **encoding-selector** | [`atom/encoding-selector`][encoding-selector] | [#17841](https://github.com/atom/atom/issues/17841) |
| **exception-reporting** | [`./exception-reporting`](./exception-reporting) | [#17842](https://github.com/atom/atom/issues/17842) |
| **find-and-replace** | [`atom/find-and-replace`][find-and-replace] | |
Expand Down Expand Up @@ -115,7 +115,6 @@ See [RFC 003](https://github.com/atom/atom/blob/master/docs/rfcs/003-consolidate
[command-palette]: https://github.com/atom/command-palette
[dalek]: https://github.com/atom/dalek
[deprecation-cop]: https://github.com/atom/deprecation-cop
[dev-live-reload]: https://github.com/atom/dev-live-reload
[encoding-selector]: https://github.com/atom/encoding-selector
[find-and-replace]: https://github.com/atom/find-and-replace
[fuzzy-finder]: https://github.com/atom/fuzzy-finder
Expand Down
1 change: 1 addition & 0 deletions packages/dev-live-reload/.gitignore
@@ -0,0 +1 @@
node_modules
20 changes: 20 additions & 0 deletions packages/dev-live-reload/LICENSE.md
@@ -0,0 +1,20 @@
Copyright (c) 2014 GitHub Inc.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 changes: 13 additions & 0 deletions packages/dev-live-reload/README.md
@@ -0,0 +1,13 @@
# Dev Live Reload package

This live reloads the Atom `.less` files. You edit styles and they are magically reflected in any running Atom windows. Magic! :tophat: :sparkles: :rabbit2:

Installed by default on Atom windows running in dev mode. Use the "Application: Open Dev" command to open a new dev mode window.

Use <kbd>meta-shift-ctrl-r</kbd> to reload all core and package stylesheets.

This package is __experimental__, it does not handle the following:

* File additions to a theme. New files will not be watched.

![gif](https://f.cloud.github.com/assets/69169/1387004/d2dc45f2-3b84-11e3-877e-cac8c51e9702.gif)
5 changes: 5 additions & 0 deletions packages/dev-live-reload/keymaps/dev-live-reload.cson
@@ -0,0 +1,5 @@
'.platform-darwin':
'cmd-ctrl-R': 'dev-live-reload:reload-all'

'.platform-win32':
'alt-ctrl-R': 'dev-live-reload:reload-all'
28 changes: 28 additions & 0 deletions packages/dev-live-reload/lib/base-theme-watcher.js
@@ -0,0 +1,28 @@
const fs = require('fs-plus')
const path = require('path')
const Watcher = require('./watcher')

module.exports =
class BaseThemeWatcher extends Watcher {
constructor () {
super()
this.stylesheetsPath = path.dirname(atom.themes.resolveStylesheet('../static/atom.less'))
this.watch()
}

watch () {
const filePaths = fs.readdirSync(this.stylesheetsPath).filter(filePath => path.extname(filePath).includes('less'))

for (const filePath of filePaths) {
this.watchFile(path.join(this.stylesheetsPath, filePath))
}
}

loadStylesheet () {
this.loadAllStylesheets()
}

loadAllStylesheets () {
atom.themes.reloadBaseStylesheets()
}
}
24 changes: 24 additions & 0 deletions packages/dev-live-reload/lib/main.js
@@ -0,0 +1,24 @@
module.exports = {
activate (state) {
if (!atom.inDevMode() || atom.inSpecMode()) return

if (atom.packages.hasActivatedInitialPackages()) {
this.startWatching()
} else {
this.activatedDisposable = atom.packages.onDidActivateInitialPackages(() => this.startWatching())
}
},

deactivate () {
if (this.activatedDisposable) this.activatedDisposable.dispose()
if (this.commandDisposable) this.commandDisposable.dispose()
if (this.uiWatcher) this.uiWatcher.destroy()
},

startWatching () {
const UIWatcher = require('./ui-watcher')
this.uiWatcher = new UIWatcher({themeManager: atom.themes})
this.commandDisposable = atom.commands.add('atom-workspace', 'dev-live-reload:reload-all', () => this.uiWatcher.reloadAll())
if (this.activatedDisposable) this.activatedDisposable.dispose()
}
}
48 changes: 48 additions & 0 deletions packages/dev-live-reload/lib/package-watcher.js
@@ -0,0 +1,48 @@
const fs = require('fs-plus')

const Watcher = require('./watcher')

module.exports =
class PackageWatcher extends Watcher {
static supportsPackage (pack, type) {
if (pack.getType() === type && pack.getStylesheetPaths().length) return true
return false
}

constructor (pack) {
super()
this.pack = pack
this.watch()
}

watch () {
const watchedPaths = []
const watchPath = stylesheet => {
if (!watchedPaths.includes(stylesheet)) this.watchFile(stylesheet)
watchedPaths.push(stylesheet)
}

const stylesheetsPath = this.pack.getStylesheetsPath()

if (fs.isDirectorySync(stylesheetsPath)) this.watchDirectory(stylesheetsPath)

const stylesheetPaths = new Set(this.pack.getStylesheetPaths())
const onFile = stylesheetPath => stylesheetPaths.add(stylesheetPath)
const onFolder = () => true
fs.traverseTreeSync(stylesheetsPath, onFile, onFolder)

for (let stylesheet of stylesheetPaths) {
watchPath(stylesheet)
}
}

loadStylesheet (pathName) {
if (pathName.includes('variables')) this.emitGlobalsChanged()
this.loadAllStylesheets()
}

loadAllStylesheets () {
console.log('Reloading package', this.pack.name)
this.pack.reloadStylesheets()
}
}
81 changes: 81 additions & 0 deletions packages/dev-live-reload/lib/ui-watcher.js
@@ -0,0 +1,81 @@
const {CompositeDisposable} = require('atom')

const BaseThemeWatcher = require('./base-theme-watcher')
const PackageWatcher = require('./package-watcher')

module.exports =
class UIWatcher {
constructor () {
this.subscriptions = new CompositeDisposable()
this.reloadAll = this.reloadAll.bind(this)
this.watchers = []
this.baseTheme = this.createWatcher(new BaseThemeWatcher())
this.watchPackages()
}

watchPackages () {
this.watchedThemes = new Map()
this.watchedPackages = new Map()
for (const theme of atom.themes.getActiveThemes()) { this.watchTheme(theme) }
for (const pack of atom.packages.getActivePackages()) { this.watchPackage(pack) }
this.watchForPackageChanges()
}

watchForPackageChanges () {
this.subscriptions.add(atom.themes.onDidChangeActiveThemes(() => {
// We need to destroy all theme watchers as all theme packages are destroyed
// when a theme changes.
for (const theme of this.watchedThemes.values()) { theme.destroy() }

this.watchedThemes.clear()

// Rewatch everything!
for (const theme of atom.themes.getActiveThemes()) { this.watchTheme(theme) }
}))

this.subscriptions.add(atom.packages.onDidActivatePackage(pack => this.watchPackage(pack)))

this.subscriptions.add(atom.packages.onDidDeactivatePackage(pack => {
// This only handles packages - onDidChangeActiveThemes handles themes
const watcher = this.watchedPackages.get(pack.name)
if (watcher) watcher.destroy()
this.watchedPackages.delete(pack.name)
}))
}

watchTheme (theme) {
if (PackageWatcher.supportsPackage(theme, 'theme')) this.watchedThemes.set(theme.name, this.createWatcher(new PackageWatcher(theme)))
}

watchPackage (pack) {
if (PackageWatcher.supportsPackage(pack, 'atom')) this.watchedPackages.set(pack.name, this.createWatcher(new PackageWatcher(pack)))
}

createWatcher (watcher) {
watcher.onDidChangeGlobals(() => {
console.log('Global changed, reloading all styles')
this.reloadAll()
})
watcher.onDidDestroy(() => this.watchers.splice(this.watchers.indexOf(watcher), 1))
this.watchers.push(watcher)
return watcher
}

reloadAll () {
this.baseTheme.loadAllStylesheets()
for (const pack of atom.packages.getActivePackages()) {
if (PackageWatcher.supportsPackage(pack, 'atom')) pack.reloadStylesheets()
}

for (const theme of atom.themes.getActiveThemes()) {
if (PackageWatcher.supportsPackage(theme, 'theme')) theme.reloadStylesheets()
}
}

destroy () {
this.subscriptions.dispose()
this.baseTheme.destroy()
for (const pack of this.watchedPackages.values()) { pack.destroy() }
for (const theme of this.watchedThemes.values()) { theme.destroy() }
}
}
72 changes: 72 additions & 0 deletions packages/dev-live-reload/lib/watcher.js
@@ -0,0 +1,72 @@
const {CompositeDisposable, File, Directory, Emitter} = require('atom')
const path = require('path')

module.exports =
class Watcher {
constructor () {
this.destroy = this.destroy.bind(this)
this.emitter = new Emitter()
this.disposables = new CompositeDisposable()
this.entities = [] // Used for specs
}

onDidDestroy (callback) {
this.emitter.on('did-destroy', callback)
}

onDidChangeGlobals (callback) {
this.emitter.on('did-change-globals', callback)
}

destroy () {
this.disposables.dispose()
this.entities = null
this.emitter.emit('did-destroy')
this.emitter.dispose()
}

watch () {
// override me
}

loadStylesheet (stylesheetPath) {
// override me
}

loadAllStylesheets () {
// override me
}

emitGlobalsChanged () {
this.emitter.emit('did-change-globals')
}

watchDirectory (directoryPath) {
if (this.isInAsarArchive(directoryPath)) return
const entity = new Directory(directoryPath)
this.disposables.add(entity.onDidChange(() => this.loadAllStylesheets()))
this.entities.push(entity)
}

watchGlobalFile (filePath) {
const entity = new File(filePath)
this.disposables.add(entity.onDidChange(() => this.emitGlobalsChanged()))
this.entities.push(entity)
}

watchFile (filePath) {
if (this.isInAsarArchive(filePath)) return
const reloadFn = () => this.loadStylesheet(entity.getPath())

const entity = new File(filePath)
this.disposables.add(entity.onDidChange(reloadFn))
this.disposables.add(entity.onDidDelete(reloadFn))
this.disposables.add(entity.onDidRename(reloadFn))
this.entities.push(entity)
}

isInAsarArchive (pathToCheck) {
const {resourcePath} = atom.getLoadSettings()
return pathToCheck.startsWith(`${resourcePath}${path.sep}`) && path.extname(resourcePath) === '.asar'
}
}
9 changes: 9 additions & 0 deletions packages/dev-live-reload/menus/dev-live-reload.cson
@@ -0,0 +1,9 @@
'menu': [
'label': 'Packages'
'submenu': [
'label': 'Dev Live Reload'
'submenu': [
{ 'label': 'Reload All Styles', 'command': 'dev-live-reload:reload-all' }
]
]
]
28 changes: 28 additions & 0 deletions packages/dev-live-reload/package.json
@@ -0,0 +1,28 @@
{
"name": "dev-live-reload",
"main": "./lib/main",
"version": "0.48.1",
"description": "Live reload atom themes and packages.",
"repository": "https://github.com/atom/atom",
"license": "MIT",
"dependencies": {
"fs-plus": "^3.0.0"
},
"engines": {
"atom": "*"
},
"devDependencies": {
"standard": "^10.0.3"
},
"standard": {
"env": {
"atomtest": true,
"browser": true,
"jasmine": true,
"node": true
},
"globals": [
"atom"
]
}
}

0 comments on commit 1db6273

Please sign in to comment.