From 76a6cbcd84112c80d04925668ef67ce3b8a1f353 Mon Sep 17 00:00:00 2001 From: Andrew Stanton-Nurse Date: Fri, 11 May 2018 12:54:30 -0700 Subject: [PATCH] Embed version in the JavaScript client (#2229) --- clients/ts/build/embed-version.js | 44 +++++++++++++++++++ clients/ts/rollup-base.js | 5 ++- .../ts/signalr-protocol-msgpack/package.json | 1 + .../ts/signalr-protocol-msgpack/src/index.ts | 4 ++ clients/ts/signalr/package.json | 1 + clients/ts/signalr/src/index.ts | 4 ++ 6 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 clients/ts/build/embed-version.js diff --git a/clients/ts/build/embed-version.js b/clients/ts/build/embed-version.js new file mode 100644 index 0000000000..3eaa7b36f2 --- /dev/null +++ b/clients/ts/build/embed-version.js @@ -0,0 +1,44 @@ +const path = require("path"); +const fs = require("fs"); +const pkg = require(path.resolve(process.cwd(), "package.json")); + +const args = process.argv.slice(2); +let verbose = false; +for (let i = 0; i < args.length; i++) { + switch (args[i]) { + case "-v": + verbose = true; + break; + } +} + +function processDirectory(dir) { + if (verbose) { + console.log(`processing ${dir}`); + } + for (const item of fs.readdirSync(dir)) { + const fullPath = path.resolve(dir, item); + const stats = fs.statSync(fullPath); + if (stats.isDirectory()) { + processDirectory(fullPath); + } else if (stats.isFile()) { + processFile(fullPath); + } + } +} + +const SEARCH_STRING = "0.0.0-DEV_BUILD"; +function processFile(file) { + if (file.endsWith(".js") || file.endsWith(".ts")) { + if (verbose) { + console.log(`processing ${file}`); + } + let content = fs.readFileSync(file); + content = content.toString().replace(SEARCH_STRING, pkg.version); + fs.writeFileSync(file, content); + } else if (verbose) { + console.log(`skipping ${file}`); + } +} + +processDirectory(path.resolve(process.cwd(), "dist")); \ No newline at end of file diff --git a/clients/ts/rollup-base.js b/clients/ts/rollup-base.js index eea43035bf..46cabee09b 100644 --- a/clients/ts/rollup-base.js +++ b/clients/ts/rollup-base.js @@ -19,7 +19,10 @@ export default function(rootDir, moduleGlobals) { format: "umd", name: pkg.umd_name, sourcemap: true, - banner: "/* @license\r\n" + + banner: "/** \r\n" + + " * @overview ASP.NET Core SignalR JavaScript Client.\r\n" + + " * @version 0.0.0-DEV_BUILD.\r\n" + + " * @license\r\n" + " * Copyright (c) .NET Foundation. All rights reserved.\r\n" + " * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n" + " */", diff --git a/clients/ts/signalr-protocol-msgpack/package.json b/clients/ts/signalr-protocol-msgpack/package.json index 0c0c2a232a..d6e6f673b2 100644 --- a/clients/ts/signalr-protocol-msgpack/package.json +++ b/clients/ts/signalr-protocol-msgpack/package.json @@ -18,6 +18,7 @@ "build:cjs": "node ../node_modules/typescript/bin/tsc --project ./tsconfig.json --module commonjs --outDir ./dist/cjs", "build:browser": "node ../node_modules/rollup/bin/rollup -c", "build:uglify": "node ../node_modules/uglify-js/bin/uglifyjs --source-map \"url='signalr-protocol-msgpack.min.js.map',content='./dist/browser/signalr-protocol-msgpack.js.map'\" --comments -o ./dist/browser/signalr-protocol-msgpack.min.js ./dist/browser/signalr-protocol-msgpack.js", + "prepack": "node ../build/embed-version.js", "test": "echo \"Run 'npm test' in the 'clients\\ts' folder to test this package\" && exit 1" }, "keywords": [ diff --git a/clients/ts/signalr-protocol-msgpack/src/index.ts b/clients/ts/signalr-protocol-msgpack/src/index.ts index eaf3037c3d..55745ddf7c 100644 --- a/clients/ts/signalr-protocol-msgpack/src/index.ts +++ b/clients/ts/signalr-protocol-msgpack/src/index.ts @@ -1,4 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Version token that will be replaced by the prepack command +/** The version of the SignalR Message Pack protocol library. */ +export const VERSION = "0.0.0-DEV_BUILD"; + export { MessagePackHubProtocol } from "./MessagePackHubProtocol"; diff --git a/clients/ts/signalr/package.json b/clients/ts/signalr/package.json index 13b6a525d5..4500174b2a 100644 --- a/clients/ts/signalr/package.json +++ b/clients/ts/signalr/package.json @@ -18,6 +18,7 @@ "build:cjs": "node ../node_modules/typescript/bin/tsc --project ./tsconfig.json --module commonjs --outDir ./dist/cjs", "build:browser": "node ../node_modules/rollup/bin/rollup -c", "build:uglify": "node ../node_modules/uglify-js/bin/uglifyjs --source-map \"url='signalr.min.js.map',content='./dist/browser/signalr.js.map'\" --comments -o ./dist/browser/signalr.min.js ./dist/browser/signalr.js", + "prepack": "node ../build/embed-version.js", "test": "echo \"Run 'npm test' in the 'clients\\ts' folder to test this package\" && exit 1" }, "repository": { diff --git a/clients/ts/signalr/src/index.ts b/clients/ts/signalr/src/index.ts index 9489f5d829..548a0a5929 100644 --- a/clients/ts/signalr/src/index.ts +++ b/clients/ts/signalr/src/index.ts @@ -1,6 +1,10 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Version token that will be replaced by the prepack command +/** The version of the SignalR client. */ +export const VERSION: string = "0.0.0-DEV_BUILD"; + // Everything that users need to access must be exported here. Including interfaces. export { AbortSignal } from "./AbortController"; export { HttpError, TimeoutError } from "./Errors";