UNPKG

typescript-file-copy-plugin

Version:

[![NPM version](https://img.shields.io/npm/v/typescript-file-copy-plugin.svg?style=flat-square)](https://www.npmjs.com/package/typescript-file-copy-plugin) ![NPM Downloads](https://img.shields.io/npm/dm/typescript-file-copy-plugin) [![TypeScript](https://

1 lines 4.97 kB
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { copyFileSync, existsSync, statSync } from \"node:fs\";\nimport { dirname, extname, resolve } from \"node:path\";\nimport { copySync } from \"cpx2\";\nimport { mkdirpSync } from \"mkdirp\";\nimport type * as ts from \"typescript\";\n\ninterface FileCopyConfig {\n /**\n * The source file or directory to copy.\n */\n src: string;\n /**\n * The destination file or directory to copy to.\n */\n dest: string;\n}\n\ninterface CopyFilesPluginOptions {\n copy: FileCopyConfig[];\n}\n\n/**\n * Validates the options passed to the plugin.\n * Ensures that the 'copy' property is defined, is an array,\n * and each item in the array has both 'src' and 'dest' properties.\n *\n * @param options - The options to validate\n * @throws Will throw an error if validation fails\n */\nfunction validateOptions(options: CopyFilesPluginOptions): void {\n if (!options.copy || !Array.isArray(options.copy)) {\n throw new Error(\"The 'copy' parameter must be defined and be an array.\");\n }\n\n options.copy.forEach((config, index) => {\n if (!config.src) {\n throw new Error(`The 'src' property is missing in the copy config at index ${index}.`);\n }\n if (!config.dest) {\n throw new Error(`The 'dest' property is missing in the copy config at index ${index}.`);\n }\n });\n}\n\n/**\n * Creates a transformer factory for the TypeScript program.\n * In this case, it simply returns the source file without any transformations.\n *\n * @param program - The TypeScript program\n * @param options - The plugin options\n * @returns A factory function for creating transformers\n */\nfunction createCopyFilesTransformer(\n program: ts.Program,\n options: CopyFilesPluginOptions,\n): ts.TransformerFactory<ts.SourceFile> {\n return (context) => (sourceFile) => {\n return sourceFile; // No transformation, just copying files post compilation\n };\n}\n\n/**\n * Handles the copying of files post compilation.\n * Overrides the emit method of the TypeScript program to perform file copying.\n *\n * @param program - The TypeScript program\n * @param options - The plugin options\n */\nfunction copyFilesPostCompilation(program: ts.Program, options: CopyFilesPluginOptions) {\n const copiedPaths = new Set<string>();\n const origEmit = program.emit;\n\n // Override the emit method\n program.emit = (...args) => {\n const result = origEmit(...args);\n\n for (const config of options.copy) {\n const resolvedDest = resolve(config.dest);\n // Check if the destination has already been copied\n if (!copiedPaths.has(resolvedDest)) {\n // Check if the source is a file and the destination string contains a file extension\n if (existsSync(config.src) && statSync(config.src).isFile() && extname(config.dest)) {\n // Create the destination directory if it doesn't exist\n mkdirpSync(dirname(resolvedDest));\n // Use the native fs.copyFileSync for file-to-file copying\n copyFileSync(config.src, resolvedDest);\n } else {\n // Use cpx2 for directory or wildcard pattern copying\n copySync(config.src, config.dest);\n }\n copiedPaths.add(resolvedDest);\n }\n }\n\n return result;\n };\n}\n\n/**\n * The main plugin function.\n * Validates the options, sets up post compilation file copying,\n * and creates the necessary transformer.\n *\n * @param program - The TypeScript program\n * @param pluginOptions - The options for the plugin\n * @returns A factory function for creating transformers\n */\nexport default function (program: ts.Program, pluginOptions: CopyFilesPluginOptions) {\n validateOptions(pluginOptions);\n copyFilesPostCompilation(program, pluginOptions);\n return createCopyFilesTransformer(program, pluginOptions);\n}\n"],"mappings":";AAAA,SAAS,cAAc,YAAY,gBAAgB;AACnD,SAAS,SAAS,SAAS,eAAe;AAC1C,SAAS,gBAAgB;AACzB,SAAS,kBAAkB;AA0B3B,SAAS,gBAAgB,SAAuC;AAC9D,MAAI,CAAC,QAAQ,QAAQ,CAAC,MAAM,QAAQ,QAAQ,IAAI,GAAG;AACjD,UAAM,IAAI,MAAM,uDAAuD;AAAA,EACzE;AAEA,UAAQ,KAAK,QAAQ,CAAC,QAAQ,UAAU;AACtC,QAAI,CAAC,OAAO,KAAK;AACf,YAAM,IAAI,MAAM,6DAA6D,KAAK,GAAG;AAAA,IACvF;AACA,QAAI,CAAC,OAAO,MAAM;AAChB,YAAM,IAAI,MAAM,8DAA8D,KAAK,GAAG;AAAA,IACxF;AAAA,EACF,CAAC;AACH;AAUA,SAAS,2BACP,SACA,S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