Skip to content

Commit

Permalink
feat: add syncThemes setting (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech authored Feb 10, 2020
1 parent b3c1f0c commit 9ca9177
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 7 deletions.
8 changes: 6 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ const config = {
type: 'boolean',
default: true,
},
syncThemes: {
type: 'boolean',
default: true,
},
installLatestVersion: {
description: 'Always install latest version of packages instead of backed up version.',
description: 'Always install latest version of packages and themes instead of backed up version.',
type: 'boolean',
default: false,
},
removeObsoletePackages: {
description: 'Packages installed but not in the backup will be removed when restoring backups.',
description: 'Packages and themes installed but not in the backup will be removed when restoring backups.',
type: 'boolean',
default: false,
},
Expand Down
18 changes: 14 additions & 4 deletions lib/sync-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ module.exports = {
if (atom.config.get('sync-settings.syncSettings')) {
files['settings.json'] = { content: await this.getFilteredSettings() }
}
if (atom.config.get('sync-settings.syncPackages')) {
if (atom.config.get('sync-settings.syncPackages') || atom.config.get('sync-settings.syncThemes')) {
files['packages.json'] = { content: JSON.stringify(this.getPackages(), null, '\t') }
}
if (atom.config.get('sync-settings.syncKeymap')) {
Expand Down Expand Up @@ -228,12 +228,16 @@ module.exports = {
},

getPackages () {
const syncPackages = atom.config.get('sync-settings.syncPackages')
const syncThemes = atom.config.get('sync-settings.syncThemes')
const packages = []
const object = this._getAvailablePackageMetadataWithoutDuplicates()
for (const i in object) {
const metadata = object[i]
const { name, version, theme, apmInstallSource } = metadata
packages.push({ name, version, theme, apmInstallSource })
if ((syncThemes && theme) || (syncPackages && !theme)) {
packages.push({ name, version, theme, apmInstallSource })
}
}
return _.sortBy(packages, 'name')
},
Expand Down Expand Up @@ -299,8 +303,14 @@ module.exports = {
break

case 'packages.json': {
if (atom.config.get('sync-settings.syncPackages')) {
const packages = JSON.parse(file.content)
if (atom.config.get('sync-settings.syncPackages') || atom.config.get('sync-settings.syncThemes')) {
let packages = JSON.parse(file.content)
if (!atom.config.get('sync-settings.syncPackages')) {
packages = packages.filter(p => p.theme)
}
if (!atom.config.get('sync-settings.syncThemes')) {
packages = packages.filter(p => !p.theme)
}
await this.installMissingPackages(packages)
if (atom.config.get('sync-settings.removeObsoletePackages')) {
await this.removeObsoletePackages(packages)
Expand Down
92 changes: 91 additions & 1 deletion spec/sync-settings-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,43 @@ describe('SyncSettings', () => {
})
})

describe('::getPackages', () => {
beforeEach(async () => {
await atom.packages.activatePackage('sync-settings')
// wait for package to activate
await new Promise(resolve => setImmediate(resolve))
})

afterEach(async () => {
await atom.packages.deactivatePackage('sync-settings')
})
it('returns packages and themes', () => {
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)
})

it('returns packages and not themes', () => {
atom.config.set('sync-settings.syncThemes', false)
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)
})

it('returns not packages and themes', () => {
atom.config.set('sync-settings.syncPackages', false)
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)
})
})

describe('mocks', () => {
beforeEach(async () => {
await atom.packages.activatePackage('sync-settings')
Expand Down Expand Up @@ -218,16 +255,37 @@ describe('SyncSettings', () => {
expect(settings['.dummy.scope'].package.dummy).toBe(true)
})

it('back up the installed packages list', async () => {
it('only back up the installed packages list', async () => {
atom.config.set('sync-settings.syncPackages', true)
atom.config.set('sync-settings.syncThemes', false)
await SyncSettings.backup()
const res = await SyncSettings.getGist()

expect(res.data.files['packages.json']).toBeDefined()
const json = JSON.parse(res.data.files['packages.json'].content)
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)
})

it('only back up the installed theme list', async () => {
atom.config.set('sync-settings.syncPackages', false)
atom.config.set('sync-settings.syncThemes', true)
await SyncSettings.backup()
const res = await SyncSettings.getGist()

expect(res.data.files['packages.json']).toBeDefined()
const json = JSON.parse(res.data.files['packages.json'].content)
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("don't back up the installed packages list", async () => {
atom.config.set('sync-settings.syncPackages', false)
atom.config.set('sync-settings.syncThemes', false)
await SyncSettings.backup()
const res = await SyncSettings.getGist()

Expand Down Expand Up @@ -401,6 +459,38 @@ describe('SyncSettings', () => {
expect(atom.config.get('package.dummy', { scope: [scopeSelector] })).toBe(true)
})

it('restores only themes', async () => {
atom.config.set('sync-settings.blacklistedKeys', ['sync-settings.syncPackages', 'sync-settings.syncThemes'])
spyOn(SyncSettings, 'installMissingPackages')
atom.config.set('sync-settings.syncPackages', true)
atom.config.set('sync-settings.syncThemes', true)
await SyncSettings.backup()
atom.config.set('sync-settings.syncPackages', false)
await SyncSettings.restore()
const json = SyncSettings.installMissingPackages.calls.first().args[0]
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('restores only packages', async () => {
atom.config.set('sync-settings.blacklistedKeys', ['sync-settings.syncPackages', 'sync-settings.syncThemes'])
spyOn(SyncSettings, 'installMissingPackages')
atom.config.set('sync-settings.syncPackages', true)
atom.config.set('sync-settings.syncThemes', true)
await SyncSettings.backup()
atom.config.set('sync-settings.syncThemes', false)
await SyncSettings.restore()
const json = SyncSettings.installMissingPackages.calls.first().args[0]
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)
})

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 9ca9177

Please sign in to comment.