Set version number in package.json - gruntjs

I'd like to add a grunt task that accepts a version number. This version number will then be set in the package.json file. I have found grunt-bump, which bumps the version number, but I would like to set the version number to a known value, that will come from the build server.
Grunt task:
grunt.registerTask('setversion', function() {
// Something to go here to update the version number.
});
package.json:
{
"name": "scoreboard",
"version": "0.2",
...
}
Anyone got any idea's?

You could use something like:
grunt.registerTask('setversion', function(arg1) {
console.log("Attempting to update version to "+arg1);
var parsedJson= grunt.file.readJSON("package.json");//read in the current
parsedJson["version"] = arg1; //set the top level version field to arg1
grunt.file.write("package.json", JSON.stringify(parsedJson, null, 2));
});
add in some error checking etc.. make sure package.json is writable and execute with grunt setversion:newVersion e.g.: grunt setversion:0.3

Thanks for the answer, but it turned out to be a lot more straightforward. I am using TeamCity, so I ran an NPM task with the following command, where %system.build.number% follows the pattern n.n.n, e.g.: 0.1.6.
--no-git-tag-version version %system.build.number%

Related

How to use the Google Closure Compiler to optimize Kotlin JS

Is it possible to use the Google Closure Compiler to minify Kotlin JS code further than what Webpack offers? If so, how can it be done?
First, you should declare an NPM dependency on the Closure Compiler (latest version):
dependencies {
implementation(devNpm("google-closure-compiler", "20210808.0.0"))
}
Then, create a task that will run after webpack minification:
tasks.create<Exec>("compileWithClosure") {
// browserProductionWebpack: production minified version by Webpack
// kotlinNodeJsSetup: needed to execute Node scripts (':' because it is on the root project)
dependsOn("browserProductionWebpack", ":kotlinNodeJsSetup")
// Get the Node installation directory
val kotlinNodeJsSetup = rootProject.tasks["kotlinNodeJsSetup"] as org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsSetupTask
workingDir = File(kotlinNodeJsSetup.destination, "bin")
// Execute the script
commandLine(
"node",
"${File(rootProject.buildDir, "js/node_modules/google-closure-compiler/cli.js")}",
"--js=${File(buildDir, "distributions/<your module name here>.js")}",
"--js_output_file=${File(buildDir, "distributions/optimized.js")}",
"-O=SIMPLE",
"--env=BROWSER",
"--warning_level=QUIET",
)
}
Full list of arguments:
-O=ADVANCED seems to break the Kotlin generated file (at least in my case with Kotlin JS 1.5.21 IR)
-O=SIMPLE removes ~600kB from the binary (2.1MB → 1.5MB, using Kotlin React, KotlinX.Serialization & Ktor)
--warning_level=QUIET because we are passing an already-minified file, console output is completely unreadable, and actually slows down compilation

R: How can I install a specific release by install_github()?

If the current version of a package, gives some errors, users may prefer to install a specific release (e.g. version 1.0.1). What kind of R code can be used to achieve that?
Take for example for the release of the latest OhdsiRTools R packages:
https://github.com/OHDSI/OhdsiRTools/tree/v1.0.1
The command something like:
install_github("OHDSI/OhdsiRTools", ref = 'v1.0.1')
The code above is not correct. It only works for branches (e.g., master or devA). But the devtools package has functions to refer to releases.
Ideally I would refer to releases by their tag (but solution with commit ID would work too).
EXTRA BONUS: What code can install "latest" release. (but consider this a bonus question. The question about is the main one)
You need to append tags for releases directly onto the name of the repository argument. So, username/repo#releasetag will work. Only use the parameter ref = "devA" when you need to refer to a specific branch of the git repository.
For your example, regarding OhdsiRTools v1.0.1, we have
we have:
devtools::install_github("OHDSI/OhdsiRTools#v1.0.1")
Edit
After toying around with devtools source, it has come to my attention that one can request the latest source with:
username/repo#*release
Hence, you could use:
devtools::install_github("OHDSI/OhdsiRTools#*release")
End Edit
Outdated, see edit
Unfortunately, to obtain the latest release tag, the work for that is a bit more complicated as it would involve parsing a response from the GitHub API. Here are some notes if you really do need the tagged version... You would have to parse JSON from:
https://api.github.com/repos/<user>/<repo>/releases/latest
using either RJSONIO, jsonlite, rjson
To extract "tag_name" from:
{
"url": "https://api.github.com/repos/OHDSI/OhdsiRTools/releases/2144150",
"assets_url": "https://api.github.com/repos/OHDSI/OhdsiRTools/releases/2144150/assets",
"upload_url": "https://uploads.github.com/repos/OHDSI/OhdsiRTools/releases/2144150/assets{?name,label}",
"html_url": "https://github.com/OHDSI/OhdsiRTools/releases/tag/v1.0.1",
"id": 2144150,
"tag_name": "v1.0.1",
"target_commitish": "master",
"name": "Minor bug fix",
"draft": false,
"author": {
"login": "schuemie",
"id": 6713328,
"avatar_url": "https://avatars.githubusercontent.com/u/6713328?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/schuemie",
"html_url": "https://github.com/schuemie",
"followers_url": "https://api.github.com/users/schuemie/followers",
"following_url": "https://api.github.com/users/schuemie/following{/other_user}",
"gists_url": "https://api.github.com/users/schuemie/gists{/gist_id}",
"starred_url": "https://api.github.com/users/schuemie/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/schuemie/subscriptions",
"organizations_url": "https://api.github.com/users/schuemie/orgs",
"repos_url": "https://api.github.com/users/schuemie/repos",
"events_url": "https://api.github.com/users/schuemie/events{/privacy}",
"received_events_url": "https://api.github.com/users/schuemie/received_events",
"type": "User",
"site_admin": false
},
"prerelease": false,
"created_at": "2015-11-18T00:55:28Z",
"published_at": "2015-11-18T06:35:57Z",
"assets": [
],
"tarball_url": "https://api.github.com/repos/OHDSI/OhdsiRTools/tarball/v1.0.1",
"zipball_url": "https://api.github.com/repos/OHDSI/OhdsiRTools/zipball/v1.0.1",
"body": "Fixed bug in `convertArgsToList ` function."
}
Above is taken from https://api.github.com/repos/OHDSI/OhdsiRTools/releases/latest
For anyone who arrives here looking for how to install from a specific commit SHA, it's simply:
remotes::install_github("username/repository#commitSHA")
Example
Look for the SHA for the commit you want to install from the 'commits' page on github:
In this case the commit SHA is: 8bc79ec6dd57f46f753cc073a3a50e0921825260, so simply:
remotes::install_github("wilkelab/ggtext#8bc79ec6dd57f46f753cc073a3a50e0921825260")

