diff --git a/lib/base-theme-watcher.js b/lib/base-theme-watcher.js index 72ce21e..8d8658a 100644 --- a/lib/base-theme-watcher.js +++ b/lib/base-theme-watcher.js @@ -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)) diff --git a/lib/main.js b/lib/main.js index f24e638..81ecaf0 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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() } } diff --git a/lib/package-watcher.js b/lib/package-watcher.js index 43ddae8..eb9a213 100644 --- a/lib/package-watcher.js +++ b/lib/package-watcher.js @@ -1,4 +1,3 @@ -const _ = require('underscore-plus') const fs = require('fs-plus') const Watcher = require('./watcher') @@ -20,7 +19,7 @@ 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) } @@ -28,12 +27,12 @@ class PackageWatcher extends Watcher { 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) } @@ -41,7 +40,7 @@ class PackageWatcher extends Watcher { } loadStylesheet (pathName) { - if (pathName.indexOf('variables') > -1) this.emitGlobalsChanged() + if (pathName.includes('variables')) this.emitGlobalsChanged() this.loadAllStylesheets() } diff --git a/lib/ui-watcher.js b/lib/ui-watcher.js index 33a8ae1..e7c7c64 100644 --- a/lib/ui-watcher.js +++ b/lib/ui-watcher.js @@ -1,4 +1,3 @@ -const _ = require('underscore-plus') const BaseThemeWatcher = require('./base-theme-watcher') const PackageWatcher = require('./package-watcher') @@ -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() @@ -23,20 +22,20 @@ 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) { @@ -44,7 +43,7 @@ class UIWatcher { 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 } @@ -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() } } } diff --git a/package.json b/package.json index fc2ef30..0082495 100644 --- a/package.json +++ b/package.json @@ -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": "*" diff --git a/spec/ui-watcher-spec.js b/spec/ui-watcher-spec.js index b007d85..e6b2af1 100644 --- a/spec/ui-watcher-spec.js +++ b/spec/ui-watcher-spec.js @@ -1,4 +1,3 @@ -const _ = require('underscore-plus') const path = require('path') const UIWatcher = require('../lib/ui-watcher') @@ -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 () => { @@ -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() }) @@ -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') @@ -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) @@ -136,7 +137,7 @@ 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) @@ -144,7 +145,7 @@ describe('UIWatcher', () => { 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() }) @@ -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() })