-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(generators): {N} HMR livesync routing navigation restore (#52)
- Loading branch information
1 parent
d072e19
commit f975eeb
Showing
5 changed files
with
118 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { | ||
chain, | ||
Rule, | ||
SchematicContext, | ||
Tree | ||
} from "@angular-devkit/schematics"; | ||
import { join } from 'path'; | ||
import * as fs from 'fs'; | ||
|
||
import { getJsonFromFile, updateJsonFile, createOrUpdate, updateJsonInTree } from '../../utils'; | ||
|
||
function updateNativeScriptApps(tree: Tree, context: SchematicContext) { | ||
const appsDir = tree.getDir("apps"); | ||
const appFolders = appsDir.subdirs; | ||
const nxConfigPath = `nx.json`; | ||
const nxJson = getJsonFromFile(tree, nxConfigPath); | ||
const npmScope = nxJson.npmScope; | ||
const cwd = process.cwd(); | ||
// console.log('webpackConfig:',webpackConfig); | ||
const mainPath = join(cwd, 'node_modules/@nstudio/schematics/src/app.nativescript/_files/app/main.ts'); | ||
// console.log('webpackConfigPath:', webpackConfigPath); | ||
let mainFile = fs.readFileSync(mainPath, 'UTF-8'); | ||
mainFile = mainFile.replace('<%= npmScope %>', npmScope); | ||
|
||
const hmrNavigationPath = join(cwd, 'node_modules/@nstudio/schematics/src/xplat/_nativescript_files/utils/livesync-navigation.ts'); | ||
let hmrNavigation = fs.readFileSync(hmrNavigationPath, 'UTF-8'); | ||
|
||
// update {N} apps and configs | ||
for (const dir of appFolders) { | ||
// console.log(dir); | ||
if (dir.indexOf("nativescript-") === 0) { | ||
const appDir = `${appsDir.path}/${dir}`; | ||
// console.log('appDir:', appDir); | ||
|
||
createOrUpdate( | ||
tree, | ||
`${appDir}/app/main.ts`, | ||
mainFile | ||
); | ||
} | ||
} | ||
|
||
if (tree.exists('xplat/nativescript')) { | ||
createOrUpdate( | ||
tree, | ||
`xplat/nativescript/utils/livesync-navigation.ts`, | ||
hmrNavigation | ||
); | ||
|
||
const utilsTreePath = 'xplat/nativescript/utils/index.ts'; | ||
const utilsFullPath = join(cwd, utilsTreePath); | ||
let utilsIndex; | ||
if (fs.existsSync(utilsFullPath)) { | ||
utilsIndex = fs.readFileSync(utilsFullPath, 'UTF-8'); | ||
utilsIndex += `\nexport * from './livesync-navigation';`; | ||
createOrUpdate( | ||
tree, | ||
utilsTreePath, | ||
utilsIndex | ||
); | ||
} | ||
} | ||
return tree; | ||
} | ||
|
||
export default function(): Rule { | ||
return chain([updateNativeScriptApps]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './icons'; | ||
export * from './icons'; | ||
export * from './livesync-navigation'; |
30 changes: 30 additions & 0 deletions
30
src/xplat/_nativescript_files/utils/livesync-navigation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { NgZone } from '@angular/core'; | ||
import { Router } from '@angular/router'; | ||
import { onBeforeLivesync, onAfterLivesync } from 'nativescript-angular/platform-common'; | ||
import { RouterExtensions } from 'nativescript-angular/router'; | ||
|
||
let cachedUrl: string; | ||
export function attachLivesyncNavigation() { | ||
onBeforeLivesync.subscribe(moduleRef => { | ||
console.log('#### onBeforeLivesync'); | ||
if (moduleRef) { | ||
const router = <Router>moduleRef.injector.get(Router); | ||
cachedUrl = router.url; | ||
console.log('-------> Caching URL: ' + cachedUrl); | ||
} | ||
}); | ||
|
||
onAfterLivesync.subscribe(({ moduleRef, error }) => { | ||
console.log(`#### onAfterLivesync moduleRef: ${moduleRef} error: ${error}`); | ||
if (moduleRef) { | ||
const router = <RouterExtensions>moduleRef.injector.get(RouterExtensions); | ||
const ngZone = <NgZone>moduleRef.injector.get(NgZone); | ||
if (router && cachedUrl) { | ||
ngZone.run(() => { | ||
router.navigateByUrl(cachedUrl, { animated: false }); | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
|