Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c3e0ee0

Browse files
authoredMar 7, 2017
Merge pull request #28 from NativeScript/hdeshev/tns-core-modules-path-mapping
tns-core-modules path mapping
2 parents 2358571 + 6119500 commit c3e0ee0

File tree

5 files changed

+145
-104
lines changed

5 files changed

+145
-104
lines changed
 

‎bin/ns-upgrade-tsconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env node
2+
var path = require("path");
3+
var upgrader = require("../tsconfig-upgrader");
4+
5+
var projectDir = path.dirname(path.dirname(path.dirname(__dirname)));
6+
var tsConfigPath = path.join(projectDir, "tsconfig.json");
7+
upgrader.migrateTsConfig(tsConfigPath, projectDir);

‎bin/ns-upgrade-tsconfig.cmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@node %~dp0\ns-upgrade-tsconfig %*

‎package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
{
22
"name": "nativescript-dev-typescript",
3-
"version": "0.4.1",
3+
"version": "0.4.2",
44
"description": "TypeScript support for NativeScript projects. Install using `tns install typescript`.",
55
"scripts": {
66
"test": "exit 0",
77
"postinstall": "node postinstall.js",
88
"preuninstall": "node preuninstall.js"
99
},
10+
"bin": {
11+
"ns-upgrade-tsconfig": "./bin/ns-upgrade-tsconfig"
12+
},
1013
"nativescript": {
1114
"hooks": [
1215
{

‎postinstall.js

Lines changed: 7 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -3,126 +3,31 @@ hook.postinstall();
33

44
var fs = require("fs");
55
var path = require("path");
6-
7-
var __migrations = [
8-
inlineSourceMapMigration,
9-
addDomLibs,
10-
addIterableToAngularProjects,
11-
];
12-
6+
var upgrader = require("./tsconfig-upgrader");
137

148
var projectDir = hook.findProjectDir();
159
if (projectDir) {
1610
var tsconfigPath = path.join(projectDir, "tsconfig.json");
1711
if (fs.existsSync(tsconfigPath)) {
18-
migrateTsconfig(tsconfigPath);
12+
upgrader.migrateTsConfig(tsconfigPath, projectDir);
1913
} else {
2014
createTsconfig(tsconfigPath);
2115
}
22-
createReferenceFile();
16+
if (!upgrader.hasModules30(projectDir)) {
17+
createReferenceFile();
18+
}
2319
installTypescript();
2420
}
2521

2622
function createReferenceFile() {
2723
var referenceFilePath = path.join(projectDir, "references.d.ts"),
28-
content = '/// <reference path="./node_modules/tns-core-modules/tns-core-modules.d.ts" /> Needed for autocompletion and compilation.';
24+
content = "/// <reference path=\"./node_modules/tns-core-modules/tns-core-modules.d.ts\" /> Needed for autocompletion and compilation.";
2925

3026
if (!fs.existsSync(referenceFilePath)) {
3127
fs.appendFileSync(referenceFilePath, content);
3228
}
3329
}
3430

35-
function inlineSourceMapMigration(existingConfig, displayableTsconfigPath) {
36-
if (existingConfig.compilerOptions) {
37-
if ("sourceMap" in existingConfig["compilerOptions"]) {
38-
delete existingConfig["compilerOptions"]["sourceMap"];
39-
console.warn("> Deleted \"compilerOptions.sourceMap\" setting in \"" + displayableTsconfigPath + "\".");
40-
console.warn("> Inline source maps will be used when building in Debug configuration from now on.");
41-
}
42-
}
43-
}
44-
45-
function addIterableToAngularProjects(existingConfig) {
46-
var packageJsonPath = path.join(projectDir, "package.json");
47-
var packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
48-
var dependencies = packageJson.dependencies || [];
49-
50-
var hasAngular = Object.keys(dependencies).includes("nativescript-angular");
51-
var hasRelevantAngularVersion = /[4-9]\.\d+\.\d+/i.test(dependencies["@angular/core"]);
52-
if (hasAngular && hasRelevantAngularVersion) {
53-
console.log("Adding 'es2015.iterable' lib to tsconfig.json...");
54-
addTsLib(existingConfig, "es2015.iterable");
55-
}
56-
}
57-
58-
function addDomLibs(existingConfig) {
59-
function relevantModulesVersion(version) {
60-
return /[3-9]\.\d+\.\d+/i.test(version);
61-
}
62-
63-
function hasRelevantModulesDependency() {
64-
var packageJsonPath = path.join(projectDir, "package.json");
65-
var packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
66-
var dependencies = packageJson.dependencies || [];
67-
68-
return relevantModulesVersion(dependencies["tns-core-modules"]);
69-
}
70-
71-
function hasRelevantModulesPackage() {
72-
var packageJsonPath = path.join(projectDir, "node_modules", "tns-core-modules", "package.json");
73-
if (!fs.existsSync(packageJsonPath)) {
74-
return false;
75-
}
76-
77-
var packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
78-
return relevantModulesVersion(packageJson.version);
79-
}
80-
81-
if (hasRelevantModulesDependency() || hasRelevantModulesPackage()) {
82-
console.log("Adding 'es6' lib to tsconfig.json...");
83-
addTsLib(existingConfig, "es6");
84-
console.log("Adding 'dom' lib to tsconfig.json...");
85-
addTsLib(existingConfig, "dom");
86-
}
87-
}
88-
89-
function addTsLib(existingConfig, libName) {
90-
if (existingConfig.compilerOptions) {
91-
var options = existingConfig.compilerOptions;
92-
if (!options.lib) {
93-
options.lib = [];
94-
}
95-
if (!options.lib.find(function(l) {
96-
return libName.toLowerCase() === l.toLowerCase();
97-
})) {
98-
options.lib.push(libName);
99-
}
100-
}
101-
}
102-
103-
function migrateTsconfig(tsconfigPath) {
104-
var displayableTsconfigPath = path.relative(projectDir, tsconfigPath);
105-
106-
function withTsConfig(action) {
107-
var existingConfig = null;
108-
try {
109-
var existingConfigContents = fs.readFileSync(tsconfigPath);
110-
existingConfig = JSON.parse(existingConfigContents);
111-
} catch (e) {
112-
console.error("Invalid " + displayableTsconfigPath + ": " + e);
113-
return;
114-
}
115-
action(existingConfig);
116-
fs.writeFileSync(tsconfigPath, JSON.stringify(existingConfig, null, 4));
117-
}
118-
119-
withTsConfig(function(existingConfig) {
120-
__migrations.forEach(function(migration) {
121-
migration(existingConfig, displayableTsconfigPath);
122-
});
123-
});
124-
}
125-
12631
function createTsconfig(tsconfigPath) {
12732
var tsconfig = {};
12833

@@ -134,8 +39,7 @@ function createTsconfig(tsconfigPath) {
13439
noEmitHelpers: true,
13540
noEmitOnError: true,
13641
};
137-
addDomLibs(tsconfig);
138-
addIterableToAngularProjects(tsconfig);
42+
upgrader.migrateProject(tsconfig, tsconfigPath, projectDir);
13943

14044
tsconfig.exclude = ["node_modules", "platforms", "**/*.aot.ts"];
14145

‎tsconfig-upgrader.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
var fs = require("fs");
2+
var path = require("path");
3+
4+
var __migrations = [
5+
inlineSourceMapMigration,
6+
addDomLibs,
7+
addIterableToAngularProjects,
8+
addTnsCoreModulesPathMappings,
9+
];
10+
11+
function migrateProject(tsConfig, tsconfigPath, projectDir) {
12+
var displayableTsconfigPath = path.relative(projectDir, tsconfigPath);
13+
__migrations.forEach(function(migration) {
14+
migration(tsConfig, displayableTsconfigPath, projectDir);
15+
});
16+
}
17+
exports.migrateProject = migrateProject;
18+
19+
function migrateTsConfig(tsconfigPath, projectDir) {
20+
var displayableTsconfigPath = path.relative(projectDir, tsconfigPath);
21+
22+
function withTsConfig(action) {
23+
var existingConfig = null;
24+
try {
25+
var existingConfigContents = fs.readFileSync(tsconfigPath);
26+
existingConfig = JSON.parse(existingConfigContents);
27+
} catch (e) {
28+
console.error("Invalid " + displayableTsconfigPath + ": " + e);
29+
return;
30+
}
31+
action(existingConfig);
32+
fs.writeFileSync(tsconfigPath, JSON.stringify(existingConfig, null, 4));
33+
}
34+
35+
withTsConfig(function(existingConfig) {
36+
migrateProject(existingConfig, displayableTsconfigPath, projectDir);
37+
});
38+
}
39+
exports.migrateTsConfig = migrateTsConfig;
40+
41+
function inlineSourceMapMigration(existingConfig, displayableTsconfigPath) {
42+
if (existingConfig.compilerOptions) {
43+
if ("sourceMap" in existingConfig["compilerOptions"]) {
44+
delete existingConfig["compilerOptions"]["sourceMap"];
45+
console.warn("> Deleted \"compilerOptions.sourceMap\" setting in \"" + displayableTsconfigPath + "\".");
46+
console.warn("> Inline source maps will be used when building in Debug configuration from now on.");
47+
}
48+
}
49+
}
50+
51+
function addIterableToAngularProjects(existingConfig, displayableTsconfigPath, projectDir) {
52+
var packageJsonPath = path.join(projectDir, "package.json");
53+
var packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
54+
var dependencies = packageJson.dependencies || [];
55+
56+
var hasAngular = Object.keys(dependencies).includes("nativescript-angular");
57+
var hasRelevantAngularVersion = /[4-9]\.\d+\.\d+/i.test(dependencies["@angular/core"]);
58+
if (hasAngular && hasRelevantAngularVersion) {
59+
console.log("Adding 'es2015.iterable' lib to tsconfig.json...");
60+
addTsLib(existingConfig, "es2015.iterable");
61+
}
62+
}
63+
64+
function hasModules30(projectDir) {
65+
function relevantModulesVersion(version) {
66+
return /[3-9]\.\d+\.\d+/i.test(version);
67+
}
68+
69+
function hasRelevantModulesDependency() {
70+
var packageJsonPath = path.join(projectDir, "package.json");
71+
var packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
72+
var dependencies = packageJson.dependencies || [];
73+
74+
return relevantModulesVersion(dependencies["tns-core-modules"]);
75+
}
76+
77+
function hasRelevantModulesPackage() {
78+
var packageJsonPath = path.join(projectDir, "node_modules", "tns-core-modules", "package.json");
79+
if (!fs.existsSync(packageJsonPath)) {
80+
return false;
81+
}
82+
83+
var packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
84+
return relevantModulesVersion(packageJson.version);
85+
}
86+
87+
return hasRelevantModulesDependency() || hasRelevantModulesPackage();
88+
}
89+
exports.hasModules30 = hasModules30;
90+
91+
function addDomLibs(existingConfig, displayableTsconfigPath, projectDir) {
92+
if (hasModules30(projectDir)) {
93+
console.log("Adding 'es6' lib to tsconfig.json...");
94+
addTsLib(existingConfig, "es6");
95+
console.log("Adding 'dom' lib to tsconfig.json...");
96+
addTsLib(existingConfig, "dom");
97+
}
98+
}
99+
100+
function addTsLib(existingConfig, libName) {
101+
if (existingConfig.compilerOptions) {
102+
var options = existingConfig.compilerOptions;
103+
if (!options.lib) {
104+
options.lib = [];
105+
}
106+
if (!options.lib.find(function(l) {
107+
return libName.toLowerCase() === l.toLowerCase();
108+
})) {
109+
options.lib.push(libName);
110+
}
111+
}
112+
}
113+
114+
function addTnsCoreModulesPathMappings(existingConfig, displayableTsconfigPath, projectDir) {
115+
if (hasModules30(projectDir)) {
116+
console.log("Adding tns-core-modules path mappings lib to tsconfig.json...");
117+
existingConfig["compilerOptions"] = existingConfig["compilerOptions"] || {};
118+
var compilerOptions = existingConfig["compilerOptions"];
119+
compilerOptions["baseUrl"] = ".";
120+
compilerOptions["paths"] = compilerOptions["paths"] || {};
121+
compilerOptions["paths"]["*"] = [
122+
"./node_modules/tns-core-modules/*",
123+
"./node_modules/*"
124+
];
125+
}
126+
}

0 commit comments

Comments
 (0)
Please sign in to comment.