module.exports = function(grunt) { var fs=require("fs"); var pathModule=require("path"); var filterTypeScriptFiles = function(content, srcPath) { var matchRule = /^.*@private/ig; if (matchRule.test(content)) { return false; } var processed = content; processed = processed.replace(/\/\/[\/\s]*@private((.|\s)*?)\/\/[\/\s]*@endprivate/gm, ""); return processed; }; var updatePackageDef = function(content, update) { var contentAsObject = JSON.parse(content); update(contentAsObject); return JSON.stringify(contentAsObject, null, "\t"); } var updateModulesPackageDef = function(content, srcPath) { return updatePackageDef(content, function(contentAsObject) { contentAsObject.version = localCfg.packageVersion; if (localCfg.commitSHA) { contentAsObject.repository.url += "/commit/" + localCfg.commitSHA; } }); }; var updateAppPackageDef = function(content, srcPath) { var currentAppName = grunt.task.current.data.appName; return updatePackageDef(content, function(contentAsObject) { contentAsObject.version = localCfg.packageVersion; contentAsObject.author = "Telerik "; var specificKeywords = ["telerik", "mobile", "nativescript", "{N}", "tns", "appbuilder"]; if (currentAppName.indexOf("template-") == 0) { var templateName = currentAppName.substring("template-".length); contentAsObject.name = "tns-" + currentAppName; contentAsObject.description = "Nativescript " + templateName + " template"; specificKeywords.push("template"); } else { contentAsObject.name = "tns-samples-" + currentAppName; contentAsObject.description = "Nativescript " + currentAppName + " sample application"; specificKeywords.push("sample"); } contentAsObject.license = "BSD"; addKeywords(contentAsObject, specificKeywords); if (!contentAsObject.repository) { contentAsObject.repository = {}; } if (!contentAsObject.repository.url) { contentAsObject.repository.url = localCfg.mainPackageContent.repository.url; } if (localCfg.commitSHA) { contentAsObject.repository.url += "/commit/" + localCfg.commitSHA; } }); }; var addKeywords = function(packageObject, newKeywords) { if (!packageObject.keywords) { packageObject.keywords = newKeywords; return; } if (typeof(packageObject.keywords) == "string") { packageObject.keywords = packageObject.keywords.split(" "); } packageObject.keywords = packageObject.keywords.concat(newKeywords); }; var updateDefinitionsPackageDef = function(content, srcPath) { return updatePackageDef(content, function(contentAsObject) { contentAsObject.version = localCfg.packageVersion; contentAsObject.name = "tns-definitions"; contentAsObject.description = "NativeScript Module definitions"; contentAsObject.license = "Apache-2.0"; if (localCfg.commitSHA) { contentAsObject.repository.url += "/commit/" + localCfg.commitSHA; } }); }; var getCommitSha = function() { if (process.env.GIT_COMMIT) { return process.env.GIT_COMMIT; } return ""; }; var assignGitSHA = function(err, stdout, stderr, cb) { if (!localCfg.commitSHA) { localCfg.commitSHA = stdout.replace("\n", ""); } cb(); }; var getPackageVersion = function() { var buildVersion = process.env.PACKAGE_VERSION; if (!buildVersion) { return localCfg.mainPackageContent.version; } return localCfg.mainPackageContent.version + "-" + buildVersion; }; var processAppFile = function(content, srcPath) { return content; }; var getSubDirs = function(dir) { var allObjects = fs.readdirSync(dir); var allDirs = []; for (var i=0; i/", cwd: localCfg.srcDir }, license: { expand: true, src: [ "./LICENSE", ], dest: "<%= localCfg.outModulesDir %>/", cwd: localCfg.srcDir }, appLicense: { expand: true, src: ["./LICENSE"], cwd: localCfg.srcAppsDir, dest: "__dummy__" }, definitionFiles: { src: [ localCfg.srcDir + "/**/*.d.ts", pathModule.join(localCfg.srcDir, "LICENSE"), //Exclude the d.ts files in the apps folder - these are part of the apps and are already packed there! "!" + localCfg.srcDir + "/apps/**" ].concat(localCfg.defaultExcludes).concat(localCfg.excludedModules), dest: localCfg.outDefinitionsDir + "/", options: { process: filterTypeScriptFiles } }, modulesPackageDef: { src: localCfg.packageJsonFilePath, dest: localCfg.outModulesDir + "/", options: { process: updateModulesPackageDef } }, definitionsPackageDef: { src: localCfg.packageJsonFilePath, dest: localCfg.outDefinitionsDir + "/", options: { process: updateDefinitionsPackageDef } }, appPackageDef: { expand: true, flatten: true, src: "__app_package_json", dest: "__dummy__", options: { process: updateAppPackageDef } }, childPackageFiles: { src: [ localCfg.srcDir + "/**/package.json", "!./package.json", "!./Deploy/**/*.*", "!./bin/**/*.*", "!./Tests/**/*.*", "!" + localCfg.outDir + "/**/*.*" ].concat(localCfg.excludedModules), dest: localCfg.outModulesDir + "/" }, rawAppsFiles: { expand: true, src: [ "**/*.*", "**/*", "!**/*.map", "!**/*.ts" ], dest: localCfg.outAppsDir, cwd: localCfg.srcAppsDir, dot: true }, readyAppFiles: { expand: true, src: ["./**/*.*"], dest: localCfg.outAppsDir + "/", cwd: localCfg.outModulesDir + "/apps/", options: { process: processAppFile } }, readyTsAppFiles: { expand: true, src: ["./**/*.*", "!./**/*.map"], dest: localCfg.outTsAppsDir + "/", cwd: localCfg.srcAppsDir }, readyPackages: { expand: true, src: ["./**/*.tgz"], dest: localCfg.outDir + "/", cwd: localCfg.outDir, flatten: true } }, ts: { build: { src: localCfg.typeScriptSrc, outDir: localCfg.outModulesDir, options: { module: "commonjs", target: "es5", sourceMap: false, declaration: false, removeComments: "<%= !grunt.option('leavecomments') || '' %>", compiler: "node_modules/typescript/bin/tsc", noEmitOnError: true } } }, tslint: { build: { files: { src: localCfg.typeScriptSrcForTsLint }, options: { configuration: grunt.file.readJSON("./build/tslint.json") } } }, exec: { packModules: { cmd: "npm pack", cwd: localCfg.outModulesDir + "/" }, packDefinitions: { cmd: "npm pack", cwd: localCfg.outDefinitionsDir + "/" }, packApp: { cmd: "npm pack", cwd: "__dummy__" } }, multidest: { copyLicenseFiles: { tasks: ["copy:appLicense"], dest: function() { var apps = getSubDirs(localCfg.srcAppsDir); var targetDirs = []; apps.forEach(function(item){ targetDirs.push(pathModule.join(localCfg.outAppsDir, item.name)); targetDirs.push(pathModule.join(localCfg.outTsAppsDir, item.name)); }); return targetDirs; }() } }, shell: { getGitSHA: { command: "git rev-parse HEAD", options: { callback: assignGitSHA } } } }); grunt.loadNpmTasks("grunt-ts"); grunt.loadNpmTasks("grunt-contrib-clean"); grunt.loadNpmTasks("grunt-contrib-copy"); grunt.loadNpmTasks("grunt-exec"); grunt.loadNpmTasks("grunt-tslint"); grunt.loadNpmTasks("grunt-multi-dest"); grunt.loadNpmTasks("grunt-shell"); var cloneTasks = function(originalTasks, taskNameSuffix) { var clonedTasks = []; for(var i=0; i