Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/fair-cobras-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@flatfile/plugin-automap': minor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let change this to a major

---

Add onSuccess callback.
Add new options to only map if there is no unmatched columns (none, both, only-source, only-target)
Add options to change file name in success/failure case
Add options to overwrite file name patterns
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 63 additions & 2 deletions plugins/automap/src/automap.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { AutomapService } from './automap.service'
* @param options - config options
*/
export function automap(options: AutomapOptions) {
const automapper = new AutomapService(options)
const optionsDefaulted = defaultOptions(options)
const automapper = new AutomapService(optionsDefaulted)

return (listener: FlatfileListener): void => {
automapper.assignListeners(listener)
Expand All @@ -21,8 +22,17 @@ export function automap(options: AutomapOptions) {
* @property {boolean} debug - show helpul messages useful for debugging (use intended for development).
* @property {string} defaultTargetSheet - exact sheet name to import data to.
* @property {RegExp} matchFilename - a regular expression to match specific files to perform automapping on.
* @property {Function} onFailure - callback to be executed when plugin bails.
* @property {string} allColumnsMustBeMapped - specify if all columns must be mapped. Values: 'none', 'both', 'only-source', 'only-target'.
* @property {Function} onSuccess - callback to be executed when plugin succeeds.
* @property {Function} onFailure - callback to be executed when plugin fails.
* @property {string} targetWorkbook - specify destination Workbook id or name.
* @property {boolean} disableFileNameUpdate - disable filename update on automap.
* @property {boolean} disableFileNameUpdateOnSuccess - disable filename update on success.
* @property {boolean} disableFileNameUpdateOnFailure - disable filename update on failure.
* @property {string} filenameOnCheck - filename on check mapping. Can use {{fileName}}.
* @property {string} filenameOnStart - filename on start mapping. Can use {{fileName}}, {{destinationSheetName}}.
* @property {string} filenameOnSuccess - filename on success mapping. Can use {{fileName}}, {{destinationSheetName}}.
* @property {string} filenameOnFailure - filename on failure mapping. Can use {{fileName}}.
*/
export interface AutomapOptions {
readonly accuracy: 'confident' | 'exact'
Expand All @@ -31,7 +41,58 @@ export interface AutomapOptions {
| string
| ((fileName?: string, event?: FlatfileEvent) => string | Promise<string>)
readonly matchFilename?: RegExp
readonly allColumnsMustBeMapped?:
| 'none'
| 'both'
| 'only-source'
| 'only-target'
readonly onSuccess?: (event: FlatfileEvent) => void
readonly onFailure?: (event: FlatfileEvent) => void
readonly targetWorkbook?: string
readonly disableFileNameUpdate?: boolean
readonly disableFileNameUpdateOnSuccess?: boolean
readonly disableFileNameUpdateOnFailure?: boolean
readonly filenameOnCheck?: string
readonly filenameOnStart?: string
readonly filenameOnSuccess?: string
readonly filenameOnFailure?: string
}

export function defaultOptions(options: AutomapOptions): AutomapOptions {
const defaultedOptions = {
...options,
accuracy: options.accuracy || 'confident',
allColumnsMustBeMapped: options.allColumnsMustBeMapped || 'none',
filenameOnCheck: options.filenameOnCheck || '⚡️ {{fileName}}',
filenameOnStart:
options.filenameOnStart || '⚡️ {{fileName}} 🔁 {{destinationSheetName}}',
filenameOnSuccess:
options.filenameOnSuccess ||
'⚡️ {{fileName}} 🔁 {{destinationSheetName}}',
filenameOnFailure:
options.filenameOnFailure ||
'⚡️ {{fileName}} 🔁 {{destinationSheetName}}',
}

if (!defaultedOptions.filenameOnCheck.includes('{{fileName}}')) {
defaultedOptions.filenameOnCheck =
defaultedOptions.filenameOnCheck + ' {{fileName}}'
}

if (!defaultedOptions.filenameOnStart.includes('{{fileName}}')) {
defaultedOptions.filenameOnStart =
defaultedOptions.filenameOnStart + ' {{fileName}}'
}

if (!defaultedOptions.filenameOnSuccess.includes('{{fileName}}')) {
defaultedOptions.filenameOnSuccess =
defaultedOptions.filenameOnSuccess + ' {{fileName}}'
}

if (!defaultedOptions.filenameOnFailure.includes('{{fileName}}')) {
defaultedOptions.filenameOnFailure =
defaultedOptions.filenameOnFailure + ' {{fileName}}'
}

return defaultedOptions
}
Loading
Loading