grunt build or bower adds a useless dependency for highstocks

I'm using yeoman, grunt and bower and highstock library.
When I build my app (grunt build) this generate these 3 lines
<script src="bower_components/highstock/js/highcharts.src.js"></script>
<script src="bower_components/highstock/js/highstock.src.js"></script>
<script src="bower_components/highstock/js/highcharts-more.src.js"></script>
BUT, as highstock includes high charts this line should not be there and this makes errors
<script src="bower_components/highstock/js/highcharts.src.js"></script>
the error is http://www.highcharts.com/errors/16
How to remove this line in the build ?
Thanks for your help.
I believe this is grunt-bower-install at work. gbi is a task that looks through the dependencies in your project's bower.json, and injects their appropriate references into your HTML. In order for it to work, each Bower package needs to specify a main property in its bower.json file.
So, the bower.json file for highcharts looks like this:
{
"name": "highcharts",
"version": "v3.0.10-modified",
"main": [
"js/highcharts.src.js",
"js/highcharts-more.src.js",
"js/modules/exporting.src.js"
],
"ignore": [
"errors",
"exporting-server",
"gfx",
"lib",
"samples",
"studies",
"test",
"tools",
"utils",
"ant",
"build.md",
"build.properties",
"build.xml"
]
}
Because it lists 3 different files, gbi is interpreting that as "you need these three files to make this Bower package work." I have no familiarity with highcharts, but if you know that to be false-- in other words, you only need one of those files for it to work-- then it would be helpful to you and others to send a pull request correcting their bower.json file.
As far as a fix for now, you can specify an overrides property in your project's bower.json, that lists only the file you need, such as:
{
"name": "your-project",
"dependencies": {
"highcharts": "~3.0.0"
},
"overrides": {
"highcharts": {
"main": "js/highcharts.src.js"
}
}
}
The next time you run grunt bowerInstall, it will sort itself out.
Note: Make sure you're using the latest grunt-bower-install to use the new overrides feature.

Gruntfile - Warning Task "livereload-start" not found

I am trying to run the following grunt command grunt test:e2e but this does not seem to work as I get the warning as pointed in the title. I don't want to post the entire gruntfile.js so I have supplied the gist link. I would really appreciate if someone can point me in the right direction.
Gruntfile.js
The error should be your clue here; there are no task targets named "livereload-start". If you want to point to a specific task config given this structure:
connect: {
target1: {
// opts
},
target2: {
// opts
}
}
You would run connect:target1 instead of connect-target1. If you remove livereload-start out of your task configuration (line 379), what happens?

Grunt - Command Line Arguments, not working

I am using command line options in my grunt script: http://kurst.co.uk/transfer/Gruntfile.js
However the command grunt --vers:0.0.1 always returns 'undefined' when I try to get the option:
var version = grunt.option('vers') || '';
Can you help me get this working ?
I tried different (CLI) commands:
grunt vers:asd
grunt -vers:asd
grunt vers=asd
as well as using :
grunt.option('-vers');
grunt.option('--vers');
But no luck so far. Hopefully I am missing something simple.
This is my package.js file:
{
"name": "",
"version": "0.1.0",
"description": "Kurst EventDispatcher / Docs Demo ",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-yuidoc": "*",
"grunt-typescript": "~0.1.3",
"uglify-js": "~2.3.5",
"grunt-lib-contrib": "~0.6.0",
"grunt-contrib-uglify":"*"
}
}
The proper syntax for specifying a command line argument in Grunt is:
grunt --option1=myValue
Then, in the grunt file you can access the value and print it like this:
console.log( grunt.option( "option1" ) );
Also, another reason you are probably having issues with --vers is because its already a grunt option that returns the version:
★ grunt --vers
grunt-cli v0.1.7
grunt v0.4.1
So it would probably be a good idea to switch to a different option name.
It is worth mentioning that as the amount of command line arguments you want to use grows, you will run into collisions with some arguments that grunt uses internally.
I got around this problem with nopt-grunt
From the Plugin author:
Grunt is awesome. Grunt's support for using additional command line options is not awesome. The current documentation is misleading in that they give examples of using boolean flags and options with values, but they don't tell you that it only works that way with a single option. Try and use more than one option and things fall apart quickly.
It's definitely worth checking out

Resources