plugins_release_infoproj.js

/**
 * @file plugins/release/banner.js
 * @copyright Scott P Meesseman 2024
 * @author Scott Meesseman @spmeesseman
 *//** */

const WpwPlugin = require("../base");


/**
 * @augments WpwPlugin
 */
class WpwInfoProjPlugin extends WpwPlugin
{
    /**
     * @param {WpwPluginOptions} options Plugin options to be applied
     */
    constructor(options)
    {
        super(options);
        this.buildOptions = /** @type {WpwBuildOptionsPluginConfig<"infoproj">} */(this.buildOptions);
    }


	/**
     * @override
     * @param {WpwBuild} b
     */
	static create = WpwInfoProjPlugin.wrap.bind(this);


    /**
     * @override
     * @returns {WpwPluginTapOptions<any, any, boolean>}
     */
    onApply()
    {
        return {
            executeInfoProjectBuild: {
                async: true,
                hook: "afterEmit",
                callback: this.updateInfoProjectClone.bind(this)
            }
        };
    }


    /**
	 * @private
	 * @param {WebpackCompilation} _compilation
	 * @returns {Promise<void | SpmhError>}
     */
    async updateInfoProjectClone(_compilation)
    {
		let tct = 0;
		const b = this.build,
		      l = b.logger,
              bp = b.getBasePath(),
			  bo = this.buildOptions;
		try
		{
            if (bo.changelog)
            {
                const changelog = this._path_.resolve(bp, bo.changelog);
                if (this._fs_.existsSync(changelog))
                {
                    await this._fs_.copyFileAsync(changelog, this._path_.resolve(bo.path, bo.changelog));
				    tct += 1;
                }
            }
            if (bo.license)
            {
                const license = this._path_.resolve(bp, bo.license);
                if (this._fs_.existsSync(license))
                {
                    await this._fs_.copyFileAsync(license, this._path_.resolve(bo.path, bo.license));
				    tct += 1;
                }
            }
            if (bo.readme)
            {
                const readme = this._path_.resolve(bp, bo.readme);
                if (this._fs_.existsSync(readme))
                {
                    await this._fs_.copyFileAsync(readme, this._path_.resolve(bo.path, bo.readme));
				    tct += 1;
                }
            }
            for (const i of this._arr_.asArray(bo.items))
			{
                if (this._fs_.isDirectory(i.path))
				{
					tct += await this._fs_.copyDirAsync(i.path, bo.path, i.exclude, true);
				}
				else
				{   await this._fs_.copyFileAsync(i.path, this._path_.resolve(bo.path, i.path));
					tct += 1;
				}
			}
			l.write(`${tct} total files copied to info project clone`, 1);
		}
		catch (e)
		{   this.addMessage(
            {   exception: e,
                code: this.MsgCode.ERROR_PLUGIN_HOOK_FAILED,
                message: "faied to execute 'schema' type build"
            }, true);
		}
    }

}


module.exports = WpwInfoProjPlugin.create;