This document describes all methods that can be invoked when NativeScript CLI is required as library, i.e.
JavaScript | TypeScript |
const tns = require("nativescript"); |
import * as tns from "nativescript"; |
projectService
modules allow you to create new NativeScript application.
createProject(projectSettings: IProjectSettings): Promise<void>
- Creates new NativeScript application. By passingprojectSettings
argument you specify the name of the application, the template that will be used, etc.:
/**
* Describes available settings when creating new NativeScript application.
*/
interface IProjectSettings {
/**
* Name of the newly created application.
*/
projectName: string;
/**
* Selected template from which to create the project. If not specified, defaults to hello-world template.
* Template can be any npm package, local dir, github url, .tgz file.
* If it is set to `angular` or `ng`, default NativeScript Angular Hello World template will be used.
* If it is set to `typescript` or `tsc`, default NativeScript TypeScript Hello World template will be used.
*/
template?: string;
/**
* Application identifier for the newly created application. If not specified, defaults to org.nativescript.<projectName>.
*/
appId?: string;
/**
* Path where the project will be created. If not specified, defaults to current working dir.
*/
pathToProject?: string;
/**
* Defines if invalid application name can be used for project creation.
*/
force?: boolean;
/**
* Defines whether the `npm install` command should be executed with `--ignore-scripts` option.
* When it is passed, all scripts (postinstall for example) will not be executed.
*/
ignoreScripts?: boolean;
}
Sample usage:
JavaScript | TypeScript |
const projectSettings = {
projectName: "my-ns-app",
template: "ng",
pathToProject: "/home/my-user/project-dir"
};
tns.projectService.createProject(projectSettings)
.then(() => console.log("Project successfully created."))
.catch((err) => console.log("Unable to create project, reason: ", err); |
const projectSettings: IProjectSettings = {
projectName: "my-ns-app",
template: "ng",
pathToProject: "/home/my-user/project-dir"
};
tns.projectService.createProject(projectSettings)
.then(() => console.log("Project successfully created."))
.catch((err) => console.log("Unable to create project, reason: ", err);
|
isValidNativeScriptProject(projectDir: string): boolean
- Checks if the specified path is a valid NativeScript project. Returnstrue
in case the directory is a valid project,false
otherwise.
Sample usage:
JavaScript | TypeScript |
const isValidProject = tns.projectService.isValidNativeScriptProject("/tmp/myProject"); |
const isValidProject = tns.projectService.isValidNativeScriptProject("/tmp/myProject"); |
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.
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
.
More information how to add a method to public API is available here.
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.