From 7f6c585ad3c5258226eea375d74ea2a96a1ab48b Mon Sep 17 00:00:00 2001 From: Hristo Deshev Date: Mon, 6 Mar 2017 14:38:23 +0200 Subject: [PATCH 1/4] Pull tsconfig upgrade code to a separate module. Add a bin script. --- bin/ns-upgrade-tsconfig | 7 +++ bin/ns-upgrade-tsconfig.cmd | 1 + package.json | 3 + postinstall.js | 106 ++---------------------------------- tsconfig-upgrader.js | 106 ++++++++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 102 deletions(-) create mode 100644 bin/ns-upgrade-tsconfig create mode 100644 bin/ns-upgrade-tsconfig.cmd create mode 100644 tsconfig-upgrader.js diff --git a/bin/ns-upgrade-tsconfig b/bin/ns-upgrade-tsconfig new file mode 100644 index 0000000..e8e093e --- /dev/null +++ b/bin/ns-upgrade-tsconfig @@ -0,0 +1,7 @@ +#!/usr/bin/env node +var path = require("path"); +var upgrader = require("../tsconfig-upgrader"); + +var projectDir = path.dirname(path.dirname(path.dirname(__dirname))); +var tsConfigPath = path.join(projectDir, "tsconfig.json"); +upgrader.migrateTsConfig(tsConfigPath, projectDir); diff --git a/bin/ns-upgrade-tsconfig.cmd b/bin/ns-upgrade-tsconfig.cmd new file mode 100644 index 0000000..2f7a4c2 --- /dev/null +++ b/bin/ns-upgrade-tsconfig.cmd @@ -0,0 +1 @@ +@node %~dp0\ns-upgrade-tsconfig %* diff --git a/package.json b/package.json index 3c8deac..e3d7677 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,9 @@ "postinstall": "node postinstall.js", "preuninstall": "node preuninstall.js" }, + "bin": { + "ns-upgrade-tsconfig": "./bin/ns-upgrade-tsconfig" + }, "nativescript": { "hooks": [ { diff --git a/postinstall.js b/postinstall.js index 1a95f6c..64ad2a3 100644 --- a/postinstall.js +++ b/postinstall.js @@ -3,19 +3,13 @@ hook.postinstall(); var fs = require("fs"); var path = require("path"); - -var __migrations = [ - inlineSourceMapMigration, - addDomLibs, - addIterableToAngularProjects, -]; - +var upgrader = require("./tsconfig-upgrader"); var projectDir = hook.findProjectDir(); if (projectDir) { var tsconfigPath = path.join(projectDir, "tsconfig.json"); if (fs.existsSync(tsconfigPath)) { - migrateTsconfig(tsconfigPath); + upgrader.migrateTsConfig(tsconfigPath, projectDir); } else { createTsconfig(tsconfigPath); } @@ -25,104 +19,13 @@ if (projectDir) { function createReferenceFile() { var referenceFilePath = path.join(projectDir, "references.d.ts"), - content = '/// Needed for autocompletion and compilation.'; + content = "/// Needed for autocompletion and compilation."; if (!fs.existsSync(referenceFilePath)) { fs.appendFileSync(referenceFilePath, content); } } -function inlineSourceMapMigration(existingConfig, displayableTsconfigPath) { - if (existingConfig.compilerOptions) { - if ("sourceMap" in existingConfig["compilerOptions"]) { - delete existingConfig["compilerOptions"]["sourceMap"]; - console.warn("> Deleted \"compilerOptions.sourceMap\" setting in \"" + displayableTsconfigPath + "\"."); - console.warn("> Inline source maps will be used when building in Debug configuration from now on."); - } - } -} - -function addIterableToAngularProjects(existingConfig) { - var packageJsonPath = path.join(projectDir, "package.json"); - var packageJson = JSON.parse(fs.readFileSync(packageJsonPath)); - var dependencies = packageJson.dependencies || []; - - var hasAngular = Object.keys(dependencies).includes("nativescript-angular"); - var hasRelevantAngularVersion = /[4-9]\.\d+\.\d+/i.test(dependencies["@angular/core"]); - if (hasAngular && hasRelevantAngularVersion) { - console.log("Adding 'es2015.iterable' lib to tsconfig.json..."); - addTsLib(existingConfig, "es2015.iterable"); - } -} - -function addDomLibs(existingConfig) { - function relevantModulesVersion(version) { - return /[3-9]\.\d+\.\d+/i.test(version); - } - - function hasRelevantModulesDependency() { - var packageJsonPath = path.join(projectDir, "package.json"); - var packageJson = JSON.parse(fs.readFileSync(packageJsonPath)); - var dependencies = packageJson.dependencies || []; - - return relevantModulesVersion(dependencies["tns-core-modules"]); - } - - function hasRelevantModulesPackage() { - var packageJsonPath = path.join(projectDir, "node_modules", "tns-core-modules", "package.json"); - if (!fs.existsSync(packageJsonPath)) { - return false; - } - - var packageJson = JSON.parse(fs.readFileSync(packageJsonPath)); - return relevantModulesVersion(packageJson.version); - } - - if (hasRelevantModulesDependency() || hasRelevantModulesPackage()) { - console.log("Adding 'es6' lib to tsconfig.json..."); - addTsLib(existingConfig, "es6"); - console.log("Adding 'dom' lib to tsconfig.json..."); - addTsLib(existingConfig, "dom"); - } -} - -function addTsLib(existingConfig, libName) { - if (existingConfig.compilerOptions) { - var options = existingConfig.compilerOptions; - if (!options.lib) { - options.lib = []; - } - if (!options.lib.find(function(l) { - return libName.toLowerCase() === l.toLowerCase(); - })) { - options.lib.push(libName); - } - } -} - -function migrateTsconfig(tsconfigPath) { - var displayableTsconfigPath = path.relative(projectDir, tsconfigPath); - - function withTsConfig(action) { - var existingConfig = null; - try { - var existingConfigContents = fs.readFileSync(tsconfigPath); - existingConfig = JSON.parse(existingConfigContents); - } catch (e) { - console.error("Invalid " + displayableTsconfigPath + ": " + e); - return; - } - action(existingConfig); - fs.writeFileSync(tsconfigPath, JSON.stringify(existingConfig, null, 4)); - } - - withTsConfig(function(existingConfig) { - __migrations.forEach(function(migration) { - migration(existingConfig, displayableTsconfigPath); - }); - }); -} - function createTsconfig(tsconfigPath) { var tsconfig = {}; @@ -134,8 +37,7 @@ function createTsconfig(tsconfigPath) { noEmitHelpers: true, noEmitOnError: true, }; - addDomLibs(tsconfig); - addIterableToAngularProjects(tsconfig); + upgrader.migrateProject(tsconfig, tsconfigPath, projectDir); tsconfig.exclude = ["node_modules", "platforms", "**/*.aot.ts"]; diff --git a/tsconfig-upgrader.js b/tsconfig-upgrader.js new file mode 100644 index 0000000..b7494f2 --- /dev/null +++ b/tsconfig-upgrader.js @@ -0,0 +1,106 @@ +var fs = require("fs"); +var path = require("path"); + +var __migrations = [ + inlineSourceMapMigration, + addDomLibs, + addIterableToAngularProjects, +]; + +function migrateProject(tsConfig, tsconfigPath, projectDir) { + var displayableTsconfigPath = path.relative(projectDir, tsconfigPath); + __migrations.forEach(function(migration) { + migration(tsConfig, displayableTsconfigPath, projectDir); + }); +} +exports.migrateProject = migrateProject; + +function migrateTsConfig(tsconfigPath, projectDir) { + var displayableTsconfigPath = path.relative(projectDir, tsconfigPath); + + function withTsConfig(action) { + var existingConfig = null; + try { + var existingConfigContents = fs.readFileSync(tsconfigPath); + existingConfig = JSON.parse(existingConfigContents); + } catch (e) { + console.error("Invalid " + displayableTsconfigPath + ": " + e); + return; + } + action(existingConfig); + fs.writeFileSync(tsconfigPath, JSON.stringify(existingConfig, null, 4)); + } + + withTsConfig(function(existingConfig) { + migrateProject(existingConfig, displayableTsconfigPath, projectDir); + }); +} +exports.migrateTsConfig = migrateTsConfig; + +function inlineSourceMapMigration(existingConfig, displayableTsconfigPath) { + if (existingConfig.compilerOptions) { + if ("sourceMap" in existingConfig["compilerOptions"]) { + delete existingConfig["compilerOptions"]["sourceMap"]; + console.warn("> Deleted \"compilerOptions.sourceMap\" setting in \"" + displayableTsconfigPath + "\"."); + console.warn("> Inline source maps will be used when building in Debug configuration from now on."); + } + } +} + +function addIterableToAngularProjects(existingConfig, displayableTsconfigPath, projectDir) { + var packageJsonPath = path.join(projectDir, "package.json"); + var packageJson = JSON.parse(fs.readFileSync(packageJsonPath)); + var dependencies = packageJson.dependencies || []; + + var hasAngular = Object.keys(dependencies).includes("nativescript-angular"); + var hasRelevantAngularVersion = /[4-9]\.\d+\.\d+/i.test(dependencies["@angular/core"]); + if (hasAngular && hasRelevantAngularVersion) { + console.log("Adding 'es2015.iterable' lib to tsconfig.json..."); + addTsLib(existingConfig, "es2015.iterable"); + } +} + +function addDomLibs(existingConfig, displayableTsconfigPath, projectDir) { + function relevantModulesVersion(version) { + return /[3-9]\.\d+\.\d+/i.test(version); + } + + function hasRelevantModulesDependency() { + var packageJsonPath = path.join(projectDir, "package.json"); + var packageJson = JSON.parse(fs.readFileSync(packageJsonPath)); + var dependencies = packageJson.dependencies || []; + + return relevantModulesVersion(dependencies["tns-core-modules"]); + } + + function hasRelevantModulesPackage() { + var packageJsonPath = path.join(projectDir, "node_modules", "tns-core-modules", "package.json"); + if (!fs.existsSync(packageJsonPath)) { + return false; + } + + var packageJson = JSON.parse(fs.readFileSync(packageJsonPath)); + return relevantModulesVersion(packageJson.version); + } + + if (hasRelevantModulesDependency() || hasRelevantModulesPackage()) { + console.log("Adding 'es6' lib to tsconfig.json..."); + addTsLib(existingConfig, "es6"); + console.log("Adding 'dom' lib to tsconfig.json..."); + addTsLib(existingConfig, "dom"); + } +} + +function addTsLib(existingConfig, libName) { + if (existingConfig.compilerOptions) { + var options = existingConfig.compilerOptions; + if (!options.lib) { + options.lib = []; + } + if (!options.lib.find(function(l) { + return libName.toLowerCase() === l.toLowerCase(); + })) { + options.lib.push(libName); + } + } +} From a7fcd4fbd08c152fa55ac733436e588a5b00148c Mon Sep 17 00:00:00 2001 From: Hristo Deshev Date: Mon, 6 Mar 2017 14:51:24 +0200 Subject: [PATCH 2/4] Add a migration for tns-core-modules path mappings. --- tsconfig-upgrader.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/tsconfig-upgrader.js b/tsconfig-upgrader.js index b7494f2..e8c59e4 100644 --- a/tsconfig-upgrader.js +++ b/tsconfig-upgrader.js @@ -5,6 +5,7 @@ var __migrations = [ inlineSourceMapMigration, addDomLibs, addIterableToAngularProjects, + addTnsCoreModulesPathMappings, ]; function migrateProject(tsConfig, tsconfigPath, projectDir) { @@ -60,7 +61,7 @@ function addIterableToAngularProjects(existingConfig, displayableTsconfigPath, p } } -function addDomLibs(existingConfig, displayableTsconfigPath, projectDir) { +function hasModules30(projectDir) { function relevantModulesVersion(version) { return /[3-9]\.\d+\.\d+/i.test(version); } @@ -83,10 +84,14 @@ function addDomLibs(existingConfig, displayableTsconfigPath, projectDir) { return relevantModulesVersion(packageJson.version); } - if (hasRelevantModulesDependency() || hasRelevantModulesPackage()) { - console.log("Adding 'es6' lib to tsconfig.json..."); + return hasRelevantModulesDependency() || hasRelevantModulesPackage(); +} + +function addDomLibs(existingConfig, displayableTsconfigPath, projectDir) { + if (hasModules30(projectDir)) { + console.log("Adding 'es6' lib to tsconfig.json..."); addTsLib(existingConfig, "es6"); - console.log("Adding 'dom' lib to tsconfig.json..."); + console.log("Adding 'dom' lib to tsconfig.json..."); addTsLib(existingConfig, "dom"); } } @@ -104,3 +109,17 @@ function addTsLib(existingConfig, libName) { } } } + +function addTnsCoreModulesPathMappings(existingConfig, displayableTsconfigPath, projectDir) { + if (hasModules30(projectDir)) { + console.log("Adding tns-core-modules path mappings lib to tsconfig.json..."); + existingConfig["compilerOptions"] = existingConfig["compilerOptions"] || {}; + var compilerOptions = existingConfig["compilerOptions"]; + compilerOptions["baseUrl"] = "."; + compilerOptions["paths"] = compilerOptions["paths"] || {}; + compilerOptions["paths"]["*"] = [ + "./node_modules/tns-core-modules/*", + "./node_modules/*" + ]; + } +} From ef1bcdfa673f9c83f6fbc0f6a411bca1e0814d9a Mon Sep 17 00:00:00 2001 From: Hristo Deshev Date: Tue, 7 Mar 2017 09:22:20 +0200 Subject: [PATCH 3/4] Don't create references.d.ts if using tns-core-modules@3.0.0 --- postinstall.js | 4 +++- tsconfig-upgrader.js | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/postinstall.js b/postinstall.js index 64ad2a3..6f350e5 100644 --- a/postinstall.js +++ b/postinstall.js @@ -13,7 +13,9 @@ if (projectDir) { } else { createTsconfig(tsconfigPath); } - createReferenceFile(); + if (!upgrader.hasModules30(projectDir)) { + createReferenceFile(); + } installTypescript(); } diff --git a/tsconfig-upgrader.js b/tsconfig-upgrader.js index e8c59e4..fa5aba4 100644 --- a/tsconfig-upgrader.js +++ b/tsconfig-upgrader.js @@ -86,6 +86,7 @@ function hasModules30(projectDir) { return hasRelevantModulesDependency() || hasRelevantModulesPackage(); } +exports.hasModules30 = hasModules30; function addDomLibs(existingConfig, displayableTsconfigPath, projectDir) { if (hasModules30(projectDir)) { From 61195000195b63b3d62666305681392190a62978 Mon Sep 17 00:00:00 2001 From: Hristo Deshev Date: Mon, 6 Mar 2017 15:43:23 +0200 Subject: [PATCH 4/4] 0.4.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e3d7677..dd233c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nativescript-dev-typescript", - "version": "0.4.1", + "version": "0.4.2", "description": "TypeScript support for NativeScript projects. Install using `tns install typescript`.", "scripts": { "test": "exit 0",