Skip to content

Commit dd54238

Browse files
authored
feat(nativescript): 6.3 and scoping updates (#177)
1 parent f9b9cbf commit dd54238

File tree

22 files changed

+336
-65
lines changed

22 files changed

+336
-65
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
{
2-
"schematics": {}
2+
"schematics": {
3+
"update-to-8.1.2": {
4+
"version": "8.1.2",
5+
"description": "Migrate Electron apps to 7",
6+
"factory": "./migrations/update-8-1-2/update-8-1-2"
7+
}
8+
}
39
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import {
2+
chain,
3+
Rule,
4+
SchematicContext,
5+
Tree
6+
} from '@angular-devkit/schematics';
7+
import { join } from 'path';
8+
import * as fs from 'fs';
9+
import { updateJsonInTree, createOrUpdate } from '@nrwl/workspace';
10+
import { getJsonFromFile, updateJsonFile, output } from '@nstudio/xplat';
11+
import { electronBuilderVersion, electronInstallerDmgVersion, electronPackagerVersion, electronRebuildVersion, electronReloadVersion, electronStoreVersion, electronUpdaterVersion, electronVersion, npmRunAllVersion, waitOnVersion } from '@nstudio/electron';
12+
13+
function updateElectronApps(tree: Tree, context: SchematicContext) {
14+
const nxConfigPath = `nx.json`;
15+
const nxJson = getJsonFromFile(tree, nxConfigPath);
16+
const npmScope = nxJson.npmScope;
17+
18+
const appsDir = tree.getDir('apps');
19+
const appFolders = appsDir.subdirs;
20+
const cwd = process.cwd();
21+
const indexPath = join(
22+
cwd,
23+
'node_modules/@nstudio/electron-angular/src/schematics/application/_files/src/index.ts__tmpl__'
24+
);
25+
// console.log('webpackConfigPath:', webpackConfigPath);
26+
const indexContent = fs.readFileSync(indexPath, 'UTF-8');
27+
const servicePath = join(
28+
cwd,
29+
'node_modules/@nstudio/electron-angular/src/schematics/xplat/_files/core/services/electron.service.ts__tmpl__'
30+
);
31+
// console.log('webpackConfigPath:', webpackConfigPath);
32+
let electronService = fs.readFileSync(servicePath, 'UTF-8');
33+
electronService = electronService.replace(/<%= npmScope %>/ig, npmScope);
34+
35+
const appsNames = [];
36+
// update electron apps and configs
37+
for (const dir of appFolders) {
38+
// console.log(dir);
39+
if (
40+
dir.indexOf('nativescript-') === 0 ||
41+
dir.indexOf('-nativescript') === 0
42+
) {
43+
const appDir = `${appsDir.path}/${dir}`;
44+
// console.log('appDir:', appDir);
45+
appsNames.push(dir);
46+
47+
createOrUpdate(tree, `${appDir}/src/index.ts`, indexContent);
48+
createOrUpdate(tree, `xplat/electron/core/services/electron.service.ts`, electronService);
49+
}
50+
output.log({
51+
title: 'Migration Note:',
52+
bodyLines: [
53+
`The following Electron apps have been updated to 7: ${appsNames}.`
54+
]
55+
});
56+
}
57+
return tree;
58+
}
59+
60+
function updateRootPackage(tree: Tree, context: SchematicContext) {
61+
return updateJsonInTree('package.json', json => {
62+
json.devDependencies = json.devDependencies || {};
63+
json.devDependencies = {
64+
...json.devDependencies,
65+
electron: electronVersion,
66+
'electron-builder': electronBuilderVersion,
67+
'electron-rebuild': electronRebuildVersion,
68+
'electron-installer-dmg': electronInstallerDmgVersion,
69+
'electron-packager': electronPackagerVersion,
70+
'electron-reload': electronReloadVersion,
71+
'electron-store': electronStoreVersion,
72+
'electron-updater': electronUpdaterVersion,
73+
'npm-run-all': npmRunAllVersion,
74+
'wait-on': waitOnVersion
75+
};
76+
77+
return json;
78+
})(tree, context);
79+
}
80+
81+
export default function(): Rule {
82+
return chain([updateElectronApps, updateRootPackage]);
83+
}

packages/electron/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from './versions';
12
export * from './xplat';

packages/nativescript-angular/migrations.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
"version": "8.0.13",
55
"description": "Migrate NativeScript apps to 6.2",
66
"factory": "./migrations/update-8-0-13/update-8-0-13"
7+
},
8+
"update-to-8.1.2": {
9+
"version": "8.1.2",
10+
"description": "Migrate NativeScript apps to 6.3",
11+
"factory": "./migrations/update-8-1-2/update-8-1-2"
712
}
813
}
914
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import {
2+
chain,
3+
Rule,
4+
SchematicContext,
5+
Tree
6+
} from '@angular-devkit/schematics';
7+
import { join } from 'path';
8+
import * as fs from 'fs';
9+
import { updateJsonInTree, createOrUpdate } from '@nrwl/workspace';
10+
import { getJsonFromFile, updateJsonFile, output } from '@nstudio/xplat';
11+
import { nsNgVersion, nsCoreVersion, terserWebpackVersion } from '../../src/utils/versions';
12+
13+
function updateNativeScriptApps(tree: Tree, context: SchematicContext) {
14+
const appsDir = tree.getDir('apps');
15+
const appFolders = appsDir.subdirs;
16+
const cwd = process.cwd();
17+
const webpackConfigPath = join(
18+
cwd,
19+
'node_modules/@nstudio/nativescript-angular/src/schematics/application/_files/webpack.config.js'
20+
);
21+
// console.log('webpackConfigPath:', webpackConfigPath);
22+
const webpackConfig = fs.readFileSync(webpackConfigPath, 'UTF-8');
23+
const srcPackagePath = join(
24+
cwd,
25+
'node_modules/@nstudio/nativescript-angular/src/schematics/application/_files/src/package.json'
26+
);
27+
// console.log('webpackConfigPath:', webpackConfigPath);
28+
const srcPackage = fs.readFileSync(srcPackagePath, 'UTF-8');
29+
30+
const appsNames = [];
31+
// update {N} apps and configs
32+
for (const dir of appFolders) {
33+
// console.log(dir);
34+
if (
35+
dir.indexOf('nativescript-') === 0 ||
36+
dir.indexOf('-nativescript') === 0
37+
) {
38+
const appDir = `${appsDir.path}/${dir}`;
39+
// console.log('appDir:', appDir);
40+
appsNames.push(dir);
41+
42+
createOrUpdate(tree, `${appDir}/webpack.config.js`, webpackConfig);
43+
createOrUpdate(tree, `${appDir}/src/package.json`, srcPackage);
44+
45+
// update {N} app deps
46+
const packagePath = `${appDir}/package.json`;
47+
const packageJson = getJsonFromFile(tree, packagePath);
48+
49+
if (packageJson) {
50+
packageJson.dependencies = packageJson.dependencies || {};
51+
packageJson.devDependencies = packageJson.devDependencies || {};
52+
packageJson.devDependencies = {
53+
...packageJson.devDependencies,
54+
'@angular/compiler-cli': '~8.2.0',
55+
'@ngtools/webpack': '~8.3.0',
56+
'nativescript-dev-webpack': '~1.4.0'
57+
};
58+
59+
// console.log('path:',path);
60+
// console.log('packageJson overwrite:', JSON.stringify(packageJson));
61+
tree = updateJsonFile(tree, packagePath, packageJson);
62+
}
63+
}
64+
output.log({
65+
title: 'Migration Note:',
66+
bodyLines: [
67+
`Please ensure you have the latest NativeScript cli installed: npm i -g nativescript`,
68+
`The following NativeScript apps have been updated to 6.3: ${appsNames}. The following files in those apps have been updated: webpack.config.js, src/package.json, and package.json. You may want to check the changeset to keep any customizations you may have made.`
69+
]
70+
});
71+
}
72+
return tree;
73+
}
74+
75+
function updateRootPackage(tree: Tree, context: SchematicContext) {
76+
return updateJsonInTree('package.json', json => {
77+
json.scripts = json.scripts || {};
78+
json.dependencies = json.dependencies || {};
79+
json.dependencies = {
80+
...json.dependencies,
81+
'nativescript-angular': nsNgVersion,
82+
'tns-core-modules': nsCoreVersion
83+
};
84+
json.devDependencies = json.devDependencies || {};
85+
json.devDependencies = {
86+
...json.devDependencies,
87+
'terser-webpack-plugin': terserWebpackVersion,
88+
'tns-platform-declarations': nsCoreVersion
89+
};
90+
91+
return json;
92+
})(tree, context);
93+
}
94+
95+
export default function(): Rule {
96+
return chain([updateNativeScriptApps, updateRootPackage]);
97+
}

