Skip to content

Commit

Permalink
feat: add only sync community packages setting (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech authored Feb 11, 2020
1 parent 46c0007 commit bb11814
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 16 deletions.
5 changes: 5 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
40 changes: 24 additions & 16 deletions lib/sync-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = {
}
if (!PackageManager) {
PackageManager = require('./package-manager')
this.packageManager = new PackageManager()
}

this.updateLegacyConfigSettings()
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
}
})
Expand Down
51 changes: 51 additions & 0 deletions spec/sync-settings-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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)
})
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit bb11814

Please sign in to comment.