Skip to content

Commit

Permalink
Generate types and lint files
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon committed Oct 17, 2021
1 parent ffea465 commit a166f96
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 51 deletions.
61 changes: 36 additions & 25 deletions bin/locale-packs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dedent from 'dedent'
import stringifyObject from 'stringify-object'
import remark from 'remark'
import { headingRange } from 'mdast-util-heading-range'
import { ESLint } from 'eslint'
import remarkFrontmatter from 'remark-frontmatter'
import Task from 'data.task'

Expand All @@ -22,13 +23,17 @@ const localesPath = path.join('packages', '@uppy', 'locales')
const templatePath = path.join(localesPath, 'template.js')
const englishLocalePath = path.join(localesPath, 'src', 'en_US.js')

const linter = new ESLint({ fix: true })

main().fork(
function onError (error) {
console.error(error)
process.exit(1)
},
function onSuccess () {
console.log(`✅ Written '${englishLocalePath}'`)
console.log(`✅ Generated '${englishLocalePath}'`)
console.log('✅ Generated locale docs')
console.log('✅ Generated types')
}
)

Expand All @@ -41,8 +46,17 @@ function main () {
locales,
}))
.chain(({ combinedLocale, locales }) => {
return writeLocale(combinedLocale).map(() => docs(locales))
// .ap(types(locales))
return readFile(templatePath)
.map((fileString) => populateTemplate(fileString, combinedLocale))
.chain(lint)
.chain((file) => writeFile(englishLocalePath, file))
.map(() => {
for (const [pluginName, locale] of Object.entries(locales)) {
generateLocaleDocs(pluginName, locale)
generateTypes(pluginName, locale)
}
return locales
})
})
}

Expand Down Expand Up @@ -75,34 +89,31 @@ function createCombinedLocale (locales) {
return { combinedLocale, locales }
}

