Skip to content

Commit

Permalink
feat(writetofile): add ability to insert date to filename (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
arszh authored and lirantal committed Nov 29, 2019
1 parent c9dc7d3 commit 0a56e8f
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ typeformToExcel
.createWorkbookFromForm(formId)
.then(() => {
return typeformToExcel.writeToFile({
filename: 'out.xlsx'
filename: 'out.xlsx',
isDated: true
})
})
.then(() => {
Expand All @@ -82,6 +83,8 @@ The following is supported command line arguments:
| --apiKey | 1234 | typeform's api key, for example: `--apiKey 1234` |
| --formId | Pdi981 | the relevant form id, usually shows up in the URL, for example: `--formId Pdi981` |
| --filename | out.xlsx | the filename to create and write to, for example: `--filename out.xlsx` |
| --dated | | (optional) the argument for adding export date to filename, for example: `--dated`|


Example:

Expand Down
42 changes: 42 additions & 0 deletions __tests__/export.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,46 @@ describe('E2E Export to Excel', () => {
fs.unlinkSync(testOutputFilename)
}
})

test('Creating an Excel file with date in filename', async () => {
expect.assertions(1)

const testOutputFilename = 'test-out.xlsx'
const expectedTestOutputFilename = 'test-out_2016-06-20--03-08-10.xlsx'
const RealDate = Date
const mockDate = new Date(2016, 5, 20, 3, 8, 10)

global.Date = class extends RealDate {
constructor() {
return new RealDate(mockDate)
}
}

Form.prototype.fetchFormResponses = jest.fn(() => mockForm)

const typeformToExcel = new TypeformExportExcel({
credentials: {
apiKey: mockApiKey
},
workbookConfig: {
creator: 'Creator',
date: new Date()
}
})

await typeformToExcel.createWorkbookFromForm(mockForm.id)

await typeformToExcel.writeToFile({
filename: testOutputFilename,
isDated: true
})

// eslint-disable-next-line
const isFileExist = fs.existsSync(path.resolve(expectedTestOutputFilename))
expect(isFileExist).toBeTruthy()

global.Date = RealDate
// eslint-disable-next-line
fs.unlinkSync(expectedTestOutputFilename)
})
})
7 changes: 6 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const args = arg({
'--apiKey': String,
'--formId': String,
'--filename': String,
'--dated': Boolean,
'--author': String,
'-v': '--version',
'-h': '--help',
Expand All @@ -26,6 +27,9 @@ debug(`parsed formId: ${formId}`)
const fileName = args['--filename'] || 'typeform-export-excel.xlsx'
debug(`parsed filename: ${fileName}`)

const isDated = args['--dated']
debug(`parsed filename: ${fileName}`)

if (!apiKey || !formId) {
console.error('Error: Please provide both apiKey and formId')
}
Expand All @@ -44,7 +48,8 @@ typeformToExcel
.createWorkbookFromForm(formId)
.then(() => {
return typeformToExcel.writeToFile({
filename: fileName
filename: fileName,
isDated: isDated
})
})
.then(() => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"@lirantal/typeform-client": "^1.0.5",
"arg": "^3.0.0",
"debug": "^4.1.1",
"exceljs": "^1.15.0"
"exceljs": "^1.15.0",
"moment": "^2.24.0"
},
"devDependencies": {
"@commitlint/cli": "^7.2.1",
Expand Down
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Excel = require('exceljs')
const debug = require('debug')('typeform-export-excel')
const {Form} = require('@lirantal/typeform-client')
const {HEADER_NAME, HEADER_WIDTH, HEADER_STYLE, WORKSHEET_COLOR} = require('./constants')
const utils = require('./utils')

const WORKSHEET_NAME_CHAR_LIMIT = 64

Expand Down Expand Up @@ -232,13 +233,22 @@ module.exports = class TypeformExportExcel {
})
}

async writeToFile({filename}) {
async writeToFile({filename, isDated}) {
try {
const finalFileName = isDated ? this._addDateInFileName(filename, new Date()) : filename
// eslint-disable-next-line
await this.workbook.xlsx.writeFile(filename)
await this.workbook.xlsx.writeFile(finalFileName)
} catch (err) {
debug(err)
throw err
}
}

_addDateInFileName(filename, date) {
return utils.insertString(
filename,
'_' + utils.getFormattedDate(date, 'YYYY-MM-DD--HH-mm-ss'),
filename.lastIndexOf('.')
)
}
}
15 changes: 15 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'
const moment = require('moment')

const insertString = (string, subString, index) => {
return string.slice(0, index) + subString + string.slice(index)
}

const getFormattedDate = (date, format) => {
return moment(date).format(format)
}

module.exports = {
insertString,
getFormattedDate
}

0 comments on commit 0a56e8f

Please sign in to comment.