Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I use standard-version for composer-based php projects? #844

Open
rowild opened this issue Oct 22, 2021 · 3 comments
Open

Can I use standard-version for composer-based php projects? #844

rowild opened this issue Oct 22, 2021 · 3 comments
Labels

Comments

@rowild
Copy link

rowild commented Oct 22, 2021

I would like to manage my release versions of my php projects locally. The version is such projects is in 2 files, a composer file, and - CMS specific - a ext-emconf.php file, which has an ARRAY (not a JSON) like this:

$EM_CONF[$_EXTKEY] = [
  [...]
  'version' => '1.0.0',
  [...]
]

Would that be at all possible?

So far I have (testing with a simple text file first):

// .versionrc

{
  "bumpFiles": [
    {
      "filename": "rwfm-version-tracker.txt",
      "type": "standard-version-composer-updater.js"
    }
  ]
}

// standard-version-composer-updater.js

const stringifyPackage = require('stringify-package')
const detectIndent = require('detect-indent')
const detectNewline = require('detect-newline')

console.log('standard-version-composer-updater initiated');

module.exports.readVersion = function (contents) {
  console.log('contents =', contents);
  return JSON.parse(contents).tracker.package.version;
}

module.exports.writeVersion = function (contents, version) {
  console.log('WRITE contents =', contents);
  console.log('WRITE version =', version);
  // const json = JSON.parse(contents)
  // let indent = detectIndent(contents).indent
  // let newline = detectNewline(contents)
  // json.tracker.package.version = version
  // return stringifyPackage(json, indent, newline)
}

All these files are at the root level.

Currently I get this:

Must use import to load ES Module: /Volumes/_III_/Z_WWW/dennis-kam/typo3-10.dennis-kam.lokal/packages/rwfm/node_modules/detect-indent/index.js
require() of ES modules is not supported.
require() of /ath/to/project/rwfm/node_modules/detect-indent/index.js from /Volumes/_III_/Z_WWW/dennis-kam/typo3-10.dennis-kam.lokal/packages/rwfm/standard-version-composer-updater.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /path/to/project/rwfm/node_modules/detect-indent/package.json.

updater.updater.readVersion is not a function

If I change all requires to imports

import stringifyPackage from 'stringify-package'
import detectIndent from 'detect-indent'
import detectNewline from 'detect-newline'

I get

Cannot use import statement outside a module
updater.updater.readVersion is not a function

I am confused...

@jbottigliero
Copy link
Member

At a glance, it looks like you are running into a few different issues.

For your .versionrc file, you should be using updater key instead of type:

{
  "bumpFiles": [
    {
      "filename": "rwfm-version-tracker.txt",
      "updater": "standard-version-composer-updater.js"
    }
  ]
}

The module-related errors you are seeing (Must use import to load ES Module, Cannot use import statement outside a module) are more based on your environment and package versions. If you've installed the latest detect-indent and detect-newline, those packages have been converted to ESM (https://github.com/sindresorhus/detect-indent/releases/tag/v7.0.0). If they are required for your updater you could use an older version and stick with the require syntax – this will likely be the path of least resistance to get your updater up and running.

We'll likely move to support ESM in an upcoming release (#837), just not quite there yet.

@rowild
Copy link
Author

rowild commented Oct 23, 2021

@jbottigliero Thank you very much for your help! All the issues you mentioned are a total hit, and after applying them, the scripts works! That's great! 👍

Would you have any suggestions on how to approach the php array?
I tried a text-only approach so far (only reading):

module.exports.readVersion = function (contents) {
  let lines = contents.split('\n')
  let version = null

  lines.forEach(line => {
    if (line.includes("'version' => '")) {
      version = line.match(/'((\d|\.)+)'/)[1]
    }
  })

  return version
}

However, I have no idea (yet) how to write back the version... (I also tried php-parser, but it ignores recognising the way the ext_emconf.php file is written .. at least I couldn't get the php parser to do its work...)

@damlys
Copy link

damlys commented Oct 5, 2022

Maybe you can try to read the version from the composer.json file 🤔

I don't remember PHP syntax very well, there is pseudo-code:

$EM_CONF[$_EXTKEY] = [
  [...]
  'version' => require("./composer.json")["version"],
  [...]
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants