forked from cypress-io/cypress-documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy-english-docs.js
122 lines (95 loc) · 3.12 KB
/
copy-english-docs.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
// @ts-check
const debug = require('debug')('copy-english-docs')
const execa = require('execa')
const R = require('ramda')
const pluralize = require('pluralize')
const fs = require('fs-extra')
const Promise = require('bluebird')
const path = require('path')
// work in progress
/* eslint-disable no-console */
const getLanguageName = (short) => {
const names = {
ja: 'Japanese',
'zh-cn': 'Chinese',
}
if (!names[short]) {
throw new Error(`Unknown language short name ${short}`)
}
return names[short]
}
const findFilesTrackedByGit = (folder) => {
return execa('git', ['ls-files', folder]).then(R.prop('stdout')).then(R.split('\n'))
}
const removePrefix = (prefix) => {
const n = prefix.length + 1 // prefix + "/"
const remove = (relativeFilename) => {
return R.startsWith(prefix, relativeFilename) ? relativeFilename.substr(n) : relativeFilename
}
return R.map(remove)
}
const findAllDocs = () => {
debug('finding all docs')
const sourcesFolderName = 'source'
return findFilesTrackedByGit(sourcesFolderName)
}
const isJapaneseDoc = R.test(/\/ja\//)
const isChineseDoc = R.test(/\/zh-cn\//)
const isImage = R.test(/\/img\//)
const isTranslation = R.anyPass([isJapaneseDoc, isChineseDoc, isImage])
const translationsFilter = R.reject(isTranslation)
const findAllEnglishDocs = () => {
debug('finding tracked English docs')
return findAllDocs().then(translationsFilter).then(removePrefix('source'))
}
/**
* @param {("ja" | "zh-cn")} shortName The short language name
*/
const findAllDocsFor = (shortName) => {
const relativeSourceFolder = `source/${shortName}`
return findFilesTrackedByGit(relativeSourceFolder).then(removePrefix(relativeSourceFolder))
}
/**
* @param {("ja" | "zh-cn")} targetLanguage
*/
const copyAllEnglishDocsNotTranslatedTo = (targetLanguage) => {
return Promise.all([
findAllEnglishDocs(),
findAllDocsFor(targetLanguage),
]).then(([englishFiles, translatedFiles]) => {
return R.difference(englishFiles, translatedFiles)
})
.then((untransledEnglishFiles) => {
if (!untransledEnglishFiles.length) {
console.log('No untranslated English docs compared to %s', getLanguageName(targetLanguage))
return
}
console.log('Copying %s from English to %s',
pluralize('file', untransledEnglishFiles.length, true), getLanguageName(targetLanguage))
console.table(untransledEnglishFiles)
return Promise.mapSeries(untransledEnglishFiles, (relativePathToEnglishFile) => {
const sourcePath = path.join('source', relativePathToEnglishFile)
const destinationPath = path.join('source', targetLanguage, relativePathToEnglishFile)
const destinationFolder = path.dirname(destinationPath)
return fs.ensureDir(destinationFolder)
.then(() => {
return fs.copyFile(sourcePath, destinationPath)
})
})
})
}
const copyUntranslatedDocs = () => {
return copyAllEnglishDocsNotTranslatedTo('ja')
.then(() => {
return copyAllEnglishDocsNotTranslatedTo('zh-cn')
})
}
// allow using module.parent
// @ts-ignore
if (!module.parent) {
copyUntranslatedDocs()
} else {
module.exports = {
copyUntranslatedDocs,
}
}