packages/nativescript-angular/src/schematics/application/_files/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"@angular/language-service": "file:<%= pathOffset %>node_modules/@angular/language-service",
4343
"@ngtools/webpack": "~8.3.0",
4444
"codelyzer": "file:<%= pathOffset %>node_modules/codelyzer",
45-
"nativescript-dev-webpack": "~1.3.0",
45+
"nativescript-dev-webpack": "~1.4.0",
4646
"terser-webpack-plugin": "file:<%= pathOffset %>node_modules/terser-webpack-plugin",
4747
"tns-platform-declarations": "file:<%= pathOffset %>node_modules/tns-platform-declarations",
4848
"typescript": "file:<%= pathOffset %>node_modules/typescript"

packages/nativescript-angular/src/schematics/application/_files/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
],
2121
"@<%= npmScope %>/nativescript/*": [
2222
"<%= pathOffset %>xplat/<%= xplatFolderName %>/*"
23-
]
23+
],
24+
"*": ["./node_modules/*"]
2425
}
2526
},
2627
"files": [

packages/nativescript-angular/src/schematics/application/_files/webpack.config.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ const hashSalt = Date.now().toString();
3030
module.exports = env => {
3131
// Add your custom Activities, Services and other Android app components here.
3232
const appComponents = [
33-
'tns-core-modules/ui/frame',
34-
'tns-core-modules/ui/frame/activity'
33+
"@nativescript/core/ui/frame", "@nativescript/core/ui/frame/activity"
3534
];
3635

3736
const platform = env && ((env.android && 'android') || (env.ios && 'ios'));
@@ -83,9 +82,9 @@ module.exports = env => {
8382
const entries = { bundle: entryPath };
8483
const areCoreModulesExternal =
8584
Array.isArray(env.externals) &&
86-
env.externals.some(e => e.indexOf('tns-core-modules') > -1);
85+
env.externals.some(e => e.indexOf('@nativescript') > -1);
8786
if (platform === 'ios' && !areCoreModulesExternal) {
88-
entries['tns_modules/tns-core-modules/inspector_modules'] =
87+
entries['tns_modules/@nativescript/core/inspector_modules'] =
8988
'inspector_modules';
9089
}
9190

@@ -97,7 +96,7 @@ module.exports = env => {
9796

9897
const copyTargets = [
9998
{ from: { glob: 'assets/**' } },
100-
{ from: { glob: 'fonts/**' } }
99+
{ from: { glob: 'fonts/**' } },
101100
// copy monorepo shared assests
102101
// for example:
103102
// { from: '../../../libs/assets/i18n', to: 'assets/i18n' }
@@ -204,13 +203,15 @@ module.exports = env => {
204203
extensions: ['.ts', '.js', '.scss', '.css'],
205204
// Resolve {N} system modules from tns-core-modules
206205
modules: [
207-
resolve(__dirname, 'node_modules/tns-core-modules'),
206+
resolve(__dirname, 'node_modules/@nativescript/core'),
208207
resolve(__dirname, 'node_modules'),
209-
'node_modules/tns-core-modules',
208+
'node_modules/@nativescript/core',
210209
'node_modules'
211210
],
212211
alias: {
213-
'~': appFullPath
212+
'~': appFullPath,
213+
"tns-core-modules": "@nativescript/core",
214+
"nativescript-angular": "@nativescript/angular"
214215
},
215216
symlinks: true
216217
},

packages/nativescript-angular/src/schematics/application/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import {
2525
getJsonFromFile,
2626
updateJsonFile,
2727
getDefaultTemplateOptions,
28-
XplatHelpers
28+
XplatHelpers,
29+
updateTsConfig
2930
} from '@nstudio/xplat';
3031
import { Schema } from './schema';
3132
import { XplatNativeScriptAngularHelpers } from '../../utils';
@@ -76,6 +77,27 @@ export default function(options: Schema) {
7677
{ interactive: false }
7778
)(tree, context);
7879
},
80+
// adjust root tsconfig
81+
(tree: Tree, context: SchematicContext) => {
82+
return updateTsConfig(tree, (tsConfig: any) => {
83+
if (tsConfig) {
84+
if (!tsConfig.exclude) {
85+
tsConfig.exclude = [];
86+
}
87+
const excludeNSApps = 'apps/nativescript-*';
88+
if (!tsConfig.exclude.includes(excludeNSApps)) {
89+
tsConfig.exclude.push(excludeNSApps);
90+
}
91+
if (!tsConfig.includes) {
92+
tsConfig.includes = [];
93+
}
94+
const platformFiles = 'xplat/**/*.{ios,android}.ts';
95+
if (!tsConfig.includes.includes(platformFiles)) {
96+
tsConfig.includes.push(platformFiles);
97+
}
98+
}
99+
});
100+
},
79101
// add root package dependencies
80102
XplatNativeScriptAngularHelpers.updateRootDeps(options),
81103
XplatNativeScriptHelpers.updatePrettierIgnore(),

packages/nativescript-angular/src/schematics/xplat/_files/core/core.module.ts__tmpl__

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { NgModule, Optional, SkipSelf } from '@angular/core';
22

33
// nativescript
4-
import { NativeScriptModule } from 'nativescript-angular/nativescript.module';
5-
import { NativeScriptHttpClientModule } from 'nativescript-angular/http-client';
6-
import { device } from 'tns-core-modules/platform';
4+
import { NativeScriptModule, NativeScriptHttpClientModule } from '@nativescript/angular';
5+
import { Device } from '@nativescript/core';
76
import { TNSFontIconModule } from 'nativescript-ngx-fonticon';
87

98
// libs
@@ -18,7 +17,7 @@ import { TNSTranslateLoader } from './services/tns-translate.loader';
1817

1918
// factories
2019
export function platformLangFactory() {
21-
return device.language;
20+
return Device.language;
2221
}
2322

2423
export function createTranslateLoader() {

0 commit comments

Comments
 (0)