Fatme
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Migrating hooks to NativeScript 6.0 As you probably know, NativeScript 6.0 release will [support only bundle workflow](https://www.nativescript.org/blog/the-future-of-building-nativescript-apps) and there will be no other way to run your applications. We're dropping the legacy workflow for building the applications which is a breaking change that alters the hooks mechanism in NativeScript CLI. But… What are NativeScript hooks? # NativeScript hooks in short NativeScript hooks are executable pieces of code or Node.js scripts which can be added by application or plugin developers in order to customize the execution of particular NativeScript commands. They give you the power to perform special activities by plugging in different parts of the build process of the application. NativeScript CLI supports two different ways of executing hooks: * In-process execution * Spawned execution via Node.js spawn function ### In-process execution of hooks In-process execution is available only for JavaScript hooks. NativeScript CLI determinates if the hook should be executed `in-process` based on the `module.exports` statement. So in order to enable in-process execution all you need to have is `module.exports =` ... statement in the hook. For example, if the hook script is: ```typescript module.exports = function($logger) { } ``` Then, the hook script will be require'd by the CLI and the exported function will be called through the injector. The `in-process` execution gives you the flexibility to use any available service from NativeScript CLI. ### spawned execution of hooks If NativeScript CLI cannot find `module.exports = statement`, it will execute the hook using Node.js [childProcess.spawn](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) function. Using this approach you’re unable to use any of the services from NativeScript CLI. ### hookArgs NativeScript CLI supports a special argument named `hookArgs`. The `hookArgs` is an object containing all the arguments passed to the hooked method. For example, let's say NativeScript CLI has a method `checkForChanges` with the following definition: ```typescript= @hook("checkForChanges") public async checkForChanges(platformData: IPlatformData, projectData: IProjectData, prepareData: IPrepareData) { } ``` Then,`hookArgs` will have the following structure: ```json= hookArgs: { platformData, projectData, prepareData } ``` > NOTE: `hookArgs` object is different for each hook. # Do I need to migrate? Let's see what you, as application developer or plugin developer, need to do to migrate your hooks to NativeScript 6.0. If your application or plugin doesn't use hooks, there is no need to migrate and no additional actions are required. You can stop reading now. If your application or plugin has hooks, you need to check if your hooks are affected from the changes for NativeScript 6.0. # What are exactly the changes in NativeScript hooks? NativeScript CLI 6.0 will NOT change the way the hooks are loaded, processed and executed althought the entire NativeScript CLI was refactored. Here are the main changes around the hooks: * The hooks workflow * The structure of `hookArgs` * The names of injected services from NativeScript CLI > NOTE: The changes about the structure of hookArgs and the names of injected services affects only `in-process` hooks. You can safely skip the next two sections in the following cases: 1. You're an application developer and all your hooks are from plugins. In this case you should update to the latest version of the plugins and log an issue in plugin's repo if there is a plugin that is still not compatible with CLI 6.0. 2. You're an application developer and all your custom hooks are missing the `module.exports = ` statement e.g. the hooks will NOT be executed `in-process`. 3. You're a plugin developer and all your hooks are marked with `{ inject: false }` inside `package.json` of the plugin e.g. the hooks will NOT be executed `in-process`. # The hooks workflow The hooks mechanism in CLI is designed to allow executing of additional actions before and after some specific parts of CLI's code is executed. In case you return concrete value or Promise, the hook is considered as before/after hook. The hook below will be executed as a before/after hook: ```typescript module.exports = function($logger) { return new Promise((resolve, reject) => { $logger.info("Executed before/after some CLI action/function."); }); } ``` Here are all hooks supported by NativeScript CLI: ## prepare This hook is executed exactly before/after NativeScript CLI starts the webpack compilation and the watcher for native files e.g. the actual preparation of the application. * Using legacy workflow it is executed on initial sync of the application and on every change when NativeScript CLI is started in watch mode. * In CLI 6.0 the hook is executed only on initial sync of the application. ## checkForChanges The hook is executed before/after NativeScript CLI checks if there are changes in the application. NativeScript CLI keeps a state of application and based on this state decides if the application should be rebuilt, reinstalled or restarted on device. The hook is executed before prepare hooks using legacy workflow, but will be executed after it with CLI 6.0. * Using legacy workflow this hook is executed on initial sync of the application and on every change when NativeScript CLI is started in watch mode. * In CLI 6.0 this hook is executed on initial sync of the application. NativeScript CLI checks if there are changed native files from the last execution. Native files are all files from App_Resources and from platforms folders of the nativescript's plugins. NativeScript CLI rebuilds the application when there are changed native files. ## before-preview-sync When preview command is executed, NativeScript CLI shows a QR code. This hook is executed when the QR code is scanned with device and the device is reported to the CLI. ## watchPatterns This hook is executed exactly before NativeScript CLI tries to determine the glob patterns for watching the application. The purpose here is that you can modify the patterns in case you want to watch additional directories or exclude directories from watch. ## before-watch This hook is executed exactly before NativeScript CLI starts the watch of the project directory. ## after-watch This hook is executed when NativeScript CLI stops watching the project directory. This can happen in the following cases: * When you press Ctrl+C in the terminal where NativeScipt CLI is running * When all devices for the LiveSync process are disconnected * When there is a problem with the LiveSync process and NativeScript CLI is unable to livesync the changes. ## after-createProject This hook is executed after creating the project from template. ## buildAndroidPlugin This hook is executed before/after building the .aar of android plugin. ## install This hook is executed before/after the application is installed on connected device or emulator/simulator. ## shouldPrepare This hook is executed before/after NativeScript CLI had decided if it needs to prepare any part of the application. The hook is deleted as from NativeScript 6.0. ## cleanApp This hook is executed before/after NativeScript CLI cleans the app directory in the platforms directory. It is deleted as from NativeScript 6.0. # The structure of hookArgs The `hookArgs` is an object containing all the arguments of the hooked method. More info can be found [here]() > NOTE: `hookArgs` object is different for each hook. For example, let's say we have `checkForChanges` hook in our plugin or application. Currently the structure of `hookArgs` for `checkForChanges` hook is the following: ```json= { checkForChangesOpts: { platform, projectData, projectChangesOptions: { nativePlatformStatus, provision, teamId, bundle, release } } } ``` With the upcoming 6.0 release the above structure will be changed to the following: ```json= { projectData, platformData, prepareData } ``` Let's see the case when the hook uses platform property from `hookArgs`: ```javascript= module.exports = function(hookArgs) { const platform = hookArgs.checkForChangesOpts.platform; console.log(`The hook will be executed for ${platform} platform.`); } ``` For the NativeScript 6.0 the above code have to be changed to: ```javascript= module.exports = function(hookArgs) { const platform = hookArgs.prepareData.platform; console.log(`The hook will be executed for platform ${platform}.`); } ``` You can easly make it backwards and forwards compatible: ```javascript= module.exports = function(hookArgs) { const platform = (hookArgs.checkForChangesOpts && hookArgs.checkForChangesOpts.platform) || (hookArgs.prepareData && hookArgs.prepareData.platform); } ``` We've already migrated the following hooks and make them backward and forward compatible: * [nativescript-vue](https://github.com/nativescript-vue/nativescript-vue/tree/master/platform/nativescript/hooks) * [nativescript-babel](https://github.com/NativeScript/nativescript-dev-babel/blob/master/lib/after-prepare.js) * [nativescript-unit-test-runner](https://github.com/NativeScript/nativescript-unit-test-runner/blob/master/lib/after-prepare.ts) * [nativescript-plugin-firebase](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/tree/master/src/scripts) * [kinvey-nativescript-sdk](https://github.com/Kinvey/js-sdk/tree/master/packages/nativescript-sdk/nativescript-hook-scripts) Here is a full mapping for all hooks that NativeScript CLI supports: <table width="0"> <tr> <th></th> <th>HookArgs prior 6.0</th> <th>HookArgs for 6.0</th> <th>Status</th> </tr> <tr> <td> prepare </td> <td> { config } </td> <td> { prepareData } </td> <td> <span style="color:orange"> CHANGED </span> <br /> More info can be found <a href="https://hackmd.io/O6PlaaA3RY23B04SJm7qPA?both#prepare">here.</a></td> </tr> <tr> <td> checkForChanges </td> <td> { platform, projectData, projectChangesOptions } </td> <td> { platformData, projectData, prepareData } </td> <td> <span style="color:orange"> CHANGED </span> <br /> More info can be found <a href="https://hackmd.io/O6PlaaA3RY23B04SJm7qPA?both#checkForChanges">here.</a></td> </tr> <tr> <td> before-preview-sync </td> <td> { projectData, hmrData, config, externals } </td> <td> { platform, projectDir, useHotModuleReload, env } </td> <td> <span style="color:orange"> CHANGED </span> <br /> More info can be found <a href="https://hackmd.io/O6PlaaA3RY23B04SJm7qPA?both#watch">here</a></td> </tr> <tr> <td> watchPatterns </td> <td> { liveSyncData, projectData } </td> <td> { platformData, projectData } </td> <td> <span style="color:orange"> CHANGED </span> <br /> More info can be found <a href="https://hackmd.io/O6PlaaA3RY23B04SJm7qPA?both#watchPatterns">here</a></td> </tr> <tr> <td> before-watch </td> <td> { projectData, config, filesToSync, filesToRemove, hmrData } </td> <td> { platformData, projectData, prepareData } </td> <td> <span style="color:orange"> CHANGED </span> <br /> More info can be found <a href="https://hackmd.io/O6PlaaA3RY23B04SJm7qPA?both#watch">here</a></td> </tr> <tr> <td> after-watch </td> <td colspan="2"> { projectData } </td> <td> <span style="color:green"> NOT CHANGED </span> <br /> More info can be found <a href="https://hackmd.io/O6PlaaA3RY23B04SJm7qPA?both#watch">here.</a></td> </tr> <tr> <td> after-createProject </td> <td colspan="2"> { projectCreationSettings } </td> <td> <span style="color:green"> NOT CHANGED </span> <br /> More info can be found <a href="https://hackmd.io/O6PlaaA3RY23B04SJm7qPA?both#after-create">here.</a></td> </tr> <tr> <td> beforeAndroidPlugin </td> <td colspan="2"> { pluginBuildSettings } </td> <td> <span style="color:green"> NOT CHANGED </span> <br /> More info can be found <a href="https://hackmd.io/O6PlaaA3RY23B04SJm7qPA?both#beforeAndroidPlugin">here.</a></td> </tr> <tr> <td> install </td> <td colspan="2"> { packageFilePath, appIdentifier } </td> <td> <span style="color:green"> NOT CHANGED </span> <br /> More info can be found <a href="https://hackmd.io/O6PlaaA3RY23B04SJm7qPA?both#install">here.</a></td> </tr> <tr> <td> shouldPrepare </td> <td> { shouldPrepareInfo } </td> <td> - </td> <td> <span style="color:red"> HOOK DELETED </span> <br /> More info can be found <a href="https://hackmd.io/O6PlaaA3RY23B04SJm7qPA?both#shouldPrepare">here.</a></td> </tr> <tr> <td> cleanApp </td> <td> { platformInfo } </td> <td> - </td> <td> <span style="color:red"> HOOK DELETED </span> <br /> More info can be found <a href="https://hackmd.io/O6PlaaA3RY23B04SJm7qPA?both#cleanApp">here.</a></td> </tr> </table> > NOTE: If your hook is in category <span style="color: orange"> CHANGED </span> and uses `hookArgs` you need to migrate the hook. > NOTE: If your hook is in category <span style="color: green"> NOT CHANGED </span> you still have to check the names of injected services from within the hook. > NOTE: If your hook is in category <span style="color: red"> DELETED </span> you can open an issue and describe what exactly is your scenario, so we can help you to find the appropriate hook for your case. # Injected services from NativeScript CLI NativeScript CLI provides a very powerfull `in-process` execution of hooks that allow you to use any registered services from NativeScript CLI available in injector. A full list of can be found [here](https://github.com/NativeScript/nativescript-cli/blob/master/lib/bootstrap.ts). The current resolution mechanism works based on the names, not the object types. In case the injected service is deleted or renamed from NativeScript CLI and the hook is not migrated according to that change, NativeScript CLI will not execute the hook and will show a warning. Let's see a hook with the following injected services: ```javascript= module.exports = function($logger, $platformsData, $projectData, hookArgs) { } ``` The above hook will not be executed with NativeScript CLI 6.0, as it will not be able to resolve `$platformsData` due to the fact that `$platformsData` is renamed to `$platformsDataService`. > NOTE: NativeScript CLI will show the following warning: <hookName> will NOT be executed because it has invalid arguments - $platformsData. In such a case, we need to migrate the hook: ```javascript= module.exports = function($logger, $projectData, $injector, hookArgs) { const platformsData = getPlatformsData($injector); } function getPlatformsData($injector) { try { return $injector.resolve("platformsData"); } catch (err) { return $injector.resolve("platformsDataService"); } } ``` Here is a mapping with the most frequently injected services from NativeScript CLI: | Service name prior 6.0 | Replacement in 6.0 | | -------- | -------- | -------- | | $platformsData | $platformsDataService | | $liveSyncService | $runController | | $platformService | $runController | | $projectData | $projectData | | $logger | $logger | > NOTE: `$logger.out` method is deleted in NativeScript 6.0 and is replaced with `$logger.info`. # Summary That's all changes around hooks for NativeScript 6.0 release. We encourage you to migrate your hooks and make them backwards compatible. This way, in case you are a plugin author, other developers will be able to use your plugins with both CLI 5.4 and 6.0 and you'll not force them to upgrade their existing applications. Handling breaking changes can be annoying, but we are here and will help you to make the transition smoother. Just [create a new issue](https://github.com/NativeScript/nativescript-cli/issues) discribing your problem and we'll assist you to find a solution!

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully