-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path_util.js
More file actions
200 lines (187 loc) · 4.48 KB
/
_util.js
File metadata and controls
200 lines (187 loc) · 4.48 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import process from "node:process";
import path from "node:path";
import fs from "fs-extra";
import { parseArgs } from "node:util";
import { italic, gray } from "colorette";
import { createHash } from "node:crypto";
import { pipeline } from "node:stream/promises";
/**
* Change the process's cwd to the project root (as per the normal location of this file).
*/
export function cwdToProjectRoot() {
const getProjectRoot = path.resolve(path.dirname(process.argv[1]), "..");
process.chdir(getProjectRoot);
console.log(gray(italic(`working from: ${getProjectRoot}`)));
return getProjectRoot;
}
/**
* `chmod` a file or directory to add owner execute permissions. Same as
* running `chmod o+x {path}`.
*
* @param {string} path File or directory to chmod
*/
export function chmodOwnerPlusX(path) {
const { mode = fs.constants.S_IRUSR } = fs.statSync(path);
fs.chmodSync(path, mode | fs.constants.S_IXUSR);
}
export async function isFile(path) {
return (await fs.pathExists(path)) && (await fs.stat(path)).isFile;
}
export async function isDirectory(path) {
return (await fs.pathExists(path)) && (await fs.stat(path)).isDirectory;
}
export function relativeToCwd(pathTo) {
return path.relative(path.resolve("."), path.resolve(pathTo));
}
export async function fileSha256(path) {
if (!isFile(path)) {
return "";
}
const hash = createHash("sha256");
await pipeline(fs.createReadStream(path), hash);
return hash.digest("hex");
}
/**
* @param {string[]} directories
*/
export async function ensureDirs(directories) {
return await Promise.all(
directories.map(async (dir) => {
const fullPath = path.resolve(dir);
await fs.ensureDir(fullPath);
return fullPath;
}),
);
}
export function parseCli(
{
org,
repo,
releaseTag,
workflow,
branch,
rulesetOrg,
rulesetRepo,
rulesetReleaseTag,
csharpBranch,
csharpReleaseTag,
},
useDefault = "release",
) {
const { values } = parseArgs({
options: {
"use-release": {
type: "boolean",
short: "R",
default: useDefault === "release",
},
// TODO: Would a better name be "use-latest"?
"use-workflow-artifacts": {
type: "boolean",
short: "W",
default: useDefault === "workflow",
},
org: {
type: "string",
short: "o",
default: org,
},
repo: {
type: "string",
short: "r",
default: repo,
},
"release-tag": {
type: "string",
short: "R",
default: releaseTag,
},
workflow: {
type: "string",
short: "w",
default: workflow,
},
branch: {
type: "string",
short: "b",
default: branch,
},
pr: {
type: "string",
short: "p",
default: undefined,
},
"ruleset-org": {
type: "string",
default: rulesetOrg,
},
"ruleset-repo": {
type: "string",
default: rulesetRepo,
},
"ruleset-release-tag": {
type: "string",
default: rulesetReleaseTag,
},
// C# provider specific options
"csharp-branch": {
type: "string",
default: csharpBranch,
},
"csharp-release-tag": {
type: "string",
default: csharpReleaseTag,
},
},
allowNegative: true,
});
const ruleset = {
rulesetOrg: values["ruleset-org"],
rulesetRepo: values["ruleset-repo"],
releaseTag: values["release-tag"],
};
// C# provider specific config
const csharp = {
csharpBranch: values["csharp-branch"],
csharpReleaseTag: values["csharp-release-tag"],
};
if (values["use-workflow-artifacts"] && values.workflow && values.pr) {
return {
useWorkflow: true,
usePr: true,
org: values.org,
repo: values.repo,
workflow: values.workflow,
pr: values.pr,
...ruleset,
...csharp,
};
}
if (values["use-workflow-artifacts"] && values.workflow && values.branch) {
return {
useWorkflow: true,
org: values.org,
repo: values.repo,
workflow: values.workflow,
branch: values.branch,
...ruleset,
...csharp,
};
}
if (values["use-release"] && values["release-tag"]) {
return {
useRelease: true,
org: values.org,
repo: values.repo,
releaseTag: values["release-tag"],
...ruleset,
...csharp,
};
}
return {
org: values.org,
repo: values.repo,
...ruleset,
...csharp,
};
}