From 871f8867023804e8685f405c95cebae76b955e24 Mon Sep 17 00:00:00 2001 From: rinsuki <428rinsuki+git@gmail.com> Date: Sun, 17 Jun 2018 19:09:24 +0900 Subject: [PATCH] build:ts success --- gulpfile.ts | 4 ++-- locales/index.ts | 14 ++++++++++--- src/build/fa.ts | 16 +++++++-------- src/build/i18n.ts | 21 ++++++++++--------- src/client/docs/api/gulpfile.ts | 19 ++++++++++------- src/client/docs/gulpfile.ts | 4 ++-- src/client/docs/vars.ts | 2 +- yarn.lock | 36 ++++++++++++++++++++++----------- 8 files changed, 72 insertions(+), 44 deletions(-) diff --git a/gulpfile.ts b/gulpfile.ts index fa1155878..49a80879d 100644 --- a/gulpfile.ts +++ b/gulpfile.ts @@ -8,12 +8,12 @@ import * as gutil from 'gulp-util'; import * as ts from 'gulp-typescript'; const sourcemaps = require('gulp-sourcemaps'); import tslint from 'gulp-tslint'; -import cssnano = require('gulp-cssnano'); +const cssnano = require('gulp-cssnano'); import * as uglifyComposer from 'gulp-uglify/composer'; import pug = require('gulp-pug'); import * as rimraf from 'rimraf'; import chalk from 'chalk'; -import imagemin = require('gulp-imagemin'); +const imagemin = require('gulp-imagemin'); import * as rename from 'gulp-rename'; import * as mocha from 'gulp-mocha'; import * as replace from 'gulp-replace'; diff --git a/locales/index.ts b/locales/index.ts index 319d178e0..3b4f76b06 100644 --- a/locales/index.ts +++ b/locales/index.ts @@ -5,12 +5,16 @@ import * as fs from 'fs'; import * as yaml from 'js-yaml'; -const loadLang = lang => yaml.safeLoad( - fs.readFileSync(`./locales/${lang}.yml`, 'utf-8')); +export type LangKey = 'de' | 'en' | 'fr' | 'ja' | 'pl'; +export type LocaleObjectChildren = LocaleObject | string | undefined; +export type LocaleObject = {[key: string]: LocaleObjectChildren }; + +const loadLang = (lang: LangKey) => yaml.safeLoad( + fs.readFileSync(`./locales/${lang}.yml`, 'utf-8')) as LocaleObject; const native = loadLang('ja'); -const langs = { +const langs: {[key in LangKey]: LocaleObject} = { 'de': loadLang('de'), 'en': loadLang('en'), 'fr': loadLang('fr'), @@ -23,4 +27,8 @@ Object.entries(langs).map(([, locale]) => { locale = Object.assign({}, native, locale); }); +export function isAvailableLanguage(lang: string): lang is LangKey { + return lang in langs; +} + export default langs; diff --git a/src/build/fa.ts b/src/build/fa.ts index 111c19ae6..077bb51e6 100644 --- a/src/build/fa.ts +++ b/src/build/fa.ts @@ -3,18 +3,18 @@ */ import * as fontawesome from '@fortawesome/fontawesome'; -import * as regular from '@fortawesome/fontawesome-free-regular'; -import * as solid from '@fortawesome/fontawesome-free-solid'; -import * as brands from '@fortawesome/fontawesome-free-brands'; +import regular from '@fortawesome/fontawesome-free-regular'; +import solid from '@fortawesome/fontawesome-free-solid'; +import brands from '@fortawesome/fontawesome-free-brands'; fontawesome.library.add(regular, solid, brands); export const pattern = /%fa:(.+?)%/g; -export const replacement = (match, key) => { +export const replacement = (match: string, key: string) => { const args = key.split(' '); let prefix = 'fas'; - const classes = []; + const classes: string[] = []; let transform = ''; let name; @@ -34,12 +34,12 @@ export const replacement = (match, key) => { } }); - const icon = fontawesome.icon({ prefix, iconName: name }, { - classes: classes + const icon = fontawesome.icon({ prefix, iconName: name } as fontawesome.IconLookup, { + classes: classes, + transform: fontawesome.parse.transform(transform) }); if (icon) { - icon.transform = fontawesome.parse.transform(transform); return `${icon.html[0]}`; } else { console.warn(`'${name}' not found in fa`); diff --git a/src/build/i18n.ts b/src/build/i18n.ts index 35854055d..308ff0da0 100644 --- a/src/build/i18n.ts +++ b/src/build/i18n.ts @@ -2,7 +2,7 @@ * Replace i18n texts */ -import locale from '../../locales'; +import locale, { isAvailableLanguage, LocaleObject, LocaleObjectChildren } from '../../locales'; export default class Replacer { private lang: string; @@ -16,19 +16,19 @@ export default class Replacer { this.replacement = this.replacement.bind(this); } - private get(path: string, key: string) { - const texts = locale[this.lang]; - - if (texts == null) { + private get(path: string, key: string): string { + if (!isAvailableLanguage(this.lang)) { console.warn(`lang '${this.lang}' is not supported`); return key; // Fallback } - let text = texts; + const texts = locale[this.lang]; + + let text: LocaleObjectChildren = texts; if (path) { if (text.hasOwnProperty(path)) { - text = text[path]; + text = text[path] as LocaleObject; } else { console.warn(`path '${path}' not found in '${this.lang}'`); return key; // Fallback @@ -38,7 +38,7 @@ export default class Replacer { // Check the key existance const error = key.split('.').some(k => { if (text.hasOwnProperty(k)) { - text = text[k]; + text = (text as LocaleObject)[k]; return false; } else { return true; @@ -48,12 +48,15 @@ export default class Replacer { if (error) { console.warn(`key '${key}' not found in '${path}' of '${this.lang}'`); return key; // Fallback + } else if (typeof text !== "string") { + console.warn(`key '${key}' is not string in '${path}' of '${this.lang}'`); + return key; // Fallback } else { return text; } } - public replacement(match, key) { + public replacement(match: string, key: string) { let path = null; if (key.indexOf('|') != -1) { diff --git a/src/client/docs/api/gulpfile.ts b/src/client/docs/api/gulpfile.ts index 9980ede23..0eb8b8828 100644 --- a/src/client/docs/api/gulpfile.ts +++ b/src/client/docs/api/gulpfile.ts @@ -19,9 +19,10 @@ import generateVars from '../vars'; const langs = Object.keys(locales); -const kebab = string => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase(); +const kebab = (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase(); -const parseParam = param => { +// WIP type +const parseParam = (param: any) => { const id = param.type.match(/^id\((.+?)\)|^id/); const entity = param.type.match(/^entity\((.+?)\)/); const isObject = /^object/.test(param.type); @@ -57,7 +58,7 @@ const parseParam = param => { return param; }; -const sortParams = params => { +const sortParams = (params: Array<{name: string}>) => { params.sort((a, b) => { if (a.name < b.name) return -1; @@ -68,14 +69,15 @@ const sortParams = params => { return params; }; -const extractDefs = params => { - let defs = []; +// WIP type +const extractDefs = (params: any[]) => { + let defs: any[] = []; params.forEach(param => { if (param.def) { defs.push({ name: param.defName, - params: sortParams(param.def.map(p => parseParam(p))) + params: sortParams(param.def.map((p: any) => parseParam(p))) }); const childDefs = extractDefs(param.def); @@ -109,8 +111,10 @@ gulp.task('doc:api:endpoints', async () => { path: ep.endpoint }, desc: ep.desc, + // @ts-ignore params: sortParams(ep.params.map(p => parseParam(p))), paramDefs: extractDefs(ep.params), + // @ts-ignore res: ep.res ? sortParams(ep.res.map(p => parseParam(p))) : null, resDefs: ep.res ? extractDefs(ep.res) : null, }; @@ -155,7 +159,8 @@ gulp.task('doc:api:entities', async () => { const vars = { name: entity.name, desc: entity.desc, - props: sortParams(entity.props.map(p => parseParam(p))), + // WIP type + props: sortParams(entity.props.map((p: any) => parseParam(p))), propDefs: extractDefs(entity.props), }; langs.forEach(lang => { diff --git a/src/client/docs/gulpfile.ts b/src/client/docs/gulpfile.ts index 56bf6188c..4683a0465 100644 --- a/src/client/docs/gulpfile.ts +++ b/src/client/docs/gulpfile.ts @@ -8,8 +8,8 @@ import * as glob from 'glob'; import * as gulp from 'gulp'; import * as pug from 'pug'; import * as mkdirp from 'mkdirp'; -import stylus = require('gulp-stylus'); -import cssnano = require('gulp-cssnano'); +const stylus = require('gulp-stylus'); +const cssnano = require('gulp-cssnano'); import I18nReplacer from '../../build/i18n'; import fa from '../../build/fa'; diff --git a/src/client/docs/vars.ts b/src/client/docs/vars.ts index 32b961aaa..93082767e 100644 --- a/src/client/docs/vars.ts +++ b/src/client/docs/vars.ts @@ -38,7 +38,7 @@ export default async function(): Promise<{ [key: string]: any }> { vars['docs'][name]['title'][lang] = fs.readFileSync(x, 'utf-8').match(/^h1 (.+?)\r?\n/)[1]; }); - vars['kebab'] = string => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase(); + vars['kebab'] = (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase(); vars['config'] = config; diff --git a/yarn.lock b/yarn.lock index a99e49862..8b12a99c9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,21 +2,33 @@ # yarn lockfile v1 -"@fortawesome/fontawesome-free-brands@5.0.2": - version "5.0.2" - resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-brands/-/fontawesome-free-brands-5.0.2.tgz#a1cc602eec40a379a3dd8a44c78b31110dd3d3d3" +"@fortawesome/fontawesome-common-types@^0.1.7": + version "0.1.7" + resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-common-types/-/fontawesome-common-types-0.1.7.tgz#4336c4b06d0b5608ff1215464b66fcf9f4795284" -"@fortawesome/fontawesome-free-regular@5.0.2": - version "5.0.2" - resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-regular/-/fontawesome-free-regular-5.0.2.tgz#429af86bed14689f87648e6322983c65c782c017" +"@fortawesome/fontawesome-free-brands@5.0.13": + version "5.0.13" + resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-brands/-/fontawesome-free-brands-5.0.13.tgz#4d15ff4e1e862d5e4a4df3654f8e8acbd47e9c09" + dependencies: + "@fortawesome/fontawesome-common-types" "^0.1.7" -"@fortawesome/fontawesome-free-solid@5.0.2": - version "5.0.2" - resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-solid/-/fontawesome-free-solid-5.0.2.tgz#090ce2c59dd5ec76983f3da8a43e1ab0321b42d5" +"@fortawesome/fontawesome-free-regular@5.0.13": + version "5.0.13" + resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-regular/-/fontawesome-free-regular-5.0.13.tgz#eb78c30184e3f456a423a1dcfa0f682f7b50de4a" + dependencies: + "@fortawesome/fontawesome-common-types" "^0.1.7" -"@fortawesome/fontawesome@1.0.1": - version "1.0.1" - resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome/-/fontawesome-1.0.1.tgz#8ac60e1e7b437889baf9c9d6e3a61ef3b637170d" +"@fortawesome/fontawesome-free-solid@5.0.13": + version "5.0.13" + resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-solid/-/fontawesome-free-solid-5.0.13.tgz#24b61aaf471a9d34a5364b052d64a516285ba894" + dependencies: + "@fortawesome/fontawesome-common-types" "^0.1.7" + +"@fortawesome/fontawesome@1.1.8": + version "1.1.8" + resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome/-/fontawesome-1.1.8.tgz#75fe66a60f95508160bb16bd781ad7d89b280f5b" + dependencies: + "@fortawesome/fontawesome-common-types" "^0.1.7" "@gulp-sourcemaps/identity-map@1.X": version "1.0.1"