From bb11814abc7b9521eedcb5e6df1fc55e5903b7e5 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Tue, 11 Feb 2020 12:01:37 -0600 Subject: [PATCH] feat: add only sync community packages setting (#480) --- lib/config.js | 5 ++++ lib/sync-settings.js | 40 ++++++++++++++++++------------ spec/sync-settings-spec.js | 51 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 16 deletions(-) diff --git a/lib/config.js b/lib/config.js index dd1a7632..0936f1ac 100644 --- a/lib/config.js +++ b/lib/config.js @@ -44,6 +44,11 @@ const config = { type: 'boolean', default: false, }, + onlySyncCommunityPackages: { + description: 'Do not sync packages bundled with Atom.', + type: 'boolean', + default: false, + }, syncKeymap: { type: 'boolean', default: true, diff --git a/lib/sync-settings.js b/lib/sync-settings.js index adc3fb04..fd678ce2 100644 --- a/lib/sync-settings.js +++ b/lib/sync-settings.js @@ -33,6 +33,7 @@ module.exports = { } if (!PackageManager) { PackageManager = require('./package-manager') + this.packageManager = new PackageManager() } this.updateLegacyConfigSettings() @@ -231,13 +232,16 @@ module.exports = { getPackages () { const syncPackages = atom.config.get('sync-settings.syncPackages') const syncThemes = atom.config.get('sync-settings.syncThemes') + const onlySyncCommunityPackages = atom.config.get('sync-settings.onlySyncCommunityPackages') const packages = [] const object = this._getAvailablePackageMetadataWithoutDuplicates() for (const i in object) { const metadata = object[i] const { name, version, theme, apmInstallSource } = metadata if ((syncThemes && theme) || (syncPackages && !theme)) { - packages.push({ name, version, theme, apmInstallSource }) + if (!onlySyncCommunityPackages || !atom.packages.isBundledPackage(name)) { + packages.push({ name, version, theme, apmInstallSource }) + } } } return _.sortBy(packages, 'name') @@ -312,6 +316,9 @@ module.exports = { if (!atom.config.get('sync-settings.syncThemes')) { packages = packages.filter(p => !p.theme) } + if (atom.config.get('sync-settings.onlySyncCommunityPackages')) { + packages = packages.filter(p => !atom.packages.isBundledPackage(p.name)) + } await this.installMissingPackages(packages) if (atom.config.get('sync-settings.removeObsoletePackages')) { await this.removeObsoletePackages(packages) @@ -482,9 +489,7 @@ module.exports = { const type = pkg.theme ? 'theme' : 'package' console.info(`Removing ${type} ${pkg.name}...`) await new Promise((resolve, reject) => { - // TODO: should packageManager be cached? - const packageManager = new PackageManager() - packageManager.uninstall(pkg, (err) => { + this.packageManager.uninstall(pkg, (err) => { if (err) { console.error( `Removing ${type} ${pkg.name} failed`, @@ -519,19 +524,20 @@ module.exports = { if (missingPackages.length > 0) { // start installing next package const pkg = missingPackages.shift() + const name = pkg.name const i = total - missingPackages.length - notifications[pkg.name] = notify.count(`Sync-settings: installing ${pkg.name}`, i, total) + notifications[name] = notify.count(`Sync-settings: installing ${name}`, i, total) try { await this.installPackage(pkg) - succeeded.push(pkg.name) + succeeded.push(name) } catch (err) { - failed.push(pkg.name) - notify.warning(`Sync-settings: failed to install ${pkg.name}`) + failed.push(name) + notify.warning(`Sync-settings: failed to install ${name}`) } - notifications[pkg.name].dismiss() - delete notifications[pkg.name] + notifications[name].dismiss() + delete notifications[name] return installNextPackage() } else if (Object.keys(notifications).length === 0) { @@ -556,23 +562,25 @@ module.exports = { async installPackage (pkg) { const type = pkg.theme ? 'theme' : 'package' - console.info(`Installing ${type} ${pkg.name}...`) + const name = pkg.name + console.info(`Installing ${type} ${name}...`) await new Promise((resolve, reject) => { - // TODO: should packageManager be cached? - const packageManager = new PackageManager() if (atom.config.get('sync-settings.installLatestVersion')) { pkg.version = null + } else if (pkg.apmInstallSource) { + pkg.name = pkg.apmInstallSource.source + pkg.version = null } - packageManager.install(pkg, (err) => { + this.packageManager.install(pkg, (err) => { if (err) { console.error( - `Installing ${type} ${pkg.name} failed`, + `Installing ${type} ${name} failed`, err.stack ? err.stack : err, err.stderr, ) reject(err) } else { - console.info(`Installed ${type} ${pkg.name}`) + console.info(`Installed ${type} ${name}`) resolve() } }) diff --git a/spec/sync-settings-spec.js b/spec/sync-settings-spec.js index 559e9533..ecd5a458 100644 --- a/spec/sync-settings-spec.js +++ b/spec/sync-settings-spec.js @@ -161,6 +161,7 @@ describe('SyncSettings', () => { const json = SyncSettings.getPackages() const packages = json.filter(p => !p.theme) const themes = json.filter(p => p.theme) + expect(packages.length > 0).toBe(true) expect(themes.length > 0).toBe(true) }) @@ -170,6 +171,7 @@ describe('SyncSettings', () => { const json = SyncSettings.getPackages() const packages = json.filter(p => !p.theme) const themes = json.filter(p => p.theme) + expect(packages.length > 0).toBe(true) expect(themes.length > 0).toBe(false) }) @@ -179,9 +181,22 @@ describe('SyncSettings', () => { const json = SyncSettings.getPackages() const packages = json.filter(p => !p.theme) const themes = json.filter(p => p.theme) + expect(packages.length > 0).toBe(false) expect(themes.length > 0).toBe(true) }) + + it('returns community packages and themes', () => { + // atom test environment only has bundled packages. We are pretending that `about` is not a bundled package + spyOn(atom.packages, 'isBundledPackage').and.callFake(name => name !== 'about') + atom.config.set('sync-settings.onlySyncCommunityPackages', true) + const json = SyncSettings.getPackages() + const community = json.filter(p => !atom.packages.isBundledPackage(p.name)) + const bundled = json.filter(p => atom.packages.isBundledPackage(p.name)) + + expect(community.length > 0).toBe(true) + expect(bundled.length > 0).toBe(false) + }) }) describe('::addFilteredSettings', () => { @@ -525,6 +540,42 @@ describe('SyncSettings', () => { expect(themes.length > 0).toBe(false) }) + it('restores only community packages', async () => { + // atom test environment only has bundled packages. We are pretending that `about` is not a bundled package + spyOn(atom.packages, 'isBundledPackage').and.callFake(name => name !== 'about') + spyOn(SyncSettings, 'installMissingPackages') + atom.config.set('sync-settings.blacklistedKeys', ['sync-settings.onlySyncCommunityPackages']) + atom.config.set('sync-settings.onlySyncCommunityPackages', false) + await SyncSettings.backup() + atom.config.set('sync-settings.onlySyncCommunityPackages', true) + await SyncSettings.restore() + const json = SyncSettings.installMissingPackages.calls.first().args[0] + const community = json.filter(p => !atom.packages.isBundledPackage(p.name)) + const bundled = json.filter(p => atom.packages.isBundledPackage(p.name)) + + expect(community.length > 0).toBe(true) + expect(bundled.length > 0).toBe(false) + }) + + it('installs apmInstallSource from git', async function () { + spyOn(SyncSettings.packageManager, 'install').and.callFake((pkg, cb) => cb()) + spyOn(SyncSettings, 'getPackages') + SyncSettings.getPackages.and.returnValue([ + { name: 'test1' }, + { + name: 'test', + version: '1.0.0', + apmInstallSource: { source: 'repo/test' }, + }, + ]) + await SyncSettings.backup() + SyncSettings.getPackages.and.returnValue([{ name: 'test1' }]) + await SyncSettings.restore() + const packageName = SyncSettings.packageManager.install.calls.mostRecent().args[0].name + + expect(packageName).toBe('repo/test') + }) + it('overrides keymap.cson', async () => { atom.config.set('sync-settings.syncKeymap', true) let original = await SyncSettings.fileContent(atom.keymaps.getUserKeymapPath())