This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Gruntfile.js
179 lines (153 loc) · 4.39 KB
/
Gruntfile.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const util = require("util");
const os = require("os");
const childProcess = require("child_process");
const now = new Date().toISOString();
function shallowCopy(obj) {
var result = {};
Object.keys(obj).forEach(function (key) {
result[key] = obj[key];
});
return result;
}
function getBuildVersion(version) {
var buildVersion = version !== undefined ? version : process.env["BUILD_NUMBER"];
if (process.env["BUILD_CAUSE_GHPRBCAUSE"]) {
buildVersion = "PR" + buildVersion;
}
return buildVersion;
}
module.exports = function (grunt) {
// Windows cmd does not accept paths with / and unix shell does not accept paths with \\ and we need to execute from a sub-dir.
// To circumvent the issue, hack our environment's PATH and let the OS deal with it, which in practice works
process.env.path = process.env.path + (os.platform() === "win32" ? ";" : ":") + "node_modules/.bin";
var defaultEnvironment = "sit";
grunt.initConfig({
jobName: process.env["JOB_NAME"] || defaultEnvironment,
buildNumber: process.env["BUILD_NUMBER"] || "non-ci",
dateString: now.substr(0, now.indexOf("T")),
pkg: grunt.file.readJSON("package.json"),
ts: {
options: grunt.file.readJSON("tsconfig.json").compilerOptions,
devlib: {
src: ["**/*.ts", "!node_modules/**/*.ts"],
reference: ".d.ts"
},
devall: {
src: ["**/*.ts", "!node_modules/**/*.ts", "test/**/*.ts"],
reference: ".d.ts"
},
release_build: {
src: ["**/*.ts", "!node_modules/**/*.ts", "test/**/*.ts"],
reference: ".d.ts",
options: {
sourceMap: false,
removeComments: true
}
}
},
watch: {
devall: {
files: ["**/*.ts", "!node_modules/**/*.ts", 'test/**/*.ts'],
tasks: ['ts:devall'],
options: {
atBegin: true,
interrupt: true
}
}
},
shell: {
options: {
stdout: true,
stderr: true
},
ci_unit_tests: {
command: "npm test",
options: {
execOptions: {
env: (function () {
var env = shallowCopy(process.env);
env["XUNIT_FILE"] = "test-reports.xml";
env["LOG_XUNIT"] = "true";
return env;
})()
}
}
},
build_package: {
command: "npm pack",
options: {
execOptions: {
env: (function () {
var env = shallowCopy(process.env);
env["COMMON_LIB_SKIP_POSTINSTALL_TASKS"] = "1";
return env;
})()
}
}
}
},
clean: {
src: ["test/**/*.js*",
"**/*.js*",
"!**/*.json",
"!Gruntfile.js",
"!node_modules/**/*",
"!bin/common-lib.js",
"!vendor/*.js",
"*.tgz",
"!test-scripts/**/*",
"!scripts/**/*"]
}
});
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-shell");
grunt.loadNpmTasks("grunt-ts");
grunt.registerTask("set_package_version", function (version) {
var buildVersion = getBuildVersion(version);
var packageJson = grunt.file.readJSON("package.json");
packageJson.buildVersion = buildVersion;
grunt.file.write("package.json", JSON.stringify(packageJson, null, " "));
});
grunt.registerTask("tslint:build", function (version) {
childProcess.execSync("npm run tslint", { stdio: "inherit" });
});
grunt.registerTask("setPackageName", function (version) {
var fs = require("fs");
var fileExtension = ".tgz";
var buildVersion = getBuildVersion(version);
var packageJson = grunt.file.readJSON("package.json");
var oldFileName = packageJson.name + "-" + packageJson.version + fileExtension;
if (buildVersion) {
var newFileName = oldFileName + "-" + buildVersion + fileExtension;
fs.renameSync(oldFileName, newFileName);
console.log("Renamed " + oldFileName + " to " + newFileName);
} else {
console.log("Packed file is: " + oldFileName);
}
});
grunt.registerTask("delete_coverage_dir", function () {
var done = this.async();
var rimraf = require("rimraf");
rimraf("coverage", function (err) {
if (err) {
console.log("Error while deleting coverage directory from the package.");
done(false);
}
done();
});
});
grunt.registerTask("test", ["ts:devall", "shell:ci_unit_tests"]);
grunt.registerTask("pack", [
"tslint:build",
"ts:release_build",
"shell:ci_unit_tests",
"set_package_version",
"delete_coverage_dir",
"shell:build_package",
"setPackageName"
]);
grunt.registerTask("lint", ["tslint:build"]);
grunt.registerTask("default", "ts:devlib");
grunt.registerTask("all", ["clean", "test", "lint"]);
};