This repository was archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGruntfile.js
249 lines (209 loc) · 7.79 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"use strict";
const path = require("path");
const fs = require("fs");
const childProcess = require("child_process");
const _ = require("lodash");
const os = require("os");
const nodeArgs = [];
const getBuildVersion = (version) => {
let 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";
const defaultEnvironment = "sit";
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",
pkg: grunt.file.readJSON("package.json"),
ts: {
options: grunt.file.readJSON("tsconfig.json").compilerOptions,
devlib: {
src: ["lib/**/*.ts", "test/**/*.ts", "references.d.ts", "!node_modules/**/*"]
},
release_build: {
src: ["lib/**/*.ts", "test/**/*.ts", "references.d.ts", "!node_modules/**/*"],
options: {
sourceMap: false,
removeComments: true
}
}
},
watch: {
devall: {
files: ["lib/**/*.ts", "test/**/*.ts"],
tasks: ['ts:devlib'],
options: {
atBegin: true,
interrupt: true
}
}
},
shell: {
options: {
stdout: true,
stderr: true
},
ci_unit_tests: {
command: "npm test",
options: {
execOptions: {
env: (function () {
var env = _.cloneDeep(process.env);
env["XUNIT_FILE"] = "test-reports.xml";
env["LOG_XUNIT"] = "true";
return env;
})()
}
}
},
apply_deployment_environment: {
command: "node " + nodeArgs.join(" ") + " bin/appbuilder dev-config-apply <%= deploymentEnvironment %>"
},
build_package: {
command: "npm pack",
options: {
execOptions: {
env: (function () {
var env = _.cloneDeep(process.env);
env["APPBUILDER_SKIP_POSTINSTALL_TASKS"] = "1";
return env;
})()
}
}
}
},
clean: {
src: ["test/**/*.js*",
"lib/**/*.js*",
"*.tgz"]
}
});
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) {
const buildVersion = getBuildVersion(version);
const packageJson = grunt.file.readJSON("package.json");
packageJson.buildVersion = buildVersion;
grunt.file.write("package.json", JSON.stringify(packageJson, null, " "));
});
const transpileProject = function (dirname) {
const pathToModule = path.join(__dirname, "node_modules", dirname);
const packageJsonContent = grunt.file.readJSON(path.join(pathToModule, "package.json"));
try {
// Keep the --production flag - in case we skip it, we'll instal istanbul, chai, etc. and their .d.ts will conflict with ours.
childProcess.execSync("npm i --ignore-scripts --production", { cwd: pathToModule, stdio: "ignore" });
} catch (err) {
}
try {
// grunt deps must be installed locally in the project where grunt will be called.
// also for transpilation we need typescript locally.
const searchedNames = ["grunt", "typescript"];
const dependenciesToInstall = _.map(packageJsonContent.devDependencies, (version, name) => {
for (let searchedName of searchedNames) {
if (name.indexOf(searchedName) !== -1 && !fs.existsSync(path.join(pathToModule, "node_modules", name))) {
return `${name}@${version}`
}
}
}).filter(a => !!a);
_.each(dependenciesToInstall, name => {
try {
childProcess.execSync(`npm i --ignore-scripts ${name}`, { cwd: pathToModule, stdio: "ignore" });
} catch (err) {
}
})
} catch (err) { }
try {
// we need the .js file in the tests, so we can require them, for example in order to create a new instance of injector.
// if the main file is .js and it exists, no need to transpile it again
const pathToMain = path.join(pathToModule, packageJsonContent.main);
if (!fs.existsSync(pathToMain)) {
childProcess.execSync("grunt", { cwd: pathToModule, stdio: "ignore" });
}
} catch (err) { }
};
grunt.registerTask("transpile_additional_project", function () {
transpileProject("nativescript");
});
grunt.registerTask("setPackageName", function (version) {
const fs = require("fs");
const fileExtension = ".tgz";
const buildVersion = getBuildVersion(version);
const packageJson = grunt.file.readJSON("package.json");
const oldFileName = packageJson.name + "-" + packageJson.version;
const newFileName = oldFileName + "-" + buildVersion;
fs.renameSync(oldFileName + fileExtension, newFileName + fileExtension);
});
grunt.registerTask("delete_coverage_dir", function () {
const done = this.async();
const rimraf = require("rimraf");
rimraf("coverage", function (err) {
if (err) {
console.log("Error while deleting coverage directory from the package.");
done(false);
}
done();
});
});
grunt.registerTask("tslint:build", function (version) {
childProcess.execSync("npm run tslint", { stdio: "inherit" });
});
grunt.registerTask("test", ["transpile_additional_project", "generate_references", "ts:devlib", "shell:ci_unit_tests"]);
grunt.registerTask("generate_references", () => {
const referencesPath = path.join(__dirname, "references.d.ts");
// get all .d.ts files from nativescript-cli
const node_modules = "node_modules";
const nativescript = "nativescript";
const iOSDeviceLib = "ios-device-lib";
const nodeModulesDirPath = path.join(__dirname, node_modules);
const rootPathToIosDeviceLib = path.join(nodeModulesDirPath, iOSDeviceLib);
const pathToIosDeviceLib = fs.existsSync(rootPathToIosDeviceLib) ? rootPathToIosDeviceLib : path.join(nodeModulesDirPath, nativescript, node_modules, iOSDeviceLib);
const pathsOfDtsFiles = getReferencesFromDir(path.join(nodeModulesDirPath, nativescript))
.concat(getReferencesFromDir(pathToIosDeviceLib));
const lines = pathsOfDtsFiles.map(file => `/// <reference path="${fromWindowsRelativePathToUnix(path.relative(__dirname, file))}" />`);
fs.writeFileSync(referencesPath, lines.join(os.EOL));
});
const fromWindowsRelativePathToUnix = (windowsRelativePath) => {
return windowsRelativePath.replace(/\\/g, "/");
}
// returns paths that have to be added to reference.d.ts.
const getReferencesFromDir = (dir) => {
const currentDirContent = fs.readdirSync(dir).map(item => path.join(dir, item));
let pathsToDtsFiles = [];
_.each(currentDirContent, d => {
const stat = fs.statSync(d);
if (stat.isDirectory() && path.basename(d) !== "node_modules") {
// recursively check all dirs for .d.ts files.
pathsToDtsFiles = pathsToDtsFiles.concat(getReferencesFromDir(d));
} else if (stat.isFile() && d.endsWith(".d.ts") && path.basename(d) !== ".d.ts" && path.basename(d) !== "doctor.d.ts") {
pathsToDtsFiles.push(d);
}
});
return pathsToDtsFiles;
};
grunt.registerTask("pack", [
"clean",
"generate_references",
"ts:release_build",
"transpile_additional_project",
"shell:ci_unit_tests",
"tslint:build",
"set_package_version",
"delete_coverage_dir",
"shell:build_package",
"setPackageName"
]);
grunt.registerTask("lint", ["tslint:build"]);
grunt.registerTask("all", ["clean", "test", "tslint:build"]);
grunt.registerTask("default", ["generate_references", "ts:devlib"]);
grunt.registerTask("rebuild", ["clean", "default"]);
};