Skip to content

Commit 0b70f39

Browse files
Dimitar KerezovMitko-Kerezov
Dimitar Kerezov
authored andcommitted
Docs and tests
1 parent de44599 commit 0b70f39

File tree

6 files changed

+150
-13
lines changed

6 files changed

+150
-13
lines changed

PublicAPI.md

+120
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,126 @@ interface ISettingsService {
219219
tns.settingsService.setSettings({ userAgentName: "myUserAgent" });
220220
```
221221
222+
## npm
223+
`npm` module provides a way to interact with npm specifically the use of install, uninstall, search and view commands.
224+
225+
### install
226+
Installs specified package. Note that you can use the third argument in order to pass different options to the installation like `ignore-scripts`, `save` or `save-exact` which work exactly like they would if you would execute npm from the command line and pass them as `--` flags.
227+
* Auxiliary interfaces:
228+
```TypeScript
229+
/**
230+
* Describes information about installed package.
231+
*/
232+
interface INpmInstallResultInfo {
233+
/**
234+
* Installed package's name.
235+
* @type {string}
236+
*/
237+
name: string;
238+
/**
239+
* Installed package's version.
240+
* @type {string}
241+
*/
242+
version: string;
243+
/**
244+
* The original output that npm CLI produced upon installation.
245+
* @type {INpmInstallCLIResult}
246+
*/
247+
originalOutput: INpmInstallCLIResult;
248+
}
249+
```
250+
251+
* Definition:
252+
```TypeScript
253+
/**
254+
* Installs dependency
255+
* @param {string} packageName The name of the dependency - can be a path, a url or a string.
256+
* @param {string} pathToSave The destination of the installation.
257+
* @param {IDictionary<string | boolean>} config Additional options that can be passed to manipulate installation.
258+
* @return {Promise<INpmInstallResultInfo>} Information about installed package.
259+
*/
260+
install(packageName: string, pathToSave: string, config: IDictionary<string | boolean>): Promise<INpmInstallResultInfo>;
261+
```
262+
263+
* Usage:
264+
```JavaScript
265+
tns.npm.install("lodash", "/tmp/myProject", { save: true }).then(result => {
266+
console.log(`${result.name} installed successfully`);
267+
}, err => {
268+
console.log("An error occurred during installation", err);
269+
});
270+
```
271+
272+
### uninstall
273+
Uninstalls a specified package.
274+
275+
* Definition:
276+
```TypeScript
277+
/**
278+
* Uninstalls a dependency
279+
* @param {string} packageName The name of the dependency.
280+
* @param {IDictionary<string | boolean>} config Additional options that can be passed to manipulate uninstallation.
281+
* @param {string} path The destination of the uninstallation.
282+
* @return {Promise<any>} The output of the uninstallation.
283+
*/
284+
uninstall(packageName: string, config?: IDictionary<string | boolean>, path?: string): Promise<string>;
285+
```
286+
287+
* Usage:
288+
```JavaScript
289+
tns.npm.uninstall("lodash", "/tmp/myProject", { save: true }).then(output => {
290+
console.log(`Uninstalled successfully, output: ${output}`);
291+
}, err => {
292+
console.log("An error occurred during uninstallation", err);
293+
});
294+
```
295+
296+
### search
297+
Searches for a package using keywords.
298+
299+
* Definition:
300+
```TypeScript
301+
/**
302+
* Searches for a package.
303+
* @param {string[]} filter Keywords with which to perform the search.
304+
* @param {IDictionary<string | boolean>} config Additional options that can be passed to manipulate search.
305+
* @return {Promise<string>} The output of the uninstallation.
306+
*/
307+
search(filter: string[], config: IDictionary<string | boolean>): Promise<string>;
308+
```
309+
310+
* Usage:
311+
```JavaScript
312+
tns.npm.search(["nativescript", "cloud"], { silent: true }).then(output => {
313+
console.log(`Found: ${output}`);
314+
}, err => {
315+
console.log("An error occurred during searching", err);
316+
});
317+
```
318+
319+
### view
320+
Provides information about a given package.
321+
322+
* Definition
323+
```TypeScript
324+
/**
325+
* Provides information about a given package.
326+
* @param {string} packageName The name of the package.
327+
* @param {IDictionary<string | boolean>} config Additional options that can be passed to manipulate view.
328+
* @return {Promise<any>} Object, containing information about the package.
329+
*/
330+
view(packageName: string, config: Object): Promise<any>;
331+
```
332+
333+
* Usage:
334+
```JavaScript
335+
tns.npm.view(["nativescript"], {}).then(result => {
336+
console.log(`${result.name}'s latest version is ${result["dist-tags"].latest}`);
337+
}, err => {
338+
console.log("An error occurred during viewing", err);
339+
});
340+
```
341+
222342
## How to add a new method to Public API
223343
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.
224344
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`.

lib/declarations.d.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,31 @@ interface INodePackageManager {
77
* @return {Promise<INpmInstallResultInfo>} Information about installed package.
88
*/
99
install(packageName: string, pathToSave: string, config: INodePackageManagerInstallOptions): Promise<INpmInstallResultInfo>;
10-
uninstall(packageName: string, config?: any, path?: string): Promise<any>;
10+
11+
/**
12+
* Uninstalls a dependency
13+
* @param {string} packageName The name of the dependency.
14+
* @param {IDictionary<string | boolean>} config Additional options that can be passed to manipulate uninstallation.
15+
* @param {string} path The destination of the uninstallation.
16+
* @return {Promise<string>} The output of the uninstallation.
17+
*/
18+
uninstall(packageName: string, config?: IDictionary<string | boolean>, path?: string): Promise<string>;
19+
20+
/**
21+
* Provides information about a given package.
22+
* @param {string} packageName The name of the package.
23+
* @param {IDictionary<string | boolean>} config Additional options that can be passed to manipulate view.
24+
* @return {Promise<any>} Object, containing information about the package.
25+
*/
1126
view(packageName: string, config: Object): Promise<any>;
12-
search(filter: string[], config: any): Promise<any>;
27+
28+
/**
29+
* Searches for a package.
30+
* @param {string[]} filter Keywords with which to perform the search.
31+
* @param {IDictionary<string | boolean>} config Additional options that can be passed to manipulate search.
32+
* @return {Promise<string>} The output of the uninstallation.
33+
*/
34+
search(filter: string[], config: IDictionary<string | boolean>): Promise<string>;
1335
}
1436

1537
interface INpmInstallationManager {

lib/definitions/plugins.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
interface IPluginsService {
22
add(plugin: string, projectData: IProjectData): Promise<void>; // adds plugin by name, github url, local path and et.
33
remove(pluginName: string, projectData: IProjectData): Promise<void>; // removes plugin only by name
4-
getAvailable(filter: string[]): Promise<IDictionary<any>>; // gets all available plugins
54
prepare(pluginData: IDependencyData, platform: string, projectData: IProjectData): Promise<void>;
65
getAllInstalledPlugins(projectData: IProjectData): Promise<IPluginData[]>;
76
ensureAllDependenciesAreInstalled(projectData: IProjectData): Promise<void>;

lib/node-package-manager.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ export class NodePackageManager implements INodePackageManager {
8080
}
8181

8282
@exported("npm")
83-
public async uninstall(packageName: string, config?: any, path?: string): Promise<any> {
84-
let flags = this.getFlagsString(config, false);
83+
public async uninstall(packageName: string, config?: any, path?: string): Promise<string> {
84+
const flags = this.getFlagsString(config, false);
8585
return this.$childProcess.exec(`npm uninstall ${packageName} ${flags}`, { cwd: path });
8686
}
8787

8888
@exported("npm")
89-
public async search(filter: string[], config: any): Promise<any> {
90-
let args = (<any[]>([filter] || [])).concat(config.silent);
91-
return this.$childProcess.exec(`npm search ${args.join(" ")}`);
89+
public async search(filter: string[], config: any): Promise<string> {
90+
const flags = this.getFlagsString(config, false);
91+
return this.$childProcess.exec(`npm search ${filter.join(" ")} ${flags}`);
9292
}
9393

9494
@exported("npm")

lib/services/plugins-service.ts

-5
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ export class PluginsService implements IPluginsService {
103103
}
104104
}
105105

106-
public getAvailable(filter: string[]): Promise<IDictionary<any>> {
107-
let silent: boolean = true;
108-
return this.$npm.search(filter, { "silent": silent });
109-
}
110-
111106
public async validate(platformData: IPlatformData, projectData: IProjectData): Promise<void> {
112107
return await platformData.platformProjectService.validatePlugins(projectData);
113108
}

test/nativescript-cli-lib.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe("nativescript-cli-lib", () => {
1818
projectService: ["createProject", "isValidNativeScriptProject"],
1919
localBuildService: ["build"],
2020
deviceLogProvider: null,
21+
npm: ["install", "uninstall", "view", "search"],
2122
extensibilityService: ["loadExtensions", "getInstalledExtensions", "installExtension", "uninstallExtension"]
2223
};
2324

0 commit comments

Comments
 (0)