Skip to content

Commit

Permalink
feat(nativescript): 6.3 and scoping updates (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanWalker authored Jan 24, 2020
1 parent f9b9cbf commit dd54238
Show file tree
Hide file tree
Showing 22 changed files with 336 additions and 65 deletions.
8 changes: 7 additions & 1 deletion packages/electron-angular/migrations.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{
"schematics": {}
"schematics": {
"update-to-8.1.2": {
"version": "8.1.2",
"description": "Migrate Electron apps to 7",
"factory": "./migrations/update-8-1-2/update-8-1-2"
}
}
}
83 changes: 83 additions & 0 deletions packages/electron-angular/migrations/update-8-1-2/update-8-1-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {
chain,
Rule,
SchematicContext,
Tree
} from '@angular-devkit/schematics';
import { join } from 'path';
import * as fs from 'fs';
import { updateJsonInTree, createOrUpdate } from '@nrwl/workspace';
import { getJsonFromFile, updateJsonFile, output } from '@nstudio/xplat';
import { electronBuilderVersion, electronInstallerDmgVersion, electronPackagerVersion, electronRebuildVersion, electronReloadVersion, electronStoreVersion, electronUpdaterVersion, electronVersion, npmRunAllVersion, waitOnVersion } from '@nstudio/electron';

function updateElectronApps(tree: Tree, context: SchematicContext) {
const nxConfigPath = `nx.json`;
const nxJson = getJsonFromFile(tree, nxConfigPath);
const npmScope = nxJson.npmScope;

const appsDir = tree.getDir('apps');
const appFolders = appsDir.subdirs;
const cwd = process.cwd();
const indexPath = join(
cwd,
'node_modules/@nstudio/electron-angular/src/schematics/application/_files/src/index.ts__tmpl__'
);
// console.log('webpackConfigPath:', webpackConfigPath);
const indexContent = fs.readFileSync(indexPath, 'UTF-8');
const servicePath = join(
cwd,
'node_modules/@nstudio/electron-angular/src/schematics/xplat/_files/core/services/electron.service.ts__tmpl__'
);
// console.log('webpackConfigPath:', webpackConfigPath);
let electronService = fs.readFileSync(servicePath, 'UTF-8');
electronService = electronService.replace(/<%= npmScope %>/ig, npmScope);

const appsNames = [];
// update electron apps and configs
for (const dir of appFolders) {
// console.log(dir);
if (
dir.indexOf('nativescript-') === 0 ||
dir.indexOf('-nativescript') === 0
) {
const appDir = `${appsDir.path}/${dir}`;
// console.log('appDir:', appDir);
appsNames.push(dir);

createOrUpdate(tree, `${appDir}/src/index.ts`, indexContent);
createOrUpdate(tree, `xplat/electron/core/services/electron.service.ts`, electronService);
}
output.log({
title: 'Migration Note:',
bodyLines: [
`The following Electron apps have been updated to 7: ${appsNames}.`
]
});
}
return tree;
}

function updateRootPackage(tree: Tree, context: SchematicContext) {
return updateJsonInTree('package.json', json => {
json.devDependencies = json.devDependencies || {};
json.devDependencies = {
...json.devDependencies,
electron: electronVersion,
'electron-builder': electronBuilderVersion,
'electron-rebuild': electronRebuildVersion,
'electron-installer-dmg': electronInstallerDmgVersion,
'electron-packager': electronPackagerVersion,
'electron-reload': electronReloadVersion,
'electron-store': electronStoreVersion,
'electron-updater': electronUpdaterVersion,
'npm-run-all': npmRunAllVersion,
'wait-on': waitOnVersion
};

return json;
})(tree, context);
}

export default function(): Rule {
return chain([updateElectronApps, updateRootPackage]);
}
1 change: 1 addition & 0 deletions packages/electron/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './versions';
export * from './xplat';
5 changes: 5 additions & 0 deletions packages/nativescript-angular/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"version": "8.0.13",
"description": "Migrate NativeScript apps to 6.2",
"factory": "./migrations/update-8-0-13/update-8-0-13"
},
"update-to-8.1.2": {
"version": "8.1.2",
"description": "Migrate NativeScript apps to 6.3",
"factory": "./migrations/update-8-1-2/update-8-1-2"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import {
chain,
Rule,
SchematicContext,
Tree
} from '@angular-devkit/schematics';
import { join } from 'path';
import * as fs from 'fs';
import { updateJsonInTree, createOrUpdate } from '@nrwl/workspace';
import { getJsonFromFile, updateJsonFile, output } from '@nstudio/xplat';
import { nsNgVersion, nsCoreVersion, terserWebpackVersion } from '../../src/utils/versions';

function updateNativeScriptApps(tree: Tree, context: SchematicContext) {
const appsDir = tree.getDir('apps');
const appFolders = appsDir.subdirs;
const cwd = process.cwd();
const webpackConfigPath = join(
cwd,
'node_modules/@nstudio/nativescript-angular/src/schematics/application/_files/webpack.config.js'
);
// console.log('webpackConfigPath:', webpackConfigPath);
const webpackConfig = fs.readFileSync(webpackConfigPath, 'UTF-8');
const srcPackagePath = join(
cwd,
'node_modules/@nstudio/nativescript-angular/src/schematics/application/_files/src/package.json'
);
// console.log('webpackConfigPath:', webpackConfigPath);
const srcPackage = fs.readFileSync(srcPackagePath, 'UTF-8');

const appsNames = [];
// update {N} apps and configs
for (const dir of appFolders) {
// console.log(dir);
if (
dir.indexOf('nativescript-') === 0 ||
dir.indexOf('-nativescript') === 0
) {
const appDir = `${appsDir.path}/${dir}`;
// console.log('appDir:', appDir);
appsNames.push(dir);

createOrUpdate(tree, `${appDir}/webpack.config.js`, webpackConfig);
createOrUpdate(tree, `${appDir}/src/package.json`, srcPackage);

// update {N} app deps
const packagePath = `${appDir}/package.json`;
const packageJson = getJsonFromFile(tree, packagePath);

if (packageJson) {
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};
packageJson.devDependencies = {
...packageJson.devDependencies,
'@angular/compiler-cli': '~8.2.0',
'@ngtools/webpack': '~8.3.0',
'nativescript-dev-webpack': '~1.4.0'
};

// console.log('path:',path);
// console.log('packageJson overwrite:', JSON.stringify(packageJson));
tree = updateJsonFile(tree, packagePath, packageJson);
}
}
output.log({
title: 'Migration Note:',
bodyLines: [
`Please ensure you have the latest NativeScript cli installed: npm i -g nativescript`,
`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.`
]
});
}
return tree;
}

