Skip to content

Commit

Permalink
Adds a script for generating translation TODO lists - see #220
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Feb 10, 2020
1 parent 5b4a58c commit 7846be5
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/playground-examples/scripts/generateTOC.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const path = require('path')
const crypto = require('crypto')
const JSON5 = require('json5')

/** Retrieve file paths from a given folder and its subfolders. */
/** Recursively retrieve file paths from a given folder and its subfolders. */
// https://gist.github.com/kethinov/6658166#gistcomment-2936675
const getFilePaths = folderPath => {
const entryPaths = fs.readdirSync(folderPath).map(entry => path.join(folderPath, entry))
Expand Down
117 changes: 117 additions & 0 deletions packages/typescriptlang-org/scripts/makeMarkdownOfTranslations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const { join } = require("path")
const fs = require("fs")
const path = require("path")

const lang = process.argv.slice(2)[0]
if (!lang) {
console.log("You need to run this script with a language arg")
console.log(
"> node packages/typescriptlang-org/scripts/makeMarkdownOfTranslations.js jp"
)
}

const getAllTODOFiles = () => {
const diffFolders = (root, lang) => {
const en = join(root, "en")
const thisLang = join(root, lang)

const englishFiles = getFilePaths(en)
const thisLangFiles = getFilePaths(thisLang)

const todo = []
const done = []
englishFiles.forEach((en, index) => {
const localFile = en.replace("/en/", "/" + lang + "/")
if (thisLangFiles.includes(localFile)) {
done.push(localFile)
} else {
todo.push(en)
}
})

return { todo, done }
}

// TS Config
const tsconfigRoot = join(__dirname, "..", "..", "tsconfig-reference", "copy")
const tsconfigFilesTODO = diffFolders(tsconfigRoot, lang)

// Playground
const playRoot = join(__dirname, "..", "..", "playground-examples", "copy")
const playgroundTODO = diffFolders(playRoot, lang)

// Layouts
const appRoot = join(__dirname, "..", "src", "copy")
const appTODO = diffFolders(appRoot, lang)

// Handbook TBD

const all = {
tsconfig: tsconfigFilesTODO,
playground: playgroundTODO,
app: appTODO,
}

return all
}

/** Recursively retrieve file paths from a given folder and its subfolders. */
// https://gist.github.com/kethinov/6658166#gistcomment-2936675
/** @returns {string[]} */
const getFilePaths = folderPath => {
if (!fs.existsSync(folderPath)) return []

const entryPaths = fs
.readdirSync(folderPath)
.map(entry => path.join(folderPath, entry))
const filePaths = entryPaths.filter(entryPath =>
fs.statSync(entryPath).isFile()
)
const dirPaths = entryPaths.filter(
entryPath => !filePaths.includes(entryPath)
)
const dirFiles = dirPaths.reduce(
(prev, curr) => prev.concat(getFilePaths(curr)),
[]
)

return [...filePaths, ...dirFiles]
.filter(f => !f.endsWith(".DS_Store") && !f.endsWith("README.md"))
.map(f => {
const root = join(__dirname, "..", "..", "..")
return f.replace(root, "")
})
}

const toMarkdown = files => {
const md = []

const markdownLink = (f, done) => {
const name = path.basename(f)
const url = "https://github.com/microsoft/TypeScript-Website/blob/v2"
const check = done ? "x" : " "
return `- [${check}] [\`${name}\`](${url}${encodeURIComponent(f)})`
}

Object.keys(files).forEach(section => {
const todo = files[section].todo
const done = files[section].done

md.push("\n\n## " + section + "\n\n")

done.forEach(f => {
md.push(markdownLink(f, true))
})

todo.forEach(f => {
md.push(markdownLink(f))
})

// console.log(todoFiles)
})

return md.join("\n")
}

const files = getAllTODOFiles()
console.log(toMarkdown(files))

0 comments on commit 7846be5

Please sign in to comment.