diff --git a/README.md b/README.md index 269128b..a0d17ed 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,8 @@ typeformToExcel .createWorkbookFromForm(formId) .then(() => { return typeformToExcel.writeToFile({ - filename: 'out.xlsx' + filename: 'out.xlsx', + isDated: true }) }) .then(() => { @@ -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: diff --git a/__tests__/export.test.js b/__tests__/export.test.js index e517d9c..598e66d 100644 --- a/__tests__/export.test.js +++ b/__tests__/export.test.js @@ -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) + }) }) diff --git a/bin/cli.js b/bin/cli.js index 631227b..af85302 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -11,6 +11,7 @@ const args = arg({ '--apiKey': String, '--formId': String, '--filename': String, + '--dated': Boolean, '--author': String, '-v': '--version', '-h': '--help', @@ -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') } @@ -44,7 +48,8 @@ typeformToExcel .createWorkbookFromForm(formId) .then(() => { return typeformToExcel.writeToFile({ - filename: fileName + filename: fileName, + isDated: isDated }) }) .then(() => { diff --git a/package.json b/package.json index 8418fed..d8dd8cd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js index 16cb75f..c108c18 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -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('.') + ) + } } diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..2693f00 --- /dev/null +++ b/src/utils.js @@ -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 +}