Skip to content

Commit 7c3d159

Browse files
Alexander Vakrilovsis0k0
authored andcommitted
feat: refactor shared structure (#196)
1 parent cdf41c6 commit 7c3d159

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1252
-386
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
],
1818
"outFiles": [
1919
"${workspaceFolder}/**/*.js"
20-
]
20+
],
21+
"preLaunchTask": "npm-build"
2122
}
2223
]
2324
}

.vscode/tasks.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"type": "npm",
8+
"script": "build",
9+
"problemMatcher": [],
10+
"label": "npm-build"
11+
}
12+
]
13+
}

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@
1515
},
1616
"schematics": "./src/collection.json",
1717
"dependencies": {
18-
"@angular-devkit/core": "~7.1.3",
19-
"@angular-devkit/schematics": "~7.1.3",
20-
"@schematics/angular": "~7.1.3"
18+
"@angular-devkit/core": "~7.3.9",
19+
"@angular-devkit/schematics": "~7.3.9",
20+
"@nativescript/tslint-rules": "~0.0.1",
21+
"@phenomnomnominal/tsquery": "^3.0.0",
22+
"@schematics/angular": "~7.3.9",
23+
"tslint": "^5.16.0"
2124
},
2225
"devDependencies": {
2326
"@types/jasmine": "^2.6.0",
2427
"@types/node": "^8.0.31",
2528
"conventional-changelog-cli": "^2.0.1",
2629
"jasmine": "^2.8.0",
2730
"jasmine-spec-reporter": "^4.2.1",
28-
"typescript": "~3.1.1"
31+
"typescript": "3.2.4"
2932
},
3033
"repository": {
3134
"type": "git",

src/add-ns/_ns-files/__sourceDir__/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"android": {
33
"v8Flags": "--expose_gc"
44
},
5-
"main": "<%= main %>.js",
5+
"main": "<%= main %><%= nsext %>.js",
66
"name": "migration-ng",
77
"version": "4.1.0"
88
}

src/add-ns/_ns-files/tsconfig__nsext__.json

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@
33
"compilerOptions": {
44
"module": "es2015",
55
"moduleResolution": "node",
6-
"baseUrl": "./",
76
"paths": {
8-
"~/*": [
9-
"src/*"
10-
],
11-
"*": [
12-
"./node_modules/tns-core-modules/*",
13-
"./node_modules/*"
7+
"@src/*": [
8+
"<%= sourceDir %>/*.android.ts",
9+
"<%= sourceDir %>/*.ios.ts",
10+
"<%= sourceDir %>/*.tns.ts",
11+
"<%= sourceDir %>/*.ts"
1412
]
1513
}
1614
},
17-
"exclude": [
18-
"**/*.tns.ts",
19-
"**/*.android.ts",
20-
"**/*.ios.ts",
21-
"**/*.spec.ts"
15+
"files": [
16+
"<%= sourceDir %>/main<%= nsext %>.ts"
2217
]
23-
}
18+
}

src/add-ns/_sample-files/barcelona/player-detail/player-detail.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22
import { ActivatedRoute } from '@angular/router';
33

4-
import { Player } from '../player';
4+
import { Player } from '../player.model';
55
import { PlayerService } from '../player.service';
66

