Is it possible to get the 'version' from a package.json file inside Kirby?

Hello
In site/config/config.php,
I set a version variable: c::set('version', '1.1.2');
Which is used to avoid caching problems on assets like this:

<?= css('assets/dist/css/styles.css?v=' . c::get('version')) ?>
<?= js(array('assets/dist/js/scripts.js?v=' . c::get('version')), true); ?>

Now, instead of manually setting the version number,
I would like to get it from a package.json file.

Is it possible?


I tried (without success):

$package = json_decode(file_get_contents('../../../package.json'), true);
c::set('version', $package['version']);

Thank you

Would be easier to use cachebuster plugin instead. Essentially does the same as what your trying to do.

I do not think there is a way to pickup on the package file version number. You might be able to go the other way, by using a Git commit hook to alter the var value in the config file. A bash script might be an option too.

This should work:

$string = f::read(kirby()->roots()->index() . '/package.json');
$json = json_decode($string, true);
$version =  $json['version'];
c::set('version', $version);
echo c::get('version');

@texnixe Nice work! But doesn’t that depend on where the package.json is in the file system? The reason i didnt think this possible is because generally this file is outside of the web root. Looking at @francoisromain attempt, his climbing up a few levels to reach the file. I think thats the problem?

Not necessarily, you can also do something like this:

$string = f::read(kirby()->roots()->index() . '/../../package.json');

(i.e. two up from the kirby root folder)

The important thing here is to check if you really get what you want, ideally check outside config.php, e.g. in your home template, using dump().

Another important thing is making sure that your package.json is correct, i.e. does not contain a comma after the last entry etc.

Ok, but I don’t think that will work on my setup, because using vagrant (and i’m guessing this will be the case with MAMP), you map a folder that gets shared inside the virtual machine. Usually you map the webroot (usually called public or www etc).

Because the package.json is outside this shared folder, it wont be seen. As far as the Vagrant filesystem is concerned, it doesnt exist.

Depending on what setup @francoisromain is using, he may have to adjust the folder sharing a level up in order for the file to be accessible.

@francoisromain what are you using locally to develop on?

I’m using Mamp and have no problem whatsoever to access a folder outside the webroot. Haven’t tested on remote server.

Anyway, as I said above, you have to test if the file is accessible to you in the first place.

If a

var_dump(f::read(kirby()->roots()->index() . '/../../package.json'));

(or whatever the path) does not return anything, the rest will not work.

But no need to guess, let’s see what @francoisromain setup is and what he gets back from this.

this works perfectly locally (on Mamp) and remotely on Gandi simple hosting. Thank you!
And thanks @jimbobrjames for the cachebuster plugin. I didn’t know about it yet.

For other Windows users, I can recommend to use the Kirby constant DS instead of / because Windows works a bit different than OSX.

Maybe f::read converts / to \ automatically? But to always be on the safeside, use DS.