This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidation.js
148 lines (133 loc) · 3.17 KB
/
validation.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { getProfileSettings } from './client.js'
// eslint-disable-next-line no-unused-vars
import * as API from '@ucanto/interface'
// @ts-ignore
import { parseLink } from '@ucanto/server'
import fs from 'fs'
import path from 'path'
import { pathToFileURL } from 'url'
/**
*
* @param {string|undefined} email?
* @returns {boolean}
*/
export const isEmail = (email) => {
if (!email) return false
return /(.+)@(.+){2,}\.(.+){2,}/.test(email)
}
/**
*
* @param {API.Link|undefined|string} cid
* @returns {boolean}
*/
export const isCID = (cid) => {
if (!cid) {
throw new Error('Empty CID was provided')
}
try {
parseLink(cid?.toString() || '')
} catch (err) {
throw new Error(`${cid} is probably not a valid CID\n${err}`)
}
return true
}
/**
*
* @param {string} targetPath
* @returns {URL}
*/
export const resolvePath = (targetPath) =>
pathToFileURL(path.resolve(process.cwd(), targetPath))
/**
*
* @param {string|undefined} targetPath
* @returns {boolean}
*/
export const isPath = (targetPath) => {
try {
if (targetPath) {
const stat = fs.lstatSync(resolvePath(targetPath))
return stat.isDirectory() || stat.isFile()
}
} catch (err) {
throw new Error(`File or directory does not exist: ${targetPath}`)
}
return false
}
/**
* @param {import('yargs').Arguments<{path?:string}>} argv
*/
export const checkPath = ({ path }) => {
try {
return isPath(path)
} catch (err) {
throw new Error(
`${path} is probably not a valid path to a file or directory: \n${err}`
)
}
}
/**
* @param {import('yargs').Arguments<{profile?:string}>} argv
*/
export const hasID = ({ profile }) => {
const settings = getProfileSettings(profile)
if (!settings.has('secret') && !settings.has('account_secret')) {
throw new Error('You have not setup an id, please run w3up id first.')
}
return true
}
/**
* @param {import('yargs').Arguments<{profile?:string}>} argv
*/
export const hasEmail = ({ profile }) => {
const settings = getProfileSettings(profile)
if (!settings.has('email')) {
throw new Error(
'You have not setup an email, please run w3up register <email> first.'
)
}
return true
}
/**
* @param {import('yargs').Arguments<{profile?:string}>} argv
*/
export const hasOtherDelegation = ({ profile }) => {
const settings = getProfileSettings(profile)
if (!settings.has('account')) {
return false
}
const delegations = settings.get('delegations')
const account = settings.get('account')
// @ts-expect-error
if (delegations[account].alias !== 'self') {
return true
} else {
return false
}
}
/**
* @param {import('yargs').Arguments<{profile?:string}>} argv
*/
export function hasSetupAccount (argv) {
try {
return hasID(argv) && hasEmail(argv)
} catch (accountError) {
if (hasOtherDelegation(argv)) {
return true
}
throw new Error(
`${accountError}\nYou can also import a delegation from another account.`
)
}
}
/**
*
* @param {string | undefined} targetPath
* @returns {boolean}
*/
export const isCarFile = (targetPath) => {
if (!targetPath || !isPath(targetPath)) {
return false
}
return path.extname(targetPath) === '.car'
}