Skip to content

Commit 7f6c585

Browse files
committed
Pull tsconfig upgrade code to a separate module. Add a bin script.
1 parent 2358571 commit 7f6c585

File tree

5 files changed

+121
-102
lines changed

5 files changed

+121
-102
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
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: 4 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@ 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
}
@@ -25,104 +19,13 @@ if (projectDir) {
2519

2620
function createReferenceFile() {
2721
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.';
22+
content = "/// <reference path=\"./node_modules/tns-core-modules/tns-core-modules.d.ts\" /> Needed for autocompletion and compilation.";
2923

3024
if (!fs.existsSync(referenceFilePath)) {
3125
fs.appendFileSync(referenceFilePath, content);
3226
}
3327
}
3428

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-
12629
function createTsconfig(tsconfigPath) {
12730
var tsconfig = {};
12831

@@ -134,8 +37,7 @@ function createTsconfig(tsconfigPath) {
13437
noEmitHelpers: true,
13538
noEmitOnError: true,
13639
};
137-
addDomLibs(tsconfig);
138-
addIterableToAngularProjects(tsconfig);
40+
upgrader.migrateProject(tsconfig, tsconfigPath, projectDir);
13941

14042
tsconfig.exclude = ["node_modules", "platforms", "**/*.aot.ts"];
14143

tsconfig-upgrader.js

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

0 commit comments

Comments
 (0)