feat(plugin-tools): first version of the package with some nice plugin utilities

This commit is contained in:
Joakim Carlstein 2023-11-09 09:26:48 +01:00
parent cccdfb817d
commit cdafd05c20
5 changed files with 193 additions and 0 deletions

View file

@ -0,0 +1,56 @@
import { type GeneratorPlugin, type StoragePlugin } from './types.js';
export const createStoragePlugin = (initialize: StoragePlugin['initialize']): StoragePlugin => {
return {
type: 'storage',
initialize,
};
};
export const createGeneratorPlugin = (generate: GeneratorPlugin['generate']): GeneratorPlugin => {
return {
type: 'generator',
generate,
};
};
export const isGeneratorPlugin = (plugin: any): plugin is GeneratorPlugin => {
if (!plugin || typeof plugin !== 'object') {
return false;
}
if (plugin.type === 'generator') {
return typeof plugin.generate === 'function';
}
return false;
};
export const isStoragePlugin = (plugin: any): plugin is StoragePlugin => {
if (!plugin || typeof plugin !== 'object') {
return false;
}
if (plugin.type === 'storage') {
return typeof plugin.initialize === 'function';
}
return false;
};
/**
* Get a timestamp string in the format YYYYMMDDHHmmssmmm based on the current time (UTC)
*
* Can be used to prefix migration filenames so that they are executed in the correct order
*
* @returns A timestamp string in the format YYYYMMDDHHmmssmmm
*/
export const getTimestampPrefix = () => new Date().toISOString().replaceAll(/[-:ZT.]/g, '');
/**
* A utility function to sanitize a migration name so that it can be used as a filename
*
* @param name A migration name to sanitize
* @returns A sanitized migration name that can be used as a filename
*/
export const sanitizeMigrationName = (name: string) => name.replaceAll(/[\W/\\:|*?'"<>]/g, '_').trim();