Skip to content

Commit 1fcb2e2

Browse files
saintsebastiankumar303
authored andcommitted
feat: web-ext --version now shows the commit hash it was built from, unless it's a production release (#565)
1 parent f09d0e3 commit 1fcb2e2

File tree

6 files changed

+49
-11
lines changed

6 files changed

+49
-11
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ before_script:
1414
- npm run nsp-check
1515

1616
script:
17-
- COVERAGE=y npm test
17+
- COVERAGE=y NODE_ENV=production npm test
1818
- npm run changelog-lint
1919

2020
after_script: npm run publish-coverage

CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ breakpoint.
5959
Type `npm run build` to build a new version of the libraries used by the
6060
`./bin/web-ext` command. When successful, you will see newly built files in
6161
the `./dist/` directory. This is done automatically by `npm start`.
62+
By default, npm run build creates a development build of web-ext. To create a
63+
production build, use the NODE_ENV variable like this:
64+
65+
NODE_ENV=production npm run build
6266

6367
## Check for lint
6468

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"es6-promisify": "5.0.0",
5353
"firefox-profile": "0.4.2",
5454
"fx-runner": "1.0.5",
55+
"git-rev-sync": "1.6.0",
5556
"minimatch": "3.0.3",
5657
"mz": "2.4.0",
5758
"node-firefox-connect": "1.2.0",

src/program.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {createLogger, consoleStream as defaultLogStream} from './util/logger';
1010
const log = createLogger(__filename);
1111
const envPrefix = 'WEB_EXT';
1212

13+
import git from 'git-rev-sync';
14+
1315

1416
/*
1517
* The command line program.
@@ -90,8 +92,8 @@ export class Program {
9092
let runCommand = this.commands[cmd];
9193

9294
if (argv.verbose) {
93-
log.info('Version:', getVersion(absolutePackageDir));
9495
logStream.makeVerbose();
96+
log.info('Version:', getVersion(absolutePackageDir));
9597
}
9698

9799
try {
@@ -123,11 +125,27 @@ export class Program {
123125
}
124126
}
125127

128+
// A global variable generated by DefinePlugin, generated in webpack.config.js
129+
declare var WEBEXT_BUILD_ENV: string;
126130

127-
export function defaultVersionGetter(absolutePackageDir: string): string {
128-
let packageData: any = readFileSync(
129-
path.join(absolutePackageDir, 'package.json'));
130-
return JSON.parse(packageData).version;
131+
//A defintion of type of argument for defaultVersionGetter
132+
type versionGetterOptions = {
133+
localEnv?: string,
134+
};
135+
136+
export function defaultVersionGetter(
137+
absolutePackageDir: string,
138+
{localEnv = WEBEXT_BUILD_ENV}: versionGetterOptions = {}
139+
): string {
140+
if (localEnv === 'production') {
141+
log.debug('Getting the version from package.json');
142+
let packageData: any = readFileSync(
143+
path.join(absolutePackageDir, 'package.json'));
144+
return JSON.parse(packageData).version;
145+
} else {
146+
log.debug('Getting version from the git revision');
147+
return `${git.branch()}-${git.long()}`;
148+
}
131149
}
132150

133151

tests/unit/test.program.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {onlyInstancesOf, UsageError} from '../../src/errors';
1212
import {fake, makeSureItFails} from './helpers';
1313
import {ConsoleStream} from '../../src/util/logger';
1414

15+
import git from 'git-rev-sync';
16+
1517

1618
describe('program.Program', () => {
1719

@@ -175,7 +177,7 @@ describe('program.Program', () => {
175177
});
176178
program.command('thing', 'does a thing', () => {});
177179

178-
return execProgram(program, {logStream})
180+
return execProgram(program, {getVersion: spy(), logStream})
179181
.then(() => {
180182
assert.equal(logStream.makeVerbose.called, true);
181183
});
@@ -329,15 +331,23 @@ describe('program.main', () => {
329331

330332

331333
describe('program.defaultVersionGetter', () => {
334+
let root = path.join(__dirname, '..', '..');
332335

333-
it('returns the package version', () => {
334-
let root = path.join(__dirname, '..', '..');
336+
it('returns the package version in production', () => {
335337
let pkgFile = path.join(root, 'package.json');
336338
return fs.readFile(pkgFile)
337339
.then((pkgData) => {
338-
assert.equal(defaultVersionGetter(root),
339-
JSON.parse(pkgData).version);
340+
const testBuildEnv = {localEnv: 'production'};
341+
assert.equal(defaultVersionGetter(root, testBuildEnv),
342+
JSON.parse(pkgData).version);
340343
});
341344
});
342345

346+
it('returns git commit information in development', () => {
347+
const commit = `${git.branch()}-${git.long()}`;
348+
const testBuildEnv = {localEnv: 'development'};
349+
assert.equal(defaultVersionGetter(root, testBuildEnv),
350+
commit);
351+
});
352+
343353
});

webpack.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ module.exports = {
5757
// This seems necessary to work with the 'when' module, which is
5858
// required by some things such as fx-runner.
5959
new webpack.IgnorePlugin(/vertx/),
60+
// Global variables are necessary to print either verson number or
61+
// git commit information for custom builds
62+
new webpack.DefinePlugin({
63+
WEBEXT_BUILD_ENV: JSON.stringify(process.env.NODE_ENV || 'development'),
64+
}),
6065
],
6166
resolve: {
6267
extensions: ['', '.js', '.json'],

0 commit comments

Comments
 (0)