Skip to content

Commit b3a4280

Browse files
committed
Change method using boilerplates
1 parent 0654781 commit b3a4280

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
"inquirer": "^3.0.6",
3232
"node": "0.0.0",
3333
"shelljs": "^0.7.7",
34+
"ts-loader": "^2.0.3",
3435
"walk": "^2.3.9",
36+
"winston": "^2.3.1",
3537
"xml2js": "^0.4.17"
3638
},
3739
"devDependencies": {

scripts/constants/APIEndpoint.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const GITHUB_CDN_ENDPOINT = "https://raw.githubusercontent.com/endlessdev/android-cli-templates/master";

scripts/controllers/GenerateController.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
11
import * as changeCase from "change-case";
22
import * as fs from "fs";
3+
import * as path from "path";
4+
import {TemplateRequest} from "../utils/TemplateRequest";
35

46
const chalk = require("chalk"),
57
commander = require("commander"),
68
walk = require('walk');
79

810
export class GenerateController {
911

10-
1112
/**
1213
* @desc Generate rendered out file
1314
*
1415
* @param {string} componentType - Type of component
1516
* @param {string} componentName - Name of component
1617
* @param {string} targetPkg - Targeted package name
1718
*/
18-
public generateRenderedOutFile(componentType: string, componentName: string, targetPkg: string) {
19-
20-
console.log(process.argv[1]);
19+
public async generateRenderedOutFile(componentType: string, componentName: string, targetPkg: string) {
2120

22-
//TODO GET REAL BOILERPLATES PATH
23-
const boilerplatesPath = `../../boilerplates/${componentType}`,
24-
boilerplateNameOfJAVA = `index.ac.src`,
25-
boilerplateNameOfXML = `index.ac.layout`,
26-
27-
javaFileName: string = `${changeCase.pascalCase(componentName)}${changeCase.pascalCase(componentType)}.java`,
21+
const javaFileName: string = `${changeCase.pascalCase(componentName)}${changeCase.pascalCase(componentType)}.java`,
2822
xmlFileName: string = `${changeCase.lowerCase(componentType)}_${changeCase.lowerCase(componentName)}.xml`,
2923

30-
javaContent: string = fs.readFileSync(`${boilerplatesPath}/${boilerplateNameOfJAVA}`, 'utf-8').toString(),
31-
xmlContent: string = fs.readFileSync(`${boilerplatesPath}/${boilerplateNameOfXML}`, 'utf-8').toString(),
24+
templates : any = await TemplateRequest.getTemplateContents(componentType),
25+
26+
javaContent: string = templates.src,
27+
xmlContent: string = templates.layout,
3228

3329
parsedJavaContent: string = this.renderAcFile(javaContent, targetPkg, componentName),
3430
parsedXMLContent: string = this.renderAcFile(xmlContent, targetPkg, componentName);

scripts/utils/TemplateRequest.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {GITHUB_CDN_ENDPOINT} from "../constants/APIEndpoint";
2+
3+
const request = require("request");
4+
5+
6+
export class TemplateRequest {
7+
8+
public static async getTemplateContents(componentName: string) {
9+
10+
const BASE_URL = `${GITHUB_CDN_ENDPOINT}/${componentName}`,
11+
SRC_ENDPOINT = `${BASE_URL}/index.ac.src`,
12+
LAYOUT_ENDPOINT = `${BASE_URL}/index.ac.layout`;
13+
14+
const srcBody = await TemplateRequest.getContentFromURL(SRC_ENDPOINT);
15+
const layoutBody = await TemplateRequest.getContentFromURL(LAYOUT_ENDPOINT);
16+
17+
return {src: srcBody, layout: layoutBody};
18+
}
19+
20+
public static async getContentFromURL(requestURL: string) {
21+
return new Promise(function (resolve, reject) {
22+
request(requestURL, function (error, res, body) {
23+
if (!error && res.statusCode == 200) {
24+
resolve(body);
25+
} else {
26+
reject(error);
27+
}
28+
});
29+
});
30+
}
31+
}

0 commit comments

Comments
 (0)