-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.js
90 lines (85 loc) · 2.7 KB
/
lib.js
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
var GitHubApi = require('@octokit/rest');
var fs = require('fs');
var github = (exports.github = new GitHubApi({
debug: false,
headers: { 'Accept-Charset': 'ISO-8859-1,utf-8' }
}));
/*
Write out content of README.md file for latest release of org/module to path.
*/
exports.processReadme = function(targetRepo, path) {
var options = {};
var fileName = path + '/' + targetRepo.repoName + '.md';
options.owner = targetRepo.org;
options.repo = targetRepo.repoName;
if (targetRepo.branch) {
// get readme for branch, if specified
options.ref = targetRepo.branch;
path = path + '/' + targetRepo.repoName + '-' + targetRepo.branch + '.md';
writeOutReadme(options, path);
} else {
github.repos.getLatestRelease(
{ owner: targetRepo.org, repo: targetRepo.repoName },
function(err, res) {
if (err) {
// Use master release
//console.log('For ' + targetRepo.org + '/' + targetRepo.repoName +
// ' -- No latest release ');
} else {
// Use latest tagged release if it exists
// console.log('For ' + targetRepo.org + '/' + targetRepo.repoName +
// ' latest release is ' + res.data.tag_name );
options.ref = res.data.tag_name;
}
writeOutReadme(options, fileName);
}
);
}
}; //processReadme
/**
* Saves the README file to the specified directory path.
* @param {Object} options options passed to
* [getReadme](http://mikedeboer.github.io/node-github/#api-repos-getReadme):
* options.owner, options.repo, and options.ref are used.
* @param {String} readmeFile Full directory path and file name where the
* README will be written.
*/
function writeOutReadme(options, readmeFile) {
github.repos.getReadme(options, function(err, res) {
if (err) {
console.error('Fail to fetch README from github', err);
} else {
console.log('Writing ' + readmeFile);
//var file = path + '/' + options.repo + '.md'
var s = new Buffer(res.content, 'base64').toString();
//var fs = require('fs');
fs.writeFile(readmeFile, s, function(err) {
if (err) {
return console.error(err);
}
});
}
});
}
/**
* Check whether a file or directory exists.
* @param {String} path Path to the file or directory.
* @param {Boolean} isDir True if testing existence of a directory.
* False or don't supply otherwise.
*/
exports.fileOrDirExists = function(path, isDir) {
try {
if (isDir) {
return fs.statSync(path).isDirectory();
} else {
return fs.statSync(path).isFile();
}
} catch (e) {
if (e.code === 'ENOENT') {
console.error(path + ': Does not exist.');
return false;
} else {
throw e;
}
}
};