Skip to content

Commit 1d3fd3e

Browse files
committed
Use ts-node
1 parent 2051435 commit 1d3fd3e

File tree

143 files changed

+439
-612
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+439
-612
lines changed

.svgrrc.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
template({ template }, _, { componentName, jsx }) {
3+
const typeScriptTpl = template.smart({ plugins: ['typescript'] })
4+
return typeScriptTpl.ast`
5+
import * as React from 'react';
6+
const ${componentName} = (props: React.SVGProps<SVGSVGElement>) => ${jsx};
7+
export default ${componentName};
8+
`
9+
}
10+
}

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,30 @@
2222
"en:hide-cards": "DEV_LOCALE=en next -p 9990",
2323
"dev": "rm -rf .next && concurrently \"yarn jp\" \"yarn en\" \"yarn contents:watch\" \"yarn twemoji:watch\"",
2424
"dev:hide-cards": "rm -rf .next && concurrently \"yarn jp:hide-cards\" \"yarn en:hide-cards\" \"yarn contents:watch\" \"yarn twemoji:watch\"",
25-
"contents": "node ./scripts/generateContentsBundle.js",
26-
"contents:watch": "node ./scripts/generateContentsBundle.js watch",
25+
"contents": "ts-node --project tsconfig.scripts.json ./scripts/generateContentsBundle.ts",
26+
"contents:watch": "ts-node --project tsconfig.scripts.json ./scripts/generateContentsBundle.ts watch",
2727
"tsc": "tsc",
2828
"eslint": "eslint --ext .js,.ts,.tsx .",
2929
"eslint:fix": "eslint --ext .js,.ts,.tsx --fix .",
3030
"build:en": "yarn tsc && yarn eslint && PRODUCTION_LOCALE=en next build && PRODUCTION_LOCALE=en next export",
3131
"build:jp": "yarn tsc && yarn eslint && PRODUCTION_LOCALE=jp next build && PRODUCTION_LOCALE=jp next export",
32-
"twemoji": "mkdir -p .twemoji && rm -f src/components/Twemoji/* && rm -f .twemoji/* && cp `node ./scripts/copyUsedEmojis.js` .twemoji && svgr --no-svgo --filename-case kebab --no-dimensions -d src/components/Twemoji .twemoji && yarn twemoji:bundle",
33-
"twemoji:bundle": "node ./scripts/generateEmojisBundle.js",
34-
"twemoji:watch": "node ./scripts/generateEmojisBundle.js watch",
35-
"svg": "rm -rf src/components/Svg && svgr -d src/components/Svg static/images/svg",
32+
"twemoji": "mkdir -p .twemoji && rm -f src/components/Twemoji/* && rm -f .twemoji/* && cp `ts-node --project tsconfig.scripts.json ./scripts/copyUsedEmojis.ts` .twemoji && svgr --ext tsx --no-svgo --filename-case kebab --no-dimensions -d src/components/Twemoji .twemoji && eslint --ext .tsx --fix src/components/Twemoji && yarn twemoji:bundle",
33+
"twemoji:bundle": "ts-node --project tsconfig.scripts.json ./scripts/generateEmojisBundle.ts",
34+
"twemoji:watch": "ts-node --project tsconfig.scripts.json ./scripts/generateEmojisBundle.ts watch",
3635
"type-check": "tsc -w",
3736
"test": "jest"
3837
},
3938
"devDependencies": {
4039
"@svgr/cli": "^4.3.0",
4140
"@types/color": "^3.0.0",
41+
"@types/glob": "^7.1.1",
4242
"@types/jest": "^24.0.13",
4343
"@types/lodash": "^4.14.134",
4444
"@types/luxon": "^1.15.1",
4545
"@types/material-ui": "^0.21.6",
4646
"@types/next": "^8.0.5",
4747
"@types/nprogress": "^0.2.0",
48+
"@types/prettier": "^1.16.4",
4849
"@types/react": "^16.8.19",
4950
"@types/react-dom": "^16.8.4",
5051
"@types/smoothscroll-polyfill": "^0.3.1",
@@ -67,6 +68,7 @@
6768
"jest": "^24.8.0",
6869
"prettier": "^1.18.2",
6970
"ts-jest": "^24.0.2",
71+
"ts-node": "^8.3.0",
7072
"typescript": "^3.5.1"
7173
}
7274
}

