-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
129 lines (112 loc) · 3.59 KB
/
index.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
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
119
120
121
122
123
124
125
126
127
128
129
const core = require('@actions/core');
const fs = require('fs');
const path = require('path');
const json2md = require("json2md");
const needGeneReadme = core.getInput('need-gene-readme');
const background = core.getInput('background');
const repo = process.env.GITHUB_REPOSITORY;
const repoInfo = repo.split("/");
const repoName = repoInfo[1];
let gitRef = process.env.GITHUB_REF;
if (gitRef) {
const refs = gitRef.split("/");
gitRef = refs[refs.length - 1];
} else {
gitRef = 'master';
}
const manifestList = [];
function walkDirs(dirPath) {
if (!dirPath) return;
const imageDir = path.join(dirPath, "images");
console.log(`image dir: ${imageDir}`);
if (fs.existsSync(imageDir)) {
console.log(`image dir exist: ${imageDir}`)
const imageList = readImageDir(imageDir);
if (imageList) {
const imageManifest = toImageManifest(imageList, dirPath);
console.log(`manifest: ${imageManifest}`);
dumpData(imageManifest, path.join(dirPath, "manifest.json"));
}
return;
}
fs.readdirSync(dirPath).forEach(file => {
if (file.startsWith('.')) return;
const subDirPath = path.join(dirPath, file);
if (fs.statSync(subDirPath).isDirectory()) {
walkDirs(subDirPath);
}
});
}
function toImageManifest(imageList, dirPath) {
let items = [];
imageList.forEach(imagePath => {
items.push({
"type": "image",
"url": toRawUrl(imagePath),
"background_style": background
})
});
return {
name: `${repo}/${dirPath}`,
version: "1.0",
repo: `https://github.com/${repo}/tree/${gitRef}/${dirPath}`,
type: "resource",
items: items
};
}
function toRawUrl(filePath) {
return `https://raw.githubusercontent.com/${repo}/${gitRef}/${filePath}`;
}
function readImageDir(imageDir) {
const imageExts = ["png", "jpg", "jpeg", "bmp", "svg", "webp"];
let imagePathList = [];
fs.readdirSync(imageDir).forEach(file => {
if (imageExts.indexOf(path.extname(file).slice(1).toLowerCase()) > -1) {
let imagePath = path.join(imageDir, file);
console.log(`image path: ${imagePath}`);
imagePathList.push(imagePath);
}
});
console.log(`>>>>>>>>>>>>>>>> image list: ${imagePathList}`)
return imagePathList;
}
function readJsonFile(jsonFilePath) {
fs.readFile(jsonFilePath, 'utf8', (err, data) => {
if (err) return {};
var jsonObj = JSON.parse(data);
if (typeof jsonObj === "object") {
return jsonObj
}
});
}
function dumpData(manifestData, dataFilePath) {
const jsonData = JSON.stringify(manifestData);
try {
fs.writeFileSync(dataFilePath, jsonData);
let albumName = repoName;
if (dataFilePath.indexOf("/") > -1) {
albumName = dataFilePath.split("/").reverse()[1]
}
console.log(`album name: ${albumName}`)
manifestList.push(`[${albumName}](${toRawUrl(dataFilePath)})`);
} catch (err) {
console.log(`dump manifest file error: ${err}`);
}
}
function geneReadme() {
const text = json2md([
{ h1: `TabHub Cards: ${repoName}` }
, { blockquote: "Generated by [TabHub Card Action](https://github.com/tabhub/tabhub-card-action)" }
, { h3: "Resource List"}
, { p: "You can copy one of link addresses below as a resource url to add in your [TabHub](https://tabhub.io) settings:" }
, { ul: manifestList }
, { hr: "" }
, { p: "<img src=https://raw.githubusercontent.com/image-store/github/master/add-tabhub-resource-url.png width=350>"}
]);
fs.writeFile('./README.md', text, (err) => {
if (err) console.log(`write README file error: ${err}`);
})
}
walkDirs('.');
console.log(`manifest list: ${manifestList}`);
if (needGeneReadme) geneReadme();