forked from nextstrain/auspice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
132 lines (122 loc) · 3.61 KB
/
utils.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
130
131
132
/* eslint no-console: off */
const fs = require('fs');
const chalk = require('chalk');
const path = require("path");
const fetch = require('node-fetch');
const verbose = (msg) => {
if (global.AUSPICE_VERBOSE) {
console.log(chalk.greenBright(`[verbose]\t${msg}`));
}
};
const log = (msg) => {
console.log(chalk.blueBright(msg));
};
const warn = (msg) => {
console.warn(chalk.yellowBright(`[warning]\t${msg}`));
};
const error = (msg) => {
console.error(chalk.redBright(`[error]\t${msg}`));
process.exit(2);
};
const isNpmGlobalInstall = () => {
return __dirname.indexOf("lib/node_modules/auspice") !== -1;
};
const cleanUpPathname = (pathIn) => {
let pathOut = pathIn;
if (!pathOut.endsWith("/")) pathOut += "/";
if (pathOut.startsWith("~")) {
pathOut = path.join(process.env.HOME, pathOut.slice(1));
}
pathOut = path.resolve(pathOut);
if (!fs.existsSync(pathOut)) {
return undefined;
}
return pathOut;
};
const getCurrentDirectoriesFor = (type) => {
const cwd = process.cwd();
const folderName = type === "data" ? "auspice" : "narratives";
if (fs.existsSync(path.join(cwd, folderName))) {
return cleanUpPathname(path.join(cwd, folderName));
}
return cleanUpPathname(cwd);
};
const resolveLocalDirectory = (providedPath, isNarratives) => {
let localPath;
if (providedPath) {
localPath = cleanUpPathname(providedPath);
} else if (isNpmGlobalInstall()) {
localPath = getCurrentDirectoriesFor(isNarratives ? "narratives" : "data");
} else {
// fallback to the auspice source directory
localPath = path.join(path.resolve(__dirname), "..", isNarratives ? "narratives" : "data");
}
return localPath;
};
const fetchJSON = (pathIn) => {
const p = fetch(pathIn)
.then((res) => {
if (res.status !== 200) throw new Error(res.statusText);
try {
const header = res.headers[Object.getOwnPropertySymbols(res.headers)[0]] || res.headers._headers;
verbose(`Got type ${header["content-type"]} with encoding ${header["content-encoding"] || "none"}`);
} catch (e) {
// potential errors here are inconsequential for the response
}
return res;
})
.then((res) => res.json());
return p;
};
const readFilePromise = (fileName) => {
return new Promise((resolve, reject) => {
fs.readFile(fileName, 'utf8', (err, data) => {
if (err) {
return reject(err);
}
try {
return resolve(JSON.parse(data));
} catch (parseErr) {
return reject(parseErr);
}
});
});
};
/* Where should the built files be saved? (or sourced??)
* This may grow more complex over time
*/
const customOutputPath = (extensions) => {
if (extensions && path.resolve(__dirname, "..") !== process.cwd()) {
return process.cwd();
}
return false;
};
/* write an index.html file to the current working directory
* Optionally set the hrefs for local files to relative links (needed for github pages)
*/
const exportIndexDotHtml = ({relative=false}) => {
if (path.resolve(__dirname, "..") === process.cwd()) {
warn("Cannot export index.html to the auspice source directory.");
return;
}
const outputFilePath = path.join(process.cwd(), "index.html");
let data = fs.readFileSync(path.resolve(__dirname, "../index.html"), {encoding: "utf8"});
verbose(`Writing ${outputFilePath}`);
if (relative) {
data = data
.replace(/\/favicon/g, "favicon")
.replace(/\/dist\/bundle.js/, "dist/bundle.js");
}
fs.writeFileSync(outputFilePath, data);
};
module.exports = {
verbose,
log,
warn,
error,
resolveLocalDirectory,
customOutputPath,
fetchJSON,
readFilePromise,
exportIndexDotHtml
};