sync-wp-manifest

Sync package metadata (package.json) to wordpress plugin entry file

Documentation

Installation

With node.js installed, use npm to install this package:

npm install sync-wp-manifest

Usage

You can run this package as a standalone executable or use it in a node.js script.

From the command line

sync-wp-manifest ./my-plugin/my-plugin.php ./my-plugin/package.json

Run sync-wp-manifest --help to view all available options

node.js API

import { replaceCommentInFile } from 'sync-wp-manifest';

replaceCommentInFile('./my-plugin/my-plugin.php', { version: '1.2.3' })
  .then(() => console.log('Done'))
  .catch((error) => {
    console.error(error);
    process.exitCode = 1;
  });

Result:

<?php
/**
 * Version: 1.2.3
 */
?>

you can also use custom transformations:

replaceCommentInFile('./my-plugin/my-plugin.php', { version: '1.2.3', description: 'My plugin is cool' }, {
  transform(
    name, // The name, as'Version'
    value, // '1.2.3'
    key // 'version'
    ) {
    // Return the resulting name and value as an array
    if (key === 'version') return [name, `${value}-alpha`];

    // ...or return null or undefined to use default transformation
    return undefined;
  }
})
  .then(() => ...)

Result:

<?php
/**
 * Version: 1.2.3-alpha
 * Description: My plugin is cool
 */
?>

Default transforms

By default, this package applies the following transformations to the input data:

  • Rename the name property to Plugin Name and capitalizes it's value, while removing the package scope, as well as the words plugin, wordpress and wp

    Example

    Input:

    {
      "name": "@my-company/extra-cool-wordpress-plugin"
    }

    Output:

    <?php
    /**
     * Plugin Name: Extra Cool
     */
    ?>
  • Replace UNLICENSED inside the license property to proprietary

Use a custom transformer to disable any of these transformations

Override values

You can use the wpPlugin property inside your manifest to override any values:

Input:

{
  "description": "This description is for internal use only",
  "wpPlugin": {
    "Description": "The public plugin description"
  }
}

Output:

<?php
/**
 * Description: The public plugin description
 */
?>

Note that values and keys inside wpPlugins will not be transformed, wo you have to pass the exact name of the property (eg Description, Plugin Name) instead of the keys used in package.json otherwise (description, name) respectively.