-
Notifications
You must be signed in to change notification settings - Fork 9
/
Gruntfile.js
213 lines (180 loc) · 6.26 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
var os = require("os");
var 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 isPackageJsonModified = false;
var defaultEnvironment = "sit";
// When there are node_modules inside lib\common directory, CLI behaves incorrectly, so delete this dir.
grunt.initConfig({
deploymentEnvironment: process.env["DeploymentEnvironment"] || defaultEnvironment,
resourceDownloadEnvironment: process.env["ResourceDownloadEnvironment"] || defaultEnvironment,
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: ["lib/**/*.ts", "!lib/common/node_modules/**/*.ts", "!lib/common/messages/**/*.ts"],
reference: "lib/.d.ts"
},
devall: {
src: ["lib/**/*.ts", "test/**/*.ts", "!lib/common/node_modules/**/*.ts", "lib/common/test/unit-tests/**/*.ts", "definitions/**/*.ts", "!lib/common/test/.d.ts", "!lib/common/messages/**/*.ts"],
reference: "lib/.d.ts"
},
release_build: {
src: ["lib/**/*.ts", "test/**/*.ts", "!lib/common/node_modules/**/*.ts", "!lib/common/messages/**/*.ts"],
reference: "lib/.d.ts",
options: {
sourceMap: false,
removeComments: true
}
}
},
tslint: {
build: {
files: {
src: ["lib/**/*.ts", "test/**/*.ts", "!lib/common/node_modules/**/*.ts", "!lib/common/messages/**/*.ts", "lib/common/test/unit-tests/**/*.ts", "definitions/**/*.ts", "!**/*.d.ts"]
},
options: {
configuration: grunt.file.readJSON("./tslint.json")
}
}
},
watch: {
devall: {
files: ["lib/**/*.ts", 'test/**/*.ts', "!lib/common/node_modules/**/*.ts", "!lib/common/messages/**/*.ts"],
tasks: ['ts:devall'],
options: {
atBegin: true,
interrupt: true
}
}
},
shell: {
options: {
stdout: true,
stderr: true
},
apply_resources_environment: {
command: "node bin/appbuilder dev-config-apply <%= resourceDownloadEnvironment %>"
},
prepare_resources: {
command: "node bin/appbuilder dev-prepackage"
},
apply_deployment_environment: {
command: "node bin/appbuilder dev-config-apply <%= deploymentEnvironment %>"
},
build_package: {
command: "npm pack",
options: {
execOptions: {
env: (function () {
var env = shallowCopy(process.env);
env["APPBUILDER_SKIP_POSTINSTALL_TASKS"] = "1";
return env;
})()
}
}
}
},
clean: {
src: ["test/**/*.js*",
"lib/**/*.js*",
"!test-scripts/**/*",
"!lib/common/vendor/*.js",
"!lib/hooks/**/*.js",
"!lib/common/**/*.json",
"!lib/common/Gruntfile.js",
"!lib/common/node_modules/**/*",
"!lib/common/hooks/**/*.js",
"!lib/common/bin/*.js",
"!lib/common/test-scripts/**/*",
"!lib/common/scripts/**/*",
"*.tgz"]
}
});
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-shell");
grunt.loadNpmTasks("grunt-ts");
grunt.loadNpmTasks("grunt-tslint");
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("setBundleDependencies", function (version) {
var buildVersion = getBuildVersion(version);
var packageJson = grunt.file.readJSON("package.json");
packageJson.bundleDependencies = Object.keys(packageJson.dependencies);
grunt.file.write("package.json", JSON.stringify(packageJson, null, " "));
});
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;
var newFileName = oldFileName + "-" + buildVersion;
fs.renameSync(oldFileName + fileExtension, newFileName + fileExtension);
});
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"]);
grunt.registerTask("remove_prepublish_script", function () {
var packageJson = grunt.file.readJSON("package.json");
if (packageJson && packageJson.scripts && packageJson.scripts.prepublish) {
delete packageJson.scripts.prepublish;
grunt.file.write("package.json", JSON.stringify(packageJson, null, " "));
isPackageJsonModified = true;
}
});
grunt.registerTask("printPackageJsonWarning", function () {
if (isPackageJsonModified) {
require("colors");
console.log("NOTE: `grunt pack` command modified package.json. DO NOT COMMIT these changes, they are required only for the produced .tgz.".red.bold);
}
});
grunt.registerTask("pack", [
"clean",
"ts:release_build",
"remove_prepublish_script",
"set_package_version",
"delete_coverage_dir",
"shell:build_package",
"setPackageName",
"printPackageJsonWarning"
]);
grunt.registerTask("bundle-pack", ["setBundleDependencies", "pack"]);
grunt.registerTask("lint", ["tslint:build"]);
grunt.registerTask("all", ["clean", "test", "lint"]);
grunt.registerTask("rebuild", ["clean", "ts:devlib"]);
grunt.registerTask("default", "ts:devlib");
};