Skip to content

Commit

Permalink
feat: Add options to generateFiles command
Browse files Browse the repository at this point in the history
  • Loading branch information
Merkur39 committed Mar 8, 2024
1 parent ca8dc89 commit ebe283f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
23 changes: 19 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,24 @@ const handleImportDirCommand = async args => {
}

const handleGenerateFilesCommand = async args => {
const { dirId = 'io.cozy.files.root-dir', filesCount = 10, url, token } = args
const { dirId, filesCount, url, token, qualify, mime } = args

if (qualify && !token) {
log.warn(
'To view files in MyPapers, an app/kconnector token is required to create qualified files.\nThis token allows you to have the `cozyMetadata.createdByApp` prop on files.'
)
}

const ach = new ACH(token || autotoken(url, ['io.cozy.files']), url, [
'io.cozy.files'
])
await ach.connect()
await ach.createFiles(parseInt(filesCount), dirId)
await ach.createFiles({
filesCount: parseInt(filesCount),
dirId,
qualify,
mime
})
}

const handleDropCommand = async args => {
Expand Down Expand Up @@ -322,14 +334,17 @@ program

program
.command('generateFiles [filesCount] [dirId]')
.option('-q, --qualify', 'Add qualification to the files')
.option('-m, --mime <s>', 'Create files with this mime type')
.description('Generates a given number of small files.')
.action(
handleErrors(async function(filesCount, dirId) {
handleErrors(async function(filesCount, dirId, options) {
await handleGenerateFilesCommand({
url: program.url,
token: program.token,
filesCount,
dirId,
filesCount
...options
})
})
)
Expand Down
24 changes: 21 additions & 3 deletions libs/createFiles.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
const crypto = require('crypto')
const ProgressBar = require('progress')
const faker = require('faker')

const {
uploadFileWithConflictStrategy
} = require('cozy-client/dist/models/file')
const { Qualification } = require('cozy-client/dist/models/document')
const {
qualifications
} = require('cozy-client/dist/assets/qualifications.json')

const addQualification = () => {
const { label } = qualifications[crypto.randomInt(0, qualifications.length)]
const qualification = Qualification.getByLabel(label)

return { qualification }
}

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

Expand All @@ -19,7 +36,8 @@ module.exports = async (
await uploadFileWithConflictStrategy(client, buffer.toString(), {
name,
dirId,
contentType: 'text/plain',
contentType: mime,
...(qualify ? { metadata: addQualification() } : {}),
conflictStrategy: 'rename'
})
bar.tick()
Expand Down

0 comments on commit ebe283f

Please sign in to comment.