jsrepo 1.12.2 → 1.12.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1210,9 +1210,20 @@ var get2 = () => {
1210
1210
  };
1211
1211
 
1212
1212
  // src/utils/git-providers.ts
1213
- var octokit = new Octokit({});
1213
+ var manifestErrorMessage = (info, defaultBranch) => {
1214
+ return Err(
1215
+ `There was an error fetching the \`${color4.bold(OUTPUT_FILE)}\` from ${color4.bold(info.url)}.
1216
+
1217
+ ${color4.bold("This may be for one of the following reasons:")}
1218
+ 1. The \`${color4.bold(OUTPUT_FILE)}\` or containing repository doesn't exist
1219
+ 2. Your repository path is incorrect (wrong branch, wrong tag) default branches other than \`${color4.bold(defaultBranch)}\` must be specified \`${color4.bold("github/<owner>/<name>/tree/<branch>")}\`
1220
+ 3. You are using an expired access token or a token that doesn't have access to this repository
1221
+ `
1222
+ );
1223
+ };
1214
1224
  var github = {
1215
1225
  name: () => "github",
1226
+ defaultBranch: () => "main",
1216
1227
  resolveRaw: async (repoPath, resourcePath) => {
1217
1228
  const info = await github.info(repoPath);
1218
1229
  return new URL(
@@ -1221,13 +1232,8 @@ var github = {
1221
1232
  );
1222
1233
  },
1223
1234
  fetchRaw: async (repoPath, resourcePath) => {
1224
- const url = await github.resolveRaw(repoPath, resourcePath);
1225
- const errorMessage = (err) => {
1226
- return Err(
1227
- `There was an error fetching the \`${OUTPUT_FILE}\` from the repository \`${url.href}\` make sure the target repository has a \`${OUTPUT_FILE}\` in its root.
1228
- Error: ${err}`
1229
- );
1230
- };
1235
+ const info = await github.info(repoPath);
1236
+ const url = await github.resolveRaw(info, resourcePath);
1231
1237
  try {
1232
1238
  const token = get2().get(`${github.name()}-token`);
1233
1239
  const headers = new Headers();
@@ -1236,11 +1242,11 @@ var github = {
1236
1242
  }
1237
1243
  const response = await fetch(url, { headers });
1238
1244
  if (!response.ok) {
1239
- return errorMessage(`${response.status} ${response.text}`);
1245
+ return manifestErrorMessage(info, github.defaultBranch());
1240
1246
  }
1241
1247
  return Ok(await response.text());
1242
- } catch (err) {
1243
- return errorMessage(`${err}`);
1248
+ } catch {
1249
+ return manifestErrorMessage(info, github.defaultBranch());
1244
1250
  }
1245
1251
  },
1246
1252
  fetchManifest: async (repoPath) => {
@@ -1253,12 +1259,20 @@ var github = {
1253
1259
  if (typeof repoPath !== "string") return repoPath;
1254
1260
  const repo = repoPath.replaceAll(/(https:\/\/github.com\/)|(github\/)/g, "");
1255
1261
  const [owner, repoName, ...rest] = repo.split("/");
1256
- let ref = "main";
1262
+ let ref = github.defaultBranch();
1263
+ const token = get2().get(`${github.name()}-token`);
1264
+ const octokit = new Octokit({ auth: token });
1257
1265
  if (rest[0] === "tree") {
1258
1266
  ref = rest[1];
1267
+ } else {
1268
+ try {
1269
+ const { data: repo2 } = await octokit.rest.repos.get({ owner, repo: repoName });
1270
+ ref = repo2.default_branch;
1271
+ } catch {
1272
+ }
1259
1273
  }
1260
1274
  let refs = "heads";
1261
- if (ref !== "main") {
1275
+ if (ref !== github.defaultBranch()) {
1262
1276
  try {
1263
1277
  const { data: tags } = await octokit.rest.git.listMatchingRefs({
1264
1278
  owner,
@@ -1286,6 +1300,7 @@ var github = {
1286
1300
  };
1287
1301
  var gitlab = {
1288
1302
  name: () => "gitlab",
1303
+ defaultBranch: () => "main",
1289
1304
  resolveRaw: async (repoPath, resourcePath) => {
1290
1305
  const info = await gitlab.info(repoPath);
1291
1306
  return new URL(
@@ -1294,13 +1309,8 @@ var gitlab = {
1294
1309
  );
1295
1310
  },
1296
1311
  fetchRaw: async (repoPath, resourcePath) => {
1297
- const url = await gitlab.resolveRaw(repoPath, resourcePath);
1298
- const errorMessage = (err) => {
1299
- return Err(
1300
- `There was an error fetching the \`${OUTPUT_FILE}\` from the repository \`${url.href}\` make sure the target repository has a \`${OUTPUT_FILE}\` in its root.
1301
- Error: ${err}`
1302
- );
1303
- };
1312
+ const info = await github.info(repoPath);
1313
+ const url = await gitlab.resolveRaw(info, resourcePath);
1304
1314
  try {
1305
1315
  const token = get2().get(`${gitlab.name()}-token`);
1306
1316
  const headers = new Headers();
@@ -1309,11 +1319,11 @@ var gitlab = {
1309
1319
  }
1310
1320
  const response = await fetch(url, { headers });
1311
1321
  if (!response.ok) {
1312
- return errorMessage(`${response.status} ${response.text}`);
1322
+ return manifestErrorMessage(info, gitlab.defaultBranch());
1313
1323
  }
1314
1324
  return Ok(await response.text());
1315
- } catch (err) {
1316
- return errorMessage(`${err}`);
1325
+ } catch {
1326
+ return manifestErrorMessage(info, gitlab.defaultBranch());
1317
1327
  }
1318
1328
  },
1319
1329
  fetchManifest: async (repoPath) => {
@@ -1326,7 +1336,7 @@ var gitlab = {
1326
1336
  if (typeof repoPath !== "string") return repoPath;
1327
1337
  const repo = repoPath.replaceAll(/(https:\/\/gitlab.com\/)|(gitlab\/)/g, "");
1328
1338
  const [owner, repoName, ...rest] = repo.split("/");
1329
- let ref = "main";
1339
+ let ref = gitlab.defaultBranch();
1330
1340
  let refs = "heads";
1331
1341
  if (rest[0] === "-" && rest[1] === "tree") {
1332
1342
  if (rest[2].includes("?")) {
@@ -1355,6 +1365,7 @@ var gitlab = {
1355
1365
  };
1356
1366
  var bitbucket = {
1357
1367
  name: () => "bitbucket",
1368
+ defaultBranch: () => "master",
1358
1369
  resolveRaw: async (repoPath, resourcePath) => {
1359
1370
  const info = await bitbucket.info(repoPath);
1360
1371
  return new URL(
@@ -1363,13 +1374,8 @@ var bitbucket = {
1363
1374
  );
1364
1375
  },
1365
1376
  fetchRaw: async (repoPath, resourcePath) => {
1366
- const url = await bitbucket.resolveRaw(repoPath, resourcePath);
1367
- const errorMessage = (err) => {
1368
- return Err(
1369
- `There was an error fetching the \`${OUTPUT_FILE}\` from the repository \`${url.href}\` make sure the target repository has a \`${OUTPUT_FILE}\` in its root.
1370
- Error: ${err}`
1371
- );
1372
- };
1377
+ const info = await bitbucket.info(repoPath);
1378
+ const url = await bitbucket.resolveRaw(info, resourcePath);
1373
1379
  try {
1374
1380
  const token = get2().get(`${bitbucket.name()}-token`);
1375
1381
  const headers = new Headers();
@@ -1378,11 +1384,11 @@ var bitbucket = {
1378
1384
  }
1379
1385
  const response = await fetch(url, { headers });
1380
1386
  if (!response.ok) {
1381
- return errorMessage(`${response.status} ${response.text}`);
1387
+ return manifestErrorMessage(info, bitbucket.defaultBranch());
1382
1388
  }
1383
1389
  return Ok(await response.text());
1384
- } catch (err) {
1385
- return errorMessage(`${err}`);
1390
+ } catch {
1391
+ return manifestErrorMessage(info, bitbucket.defaultBranch());
1386
1392
  }
1387
1393
  },
1388
1394
  fetchManifest: async (repoPath) => {
@@ -1396,7 +1402,7 @@ var bitbucket = {
1396
1402
  const repo = repoPath.replaceAll(/(https:\/\/bitbucket.org\/)|(bitbucket\/)/g, "");
1397
1403
  const [owner, repoName, ...rest] = repo.split("/");
1398
1404
  const refs = "heads";
1399
- let ref = "master";
1405
+ let ref = bitbucket.defaultBranch();
1400
1406
  if (rest[0] === "src") {
1401
1407
  ref = rest[1];
1402
1408
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "jsrepo",
3
3
  "description": "A CLI to add shared code from remote repositories.",
4
- "version": "1.12.2",
4
+ "version": "1.12.4",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/ieedan/jsrepo"
@@ -7,8 +7,6 @@ import { type Category, categorySchema } from './build';
7
7
  import { OUTPUT_FILE } from './context';
8
8
  import * as persisted from './persisted';
9
9
 
10
- const octokit = new Octokit({});
11
-
12
10
  export type Info = {
13
11
  refs: 'tags' | 'heads';
14
12
  url: string;
@@ -25,6 +23,11 @@ export interface Provider {
25
23
  * @returns the name of the provider
26
24
  */
27
25
  name: () => string;
26
+ /** Get the name of the default branch
27
+ *
28
+ * @returns
29
+ */
30
+ defaultBranch: () => string;
28
31
  /** Returns a URL to the raw path of the resource provided in the resourcePath
29
32
  *
30
33
  * @param repoPath
@@ -60,6 +63,18 @@ export interface Provider {
60
63
  matches: (repoPath: string) => boolean;
61
64
  }
62
65
 
66
+ const manifestErrorMessage = (info: Info, defaultBranch: string) => {
67
+ return Err(
68
+ `There was an error fetching the \`${color.bold(OUTPUT_FILE)}\` from ${color.bold(info.url)}.
69
+
70
+ ${color.bold('This may be for one of the following reasons:')}
71
+ 1. The \`${color.bold(OUTPUT_FILE)}\` or containing repository doesn't exist
72
+ 2. Your repository path is incorrect (wrong branch, wrong tag) default branches other than \`${color.bold(defaultBranch)}\` must be specified \`${color.bold('github/<owner>/<name>/tree/<branch>')}\`
73
+ 3. You are using an expired access token or a token that doesn't have access to this repository
74
+ `
75
+ );
76
+ };
77
+
63
78
  /** Valid paths
64
79
  *
65
80
  * `https://github.com/<owner>/<repo>/[tree]/[ref]`
@@ -68,6 +83,7 @@ export interface Provider {
68
83
  */
69
84
  const github: Provider = {
70
85
  name: () => 'github',
86
+ defaultBranch: () => 'main',
71
87
  resolveRaw: async (repoPath, resourcePath) => {
72
88
  const info = await github.info(repoPath);
73
89
 
@@ -77,13 +93,9 @@ const github: Provider = {
77
93
  );
78
94
  },
79
95
  fetchRaw: async (repoPath, resourcePath) => {
80
- const url = await github.resolveRaw(repoPath, resourcePath);
96
+ const info = await github.info(repoPath);
81
97
 
82
- const errorMessage = (err: string) => {
83
- return Err(
84
- `There was an error fetching the \`${OUTPUT_FILE}\` from the repository \`${url.href}\` make sure the target repository has a \`${OUTPUT_FILE}\` in its root.\n Error: ${err}`
85
- );
86
- };
98
+ const url = await github.resolveRaw(info, resourcePath);
87
99
 
88
100
  try {
89
101
  const token = persisted.get().get(`${github.name()}-token`);
@@ -97,12 +109,12 @@ const github: Provider = {
97
109
  const response = await fetch(url, { headers });
98
110
 
99
111
  if (!response.ok) {
100
- return errorMessage(`${response.status} ${response.text}`);
112
+ return manifestErrorMessage(info, github.defaultBranch());
101
113
  }
102
114
 
103
115
  return Ok(await response.text());
104
- } catch (err) {
105
- return errorMessage(`${err}`);
116
+ } catch {
117
+ return manifestErrorMessage(info, github.defaultBranch());
106
118
  }
107
119
  },
108
120
  fetchManifest: async (repoPath) => {
@@ -121,16 +133,28 @@ const github: Provider = {
121
133
 
122
134
  const [owner, repoName, ...rest] = repo.split('/');
123
135
 
124
- let ref = 'main';
136
+ let ref = github.defaultBranch();
137
+
138
+ const token = persisted.get().get(`${github.name()}-token`);
139
+
140
+ const octokit = new Octokit({ auth: token });
125
141
 
126
142
  if (rest[0] === 'tree') {
127
143
  ref = rest[1];
144
+ } else {
145
+ try {
146
+ const { data: repo } = await octokit.rest.repos.get({ owner, repo: repoName });
147
+
148
+ ref = repo.default_branch;
149
+ } catch {
150
+ // we just want to continue on blissfully unaware the user will get an error later
151
+ }
128
152
  }
129
153
 
130
154
  // checks if the type of the ref is tags or heads
131
155
  let refs: 'heads' | 'tags' = 'heads';
132
156
  // no need to check if ref is main
133
- if (ref !== 'main') {
157
+ if (ref !== github.defaultBranch()) {
134
158
  try {
135
159
  const { data: tags } = await octokit.rest.git.listMatchingRefs({
136
160
  owner,
@@ -173,6 +197,7 @@ const github: Provider = {
173
197
  */
174
198
  const gitlab: Provider = {
175
199
  name: () => 'gitlab',
200
+ defaultBranch: () => 'main',
176
201
  resolveRaw: async (repoPath, resourcePath) => {
177
202
  const info = await gitlab.info(repoPath);
178
203
 
@@ -182,13 +207,9 @@ const gitlab: Provider = {
182
207
  );
183
208
  },
184
209
  fetchRaw: async (repoPath, resourcePath) => {
185
- const url = await gitlab.resolveRaw(repoPath, resourcePath);
210
+ const info = await github.info(repoPath);
186
211
 
187
- const errorMessage = (err: string) => {
188
- return Err(
189
- `There was an error fetching the \`${OUTPUT_FILE}\` from the repository \`${url.href}\` make sure the target repository has a \`${OUTPUT_FILE}\` in its root.\n Error: ${err}`
190
- );
191
- };
212
+ const url = await gitlab.resolveRaw(info, resourcePath);
192
213
 
193
214
  try {
194
215
  const token = persisted.get().get(`${gitlab.name()}-token`);
@@ -202,12 +223,12 @@ const gitlab: Provider = {
202
223
  const response = await fetch(url, { headers });
203
224
 
204
225
  if (!response.ok) {
205
- return errorMessage(`${response.status} ${response.text}`);
226
+ return manifestErrorMessage(info, gitlab.defaultBranch());
206
227
  }
207
228
 
208
229
  return Ok(await response.text());
209
- } catch (err) {
210
- return errorMessage(`${err}`);
230
+ } catch {
231
+ return manifestErrorMessage(info, gitlab.defaultBranch());
211
232
  }
212
233
  },
213
234
  fetchManifest: async (repoPath) => {
@@ -226,7 +247,7 @@ const gitlab: Provider = {
226
247
 
227
248
  const [owner, repoName, ...rest] = repo.split('/');
228
249
 
229
- let ref = 'main';
250
+ let ref = gitlab.defaultBranch();
230
251
  let refs: Info['refs'] = 'heads';
231
252
 
232
253
  if (rest[0] === '-' && rest[1] === 'tree') {
@@ -271,6 +292,7 @@ const gitlab: Provider = {
271
292
  */
272
293
  const bitbucket: Provider = {
273
294
  name: () => 'bitbucket',
295
+ defaultBranch: () => 'master',
274
296
  resolveRaw: async (repoPath, resourcePath) => {
275
297
  const info = await bitbucket.info(repoPath);
276
298
 
@@ -280,13 +302,9 @@ const bitbucket: Provider = {
280
302
  );
281
303
  },
282
304
  fetchRaw: async (repoPath, resourcePath) => {
283
- const url = await bitbucket.resolveRaw(repoPath, resourcePath);
305
+ const info = await bitbucket.info(repoPath);
284
306
 
285
- const errorMessage = (err: string) => {
286
- return Err(
287
- `There was an error fetching the \`${OUTPUT_FILE}\` from the repository \`${url.href}\` make sure the target repository has a \`${OUTPUT_FILE}\` in its root.\n Error: ${err}`
288
- );
289
- };
307
+ const url = await bitbucket.resolveRaw(info, resourcePath);
290
308
 
291
309
  try {
292
310
  const token = persisted.get().get(`${bitbucket.name()}-token`);
@@ -300,12 +318,12 @@ const bitbucket: Provider = {
300
318
  const response = await fetch(url, { headers });
301
319
 
302
320
  if (!response.ok) {
303
- return errorMessage(`${response.status} ${response.text}`);
321
+ return manifestErrorMessage(info, bitbucket.defaultBranch());
304
322
  }
305
323
 
306
324
  return Ok(await response.text());
307
- } catch (err) {
308
- return errorMessage(`${err}`);
325
+ } catch {
326
+ return manifestErrorMessage(info, bitbucket.defaultBranch());
309
327
  }
310
328
  },
311
329
  fetchManifest: async (repoPath) => {
@@ -327,7 +345,7 @@ const bitbucket: Provider = {
327
345
  // pretty sure this just auto detects
328
346
  const refs = 'heads';
329
347
 
330
- let ref = 'master';
348
+ let ref = bitbucket.defaultBranch();
331
349
 
332
350
  if (rest[0] === 'src') {
333
351
  ref = rest[1];