Ember-cli: Inject version into app - node.js

I like to display my project version (from package.json), the git commit hash and working-copy status in the footer of my Ember app built using ember-cli and broccoli.
I can grab the version prefix easily enough by adding to my config/environment.js:
ENV.APP.version = require('../package.json').version
I was using grunt-git-describe previously to grab the revision hash and dirty/clean status. How can I do something similar in ember-cli?
Simply shelling out to git describe is problematic since child_process does not have a synchronous method of executing a shell command.
It there a way I can return a promise from somewhere and prevent config/environment.js from resolving until my async git describe completes?
The npm packages exec-sync and execSync don't seem to work well for me on Windows.

ember-git-version is an ember-cli addon that provides the current revision hash.
After installing the node package, the config/environment hash will have a property currentRevision. See initializers/print-git-info.js for how to access it from your app.

You can use the exec-sync package and then add something like this to Brocfile.js:
var execSync = require('exec-sync'),
gitVersion = execSync('git describe');
fs.writeFileSync('app/version.js', 'App.version = "' + gitVersion + '";';
You'll then need to import that somewhere.

Related

Problem when using process.cwd() in published npm package

I'm dipping my toes into cli tooling by building a simple program for automating Gerrit commits. Everything works locally, but after publishing the package to npm and installing it globally it looks like process.cwd() behaves differently. The program exits, but no console.log(). Even a simple console.log(process.cwd()) is ignored (again works locally). Is it possible to use process.cwd() when running a globally installed npm package?
console.log(process.cwd());
const getCurrentBranchName = (p = process.cwd()) => {
const gitHeadPath = `${p}/.git/HEAD`;
return fs.existsSync(gitHeadPath)
? fs.readFileSync(gitHeadPath, "utf-8").trim().split("/").pop()
: (console.log("not a git repo"), process.exit(0));
}
const currentBranch = getCurrentBranchName();
When ran locally (with node index):
$ /Users/jpap/.npm-packages/lib/node_modules/gittest
$ not a git repo
You haven't proved the problem is with process.cwd()
The code in your question and the results your describe only indicate that the console.log() calls aren't executing.
You could easily test this by adding the following to the top of your code:
console.log('My name is Inigo Montoya. You killed my father. Prepare to die!')
What you are publishing is likely not the same as what you are running locally
For example, you could be using a bundler (e.g. Rollup) configured to strip console.log calls.
This is easy to confirm. Simple look at the code of the npm installed version:
Use npm root -g to find out where your global packages are installed. For non-global packages, look in node_modules.
Find your package's subdir.
Look at the code, or diff it with the source code.
I suspect you will see all console.log statements removed.

Node.js qrcode library issue

I have reinstalled my web app in a new server without changing anything and now I receive this error:
TypeError: QRCode.drawBitArray is not a function
why? i haven't change my code....
this is the library : https://github.com/simwood/node-qrcode
and yess, i have install all the dependencies.
alternatively, do I recommend libraries that return an array with the length of the line?
sorry for bad english(:
drawBitArray has been removed in the last version of qrcode.
The official repo seems to be here and the last version with the function is 0.7.1.
In the last version (at this time 1.4.4) you can obtain the array of bits and the size returned by drawBitArray this way:
const myQrCode = qrcode.create("myContent")
const bits = new Uint8Array(myQrCode.modules.data)
const QrLineSize = myQrCode.modules.size
run this npm install --save node-qrcode
if that doesnt work try deleting node_modules folder and running that command again.
also check the version of node-qrcode that are installed in your package.json file. make sure theyre consistant with eachother

Publish different builds of a npm package to target different versions of node

I'm have a small npm package which I'm writing in node 9 and using all the latest and greatest features like async/await. I'm also using babel which allows me to use ES6 module imports and exports
Babel also allows me to transpile the package to a target node version. I'm using the node release schedule to define which version of node the package will support and with the the target for the babel compilation. Currently node 4.x is still in maintenance lts stage, so I'm targeting it. Unfortunately this means almost every new feature in JavaScript I'm using gets transpiled.
What I'd like to do is to transpile the package to different targets (4.x, 6.x, 7.x, 8.x, and 9.x currently apply) and have npm choose the appropriate build of the package at install time based on the user's node version. If I'm not mistaken, I've seen apt-get do this with different versions of Ubuntu.
Is this possible with npm?
I'll bump an old thread here. A good option is to use two separate directories and choose the transpiled version depending on process.version.
I'll be trying to do this in scramjet and will be updating the thread along the way. My plan is to use an index.js file like this:
const ver = process.version.slice(1).split('.');
module.exports = require(+ver[0] > 8 && +ver[1] > 3 ? 'lib/index' : 'lib-6/index')
This should do for now, but since I also have some v10+ targeted code (especially DataStream.from(async function*() { ... }) which has some ifs in it I may want to extend it later and use a table.
What's important here is that the code above runs once (unless you clear require cache) and mostly in compile time so it has minimal influence over your module.

How to make a self-updating node package?

We have a node_module bookiza which is essentially a command line tool that people install with --global flag and then use it to create and publish books online. We are following semver for it (mostly) but I envisage it will be done so strictly in the future.
Is there a way to make this module a self-updating one? So that all our clients (writers) are on the same version of bookiza at all the times (unless they disable autoupdate). Essentially, run npm update [-g] bookiza every 15 days or when a new release is outed.
How should I go about implementing this?
For those who are looking for the same thing, I have created a module for automatically updating node.js applications from git repositories. It compares the local package.json with the one from your repo, then automatically clones the repo and install dependencies.
Auto Git Update - https://github.com/chegele/AutoGitUpdate
import AutoGitUpdate from 'auto-git-update';
const config = {
repository: 'https://github.com/chegele/BackupPurger'
tempLocation: 'C:/Users/scheg/Desktop/tmp/',
ignoreFiles: ['util/config.js'],
executeOnComplete: 'C:\\Users\\scheg\\Desktop\\worksapce\\AutoGitUpdate\\startTest.bat',
exitOnComplete: true
}
const updater = new AutoGitUpdate(config);
updater.autoUpdate();
Your users could install the command line tool a number of ways.. So it's not guaranteed that npm install -g will work, particularly in their environment.
I think the best thing to do here is make some sort of "latest version" check at start. If there's a new version, warn the user, and offer to automatically run the update command with a [Y/n].
This is what bower and a few other packages do.

gulp automatic version for package.json

I would like to know if its possible to get the git version for my package.json
What i want to achieve is that i don't need to bump the version manually.
There is something like that in Java with gradle f.e.
https://github.com/palantir/gradle-git-version
It is using git describe to get the latest tag of the git repo and if it contains new commits/changes the repo is marked as dirty. On the dirty check it automatically returns the next development version for gradle.
I didn't find anything like that for gulp
Yes.
Check this npm module https://www.npmjs.com/package/git-version-json
You can use gulp-replace and git-version-json
use git-version-json to generate the json variable.
use gulp-replace to replace the version
As below:
var gulp = require('gulp');
var replace = require('gulp-replace');
var GitVersionJson = require('git-version-json');
// automatic use git-rev in version field of package.json
gulp.task('npm-git-rev', [GitVersionJson.task], ()=>{
return gulp.src('package.json')
.pipe(replace(/(\"version\"\s*:\s*\"\d+\.\d+\.)(\d+)(\")/,
"$1" + GitVersionJson.gitVer.rev + "$3"))
.pipe(gulp.dest('.'))
});
run it with gulp npm-git-rev

Resources