-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathoutputGenerator.js
More file actions
72 lines (67 loc) · 1.89 KB
/
outputGenerator.js
File metadata and controls
72 lines (67 loc) · 1.89 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
const childProcess = require("child_process");
const os = require("os");
const { changeQuality } = require("./index");
const jobs = require("./jobs");
const {
template,
imageTemplate,
imageTemplateGroup
} = require("./generateReadMe");
const fs = require("fs");
const __folderPath = `${__dirname}/output`;
if (!fs.existsSync(__folderPath)) {
fs.mkdirSync(__folderPath);
} else {
if (os.platform() === "linux") {
childProcess.spawnSync("rm", ["-rf", `${__dirname}/output`]);
fs.mkdirSync(__folderPath);
} else {
console.warn("please remove output folder");
process.exit(1);
}
}
// main content of the file
const arr = [];
/**
* generateFile - generate output file for function
*
* @param {String} name the name of output file
*
* @param {Object | Array} value the returned value of the function output is generated
*
* @returns {Undifined}
*/
function generateFile(name, value) {
fs.writeFileSync(
__dirname + "/output/" + name + ".json",
JSON.stringify(value, null, 4)
);
const arg = name.split("-");
arr.push(
template(
`${arg[0]}(${arg.slice(1).join(",")})`,
JSON.stringify(value, null, 2)
)
);
}
function generateImageTemplate(name, value) {
const values = [...Array(6).keys()];
// sample image url
const url =
"https://m.media-amazon.com/images/M/MV5BMjMzMzQ0NzI5Nl5BMl5BanBnXkFtZTgwNjc2NTY0NjM@._V1_UX182_CR0,0,182,268_AL__QL50.jpg";
const parsedUrl = values.map(val => {
return imageTemplate(changeQuality(url, val), val);
});
arr.push(imageTemplateGroup(parsedUrl));
return Promise.resolve(arr);
}
// if file is exucted directly using node
if ((module = require.main)) {
Promise.all(jobs(generateFile)).then(v => {
// create the example file
generateImageTemplate();
fs.writeFileSync(__dirname + "/EXAMPLE.md", arr.join("\n"));
console.log("generated the output and example");
process.exit(0);
});
}