-
Notifications
You must be signed in to change notification settings - Fork 39
/
generate_version_file.js
72 lines (65 loc) · 2.38 KB
/
generate_version_file.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
61
62
63
64
65
66
67
68
69
70
71
72
const compile = require('es6-template-strings/compile');
const fs = require('fs');
const resolveToString = require('es6-template-strings/resolve-to-string');
const versionFilePath = 'version.txt';
const generateFilesSpec = [
{
templatePath: 'templates/version-js.template',
outputPath: 'src/common/version.js',
},
{
templatePath: 'templates/umd-bootstrapper-js.template',
outputPath: 'umd-bootstrapper.js',
},
{
templatePath: 'templates/umd-bootstrapper-js.template',
outputPath: 'umd-bootstrapper-with-default.js',
additionalVersionString: 'default',
},
];
buildFileOutput = (semVersionString, additionalVersionString, buildVersion, template) => {
const compiled = compile(template);
return resolveToString(compiled, {
semVersionString,
additionalVersionString,
buildVersion,
});
};
generateFile = (semVersionString, additionalVersionString, templateFile, outputFile) => {
const buildVersion = process.env['BUILD_VERSION'] || 'dev';
fs.exists(outputFile, (exists) => {
if (exists) return console.log(`${outputFile} exists. Exiting...`);
fs.readFile(templateFile, 'utf8', (error, contents) => {
if (error) return console.log(error);
const outputFileContents = buildFileOutput(
semVersionString, additionalVersionString, buildVersion, contents);
fs.writeFile(outputFile, outputFileContents, (error) => {
if (error) return console.log(error);
console.log(`Wrote ${outputFile} file!`);
});
});
});
};
generateFiles = (filesSpec, semVersionString) => {
filesSpec.forEach((fileSpec) => {
const templateFile = fileSpec.templatePath;
const outputFile = fileSpec.outputPath;
const additionalVersionString = fileSpec.additionalVersionString;
generateFile(semVersionString, additionalVersionString, templateFile, outputFile);
});
};
fs.exists(versionFilePath, (exists) => {
if (!exists) {
return console.log(`${versionFilePath} file not found. Exiting...`);
}
// Environment variable VERSION_TXT_OVERRIDE can be used to override the
// contents of version.txt for testing
if (process.env['VERSION_TXT_OVERRIDE']) {
generateFiles(generateFilesSpec, process.env['VERSION_TXT_OVERRIDE']);
} else {
fs.readFile(versionFilePath, 'utf8', (error, contents) => {
if (error) return console.log(error);
generateFiles(generateFilesSpec, contents.trim());
});
}
});