UNPKG

gulp-banner-footer

Version:

gulp plugin for adding banner or footer to file.

45 lines (44 loc) 1.37 kB
// src/index.ts import { Buffer } from "node:buffer"; import { relative } from "node:path"; import process from "node:process"; import { c, createLogger } from "@ntnyq/logger"; import PluginError from "plugin-error"; import through from "through2"; var rootDir = process.cwd(); var PLUGIN_NAME = "gulp-banner-footer"; var logger = createLogger({ time: "HH:mm:ss" }); var addBannerOrFooter = (options = {}) => { function transform(file, _, cb) { if (file.isNull()) return cb(null, file); if (file.isStream()) { const errorOptions = Object.assign({}, { fileName: file.path }); const error = new PluginError( PLUGIN_NAME, new Error("Streaming not supported"), errorOptions ); return cb(error); } if (options.verbose) { logger.info( `${c.yellow(PLUGIN_NAME)}: ${c.green(relative(rootDir, file.path))}` ); } const contents = Buffer.from( [ typeof options.banner === "function" ? options.banner(file) : options.banner, file.contents?.toString() ?? "", typeof options.footer === "function" ? options.footer(file) : options.footer ].join("\n") ); file.contents = contents; return cb(null, file); } return through.obj(transform); }; var index_default = addBannerOrFooter; export { addBannerOrFooter, index_default as default };