77
@Component({

src/add-ns/_sample-files/barcelona/player.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { Player } from './player';
2+
import { Player } from './player.model';
33

44
@Injectable()
55
export class PlayerService {

src/add-ns/_sample-files/barcelona/players/players.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
22

3-
import { Player } from '../player';
3+
import { Player } from '../player.model';
44
import { PlayerService } from '../player.service';
55

66
@Component({

src/add-ns/index.ts

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join } from 'path';
1+
import { join, dirname } from 'path';
22
import {
33
Rule,
44
SchematicContext,
@@ -22,6 +22,8 @@ import { getJsonFile, getFileContents, getPackageJson, overwritePackageJson } fr
2222
import { getAngularProjectSettings, AngularProjectSettings } from '../angular-project-parser';
2323
import { Extensions } from '../generate/utils';
2424

25+
import { Schema as ConvertRelativeImportsSchema } from '../convert-relative-imports/schema';
26+
2527
let extensions: Extensions;
2628
let projectSettings: AngularProjectSettings;
2729

@@ -46,14 +48,17 @@ export default function (options: MigrationOptions): Rule {
4648
mergeGitIgnore,
4749
addRunScriptsToPackageJson,
4850
addNativeScriptProjectId,
49-
excludeNsFilesFromTsconfig,
51+
52+
modifyWebTsconfig,
5053

5154
options.skipAutoGeneratedComponent ?
5255
noop() :
5356
addSampleComponent(options.nsExtension, options.webExtension, options.project),
5457

5558
addDependencies(),
5659

60+
schematic<ConvertRelativeImportsSchema>('convert-relative-imports', options),
61+
5762
options.skipInstall ?
5863
noop() :
5964
(_tree: Tree, context: SchematicContext) => {
@@ -211,6 +216,7 @@ const mergeGitIgnore = (tree: Tree, context: SchematicContext) => {
211216
*/
212217
const addRunScriptsToPackageJson = (tree: Tree, context: SchematicContext) => {
213218
context.logger.info('Adding NativeScript run scripts to package.json');
219+
214220
const packageJson = getPackageJson(tree);
215221

216222
const scriptsToAdd = {
@@ -237,26 +243,57 @@ const addNativeScriptProjectId = (tree: Tree, context: SchematicContext) => {
237243
}
238244

239245
/**
240-
* Adds {N}-specific extensions
241-
* to the list with excluded files
242-
* in the web TypeScript configuration
246+
* Add web-specific path mappings and files
243247
*/
244-
const excludeNsFilesFromTsconfig = (tree: Tree, context: SchematicContext) => {
245-
context.logger.info('Excluding NativeScript files from web tsconfig');
246-
247-
const nsExtensions = [
248-
`**/*${extensions.ns}.ts`,
249-
'**/*.android.ts',
250-
'**/*.ios.ts',
251-
];
248+
const modifyWebTsconfig = (tree: Tree, context: SchematicContext) => {
249+
context.logger.info('Modifying web tsconfig');
252250

253251
const tsConfigPath = projectSettings.tsConfig;
254252
const tsConfig: any = getJsonFile(tree, tsConfigPath);
255253

256-
tsConfig.exclude = tsConfig.exclude || [];
257-
tsConfig.exclude = [...tsConfig.exclude, ...nsExtensions];
254+
// files
255+
const defaultFiles = ["main.ts", "polyfills.ts"];
256+
tsConfig.files = tsConfig.files || [];
257+
tsConfig.files.push(...defaultFiles);
258+
259+
// paths
260+
const srcDir = projectSettings.sourceRoot;
261+
const webPaths = {
262+
"@src/*": [
263+
`${srcDir}/*.web`,
264+
`${srcDir}/*`]
265+
};
266+
tsConfig.compilerOptions = tsConfig.compilerOptions || {};
267+
tsConfig.compilerOptions.paths = {
268+
...tsConfig.compilerOptions.paths,
269+
...webPaths
270+
}
258271

259272
tree.overwrite(tsConfigPath, JSON.stringify(tsConfig, null, 2));
273+
274+
if (!tsConfig.extends) {
275+
return
276+
}
277+
278+
const baseTsConfigPath = join(dirname(tsConfigPath), tsConfig.extends);
279+
const baseTsConfig: any = getJsonFile(tree, baseTsConfigPath);
280+
281+
const basePaths = {
282+
"@src/*": [
283+
`${srcDir}/*.android.ts`,
284+
`${srcDir}/*.ios.ts`,
285+
`${srcDir}/*.tns.ts`,
286+
`${srcDir}/*.web.ts`,
287+
`${srcDir}/*`]
288+
};
289+
290+
baseTsConfig.compilerOptions = baseTsConfig.compilerOptions || {};
291+
baseTsConfig.compilerOptions.paths = {
292+
...baseTsConfig.compilerOptions.paths,
293+
...basePaths
294+
}
295+
296+
tree.overwrite(baseTsConfigPath, JSON.stringify(baseTsConfig, null, 2));
260297
}
261298

262299
const addDependencies = () => (tree: Tree, context: SchematicContext) => {

0 commit comments

Comments
 (0)