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; }); }; var updateAppPackageDef = function(content, srcPath) { var currentAppName = grunt.task.current.data.appName; return updatePackageDef(content, function(contentAsObject) { contentAsObject.version = localCfg.packageVersion; contentAsObject.name = "tns-samples-" + currentAppName; contentAsObject.description = "Nativescript " + currentAppName + " sample application"; }); }; var updateDefinitionsPackageDef = function(content, srcPath) { return updatePackageDef(content, function(contentAsObject) { contentAsObject.version = localCfg.packageVersion; contentAsObject.name = "tns-definitions"; contentAsObject.description = "NativeScript Module definitions"; }); } var getPackageVersion = function(packageFilePath) { packageContent = grunt.file.readJSON(packageFilePath); var buildVersion = process.env.PACKAGE_VERSION; if (!buildVersion) { return packageContent.version; } return packageContent.version + "-" + buildVersion; }; var processAppFile = function(content, srcPath) { return content; }; var localCfg = { srcDir: ".", srcAppsDir: "./apps", packageJsonFilePath: "./package.json", outDir: "./bin/dist", outModulesDir: "./bin/dist/modules", outAppsDir: "./bin/dist/apps", outTsAppsDir: "./bin/dist/ts-apps", outDefinitionsDir: "./bin/dist/definitions", excludedModules: [ "!./ui/slide-out/**/*.*" ] }; localCfg.packageVersion = getPackageVersion(localCfg.packageJsonFilePath); localCfg.defaultExcludes = [ "!" + localCfg.outDir + "/**/*.*", "!./node_modules/**/*.*", "!./bin/**/*.*", "!./build/**/*.*", "!./Deploy/**/*.*", "!./obj/**/*.*", "!./out/**/*.*", "!./.*/**/*.*" ]; localCfg.typeScriptSrc = [ "./**/*.ts" ].concat(localCfg.defaultExcludes).concat(localCfg.excludedModules); localCfg.typeScriptSrcForTsLint = localCfg.typeScriptSrc.concat([ "!./ios.d.ts", "!./android17.d.ts", "!./libjs.d.ts" ]); grunt.initConfig({ localCfg : localCfg, pkg: grunt.file.readJSON('package.json'), clean: { build: { src: [localCfg.outDir] }, typeScriptLeftovers: { expand: true, src: [ "./.baseDir.*", "./_references.js", "./**/*.map" ], cwd: localCfg.outModulesDir }, readyAppFiles: { src: [localCfg.outModulesDir + "/apps/**"] } }, copy: { jsLibs: { expand: true, src: [ "./js-libs/**/*.js", ], dest: "<%= localCfg.outModulesDir %>/", cwd: localCfg.srcDir }, definitionFiles: { src: [ localCfg.srcDir + "/**/*.d.ts", //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" } } }, 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__" } } }); grunt.loadNpmTasks("grunt-ts"); grunt.loadNpmTasks("grunt-contrib-clean"); grunt.loadNpmTasks("grunt-contrib-copy"); grunt.loadNpmTasks("grunt-exec"); grunt.loadNpmTasks("grunt-tslint"); var getSubDirs = function(dir) { var allObjects = fs.readdirSync(dir); var allDirs = []; for (var i=0; i