|
| 1 | +Public API |
| 2 | +== |
| 3 | + |
| 4 | +This document describes all methods that can be invoked when NativeScript CLI is required as library, i.e. |
| 5 | + |
| 6 | +<table> |
| 7 | + <tr> |
| 8 | + <td> |
| 9 | + JavaScript |
| 10 | + </td> |
| 11 | + <td> |
| 12 | + TypeScript |
| 13 | + </td> |
| 14 | + </tr> |
| 15 | + <tr> |
| 16 | + <td> |
| 17 | +<pre lang="javascript"> |
| 18 | +const tns = require("nativescript"); |
| 19 | +</pre> |
| 20 | + </td> |
| 21 | + <td> |
| 22 | +<pre lang="typescript"> |
| 23 | +import * as tns from "nativescript"; |
| 24 | +</pre> |
| 25 | + </td> |
| 26 | + </tr> |
| 27 | + |
| 28 | +</table> |
| 29 | + |
| 30 | +## Module projectService |
| 31 | + |
| 32 | +`projectService` modules allow you to create new NativeScript application. |
| 33 | + |
| 34 | +* `createProject(projectSettings: IProjectSettings): Promise<void>` - Creates new NativeScript application. By passing `projectSettings` argument you specify the name of the application, the template that will be used, etc.: |
| 35 | +```TypeScript |
| 36 | +/** |
| 37 | + * Describes available settings when creating new NativeScript application. |
| 38 | + */ |
| 39 | +interface IProjectSettings { |
| 40 | + /** |
| 41 | + * Name of the newly created application. |
| 42 | + */ |
| 43 | + projectName: string; |
| 44 | + |
| 45 | + /** |
| 46 | + * Selected template from which to create the project. If not specified, defaults to hello-world template. |
| 47 | + * Template can be any npm package, local dir, github url, .tgz file. |
| 48 | + * If it is set to `angular` or `ng`, default NativeScript Angular Hello World template will be used. |
| 49 | + * If it is set to `typescript` or `tsc`, default NativeScript TypeScript Hello World template will be used. |
| 50 | + */ |
| 51 | + template?: string; |
| 52 | + |
| 53 | + /** |
| 54 | + * Application identifier for the newly created application. If not specified, defaults to org.nativescript.<projectName>. |
| 55 | + */ |
| 56 | + appId?: string; |
| 57 | + |
| 58 | + /** |
| 59 | + * Path where the project will be created. If not specified, defaults to current working dir. |
| 60 | + */ |
| 61 | + pathToProject?: string; |
| 62 | + |
| 63 | + /** |
| 64 | + * Defines if invalid application name can be used for project creation. |
| 65 | + */ |
| 66 | + force?: boolean; |
| 67 | + |
| 68 | + /** |
| 69 | + * Defines whether the `npm install` command should be executed with `--ignore-scripts` option. |
| 70 | + * When it is passed, all scripts (postinstall for example) will not be executed. |
| 71 | + */ |
| 72 | + ignoreScripts?: boolean; |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +Sample usage: |
| 77 | +<table> |
| 78 | + <tr> |
| 79 | + <td> |
| 80 | + JavaScript |
| 81 | + </td> |
| 82 | + <td> |
| 83 | + TypeScript |
| 84 | + </td> |
| 85 | + </tr> |
| 86 | + <tr> |
| 87 | + <td> |
| 88 | +<pre lang="javascript"> |
| 89 | +const projectSettings = { |
| 90 | + projectName: "my-ns-app", |
| 91 | + template: "ng", |
| 92 | + pathToProject: "/home/my-user/project-dir" |
| 93 | +}; |
| 94 | + |
| 95 | +tns.projectService.createProject(projectSettings) |
| 96 | + .then(() => console.log("Project successfully created.")) |
| 97 | + .catch((err) => console.log("Unable to create project, reason: ", err); |
| 98 | +</pre> |
| 99 | + </td> |
| 100 | + <td> |
| 101 | +<pre lang="typescript"> |
| 102 | +const projectSettings: IProjectSettings = { |
| 103 | + projectName: "my-ns-app", |
| 104 | + template: "ng", |
| 105 | + pathToProject: "/home/my-user/project-dir" |
| 106 | +}; |
| 107 | + |
| 108 | +tns.projectService.createProject(projectSettings) |
| 109 | + .then(() => console.log("Project successfully created.")) |
| 110 | + .catch((err) => console.log("Unable to create project, reason: ", err); |
| 111 | +</pre> |
| 112 | + </td> |
| 113 | + </tr> |
| 114 | +</table> |
| 115 | + |
| 116 | +## How to add a new method to Public API |
| 117 | +CLI is designed as command line tool and when it is used as a library, it does not give you access to all of the methods. This is mainly implementation detail. Most of the CLI's code is created to work in command line, not as a library, so before adding method to public API, most probably it will require some modification. |
| 118 | +For example the `$options` injected module contains information about all `--` options passed on the terminal. When the CLI is used as a library, the options are not populated. Before adding method to public API, make sure its implementation does not rely on `$options`. |
| 119 | + |
| 120 | +More information how to add a method to public API is available [here](https://github.com/telerik/mobile-cli-lib#how-to-make-a-method-public). |
| 121 | +After that add each method that you've exposed to the tests in `tests/nativescript-cli-lib.ts` file. There you'll find an object describing each publicly available module and the methods that you can call. |
0 commit comments