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 07f6b18 commit 25188f7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
30 changes: 26 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,31 @@ const handleImportDirCommand = async args => {
}

const handleGenerateFilesCommand = async args => {
const { dirId = 'io.cozy.files.root-dir', filesCount = 10, url, token } = args
const {
dirId = 'io.cozy.files.root-dir',
filesCount = 10,
url,
token,
qualify,
mime = 'text/plain'
} = 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 +341,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
21 changes: 21 additions & 0 deletions data/qualificationsOptions/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"qualificationLabel": "passport",
"metadata": {
"country": "France",
"number": 123456789012,
"expirationDate": "2025-01-01T12:00:00.000Z",
"noticePeriod": 90
},
"contact": {
"multiple": false
}
},
{
"qualificationLabel": "family_record_book",
"metadata": {},
"contact": {
"multiple": true
}
}
]
27 changes: 21 additions & 6 deletions libs/createFiles.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
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 data = require('../data/qualificationsOptions/data.json')

const addMetadata = () => {
const randomData = data[crypto.randomInt(0, data.length)]
const { qualificationLabel, metadata } = randomData
const qualification = Qualification.getByLabel(qualificationLabel)

return {
qualification,
...metadata,
datetime: metadata.expirationDate ?? new Date().toISOString(),
datetimeLabel: metadata.expirationDate ?? 'datetime'
}
}

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

for (let i = 0; i < filesCount; i++) {
Expand All @@ -19,7 +33,8 @@ module.exports = async (
await uploadFileWithConflictStrategy(client, buffer.toString(), {
name,
dirId,
contentType: 'text/plain',
contentType: mime,
...(qualify ? { metadata: addMetadata() } : {}),
conflictStrategy: 'rename'
})
bar.tick()
Expand Down

0 comments on commit 25188f7

Please sign in to comment.