MCPcopy Index your code
hub / github.com/github/docs / validateFileInputs

Function validateFileInputs

script/move-content.js:160–208  ·  view source on GitHub ↗
(oldPath, newPath, isFolder)

Source from the content-addressed store, hash-verified

158}
159
160function validateFileInputs(oldPath, newPath, isFolder) {
161 if (isFolder) {
162 // Make sure that only the last portion of the path is different
163 // and that all preceeding are equal.
164 const [oldBase, oldName] = splitDirectory(oldPath)
165 const [newBase] = splitDirectory(newPath)
166 if (oldBase !== newBase && !existsAndIsDirectory(newBase)) {
167 console.error(
168 chalk.red(
169 `When moving a directory, both bases need to be the same. '${oldBase}' != '${newBase}'`
170 )
171 )
172 console.warn(chalk.yellow(`Only the name (e.g. '${oldName}') can be different.`))
173 process.exit(1)
174 }
175 }
176
177 if (!path.resolve(newPath).startsWith(CONTENT_ROOT)) {
178 const relativeRoot = path.relative('.', CONTENT_ROOT)
179 console.error(chalk.red(`New path does not start with '${relativeRoot}'`))
180 process.exit(1)
181 }
182
183 if (!fs.existsSync(oldPath)) {
184 console.error(chalk.red(`${oldPath} does not resolve to an existing file or a folder`))
185 process.exit(1)
186 }
187 if (path.basename(oldPath) === 'index.md') {
188 console.error(
189 chalk.red(`File path can't be 'index.md'. Refer to it by its foldername instead.`)
190 )
191 process.exit(1)
192 }
193 if (path.basename(newPath) === 'index.md') {
194 console.error(
195 chalk.red(`File path can't be 'index.md'. Refer to it by its foldername instead.`)
196 )
197 process.exit(1)
198 }
199
200 if (fs.existsSync(newPath)) {
201 console.error(chalk.red(`Can't move to a ${isFolder ? 'folder' : 'file'} that already exists.`))
202 process.exit(1)
203 }
204
205 if (/\s/.test(newPath)) {
206 throw new Error(`New path (${newPath}) can't contain whitespace`)
207 }
208}
209
210function existsAndIsDirectory(directory) {
211 return fs.existsSync(directory) && fs.lstatSync(directory).isDirectory()

Callers 1

mainFunction · 0.85

Calls 2

splitDirectoryFunction · 0.85
existsAndIsDirectoryFunction · 0.85

Tested by

no test coverage detected