-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
69 lines (55 loc) · 1.58 KB
/
Copy pathutils.js
File metadata and controls
69 lines (55 loc) · 1.58 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
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export const markdownExtensions = [
"md",
"markdown",
"mdown",
"mkdn",
"mkd",
"mdwn",
"mkdown",
"ron",
];
export const asciidocExtensions = ["adoc", "asciidoc", "acs"];
/**
* Normalize supported extensions
*
* @param {string} extname
* @return {".md" | ".adoc" | undefined} ".md" for markdown extensions, ".adoc" for asciidoc extensions. Otherwise, undefined
*/
export const normalizeSupportedExtnames = (extname) => {
if (!extname.startsWith(".")) {
return;
}
const extnameWithoutLeadingDot = extname.slice(1).toLowerCase();
if (markdownExtensions.includes(extnameWithoutLeadingDot)) {
return ".md";
}
if (asciidocExtensions.includes(extnameWithoutLeadingDot)) {
return ".adoc";
}
return;
};
export const isNotNullOrEmptyString = (maybeString) => {
return (
maybeString != undefined &&
typeof maybeString === "string" &&
maybeString.trim() !== ""
);
};
export const fileExists = async (path) =>
!!(await fs.promises.stat(path).catch(() => false));
export const invokeInDir = async (dir, function_) => {
const cwd = process.cwd();
process.chdir(dir);
return Promise.resolve(function_(cwd, dir)).finally(() => {
process.chdir(cwd);
});
};
export const readVersion = async () => {
const packageJSONPath = path.join(__dirname, "../package.json");
const packageJSON = JSON.parse(await fs.promises.readFile(packageJSONPath));
return packageJSON.version;
};