forked from tylerbutler/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_version.js
60 lines (49 loc) · 1.82 KB
/
gen_version.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
/**
* Generate packageVersion.ts files from the name and version info from package.json
* Assume to be invoked in the package root (where package.json is located), and
* The output is in ./src/packageVersion.ts.
*/
const fs = require("fs");
const path = require("path");
const packageFile = path.resolve("./package.json");
const pkg = require(packageFile);
const outDir = "./src";
const outFile = `${outDir}/packageVersion.ts`;
const license = pkg.license === "MIT" ? " Licensed under the MIT License.\n *" : "";
const pkgName = pkg.name;
// For test builds, the package version starts with 0.0.0. Code need to know the original version.
// CI build create one with original version prefix is emitted into the environment for code logic to be used here.
const pkgVersion = process.env.SETVERSION_CODEVERSION
? process.env.SETVERSION_CODEVERSION
: pkg.version;
// Make sure the output directory exists
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir);
}
// The code to write to packageVersion.ts
const output = `/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
*${license}
* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY
*/
export const pkgName = "${pkgName}";
export const pkgVersion = "${pkgVersion}";
`;
// Only update the file if it has changed.
if (fs.existsSync(outFile)) {
const orig = fs.readFileSync(outFile, { encoding: "utf-8" });
const orig_nocrlf = orig.replace(/(\r\n|\n|\r)/gm, "");
const output_nocrlf = output.replace(/(\r\n|\n|\r)/gm, "");
if (orig_nocrlf === output_nocrlf) {
console.log(`${outFile} unchanged.`);
// Done! Exit!
process.exit();
}
}
// Write out the file.
fs.writeFileSync(outFile, output);
console.log(`${outFile} written.`);