function writeLocale (combinedLocale) {
function populateTemplate (fileString, combinedLocale) {
const formattedLocale = stringifyObject(combinedLocale, {
indent: ' ',
singleQuotes: true,
inlineCharacterLimit: 12,
})

return readFile(templatePath)
.map((string) => string.replace('en_US.strings = {}', `en_US.strings = ${formattedLocale}`))
.chain((file) => writeFile(englishLocalePath, file))
return fileString.replace('en_US.strings = {}', `en_US.strings = ${formattedLocale}`)
}

function docs (locales) {
const entries = Object.entries(locales)

for (const [pluginName, locale] of entries) {
generateLocaleDocs(pluginName, locale)
}

return locales
function lint (fileString) {
return new Task((reject, resolve) => {
linter.lintText(fileString)
.then(([result]) => resolve(result.output))
.catch(reject)
})
}

function createTypeScriptLocale (plugin, pluginName) {
const allowedStringTypes = Object.keys(plugin.defaultLocale.strings)
function generateTypes (pluginName, locale) {
const allowedStringTypes = Object.keys(locale.strings)
.map((key) => ` | '${key}'`)
.join('\n')
const pluginClassName = pluginName.split('-')
.map(str => str.replace(/^\w/, (c) => c.toUpperCase()))
.join('')

const pluginClassName = pluginName === 'core' ? 'Core' : plugin.id
const localePath = path.join(
'packages',
'@uppy',
Expand All @@ -112,14 +123,14 @@ function createTypeScriptLocale (plugin, pluginName) {
)

const localeTypes = dedent`
/* eslint-disable */
import type { Locale } from '@uppy/core'
/* eslint-disable */
import type { Locale } from '@uppy/core'
type ${pluginClassName}Locale = Locale<
${allowedStringTypes}
>
type ${pluginClassName}Locale = Locale<
${allowedStringTypes}
>
export default ${pluginClassName}Locale
export default ${pluginClassName}Locale
`

fs.writeFileSync(localePath, localeTypes)
Expand Down
52 changes: 26 additions & 26 deletions packages/@uppy/locales/src/en_US.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const en_US = {}

en_US.strings = {
addBulkFilesFailed: {
'0': 'Failed to add %{smart_count} file due to an internal error',
'1': 'Failed to add %{smart_count} files due to internal errors',
0: 'Failed to add %{smart_count} file due to an internal error',
1: 'Failed to add %{smart_count} files due to internal errors',
},
addingMoreFiles: 'Adding more files',
addMore: 'Add more',
Expand Down Expand Up @@ -60,15 +60,15 @@ en_US.strings = {
failedToFetch: 'Companion failed to fetch this URL, please make sure it’s correct',
failedToUpload: 'Failed to upload %{file}',
filesUploadedOfTotal: {
'0': '%{complete} of %{smart_count} file uploaded',
'1': '%{complete} of %{smart_count} files uploaded',
0: '%{complete} of %{smart_count} file uploaded',
1: '%{complete} of %{smart_count} files uploaded',
},
filter: 'Filter',
finishEditingFile: 'Finish editing file',
flipHorizontal: 'Flip horizontal',
folderAdded: {
'0': 'Added %{smart_count} file from %{folder}',
'1': 'Added %{smart_count} files from %{folder}',
0: 'Added %{smart_count} file from %{folder}',
1: 'Added %{smart_count} files from %{folder}',
},
folderAlreadyAdded: 'The folder "%{folder}" was already added',
generatingThumbnails: 'Generating thumbnails...',
Expand Down Expand Up @@ -102,16 +102,16 @@ en_US.strings = {
pluginNameZoom: 'Zoom',
poweredBy: 'Powered by %{uppy}',
processingXFiles: {
'0': 'Processing %{smart_count} file',
'1': 'Processing %{smart_count} files',
0: 'Processing %{smart_count} file',
1: 'Processing %{smart_count} files',
},
recording: 'Recording',
recordingLength: 'Recording length %{recording_length}',
recordingStoppedMaxSize: 'Recording stopped because the file size is about to exceed the limit',
recoveredAllFiles: 'We restored all files. You can now resume the upload.',
recoveredXFiles: {
'0': 'We could not fully recover 1 file. Please re-select it and resume the upload.',
'1': 'We could not fully recover %{smart_count} files. Please re-select them and resume the upload.',
0: 'We could not fully recover 1 file. Please re-select it and resume the upload.',
1: 'We could not fully recover %{smart_count} files. Please re-select them and resume the upload.',
},
removeFile: 'Remove file %{file}',
reSelect: 'Re-select',
Expand All @@ -126,8 +126,8 @@ en_US.strings = {
saveChanges: 'Save changes',
searchImages: 'Search for images',
selectX: {
'0': 'Select %{smart_count}',
'1': 'Select %{smart_count}',
0: 'Select %{smart_count}',
1: 'Select %{smart_count}',
},
sessionRestored: 'Session restored',
signInWithGoogle: 'Sign in with Google',
Expand All @@ -146,35 +146,35 @@ en_US.strings = {
uploadFailed: 'Upload failed',
uploading: 'Uploading',
uploadingXFiles: {
'0': 'Uploading %{smart_count} file',
'1': 'Uploading %{smart_count} files',
0: 'Uploading %{smart_count} file',
1: 'Uploading %{smart_count} files',
},
uploadPaused: 'Upload paused',
uploadXFiles: {
'0': 'Upload %{smart_count} file',
'1': 'Upload %{smart_count} files',
0: 'Upload %{smart_count} file',
1: 'Upload %{smart_count} files',
},
uploadXNewFiles: {
'0': 'Upload +%{smart_count} file',
'1': 'Upload +%{smart_count} files',
0: 'Upload +%{smart_count} file',
1: 'Upload +%{smart_count} files',
},
xFilesSelected: {
'0': '%{smart_count} file selected',
'1': '%{smart_count} files selected',
0: '%{smart_count} file selected',
1: '%{smart_count} files selected',
},
xMoreFilesAdded: {
'0': '%{smart_count} more file added',
'1': '%{smart_count} more files added',
0: '%{smart_count} more file added',
1: '%{smart_count} more files added',
},
xTimeLeft: '%{time} left',
youCanOnlyUploadFileTypes: 'You can only upload: %{types}',
youCanOnlyUploadX: {
'0': 'You can only upload %{smart_count} file',
'1': 'You can only upload %{smart_count} files',
0: 'You can only upload %{smart_count} file',
1: 'You can only upload %{smart_count} files',
},
youHaveToAtLeastSelectX: {
'0': 'You have to select at least %{smart_count} file',
'1': 'You have to select at least %{smart_count} files',
0: 'You have to select at least %{smart_count} file',
1: 'You have to select at least %{smart_count} files',
},
zoomIn: 'Zoom in',
zoomOut: 'Zoom out',
Expand Down

0 comments on commit a166f96

Please sign in to comment.