-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathpacote-service.ts
40 lines (33 loc) · 1.57 KB
/
pacote-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import * as pacote from "pacote";
import * as tar from "tar";
export class PacoteService implements IPacoteService {
constructor(private $npm: INodePackageManager) { }
public async manifest(packageName: string, options?: IPacoteManifestOptions): Promise<any> {
// In case `tns create myapp --template https://github.com/NativeScript/template-hello-world.git` command is executed, pacote module throws an error if cache option is not provided.
const manifestOptions = { cache: await this.$npm.getCachePath() };
if (options) {
_.extend(manifestOptions, options);
}
return pacote.manifest(packageName, manifestOptions);
}
public async extractPackage(packageName: string, destinationDirectory: string, options?: IPacoteExtractOptions): Promise<void> {
// strip: Remove the specified number of leading path elements. Pathnames with fewer elements will be silently skipped. More info: https://github.com/npm/node-tar/blob/e89c4d37519b1c20133a9f49d5f6b85fa34c203b/README.md
// C: Create an archive
const extractOptions = { strip: 1, C: destinationDirectory };
if (options) {
_.extend(extractOptions, options);
}
const cache = await this.$npm.getCachePath();
return new Promise<void>((resolve, reject) => {
const source = pacote.tarball.stream(packageName, { cache });
source.on("error", (err: Error) => {
reject(err);
});
const destination = tar.x(extractOptions);
source.pipe(destination);
destination.on("error", (err: Error) => reject(err));
destination.on("finish", () => resolve());
});
}
}
$injector.register("pacoteService", PacoteService);