var tsconfig = require('./tsconfig.json'); var shelljs = require("shelljs"); module.exports = function(grunt) { if (grunt.option('profile')) { grunt.log.writeln('Profiling all grunt tasks...'); require('time-grunt')(grunt); } if (grunt.cli.tasks.indexOf("testsapp") >= 0 || grunt.cli.tasks.indexOf("buildTestsApp")>= 0) { var tsTester = require("./build/run-testsapp.grunt.js"); tsTester.run(grunt); return; } var fs=require("fs"); var pathModule=require("path"); var tsLintOption = grunt.option('runtslint'); var skipTsLint = tsLintOption == 'false' || tsLintOption == false; if (tsLintOption == null) { skipTsLint = false; } 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 + " project 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 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) { return shelljs.ls(dir).filter(function (subDir) { return shelljs.test('-d', pathModule.join(dir, subDir)); }); }; var getApps = function() { var allApps = getSubDirs(localCfg.srcAppsDir); if (grunt.option('test-app-only')) { allApps = allApps.filter(function(appName) { return appName === 'tests'; }); } return allApps; }; var localCfg = { srcDir: ".", srcAppsDir: "./apps", packageJsonFilePath: "./package.json", outDir: "./bin/dist", outModulesDir: tsconfig.compilerOptions.outDir, outAppsDir: "./bin/dist/apps", outTsAppsDir: "./bin/dist/ts-apps", outApiRefDir: "./bin/dist/api-ref" }; var nodeTestEnv = JSON.parse(JSON.stringify(process.env)); nodeTestEnv.NODE_PATH = localCfg.outModulesDir; localCfg.nodeTestsDir = pathModule.join(localCfg.outModulesDir, 'node-tests'); localCfg.mainPackageContent = grunt.file.readJSON(localCfg.packageJsonFilePath); localCfg.packageVersion = getPackageVersion(localCfg.packageJsonFilePath); localCfg.commitSHA = getCommitSha(); localCfg.typeScriptSrc = tsconfig.filesGlob; localCfg.defaultExcludes = localCfg.typeScriptSrc.filter(function(item) { return /^!/.test(item); }); localCfg.typeScriptSrcForTsLint = localCfg.typeScriptSrc.concat([ "!ios.d.ts", "!android17.d.ts", "!libjs.d.ts" ]); localCfg.srcTsdFiles = [ "**/*.d.ts", "!apps/**", "!node-tests/**", "!org.nativescript.widgets.d.ts", "!android17.d.ts", "!**/*.android.d.ts", "!ios.d.ts", "!**/*.ios.d.ts" ].concat(localCfg.defaultExcludes); var tsOptions = tsconfig.compilerOptions; tsOptions.fast = 'never'; tsOptions.removeComments = !grunt.option('leavecomments') || ''; tsOptions.compiler = "node_modules/typescript/bin/tsc"; tsOptions.failOnTypeErrors = true; 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 }, nodeTests: { src: localCfg.nodeTestsDir, }, builtModules: { src: localCfg.outModulesDir }, externalModules: { expand: true, src: '<%= grunt.option("path") %>/node_modules/tns-core-modules/**/*', options: { force: true }, }, readyAppFiles: { src: [localCfg.outModulesDir + "/apps/**"] } }, copy: { jsLibs: { expand: true, src: [ "js-libs/**/*.js", "fetch/**/*.js", "css/**/*.js", "css-value/**/*.js", ], dest: localCfg.outModulesDir, 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: [ "**/*.d.ts", //Exclude the d.ts files in the apps folder - these are part of the apps and are already packed there! "!apps/**", "!ts-apps/**", "!node-tests/**", "!org.nativescript.widgets.d.ts", "!android17.d.ts", "!**/*.android.d.ts", "!ios.d.ts", "!**/*.ios.d.ts", ].concat(localCfg.defaultExcludes), dest: localCfg.outModulesDir + "/", expand: true, options: { process: filterTypeScriptFiles } }, modulesPackageDef: { expand: true, src: localCfg.packageJsonFilePath, dest: localCfg.outModulesDir + "/", options: { process: updateModulesPackageDef } }, appPackageDef: { expand: true, flatten: true, src: "__app_package_json", dest: "__dummy__", options: { process: updateAppPackageDef } }, childPackageFiles: { expand: true, src: [ "**/package.json", "!package.json", "!Deploy/**/*.*", "!bin/**/*.*", "!Tests/**/*.*", "!node_modules/**/*.*", "!" + localCfg.outDir + "/**/*.*" ], 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 }, builtModules: { expand: true, src: [ '**/*', '!*.md', '!apps/**/*', '!node_modules/**/*', '!node-tests/**/*', ], cwd: localCfg.outModulesDir, dest: "<%= grunt.option('path') %>/node_modules/tns-core-modules/", } }, ts: { build: { tsconfig: 'tsconfig.json', outDir: localCfg.outModulesDir, options: tsOptions }, buildNodeTests: { src: [ 'js-libs/easysax/**/*.ts', 'xml/**/*.ts', 'node-tests/**/*.ts', 'es-collections.d.ts', ], outDir: localCfg.outModulesDir, options: tsOptions }, buildDts: { src: [ '**/*.d.ts', '!org.nativescript.widgets.d.ts', '!**/*.android.d.ts', '!node_modules/**/*', '!bin/**/*', '!apps/**/*', '!android17.d.ts', '!ios.d.ts', ], outDir: localCfg.outModulesDir, options: tsOptions }, testCombinedDts: { src: [ pathModule.join(localCfg.outModulesDir, 'tns-core-modules.d.ts'), ], outDir: localCfg.outModulesDir, options: tsOptions } }, tslint: { build: { files: { src: localCfg.typeScriptSrcForTsLint }, options: { configuration: grunt.file.readJSON("./build/tslint.json") } } }, exec: { packModules: { cmd: "npm pack", cwd: localCfg.outModulesDir + "/" }, packApp: { cmd: "npm pack", cwd: "__dummy__" }, mochaNode: { cmd: "grunt simplemocha:node" } }, multidest: { copyLicenseFiles: { tasks: ["copy:appLicense"], dest: function() { var apps = getApps(); var targetDirs = []; apps.forEach(function(item){ targetDirs.push(pathModule.join(localCfg.outAppsDir, item)); targetDirs.push(pathModule.join(localCfg.outTsAppsDir, item)); }); return targetDirs; }() } }, shell: { getGitSHA: { command: "git rev-parse HEAD", options: { callback: assignGitSHA } }, }, simplemocha: { node: { src: localCfg.nodeTestsDir + '/**/*.js' } }, env: { nodeTests: { NODE_PATH: localCfg.outModulesDir, } }, typedoc: { build: { options: { // 'flag:undefined' will set flags without options. "module": 'commonjs', "target": 'es5', "out": localCfg.outApiRefDir, //"json": './dist/doc.json', "name": 'NativeScript', "includeDeclarations": undefined, //"excludeExternals": undefined, //"externalPattern": './declarations.d.ts', "mode": "file", //"readme": "source/README.md", //"entryPoint": '"a-module"' // verbose: undefined }, src: localCfg.srcTsdFiles } } }); 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"); grunt.loadNpmTasks("grunt-env"); grunt.loadNpmTasks("grunt-simple-mocha"); grunt.loadNpmTasks('grunt-typedoc'); var cloneTasks = function(originalTasks, taskNameSuffix) { var clonedTasks = []; for(var i=0; i 0) grunt.fail.fatal("\n" + errors.join("\n")); }); grunt.registerTask("distribute-ts-apps-files", [ "copy:readyTsAppFiles" ]); grunt.registerTask("generate-tns-core-modules-dts", function() { var dtsFiles = grunt.file.expand({cwd: localCfg.outModulesDir}, [ '**/*.d.ts', '!tns-core-modules.d.ts' ].concat(localCfg.defaultExcludes)); dtsFiles.sort(); var dtsLines = dtsFiles.map(function(dtsFile) { return '/// '; }); var combinedDtsPath = pathModule.join(localCfg.outModulesDir, 'tns-core-modules.d.ts'); grunt.file.write(combinedDtsPath, dtsLines.join('\n')); }); //aliasing pack-modules for backwards compatibility grunt.registerTask("pack-modules", [ "compile-modules", "exec:packModules" ]); grunt.registerTask("pack-apps", [ "processEachApp:" + localCfg.outAppsDir ]); grunt.registerTask("pack-ts-apps", [ "processEachApp:" + localCfg.outTsAppsDir + ":-ts" ]); grunt.registerTask("get-ready-packages", [ "copy:readyPackages" ]); grunt.registerTask("default", (skipTsLint ? [] : ["tslint:build"]).concat([ "build-all", "pack-apps", "pack-ts-apps", "get-ready-packages" ])); grunt.registerTask("compile-modules", [ "clean:build", "shell:getGitSHA", "check-packagejson-boms", "compile-ts", "collect-modules-raw-files", "copy:modulesPackageDef", "copy:definitionFiles", "copy:jsLibs", "generate-tns-core-modules-dts", "ts:testCombinedDts", ]); //alias just-build for backwards compatibility grunt.registerTask("just-build", ["build-all"]); grunt.registerTask("build-all", [ "pack-modules", "collect-apps-raw-files", "distribute-apps-files", "distribute-ts-apps-files", ]); grunt.registerTask("node-tests", [ "clean:nodeTests", "ts:buildNodeTests", "copy:childPackageFiles", "copy:jsLibs", "env:nodeTests", "exec:mochaNode", //spawn a new process to use the new NODE_PATH ]); };