forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-new-dotcom-path.js
executable file
·46 lines (34 loc) · 1.3 KB
/
get-new-dotcom-path.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
#!/usr/bin/env node
const assert = require('assert')
const { last } = require('lodash')
const fs = require('fs')
const { execSync } = require('child_process')
const markdownExtension = '.md'
const markdownRegex = new RegExp(`${markdownExtension}$`, 'm')
const newDotcomDir = 'content/github'
const oldPath = process.argv.slice(2)[0]
assert(oldPath, 'must provide old dotcom path like "foo" or "articles/foo"')
let filename = oldPath
// [start-readme]
//
// Pass this script any old dotcom path (e.g., `articles/foo` or `foo.md`) and it
// will output the new path in the content/github directory.
//
// [end-readme]
// get last part of path
if (filename.includes('/')) filename = last(filename.split('/'))
// first check whether name is a category
const categoryDir = `${newDotcomDir}/${filename.replace(markdownRegex, '')}`
if (fs.existsSync(categoryDir)) {
console.log(`New path:\n${categoryDir}/`)
process.exit(0)
}
// otherwise add extension and check whether it's a file
if (!filename.endsWith(markdownExtension)) filename = filename + markdownExtension
// run find command
const newPath = execSync(`find ${newDotcomDir} -name ${filename}`).toString()
if (!newPath) {
console.log(`Cannot find new path for "${oldPath}". Check the name and try again.\n`)
process.exit(0)
}
console.log(`New path:\n${newPath}`)