ui5-tooling icon indicating copy to clipboard operation
ui5-tooling copied to clipboard

Access package.json in custom task

Open jonaszuberbuehler opened this issue 4 years ago • 5 comments

Expected Behavior

It would be nice to be able to access the projects package.json within a custom task (e.g. for getting the version or the like).

Current Behavior

The only hacky way I've found was by using workspace._reader._readers[0]._project.

Affected components (if known)

jonaszuberbuehler avatar Apr 07 '20 06:04 jonaszuberbuehler

Hey, we already have the general requirement to read (and write) files from a projects root directory.

Therefore we are wondering whether you are only interested in the projects version or also need other information from the package.json. In case you just need the version, we could also think of enhancing the custom task API to also pass in the project version.

RandomByte avatar Apr 20 '20 15:04 RandomByte

I was only interested in the version (passing in the whole package.json wouldn't hurt though IMO).

jonaszuberbuehler avatar Apr 20 '20 16:04 jonaszuberbuehler

Ok! We'll discuss this and may add the version information as an additional parameter to the custom task interface 👍

passing in the whole package.json wouldn't hurt though IMO

The problem is that we don't want to force projects to use npm or even have a package.json. Our "translator" concept allows the use of other dependency management tools, so we can't really rely on a package.json.

RandomByte avatar Apr 20 '20 16:04 RandomByte

The problem is that we don't want to force projects to use npm or even have a package.json. Our "translator" concept allows the use of other dependency management tools, so we can't really rely on a package.json.

Ah ok, wasn't aware of this (didn't dig deep enough into the tooling so far).

jonaszuberbuehler avatar Apr 20 '20 16:04 jonaszuberbuehler

This is how I currently set the version/commit hash during build process (once this is done, this certainly can be improved):

module.exports = async function ({ workspace }) {
    const [view] = await workspace.byGlob('**/SomeView.view.xml');
    // it's a beauty!
    const version = workspace._reader._readers[0]._project.version;
    let commit = '';
    try {
        commit = ` (${require('child_process').execSync('git rev-parse --short HEAD').toString().trim()})`;
    } catch (e) {
        // fail silently
    }
    await view.setString((await view.getString()).replace('${version}', `${version}${commit}`));
    return workspace.write(view);
};

jonaszuberbuehler avatar Dec 03 '20 07:12 jonaszuberbuehler