Extend args.code functionality in extensions system

Post Reply
Adam
User
Posts: 480

Extend args.code functionality in extensions system

Post by Adam »

The args.code feature of the extensions system is incredibly powerful when used in its regex form - as in this simple example:

<# args.code = args.code.replace(/(Config\("PROJECT_STYLESHEET_FILENAME"\))(\) \?>)/gm, '$&<?php echo "?" . filemtime($RELATIVE_PATH . $1) ?>'); #>

By creating a layout.php file containing that single line and adding it to a custom extension, generated applications will always load the latest project CSS without users needing to force a refresh with Ctrl-F5 after CSS changes.

Especially when significant numbers of code changes need to be applied, this avoids the need to manually apply those same code changes to the latest version of each affected file and update the custom extension whenever the PHPM template gets updated... which is almost daily in the weeks following the launch of a new version.

Unfortunately, this great feature can only be applied to "generated" files in the /src, /models and /views folders at present - "copied" files i.e. those in the /js and other folders, cannot be processed this way and means the benefits of easy code replacement via regexes is diminished.

I would therefore like to see the current functionality extended to other files / folders to open up the possibilities for further customisation and tweaks of the PHPM system.

Neelima
User
Posts: 35

Post by Neelima »

Good suggestion

+1

arbei
User
Posts: 9387

Post by arbei »

Since PHPMaker uses Node.js to generate scripts, you should be able to read an existing file by Node.js directly, e.g. you may try:

1. In User Code (see the topic Using User Code in the help file)

global.fs = require('fs'); // Get the Node.js file system module
global.process = require('process'); // Get the Node.js process module

2. In your extension (See the topic Template Tags in the help file)

<#
let code = fs.readFileSync(process.env.APPDATA + '\\PHPMaker\\php20210\\template\\js\\xxx.js', { encoding: 'utf8' }); // Use Node.js fs to read existing file from the template cache
code = code.replace(/.../, "..."); // Replace the code
write(code); // Output the code
#>

Adam
User
Posts: 480

Post by Adam »

Thanks for the suggestion, arbei - I'll look into that approach :)

Post Reply