diff --git a/package.json b/package.json index d4a9658b..fbfa2c6d 100644 --- a/package.json +++ b/package.json @@ -118,6 +118,7 @@ "pre-commit": "^1.2.2", "prettier": "^1.19.1", "pump": "^3.0.0", + "semver": "^7.3.4", "style-loader": "^0.23.1", "terser-webpack-plugin": "^2.3.1", "ts-import-plugin": "^1.5.5", diff --git a/webpack/getVersion.js b/webpack/getVersion.js new file mode 100644 index 00000000..d6899d20 --- /dev/null +++ b/webpack/getVersion.js @@ -0,0 +1,17 @@ +const semver = require('semver'); +const { execSync } = require('child_process'); + +const branch = process.env.GITHUB_BRANCH || 'refs/heads/master'; +const masterCommitsCount = execSync(`git rev-list --count ${branch}`) + .toString() + .trim(); + +function getVersion(version) { + const currentVersion = semver.parse(version); + if (Array.isArray(currentVersion.prerelease) && currentVersion.prerelease.length > 0) { + return semver.coerce(version).version.replace(/.[0-9]$/, `.${masterCommitsCount}`); + } + return version; +} + +module.exports = getVersion; diff --git a/webpack/webpack.common.js b/webpack/webpack.common.js index 9f759cd1..0b3cd7ca 100644 --- a/webpack/webpack.common.js +++ b/webpack/webpack.common.js @@ -7,6 +7,7 @@ const ExtensionReloader = require('webpack-extension-reloader'); const tsImportPluginFactory = require('ts-import-plugin'); const WebpackCreateExtensionManifestPlugin = require('webpack-create-extension-manifest-plugin'); const fs = require('fs'); +const getVersion = require('./getVersion'); const distFiles = fs.readdirSync(resolve('dist')).filter(o => o !== '.gitkeep'); @@ -14,8 +15,11 @@ function resolve(dir) { return path.join(__dirname, '..', dir); } +const packageJson = JSON.parse(fs.readFileSync(resolve('package.json'), 'utf8')); + let manifestExtra = { name: 'Web Clipper', + version: getVersion(packageJson.version), permissions: [ 'activeTab', 'storage', @@ -40,6 +44,7 @@ let tool = resolve('src/main/tool.main.chrome.ts'); if (process.env.TARGET_BROWSER === 'Firefox') { manifestExtra = { name: 'Web Clipper', + version: getVersion(packageJson.version), commands: { 'toggle-feature-foo': { suggested_key: { diff --git a/yarn.lock b/yarn.lock index 644d000f..fe16f2f4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8510,6 +8510,13 @@ semver@^7.3.2: dependencies: lru-cache "^6.0.0" +semver@^7.3.4: + version "7.3.4" + resolved "https://registry.npm.taobao.org/semver/download/semver-7.3.4.tgz#27aaa7d2e4ca76452f98d3add093a72c943edc97" + integrity sha1-J6qn0uTKdkUvmNOt0JOnLJQ+3Jc= + dependencies: + lru-cache "^6.0.0" + serialize-javascript@^4.0.0: version "4.0.0" resolved "https://registry.npm.taobao.org/serialize-javascript/download/serialize-javascript-4.0.0.tgz#b525e1238489a5ecfc42afacc3fe99e666f4b1aa"