function updateRootPackage(tree: Tree, context: SchematicContext) {
return updateJsonInTree('package.json', json => {
json.scripts = json.scripts || {};
json.dependencies = json.dependencies || {};
json.dependencies = {
...json.dependencies,
'nativescript-angular': nsNgVersion,
'tns-core-modules': nsCoreVersion
};
json.devDependencies = json.devDependencies || {};
json.devDependencies = {
...json.devDependencies,
'terser-webpack-plugin': terserWebpackVersion,
'tns-platform-declarations': nsCoreVersion
};

return json;
})(tree, context);
}

export default function(): Rule {
return chain([updateNativeScriptApps, updateRootPackage]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@angular/language-service": "file:<%= pathOffset %>node_modules/@angular/language-service",
"@ngtools/webpack": "~8.3.0",
"codelyzer": "file:<%= pathOffset %>node_modules/codelyzer",
"nativescript-dev-webpack": "~1.3.0",
"nativescript-dev-webpack": "~1.4.0",
"terser-webpack-plugin": "file:<%= pathOffset %>node_modules/terser-webpack-plugin",
"tns-platform-declarations": "file:<%= pathOffset %>node_modules/tns-platform-declarations",
"typescript": "file:<%= pathOffset %>node_modules/typescript"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
],
"@<%= npmScope %>/nativescript/*": [
"<%= pathOffset %>xplat/<%= xplatFolderName %>/*"
]
],
"*": ["./node_modules/*"]
}
},
"files": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ const hashSalt = Date.now().toString();
module.exports = env => {
// Add your custom Activities, Services and other Android app components here.
const appComponents = [
'tns-core-modules/ui/frame',
'tns-core-modules/ui/frame/activity'
"@nativescript/core/ui/frame", "@nativescript/core/ui/frame/activity"
];

const platform = env && ((env.android && 'android') || (env.ios && 'ios'));
Expand Down Expand Up @@ -83,9 +82,9 @@ module.exports = env => {
const entries = { bundle: entryPath };
const areCoreModulesExternal =
Array.isArray(env.externals) &&
env.externals.some(e => e.indexOf('tns-core-modules') > -1);
env.externals.some(e => e.indexOf('@nativescript') > -1);
if (platform === 'ios' && !areCoreModulesExternal) {
entries['tns_modules/tns-core-modules/inspector_modules'] =
entries['tns_modules/@nativescript/core/inspector_modules'] =
'inspector_modules';
}

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

const copyTargets = [
{ from: { glob: 'assets/**' } },
{ from: { glob: 'fonts/**' } }
{ from: { glob: 'fonts/**' } },
// copy monorepo shared assests
// for example:
// { from: '../../../libs/assets/i18n', to: 'assets/i18n' }
Expand Down Expand Up @@ -204,13 +203,15 @@ module.exports = env => {
extensions: ['.ts', '.js', '.scss', '.css'],
// Resolve {N} system modules from tns-core-modules
modules: [
resolve(__dirname, 'node_modules/tns-core-modules'),
resolve(__dirname, 'node_modules/@nativescript/core'),
resolve(__dirname, 'node_modules'),
'node_modules/tns-core-modules',
'node_modules/@nativescript/core',
'node_modules'
],
alias: {
'~': appFullPath
'~': appFullPath,
"tns-core-modules": "@nativescript/core",
"nativescript-angular": "@nativescript/angular"
},
symlinks: true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import {
getJsonFromFile,
updateJsonFile,
getDefaultTemplateOptions,
XplatHelpers
XplatHelpers,
updateTsConfig
} from '@nstudio/xplat';
import { Schema } from './schema';
import { XplatNativeScriptAngularHelpers } from '../../utils';
Expand Down Expand Up @@ -76,6 +77,27 @@ export default function(options: Schema) {
{ interactive: false }
)(tree, context);
},
// adjust root tsconfig
(tree: Tree, context: SchematicContext) => {
return updateTsConfig(tree, (tsConfig: any) => {
if (tsConfig) {
if (!tsConfig.exclude) {
tsConfig.exclude = [];
}
const excludeNSApps = 'apps/nativescript-*';
if (!tsConfig.exclude.includes(excludeNSApps)) {
tsConfig.exclude.push(excludeNSApps);
}
if (!tsConfig.includes) {
tsConfig.includes = [];
}
const platformFiles = 'xplat/**/*.{ios,android}.ts';
if (!tsConfig.includes.includes(platformFiles)) {
tsConfig.includes.push(platformFiles);
}
}
});
},
// add root package dependencies
XplatNativeScriptAngularHelpers.updateRootDeps(options),
XplatNativeScriptHelpers.updatePrettierIgnore(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { NgModule, Optional, SkipSelf } from '@angular/core';

// nativescript
import { NativeScriptModule } from 'nativescript-angular/nativescript.module';
import { NativeScriptHttpClientModule } from 'nativescript-angular/http-client';
import { device } from 'tns-core-modules/platform';
import { NativeScriptModule, NativeScriptHttpClientModule } from '@nativescript/angular';
import { Device } from '@nativescript/core';
import { TNSFontIconModule } from 'nativescript-ngx-fonticon';

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

// factories
export function platformLangFactory() {
return device.language;
return Device.language;
}

export function createTranslateLoader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
import { Injectable, Inject, NgZone } from '@angular/core';

// nativescript
import * as tnsApp from 'tns-core-modules/application';
import * as tnsUtils from 'tns-core-modules/utils/utils';
import { device, isIOS, isAndroid } from 'tns-core-modules/platform';
import { DeviceOrientation } from 'tns-core-modules/ui/enums';
import { Device, isIOS, Application } from '@nativescript/core';
import { DeviceOrientation } from '@nativescript/core/ui/enums';

// libs
import { TranslateService } from '@ngx-translate/core';
Expand Down Expand Up @@ -69,13 +67,13 @@ export class AppService {
let versionName: string;
let buildNumber: string;

if (tnsApp.android) {
const pi = tnsApp.android.context
if (Application.android) {
const pi = Application.android.context
.getPackageManager()
.getPackageInfo(tnsApp.android.context.getPackageName(), 0);
.getPackageInfo(Application.android.context.getPackageName(), 0);
versionName = pi.versionName;
buildNumber = pi.versionCode.toString();
} else if (tnsApp.ios) {
} else if (Application.ios) {
versionName = NSBundle.mainBundle.objectForInfoDictionaryKey(
'CFBundleShortVersionString'
);
Expand All @@ -93,16 +91,16 @@ export class AppService {

private _initAppEvents() {
// For the future - may want to use these
tnsApp.on(tnsApp.resumeEvent, () => {
this._log.debug('tnsApp.resumeEvent');
Application.on(Application.resumeEvent, () => {
this._log.debug('app resume.');
});
tnsApp.on(tnsApp.suspendEvent, () => {
this._log.debug('tnsApp.suspendEvent');
Application.on(Application.suspendEvent, () => {
this._log.debug('app suspend.');
});
}

private _initOrientation() {
this._deviceType = device.deviceType;
this._deviceType = Device.deviceType;
this._log.debug('deviceType:', this._deviceType);
this._log.debug('initializing orientation handling.');

Expand All @@ -112,7 +110,7 @@ export class AppService {
this._log.debug('current orientation:', this.orientation);

// handle orientation changes
tnsApp.on(tnsApp.orientationChangedEvent, e => {
Application.on(Application.orientationChangedEvent, e => {
// sometimes e.newValue will be undefined, ignore those
if (e.newValue && this.orientation !== e.newValue) {
orientation = getOrientation();
Expand Down
Loading

0 comments on commit dd54238

Please sign in to comment.