Skip to content

Commit

Permalink
Merge pull request #131 from cozy/fix/windows_download
Browse files Browse the repository at this point in the history
Fix: Make ACH work on Windows
  • Loading branch information
Ldoppea authored Aug 25, 2022
2 parents a5a9653 + e9a2f64 commit b50f1e4
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 11 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,17 @@ You can create photo albums from a folder on your disk.
$ python scripts/albums-from-dir.py my-photos-directory resulting-albums.json
```

### Export data keeping the ids
### Export data removing the ids

By default, `_id` and `_rev` are stripped from the exported data. Sometimes, you want to keep the ids/rev of the documents you export. Set the
environment variable `ACH_KEEP_ID` to do so :
By default, exported data contains all `_id`. Sometimes, you want to strip the ids from the documents you export. Set the
environment variable `ACH_NO_KEEP_ID` to do so :

```bash
env ACH_KEEP_ID=true ACH export io.cozy.bills --url https://isabelledurand.cozy.rocks /tmp/bills.json
env ACH_NO_KEEP_ID=true ACH export io.cozy.bills --url https://isabelledurand.cozy.rocks ./bills.json
```

`ACH_KEEP_REV` does the same to keep the `_rev` field.
Contrary to `_id`, `_rev` is stripped by default. To keep the `_rev` field, set the
environment variable `ACH_KEEP_REV`.


### Import data from a CSV
Expand Down
4 changes: 3 additions & 1 deletion libs/ACH.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const cozyFetch = require('./cozyFetch')
const log = require('./log')
const request = require('request')
const fs = require('fs')
const path = require('path')
const os = require('os')

const { handleBadToken } = require('../libs/utils')

Expand All @@ -34,7 +36,7 @@ const getTokenPath = function(url, doctypes) {
.slice()
.sort()
.join(',')
return '/tmp/.ach-token-' + hashCode(key) + '.json'
return path.join(os.tmpdir(), '.ach-token-' + hashCode(key) + '.json')
}

class ACH {
Expand Down
6 changes: 4 additions & 2 deletions libs/exportData.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const _ = require('lodash')
const fs = require('fs').promises
const fs = require('fs')
const path = require('path')
const log = require('./log')
const { Q } = require('cozy-client')

Expand Down Expand Up @@ -81,7 +82,8 @@ module.exports = (cozyClient, doctypes, filename, last) => {
if (filename === '-' || !filename) {
console.log(json)
} else {
return fs.writeFile(filename, json)
fs.mkdirSync(path.dirname(filename), { recursive: true })
return fs.promises.writeFile(filename, json)
}
})
}
3 changes: 2 additions & 1 deletion libs/getClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ exported.getClientWithoutToken = tokenPath => (url, docTypes = []) => {
cozyClient._token = new AppToken({ token })

log.debug('Writing token file to ' + tokenPath)
fs.mkdirSync(path.dirname(tokenPath), { recursive: true })
fs.writeFileSync(tokenPath, JSON.stringify({ token: token }), 'utf8')

return revokeACHClients(cozyClient, {
Expand Down Expand Up @@ -128,7 +129,7 @@ exported.getClientWithTokenString = tokenString => async url => {

// convenience wrapper around the 2 client getters
module.exports = (tokenPath, cozyUrl, docTypes) => {
const absoluteTokenPath = tokenPath.startsWith('/')
const absoluteTokenPath = path.isAbsolute(tokenPath)
? tokenPath
: path.join(process.cwd(), tokenPath)

Expand Down
3 changes: 2 additions & 1 deletion libs/getClient.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const getClient = require('./getClient')

jest.mock('fs', () => ({
existsSync: () => true
existsSync: () => true,
mkdirSync: jest.fn()
}))

describe('getClient', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"scripts": {
"lint": "eslint '**/*.js'",
"test": "jest",
"start": "node index.js",
"start": "node ./cli.js",
"travis-deploy-once": "travis-deploy-once --pro",
"semantic-release": "semantic-release"
},
Expand Down

0 comments on commit b50f1e4

Please sign in to comment.