Skip to content

Commit

Permalink
Merge pull request #515 from UziTech/folders
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech authored Jun 28, 2020
2 parents c958c39 + 27ae2a9 commit 80c61f5
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 141 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = {
message: 'Do not commit focused tests.',
},
],
'no-sync': 'error',
'react/jsx-uses-react': 'error',
'react/jsx-uses-vars': 'error',
'react/jsx-indent': ['error', 'tab'],
Expand Down
16 changes: 7 additions & 9 deletions lib/sync-settings.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const path = require('path')
const fs = require('fs')
const util = require('util')
const fs = require('fs-extra')
const { shell } = require('electron')
const writeFile = util.promisify(fs.writeFile)
const unlink = util.promisify(fs.unlink)
const util = require('util')
const glob = util.promisify(require('glob'))
const minimatch = require('minimatch')
const diffObject = require('deep-object-diff')
Expand Down Expand Up @@ -284,7 +282,7 @@ module.exports = class SyncSettings {
if (diffData.files && diffData.files.deleted) {
for (const fileName in diffData.files.deleted) {
const file = localData.files[fileName]
await unlink(file.path)
await fs.unlink(file.path)
}
}
}
Expand All @@ -303,7 +301,7 @@ module.exports = class SyncSettings {
if (backupData.files) {
for (const fileName in backupData.files) {
const file = backupData.files[fileName]
await writeFile(file.path, file.content)
await fs.outputFile(file.path, file.content)
}
}

Expand Down Expand Up @@ -347,7 +345,7 @@ module.exports = class SyncSettings {
data.settings = utils.getFilteredSettings()
}
if (atom.config.get('sync-settings.syncPackages') || atom.config.get('sync-settings.syncThemes')) {
data.packages = utils.getPackages()
data.packages = await utils.getPackages()
}
const removeUnfamiliarFiles = atom.config.get('sync-settings.removeUnfamiliarFiles')
if (atom.config.get('sync-settings.syncKeymap')) {
Expand Down Expand Up @@ -509,7 +507,7 @@ module.exports = class SyncSettings {
let extraFiles = atom.config.get('sync-settings.extraFiles') || []
extraFiles = extraFiles.map(f => f.replace(/\\/g, '/'))
if (extraFiles.includes(fileName)) {
data.files[fileName] = {
data.files[fileName.replace(/\//g, '\\')] = {
path: filePath,
content: file.content,
}
Expand All @@ -518,7 +516,7 @@ module.exports = class SyncSettings {
const ignoreFilesGlob = atom.config.get('sync-settings.ignoreFilesGlob') || []
const match = (g) => minimatch(fileName, g, { dot: true })
if (extraFilesGlob.some(match) && !ignoreFilesGlob.some(match)) {
data.files[fileName] = {
data.files[fileName.replace(/\//g, '\\')] = {
path: filePath,
content: file.content,
}
Expand Down
22 changes: 9 additions & 13 deletions lib/utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const path = require('path')
const fs = require('fs')
const util = require('util')
const stat = util.promisify(fs.stat)
const exists = (file) => stat(file).then(() => true, () => false)
const readFile = util.promisify(fs.readFile)
const fs = require('fs-extra')
const { deleteValueAtKeyPath, setValueAtKeyPath } = require('key-path-helpers')

const PackageManager = require('./package-manager')
Expand Down Expand Up @@ -42,7 +38,7 @@ module.exports = {

async getSnippetsPath () {
const jsonPath = path.resolve(atom.getConfigDirPath(), 'snippets.json')
if (await exists(jsonPath)) {
if (await fs.pathExists(jsonPath)) {
return jsonPath
}

Expand Down Expand Up @@ -80,12 +76,12 @@ module.exports = {
return true
},

getPackages () {
async 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 pkgMetadata = this.getAvailablePackageMetadataWithoutDuplicates()
const pkgMetadata = await this.getAvailablePackageMetadataWithoutDuplicates()
for (const pkgName in pkgMetadata) {
const metadata = pkgMetadata[pkgName]
const { name, version, theme, apmInstallSource } = metadata
Expand All @@ -106,13 +102,13 @@ module.exports = {
return this.sortObject(packages)
},

getAvailablePackageMetadataWithoutDuplicates () {
async getAvailablePackageMetadataWithoutDuplicates () {
const path2metadata = {}
const packageMetadata = atom.packages.getAvailablePackageMetadata()
const iterable = atom.packages.getAvailablePackagePaths()
for (let i = 0; i < iterable.length; i++) {
const path2 = iterable[i]
path2metadata[fs.realpathSync(path2)] = packageMetadata[i]
path2metadata[await fs.realpath(path2)] = packageMetadata[i]
}

const packages = {}
Expand Down Expand Up @@ -186,7 +182,7 @@ module.exports = {
packageManager: new PackageManager(),

async removeObsoletePackages (packages) {
const installedPackages = this.getPackages()
const installedPackages = await this.getPackages()
const removePackages = Object.keys(installedPackages)
.filter(i => !packages[i])
.map(name => {
Expand Down Expand Up @@ -264,7 +260,7 @@ module.exports = {
},

async installMissingPackages (packages) {
const availablePackages = this.getPackages()
const availablePackages = await this.getPackages()
const missingPackages = Object.keys(packages)
.filter(p => !availablePackages[p] || !p.apmInstallSource !== !availablePackages[p].apmInstallSource)
.map(name => {
Expand Down Expand Up @@ -351,7 +347,7 @@ module.exports = {

async fileContent (filePath, nullString) {
try {
const content = await readFile(filePath)
const content = await fs.readFile(filePath)
return content.toString().trim() !== '' ? content : (nullString ? Buffer.from(nullString) : null)
} catch (err) {
console.error(`Error reading file ${filePath}. Probably doesn't exist.`, err)
Expand Down
89 changes: 70 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"deep-object-diff": "^1.1.0",
"diff": "^4.0.2",
"etch": "^0.14.0",
"fs-extra": "^9.0.1",
"glob": "^7.1.6",
"is-binary-path": "^2.1.0",
"key-path-helpers": "^0.4.0",
Expand Down
40 changes: 18 additions & 22 deletions spec/integration-spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const syncSettings = require('../lib/main')
const gistApi = require('../lib/location/gist')
const SyncSettings = require('../lib/sync-settings')
const fs = require('fs')
const util = require('util')
const writeFile = util.promisify(fs.writeFile)
const readFile = util.promisify(fs.readFile)
const unlink = util.promisify(fs.unlink)
const fs = require('fs-extra')
const path = require('path')

describe('integration', () => {
Expand Down Expand Up @@ -82,10 +78,10 @@ describe('integration', () => {
if (process.env.GITHUB_TOKEN && process.platform !== 'linux') {
describe('API', () => {
beforeEach(async () => {
await writeFile(atom.keymaps.getUserKeymapPath(), '# keymap')
await writeFile(atom.styles.getUserStyleSheetPath(), '// stylesheet')
await writeFile(atom.getUserInitScriptPath(), '# init')
await writeFile(path.resolve(atom.getConfigDirPath(), 'snippets.cson'), '# snippets')
await fs.writeFile(atom.keymaps.getUserKeymapPath(), '# keymap')
await fs.writeFile(atom.styles.getUserStyleSheetPath(), '// stylesheet')
await fs.writeFile(atom.getUserInitScriptPath(), '# init')
await fs.writeFile(path.resolve(atom.getConfigDirPath(), 'snippets.cson'), '# snippets')

atom.config.set('sync-settings.gistDescription', 'Test gist by Sync Settings for Atom https://github.com/atom-community/sync-settings')

Expand All @@ -97,10 +93,10 @@ describe('integration', () => {
afterEach(async () => {
await gistApi.delete()
await atom.packages.deactivatePackage('sync-settings')
await unlink(atom.keymaps.getUserKeymapPath())
await unlink(atom.styles.getUserStyleSheetPath())
await unlink(atom.getUserInitScriptPath())
await unlink(path.resolve(atom.getConfigDirPath(), 'snippets.cson'))
await fs.unlink(atom.keymaps.getUserKeymapPath())
await fs.unlink(atom.styles.getUserStyleSheetPath())
await fs.unlink(atom.getUserInitScriptPath())
await fs.unlink(path.resolve(atom.getConfigDirPath(), 'snippets.cson'))
})

it('backs up files', async () => {
Expand Down Expand Up @@ -139,7 +135,7 @@ describe('integration', () => {
it('restores files', async () => {
atom.config.set('sync-settings.extraFiles', ['README'])
await atom.commands.dispatch(atom.views.getView(atom.workspace), 'sync-settings:restore')
const readme = await readFile(path.resolve(atom.getConfigDirPath(), 'README'), { encoding: 'utf8' })
const readme = await fs.readFile(path.resolve(atom.getConfigDirPath(), 'README'), { encoding: 'utf8' })

expect(readme).toBe('# Generated by Sync Settings for Atom\n\n<https://github.com/atom-community/sync-settings>')
}, (process.env.CI ? 60 : 10) * 1000)
Expand All @@ -152,7 +148,7 @@ describe('integration', () => {
process.env.GITHUB_TOKEN = ''
atom.config.set('sync-settings.extraFiles', ['README'])
await atom.commands.dispatch(atom.views.getView(atom.workspace), 'sync-settings:restore')
const readme = await readFile(path.resolve(atom.getConfigDirPath(), 'README'), { encoding: 'utf8' })
const readme = await fs.readFile(path.resolve(atom.getConfigDirPath(), 'README'), { encoding: 'utf8' })

expect(readme).toBe('# Generated by Sync Settings for Atom\n\n<https://github.com/atom-community/sync-settings>')
} finally {
Expand All @@ -164,22 +160,22 @@ describe('integration', () => {
it('backs up and restores paths with slash', async () => {
atom.config.set('sync-settings.extraFiles', ['../test.tmp'])
const tmpPath = path.resolve(atom.getConfigDirPath(), '../test.tmp')
await writeFile(tmpPath, 'test.tmp')
await fs.writeFile(tmpPath, 'test.tmp')
try {
await atom.commands.dispatch(atom.views.getView(atom.workspace), 'sync-settings:backup')
await unlink(tmpPath)
await fs.unlink(tmpPath)
await atom.commands.dispatch(atom.views.getView(atom.workspace), 'sync-settings:restore')
const content = await readFile(tmpPath, { encoding: 'utf8' })
const content = await fs.readFile(tmpPath, { encoding: 'utf8' })

expect(content).toBe('test.tmp')
} finally {
await unlink(tmpPath)
await fs.unlink(tmpPath)
}
}, (process.env.CI ? 60 : 10) * 1000)

it('does not delete a file with only whitespace', async () => {
atom.config.set('sync-settings.extraFiles', ['README'])
await writeFile(path.resolve(atom.getConfigDirPath(), 'README'), '\n \t')
await fs.writeFile(path.resolve(atom.getConfigDirPath(), 'README'), '\n \t')
await atom.commands.dispatch(atom.views.getView(atom.workspace), 'sync-settings:backup')
const data = await gistApi.get()
expect('README' in data.files).toBe(true)
Expand All @@ -189,9 +185,9 @@ describe('integration', () => {
it('does delete an unfamiliar file', async () => {
atom.config.set('sync-settings.extraFiles', ['README'])
atom.config.set('sync-settings.removeUnfamiliarFiles', true)
await writeFile(path.resolve(atom.getConfigDirPath(), 'README'), 'README')
await fs.writeFile(path.resolve(atom.getConfigDirPath(), 'README'), 'README')
await atom.commands.dispatch(atom.views.getView(atom.workspace), 'sync-settings:backup')
await unlink(path.resolve(atom.getConfigDirPath(), 'README'))
await fs.unlink(path.resolve(atom.getConfigDirPath(), 'README'))
await atom.commands.dispatch(atom.views.getView(atom.workspace), 'sync-settings:backup')
const data = await gistApi.get()
expect('README' in data.files).toBe(false)
Expand Down
Loading

0 comments on commit 80c61f5

Please sign in to comment.