This commit is contained in:
Martin Yankov 2017-09-27 16:28:48 +03:00
Родитель 01d87905a9
Коммит 0b43ff2d49
3 изменённых файлов: 115 добавлений и 125 удалений

Просмотреть файл

@ -1,35 +1,35 @@
import { Config } from "../shared/config";
import util from "../shared/util";
const defaultHeaders = {
"user-agent": "nativescript-starter-kits"
};
export class GitService implements IGitService {
getPackageJsonFromSource(templateName: string): Promise<any> {
let content: any;
return this.getNpmPackageVersion(templateName).then((packageVersion: string) => {
return util.request({
method: "GET",
uri: util.format(
"https://raw.githubusercontent.com/NativeScript/%s/%s/package.json",
templateName,
packageVersion
),
json: true,
resolveWithFullResponse: true,
headers: util.defaultHeaders
return this.getNpmPackageVersion(templateName)
.then((packageVersion: string) => {
return util.request({
method: "GET",
uri: util.format(
"https://raw.githubusercontent.com/NativeScript/%s/%s/package.json",
templateName,
packageVersion
),
json: true,
resolveWithFullResponse: true,
headers: defaultHeaders
});
})
.then((response: any) => {
content = response.body;
if (content.hasOwnProperty("templateType")) {
return content;
}
return response.body;
})
.catch((error: any) => {
return {
return Promise.reject({
message: "Error retrieving " + templateName + " package.json from src",
err: error
};
});
});
});
}
getAssetsContent(templateName: string, versionTag: string): Promise<any> {
@ -43,122 +43,104 @@ export class GitService implements IGitService {
return this.getResourcesFromSource(templateName, assets, version);
}
clonePageTemplate(pageName: string, flavor: string, templatesDirectory: string): Promise<string> {
const command = "git";
const commandArguments: Array<any> = ["clone"];
clonePageTemplate(pageName: string, flavor: string, templatesDirectory: string): Promise<any> {
return new Promise((resolve, reject) => {
this.getPageTemplatesBaseUrl(flavor)
.then((baseUrl: string) => {
baseUrl = baseUrl + ".git";
commandArguments.push(baseUrl);
commandArguments.push(templatesDirectory);
const command = "git";
const commandArguments: Array<any> = ["clone"];
const baseUrl = this.getPageTemplatesBaseUrl(flavor);
const process = util.childProcess.spawn(command, commandArguments);
process.on("close", (code) => {
if (code !== 0) {
return Promise.reject(new Error(`child process exited with code ${code}`));
}
commandArguments.push(`${baseUrl}.git`);
commandArguments.push(templatesDirectory);
const pagePath = util.path.join(templatesDirectory, pageName);
resolve(pagePath);
});
})
.catch((downloadPageError: any) => {
reject(downloadPageError);
});
const process = util.childProcess.spawn(command, commandArguments);
process.on("close", (code) => {
if (code !== 0) {
return reject(new Error(`child process exited with code ${code}`));
}
const pagePath = util.path.join(templatesDirectory, pageName);
resolve(pagePath);
});
});
}
private getPageTemplatesBaseUrl(flavor: string) {
private getPageTemplatesBaseUrl(flavor: string): string {
let baseUrl: string;
return new Promise((resolve, reject) => {
switch (flavor) {
case "JavaScript":
baseUrl = util.format(Config.orgBaseUrl, "nativescript-page-templates");
resolve(baseUrl);
break;
switch (flavor) {
case "JavaScript":
baseUrl = util.format(Config.orgBaseUrl, "nativescript-page-templates");
break;
case "TypeScript":
baseUrl = util.format(Config.orgBaseUrl, "nativescript-page-templates-ts");
break;
case "Angular & TypeScript":
baseUrl = util.format(Config.orgBaseUrl, "nativescript-page-templates-ng");
break;
default:
throw new Error("Bad Flavor");
}
case "TypeScript":
baseUrl = util.format(Config.orgBaseUrl, "nativescript-page-templates-ts");
resolve(baseUrl);
break;
case "Angular & TypeScript":
baseUrl = util.format(Config.orgBaseUrl, "nativescript-page-templates-ng");
resolve(baseUrl);
break;
default:
reject(new Error("Bad Flavor"));
}
});
return baseUrl;
}
private getResourcesFromSource(templateName: string, assetDictionary: any, versionTag: string) {
const content: any = {};
const promises: Array<any> = [];
private getResourcesFromSource(templateName: string, assetDictionary: any, versionTag: string): Promise<any> {
const promisesMap: Map<string, Promise<any>> = new Map();
return new Promise((resolve, reject) => {
for (const key in assetDictionary) {
if (assetDictionary.hasOwnProperty(key)) {
promises.push(
util.request({
method: "GET",
// tslint:disable-next-line:max-line-length
uri: util.format("https://raw.githubusercontent.com/NativeScript/%s/%s/tools/assets/%s", templateName, versionTag, assetDictionary[key]),
resolveWithFullResponse: true,
encoding: "binary",
headers: util.defaultHeaders
})
.then((response: any) => {
// tslint:disable-next-line:max-line-length
content[key] = "data:image/png;base64," + new Buffer(response.body.toString(), "binary").toString("base64");
})
.catch((error: any) => {
return {
message: "Error retrieving " + templateName + " assets from source",
err: error
};
}));
}
for (const key in assetDictionary) {
if (assetDictionary.hasOwnProperty(key)) {
promisesMap.set(key, this.getResource(templateName, versionTag, assetDictionary[key]));
}
Promise.all(promises)
.then(() => {
resolve(content);
})
.catch((error) => {
reject(error);
}
return util.promiseAllMap(promisesMap);
}
private getResource(templateName: string, versionTag: string, asset: string): Promise<any> {
return util.request({
method: "GET",
// tslint:disable-next-line:max-line-length
uri: util.format("https://raw.githubusercontent.com/NativeScript/%s/%s/tools/assets/%s", templateName, versionTag, asset),
resolveWithFullResponse: true,
encoding: "binary",
headers: defaultHeaders
})
.then((response: any) => {
// tslint:disable-next-line:max-line-length
return "data:image/png;base64," + new Buffer(response.body.toString(), "binary").toString("base64");
})
.catch((error: any) => {
return Promise.reject({
message: "Error retrieving " + templateName + " assets from source",
err: error
});
});
});
}
private getNpmPackageVersion(templateName: string): Promise<any> {
return new Promise((resolve, reject) => {
if (templateName.indexOf("tns-") !== 0) {
templateName = "tns-" + templateName;
}
if (templateName.indexOf("tns-") !== 0) {
templateName = "tns-" + templateName;
}
util.request({
method: "GET",
uri: util.format("https://registry.npmjs.org/%s/", templateName),
json: true,
resolveWithFullResponse: true,
headers: util.defaultHeaders
return util.request({
method: "GET",
uri: util.format("https://registry.npmjs.org/%s/", templateName),
json: true,
resolveWithFullResponse: true,
headers: defaultHeaders
})
.then((response: any) => {
let version = "master";
if (response.body && response.body["dist-tags"] && response.body["dist-tags"].latest) {
version = "v" + response.body["dist-tags"].latest;
}
return version;
})
.then((response: any) => {
let version = "master";
if (response.body && response.body["dist-tags"] && response.body["dist-tags"].latest) {
version = "v" + response.body["dist-tags"].latest;
}
resolve(version);
})
.catch((error: any) => {
// fallback to using the master (latest version)
resolve("master");
});
});
.catch((error: any) => {
// fallback to using the master (latest version)
return "master";
});
}
}

Просмотреть файл

@ -2,14 +2,10 @@ import * as childProcess from "child_process";
import * as path from "path";
import * as nodeUtil from "util";
const request = require("request-promise");
const request = require("request-promise-native");
const fs = require("fs-extra");
export default class Util {
static defaultHeaders = {
"user-agent": "nativescript-starter-kits"
};
static format = nodeUtil.format;
static request = request;
static path = path;
@ -33,4 +29,16 @@ export default class Util {
});
});
}
static promiseAllMap(map: Map<string, Promise<any>>) {
return Promise.all(Array.from(map, ([key, promise]) => Promise.resolve(promise).then((value) => [key, value])))
.then((results: Array<Array<string>>) => {
const resultsAsObj: any = {};
results.forEach((result) => {
resultsAsObj[result[0]] = result[1];
});
return resultsAsObj;
});
}
}

Просмотреть файл

@ -17,9 +17,9 @@
"devDependencies": {
"@types/chai": "3.4.34",
"@types/chai-as-promised": "0.0.29",
"@types/source-map": "0.5.0",
"@types/node": "6.0.61",
"@types/node-forge": "0.6.7",
"@types/source-map": "0.5.0",
"chai": "3.5.0",
"chai-as-promised": "6.0.0",
"chai-things": "^0.2.0",
@ -48,7 +48,7 @@
"lodash": "4.13.1",
"node-cache": "^4.1.1",
"request": "^2.81.0",
"request-promise": "^4.2.1",
"request-promise-native": "^1.0.5",
"sinon": "^3.3.0"
}
}
}