scripts/copyUsedEmojis.js renamed to scripts/copyUsedEmojis.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
const twemoji = require('twemoji')
2-
const uniq = require('lodash/uniq')
3-
const letterEmojis = Object.values(
1+
import twemoji from 'twemoji'
2+
import uniq from 'lodash/uniq'
3+
4+
const letterEmojis = Object.values<string>(
45
require('../src/lib/letterEmojiMappingJson.json')
56
)
6-
const numberEmojis = Object.values(
7+
const numberEmojis = Object.values<string>(
78
require('../src/lib/numberEmojiMappingJson.json')
89
)
910

1011
// NOTE: Disabling svgo because it's causing
1112
// some emojis like 😍 to be rendered incorrectly.
12-
const allUsedEmojis = uniq([
13+
const allUsedEmojis = uniq<string>([
1314
...letterEmojis,
1415
...numberEmojis,
1516
'🤔',
@@ -110,13 +111,15 @@ const allUsedEmojis = uniq([
110111
// Copied from Twemoji
111112
const UFE0Fg = /\uFE0F/g
112113
const U200D = String.fromCharCode(0x200d)
113-
function grabTheRightIcon(rawText) {
114+
function grabTheRightIcon(rawText: string) {
114115
// if variant is present as \uFE0F
115116
return twemoji.convert.toCodePoint(
116117
rawText.indexOf(U200D) < 0 ? rawText.replace(UFE0Fg, '') : rawText
117118
)
118119
}
119120

120121
console.log(
121-
allUsedEmojis.map(x => `.twemoji_svg/${grabTheRightIcon(x)}.svg`).join(' ')
122+
allUsedEmojis
123+
.map((x: string) => `.twemoji_svg/${grabTheRightIcon(x)}.svg`)
124+
.join(' ')
122125
)

scripts/generateContentsBundle.js

Lines changed: 0 additions & 84 deletions
This file was deleted.

scripts/generateContentsBundle.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import chokidar from 'chokidar'
2+
import glob from 'glob'
3+
import fs from 'fs'
4+
import prettier from 'prettier'
5+
6+
const regenerate = (path?: string) => {
7+
glob(
8+
'./src/contents/**/*.+(en|jp).tsx',
9+
(_: any, files: readonly string[]) => {
10+
const uniqueNames = [
11+
...new Set(
12+
files.map(x =>
13+
x.replace('./src/contents/', '').replace(/\.(en|jp)\.tsx/, '')
14+
)
15+
)
16+
]
17+
18+
const importString = uniqueNames
19+
.map(
20+
name =>
21+
`import Jp${name} from 'src/contents/${name}.jp'
22+
import En${name} from 'src/contents/${name}.en'`
23+
)
24+
.join('\n')
25+
26+
const bundleObjectString = uniqueNames
27+
.map(
28+
name => `
29+
'${name}': {
30+
en: En${name},
31+
jp: Jp${name}
32+
}
33+
`
34+
)
35+
.join(',\n')
36+
37+
const fileContents = prettier.format(
38+
`// WARNING: Do not modify this file - it's generated automatically.
39+
${importString}
40+
41+
export default {
42+
${bundleObjectString}
43+
}`,
44+
{ semi: false, singleQuote: true, parser: 'typescript' }
45+
)
46+
47+
fs.writeFile('./src/lib/contentsBundle.tsx', fileContents, err => {
48+
if (err) {
49+
throw err
50+
}
51+
if (path) {
52+
console.log(`${path} updated; Bundle regenerated`)
53+
} else {
54+
console.log('Bundle regenerated')
55+
}
56+
})
57+
}
58+
)
59+
}
60+
61+
if (process.argv[2] === 'watch') {
62+
chokidar
63+
.watch('./src/contents/**/*.tsx', { ignoreInitial: true })
64+
.on('add', path => regenerate(path))
65+
chokidar
66+
.watch('./src/contents/**/*.tsx')
67+
.on('unlink', path => regenerate(path))
68+
} else {
69+
regenerate()
70+
}

scripts/generateEmojisBundle.js renamed to scripts/generateEmojisBundle.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
const chokidar = require('chokidar')
2-
const glob = require('glob')
3-
const fs = require('fs')
4-
const prettier = require('prettier')
5-
const { exec } = require('child_process')
1+
import chokidar from 'chokidar'
2+
import glob from 'glob'
3+
import fs from 'fs'
4+
import prettier from 'prettier'
5+
import { exec } from 'child_process'
66

77
const regenerate = () => {
8-
glob('./src/components/Twemoji/*.js', (err, files) => {
8+
glob('./src/components/Twemoji/*.tsx', (_: any, files: readonly string[]) => {
99
const uniqueNames = [
1010
...new Set(
1111
files.map(x =>
12-
x.replace('./src/components/Twemoji/', '').replace(/\.js/, '')
12+
x.replace('./src/components/Twemoji/', '').replace(/\.tsx/, '')
1313
)
1414
)
1515
]
1616

17-
const toComponentName = name => `Emoji${name.replace(/-/g, 'ZZ')}`
17+
const toComponentName = (name: string) => `Emoji${name.replace(/-/g, 'ZZ')}`
1818

1919
const importString = uniqueNames
2020
.map(
@@ -29,24 +29,13 @@ const regenerate = () => {
2929
.map(name => `'${name}': ${toComponentName(name)}`)
3030
.join(',\n')
3131

32-
const bundleInterfaceString = uniqueNames
33-
.map(name => `'${name}': React.ComponentType<{}>`)
34-
.join('\n')
35-
3632
const fileContents = prettier.format(
3733
`// WARNING: Do not modify this file - it's generated automatically.
38-
import React from 'react'
3934
${importString}
4035
41-
export interface BundleTypes {
42-
${bundleInterfaceString}
43-
}
44-
45-
const bundle: BundleTypes = {
36+
export default {
4637
${bundleObjectString}
47-
}
48-
49-
export default bundle`,
38+
}`,
5039
{ semi: false, singleQuote: true, parser: 'typescript' }
5140
)
5241

@@ -62,13 +51,13 @@ const regenerate = () => {
6251
if (process.argv[2] === 'watch') {
6352
chokidar
6453
.watch(
65-
['./src/lib/letterEmojiMappingJson.json', './scripts/copyUsedEmojis.js'],
54+
['./src/lib/letterEmojiMappingJson.json', './scripts/copyUsedEmojis.ts'],
6655
{ ignoreInitial: true }
6756
)
6857
.on('change', path => {
6958
exec('yarn twemoji', err => {
7059
if (err) {
71-
throw new Error(err)
60+
throw new Error()
7261
}
7362
console.log(`${path} updated`)
7463
})

scripts/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../tsconfig.scripts.json"
3+
}

src/components/Content.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react'
2-
import bundle, { BundleTypes } from 'src/lib/contentsBundle'
2+
import bundle from 'src/lib/contentsBundle'
33
import locale from 'src/lib/locale'
44

55
export interface ContentProps {
6-
name: keyof BundleTypes
6+
name: keyof typeof bundle
77
}
88

99
const Content = ({ name }: ContentProps) => {

src/components/Twemoji/1f170.js renamed to src/components/Twemoji/1f170.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React from 'react'
1+
import * as React from 'react'
22

3-
const Svg1F170 = props => (
3+
const Svg1F170 = (props: React.SVGProps<SVGSVGElement>) => (
44
<svg viewBox="0 0 36 36" {...props}>
55
<path
66
fill="#DD2E44"

src/components/Twemoji/1f171.js renamed to src/components/Twemoji/1f171.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React from 'react'
1+
import * as React from 'react'
22

3-
const Svg1F171 = props => (
3+
const Svg1F171 = (props: React.SVGProps<SVGSVGElement>) => (
44
<svg viewBox="0 0 36 36" {...props}>
55
<path
66
fill="#DD2E44"

src/components/Twemoji/1f193.js renamed to src/components/Twemoji/1f193.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React from 'react'
1+
import * as React from 'react'
22

3-
const Svg1F193 = props => (
3+
const Svg1F193 = (props: React.SVGProps<SVGSVGElement>) => (
44
<svg viewBox="0 0 36 36" {...props}>
55
<path
66
fill="#3B88C3"

src/components/Twemoji/1f195.js renamed to src/components/Twemoji/1f195.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React from 'react'
1+
import * as React from 'react'
22

3-
const Svg1F195 = props => (
3+
const Svg1F195 = (props: React.SVGProps<SVGSVGElement>) => (
44
<svg viewBox="0 0 36 36" {...props}>
55
<path
66
fill="#3B88C3"

src/components/Twemoji/1f197.js renamed to src/components/Twemoji/1f197.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React from 'react'
1+
import * as React from 'react'
22

3-
const Svg1F197 = props => (
3+
const Svg1F197 = (props: React.SVGProps<SVGSVGElement>) => (
44
<svg viewBox="0 0 36 36" {...props}>
55
<path
66
fill="#3B88C3"

src/components/Twemoji/1f19a.js renamed to src/components/Twemoji/1f19a.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React from 'react'
1+
import * as React from 'react'
22

3-
const Svg1F19A = props => (
3+
const Svg1F19A = (props: React.SVGProps<SVGSVGElement>) => (
44
<svg viewBox="0 0 36 36" {...props}>
55
<path
66
fill="#F4900C"

0 commit comments

Comments
 (0)