Skip to content

Commit

Permalink
feat: Migrate generateFiles command with new client
Browse files Browse the repository at this point in the history
BREACKING CHANGE: The `path` argument is replaced by `dirId`, and goes to second position.

Example to generate 2 files at the root of your Drive:
Before
```
ACH generateFiles <folderPath> <nbOfFile>
```

Now
```
ACH generateFiles <nbOfFile> <folderId>
```
  • Loading branch information
Merkur39 committed Mar 11, 2024
1 parent f4b2de8 commit 0aa7178
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
10 changes: 5 additions & 5 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ const handleImportDirCommand = async args => {
}

const handleGenerateFilesCommand = async args => {
const { path = '/', filesCount = 10, url, token } = args
const { dirId = 'io.cozy.files.root-dir', filesCount = 10, url, token } = args
const ach = new ACH(token || autotoken(url, ['io.cozy.files']), url, [
'io.cozy.files'
])
await ach.connect()
await ach.createFiles(path, parseInt(filesCount))
await ach.createFiles(parseInt(filesCount), dirId)
}

const handleDropCommand = async args => {
Expand Down Expand Up @@ -321,14 +321,14 @@ program
)

program
.command('generateFiles [path] [filesCount]')
.command('generateFiles [filesCount] [dirId]')
.description('Generates a given number of small files.')
.action(
handleErrors(async function(path, filesCount) {
handleErrors(async function(filesCount, dirId) {
await handleGenerateFilesCommand({
url: program.url,
token: program.token,
path,
dirId,
filesCount
})
})
Expand Down
2 changes: 1 addition & 1 deletion libs/ACH.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const methods = {
},
createFiles: {
method: createFiles,
oldClient: true
oldClient: false
},
export: {
method: exportDocs,
Expand Down
24 changes: 17 additions & 7 deletions libs/createFiles.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
const ProgressBar = require('progress')
const faker = require('faker')
const {
uploadFileWithConflictStrategy
} = require('cozy-client/dist/models/file')

module.exports = async (client, path, filesCount) => {
const { forceCreateByPath } = client.files

module.exports = async (
client,
filesCount,
dirId = 'io.cozy.files.root-dir'
) => {
const bar = new ProgressBar(':bar', { total: filesCount })

for (let i = 0; i < filesCount; i++) {
const name = faker.datatype.uuid()
await forceCreateByPath(`${path}/${name}.txt`, name, {
name: `${name}.txt`,
contentType: 'text/plain'
const name = faker.lorem.word()
const content = faker.lorem.paragraph()
const buffer = new Buffer.from(content, 'utf-8')

await uploadFileWithConflictStrategy(client, buffer.toString(), {
name,
dirId,
contentType: 'text/plain',
conflictStrategy: 'rename'
})
bar.tick()
}
Expand Down

0 comments on commit 0aa7178

Please sign in to comment.