Skip to content
This repository has been archived by the owner on Sep 29, 2018. It is now read-only.

Commit

Permalink
Merge pull request #27 from atom/wl-cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
50Wliu committed Nov 12, 2017
2 parents 8475aa6 + 58066b6 commit a7c8302
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lib/base-theme-watcher.js
Expand Up @@ -11,7 +11,7 @@ class BaseThemeWatcher extends Watcher {
}

watch () {
const filePaths = fs.readdirSync(this.stylesheetsPath).filter(filePath => path.extname(filePath).indexOf('less') > -1)
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))
Expand Down
8 changes: 4 additions & 4 deletions lib/main.js
Expand Up @@ -5,15 +5,15 @@ module.exports = {
if (!atom.inDevMode() || atom.inSpecMode()) return

let uiWatcher = null
const activatedDisposable = atom.packages.onDidActivateInitialPackages(() => {
this.activatedDisposable = atom.packages.onDidActivateInitialPackages(() => {
uiWatcher = new UIWatcher({themeManager: atom.themes})
activatedDisposable.dispose()
this.commandDisposable = atom.commands.add('atom-workspace', 'dev-live-reload:reload-all', () => uiWatcher.reloadAll())
this.activatedDisposable.dispose()
})

this.commandDisposable = atom.commands.add('atom-workspace', 'dev-live-reload:reload-all', () => uiWatcher && uiWatcher.reloadAll())
},

deactivate () {
if (this.activatedDisposable) this.activatedDisposable.dispose()
if (this.commandDisposable) this.commandDisposable.dispose()
}
}
11 changes: 5 additions & 6 deletions lib/package-watcher.js
@@ -1,4 +1,3 @@
const _ = require('underscore-plus')
const fs = require('fs-plus')

const Watcher = require('./watcher')
Expand All @@ -20,28 +19,28 @@ class PackageWatcher extends Watcher {
watch () {
const watchedPaths = []
const watchPath = stylesheet => {
if (!_.contains(watchedPaths, stylesheet)) this.watchFile(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 = this.pack.getStylesheetPaths()
const onFile = stylesheetPath => stylesheetPaths.push(stylesheetPath)
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 _.uniq(stylesheetPaths)) {
for (let stylesheet of stylesheetPaths) {
watchPath(stylesheet)
}

return this.entities
}

loadStylesheet (pathName) {
if (pathName.indexOf('variables') > -1) this.emitGlobalsChanged()
if (pathName.includes('variables')) this.emitGlobalsChanged()
this.loadAllStylesheets()
}

Expand Down
19 changes: 9 additions & 10 deletions lib/ui-watcher.js
@@ -1,4 +1,3 @@
const _ = require('underscore-plus')
const BaseThemeWatcher = require('./base-theme-watcher')
const PackageWatcher = require('./package-watcher')

Expand All @@ -12,8 +11,8 @@ class UIWatcher {
}

watchPackages () {
this.watchedThemes = {}
this.watchedPackages = {}
this.watchedThemes = new Map()
this.watchedPackages = new Map()
for (const theme of atom.themes.getActiveThemes()) { this.watchTheme(theme) }
for (const pack of atom.packages.getLoadedPackages()) { this.watchPackage(pack) }
this.watchForPackageChanges()
Expand All @@ -23,28 +22,28 @@ class UIWatcher {
atom.themes.onDidChangeActiveThemes(() => {
// we need to destroy all watchers as all theme packages are destroyed when a
// theme changes.
for (const name in this.watchedThemes) { this.watchedThemes[name].destroy() }
for (const theme of this.watchedThemes.values()) { theme.destroy() }

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

watchTheme (theme) {
if (PackageWatcher.supportsPackage(theme, 'theme')) this.watchedThemes[theme.name] = this.createWatcher(new PackageWatcher(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[pack.name] = this.createWatcher(new PackageWatcher(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 = _.without(this.watchers, watcher) })
watcher.onDidDestroy(() => this.watchers.splice(this.watchers.indexOf(watcher), 1))
this.watchers.push(watcher)
return watcher
}
Expand All @@ -62,7 +61,7 @@ class UIWatcher {

destroy () {
this.baseTheme.destroy()
for (const name in this.watchedPackages) { this.watchedPackages[name].destroy() }
for (const name in this.watchedThemes) { this.watchedThemes[name].destroy() }
for (const pack of this.watchedPackages.values()) { pack.destroy() }
for (const theme of this.watchedThemes.values()) { theme.destroy() }
}
}
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -6,8 +6,7 @@
"repository": "https://github.com/atom/dev-live-reload",
"license": "MIT",
"dependencies": {
"fs-plus": "^3.0.0",
"underscore-plus": "1.x"
"fs-plus": "^3.0.0"
},
"engines": {
"atom": "*"
Expand Down
27 changes: 14 additions & 13 deletions spec/ui-watcher-spec.js
@@ -1,4 +1,3 @@
const _ = require('underscore-plus')
const path = require('path')

const UIWatcher = require('../lib/ui-watcher')
Expand Down Expand Up @@ -34,11 +33,13 @@ describe('UIWatcher', () => {
await atom.packages.activatePackage(packagePath)
uiWatcher = new UIWatcher()

expect(_.last(uiWatcher.watchers).entities.length).toBe(4)
expect(_.last(uiWatcher.watchers).entities[0].getPath()).toBe(path.join(packagePath, 'styles'))
expect(_.last(uiWatcher.watchers).entities[1].getPath()).toBe(path.join(packagePath, 'styles', '3.css'))
expect(_.last(uiWatcher.watchers).entities[2].getPath()).toBe(path.join(packagePath, 'styles', 'sub', '1.css'))
expect(_.last(uiWatcher.watchers).entities[3].getPath()).toBe(path.join(packagePath, 'styles', 'sub', '2.less'))
const lastWatcher = uiWatcher.watchers[uiWatcher.watchers.length - 1]

expect(lastWatcher.entities.length).toBe(4)
expect(lastWatcher.entities[0].getPath()).toBe(path.join(packagePath, 'styles'))
expect(lastWatcher.entities[1].getPath()).toBe(path.join(packagePath, 'styles', '3.css'))
expect(lastWatcher.entities[2].getPath()).toBe(path.join(packagePath, 'styles', 'sub', '1.css'))
expect(lastWatcher.entities[3].getPath()).toBe(path.join(packagePath, 'styles', 'sub', '2.less'))
})

describe('when a package stylesheet file changes', async () => {
Expand All @@ -51,7 +52,7 @@ describe('UIWatcher', () => {
const pack = atom.packages.getActivePackages()[0]
spyOn(pack, 'reloadStylesheets')

_.last(uiWatcher.watchers).entities[1].emitter.emit('did-change')
uiWatcher.watchers[uiWatcher.watchers.length - 1].entities[1].emitter.emit('did-change')

expect(pack.reloadStylesheets).toHaveBeenCalled()
})
Expand Down Expand Up @@ -84,7 +85,7 @@ describe('UIWatcher', () => {
spyOn(theme, 'reloadStylesheets')
}

for (const entity of uiWatcher.watchedThemes['theme-with-multiple-imported-files'].entities) {
for (const entity of uiWatcher.watchedThemes.get('theme-with-multiple-imported-files').entities) {
if (entity.getPath().indexOf('variables') > -1) varEntity = entity
}
varEntity.emitter.emit('did-change')
Expand All @@ -110,7 +111,7 @@ describe('UIWatcher', () => {
spyOn(pack, 'reloadStylesheets')
spyOn(atom.themes, 'reloadBaseStylesheets')

const watcher = uiWatcher.watchedThemes['theme-with-index-less']
const watcher = uiWatcher.watchedThemes.get('theme-with-index-less')

expect(watcher.entities.length).toBe(1)

Expand All @@ -136,15 +137,15 @@ describe('UIWatcher', () => {
spyOn(pack, 'reloadStylesheets')
spyOn(atom.themes, 'reloadBaseStylesheets')

const watcher = uiWatcher.watchedThemes['theme-with-multiple-imported-files']
const watcher = uiWatcher.watchedThemes.get('theme-with-multiple-imported-files')

expect(watcher.entities.length).toBe(6)

watcher.entities[2].emitter.emit('did-change')
expect(pack.reloadStylesheets).toHaveBeenCalled()
expect(atom.themes.reloadBaseStylesheets).not.toHaveBeenCalled()

_.last(watcher.entities).emitter.emit('did-change')
watcher.entities[watcher.entities.length - 1].emitter.emit('did-change')
expect(atom.themes.reloadBaseStylesheets).toHaveBeenCalled()
})

Expand All @@ -159,14 +160,14 @@ describe('UIWatcher', () => {
jasmine.useRealClock()

atom.config.set('core.themes', ['theme-with-syntax-variables', 'theme-with-package-file'])
await conditionPromise(() => uiWatcher.watchedThemes['theme-with-package-file'])
await conditionPromise(() => uiWatcher.watchedThemes.get('theme-with-package-file'))

pack = atom.themes.getActiveThemes()[0]
spyOn(pack, 'reloadStylesheets')

expect(pack.name).toBe('theme-with-package-file')

const watcher = uiWatcher.watchedThemes['theme-with-package-file']
const watcher = uiWatcher.watchedThemes.get('theme-with-package-file')
watcher.entities[2].emitter.emit('did-change')
expect(pack.reloadStylesheets).toHaveBeenCalled()
})
Expand Down

0 comments on commit a7c8302

Please sign in to comment.