-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·118 lines (94 loc) · 2.51 KB
/
index.js
File metadata and controls
executable file
·118 lines (94 loc) · 2.51 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env node
var program = require("commander");
var fs = require("fs");
var request = require("request");
var unzip = require("unzipper");
var path = require("path");
/**
* Validate the CLI
*/
program
.version("0.1.1")
.description("Generate dotapp framework project skeleton")
.parse(process.argv);
if (program.args.length != 2) {
console.error("Please specify project folder name");
process.exit(1);
}
let [command] = program.args;
if (command == "new") {
/**
* getting CLI args
* @type {string}
*/
var project_folder_path = path.join(process.cwd(), program.args[1]);
if (fs.existsSync(project_folder_path)) {
console.log(program.args[0] + " directory is already exist");
process.exit();
}
/**
* Retrieving latest stable version
* @type {{headers: {User-Agent: string}, json: boolean}}
*/
var options = {
headers: { "User-Agent": "https://api.github.com/meta" },
json: true,
};
console.log("Checking the latest stable version");
request.get(
"https://api.github.com/repos/basemkhirat/dotapp-framework/releases/latest",
options,
function (error, response) {
if (error) throw error;
var url = response.body.zipball_url;
var version = response.body.tag_name;
console.log("Dotapp " + version + " is available!");
console.log("Downloading from source");
request
.get(
url,
{ headers: { "User-Agent": "https://api.github.com/meta" } },
function (error, response) {
if (error) throw error;
}
)
.pipe(fs.createWriteStream("/tmp/dotapp.zip"))
.on("finish", function () {
console.log("Extracting source files");
var unzip_operation = unzip.Extract({ path: process.cwd() });
fs.createReadStream("/tmp/dotapp.zip").pipe(unzip_operation);
unzip_operation.on("close", function () {
walkSync(process.cwd()).forEach(function (dir) {
if (dir.startsWith("basemkhirat-dotapp-framework-")) {
fs.rename(
path.join(process.cwd(), dir),
project_folder_path,
function (err) {
if (err) throw err;
console.log("Dotapp is successfully installed!");
}
);
}
});
});
});
}
);
} else {
console.log("Invalid command " + program.args[0]);
}
/**
* Read sub directories
* @param dir
* @returns {Array}
*/
var walkSync = function (dir) {
var files = fs.readdirSync(dir);
dirs = [];
files.forEach(function (file) {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
dirs.push(file.replace(".js", ""));
}
});
return dirs;
};