1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | const crypto = require('crypto');
|
18 | const path = require('path');
|
19 |
|
20 | const fse = require('fs-extra');
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function calculateBlobSha1(data) {
|
28 | return crypto.createHash('sha1').update(`blob ${data.length}\0`).update(data).digest('hex');
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function randomFileOrFolderName(len = 32) {
|
37 | if (Number.isFinite(len)) {
|
38 | return crypto.randomBytes(Math.ceil(len / 2)).toString('hex').slice(0, len);
|
39 | }
|
40 | throw new Error(`illegal argument: ${len}`);
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function resolveRepositoryPath(options, owner, repo) {
|
51 | let repPath = path.resolve(options.repoRoot, owner, repo);
|
52 |
|
53 | if (options.virtualRepos[owner] && options.virtualRepos[owner][repo]) {
|
54 | repPath = path.resolve(options.virtualRepos[owner][repo].path);
|
55 | }
|
56 | return repPath;
|
57 | }
|
58 |
|
59 | module.exports._caseInsensitiveFS = undefined;
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | async function isCaseInsensitiveFS() {
|
66 | if (typeof module.exports._caseInsensitiveFS === 'undefined') {
|
67 | let lcStat;
|
68 | let ucStat;
|
69 | try {
|
70 | lcStat = await fse.stat(process.execPath.toLowerCase());
|
71 | } catch (err) {
|
72 | lcStat = false;
|
73 | }
|
74 | try {
|
75 | ucStat = await fse.stat(process.execPath.toUpperCase());
|
76 | } catch (err) {
|
77 | ucStat = false;
|
78 | }
|
79 | if (lcStat && ucStat) {
|
80 | module.exports._caseInsensitiveFS = lcStat.dev === ucStat.dev && lcStat.ino === ucStat.ino;
|
81 | } else {
|
82 | module.exports._caseInsensitiveFS = false;
|
83 | }
|
84 | }
|
85 | return module.exports._caseInsensitiveFS;
|
86 | }
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | async function pathExists(parentDir, pathToTest) {
|
95 | if (!await isCaseInsensitiveFS()) {
|
96 | return fse.pathExists(path.join(parentDir, pathToTest));
|
97 | }
|
98 |
|
99 | let parent = parentDir;
|
100 |
|
101 |
|
102 | const names = pathToTest.split('/').filter((el) => el !== '');
|
103 | for (let i = 0; i < names.length; i += 1) {
|
104 | const nm = names[i];
|
105 | try {
|
106 |
|
107 | if (!(await fse.readdir(parent)).filter((el) => el === nm).length) {
|
108 | return false;
|
109 | }
|
110 | } catch (err) {
|
111 | return false;
|
112 | }
|
113 | parent = path.join(parent, nm);
|
114 | }
|
115 | return true;
|
116 | }
|
117 |
|
118 | module.exports = {
|
119 | resolveRepositoryPath,
|
120 | calculateBlobSha1,
|
121 | randomFileOrFolderName,
|
122 | isCaseInsensitiveFS,
|
123 | pathExists,
|
124 | };
|