Skip to content

Commit

Permalink
refactor: enhace events
Browse files Browse the repository at this point in the history
  • Loading branch information
leftstick committed Oct 3, 2019
1 parent e2c4a9b commit 75f4741
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/services/localesParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { IResourceMatcherToken, IResourceMatcher } from '@/src/services/resource
import { flatten } from '@/src/helpers/object'
import { langFromPath } from '@/src/helpers/text'
import { ILocaleKey, ILocale } from '@/src/types'
import { LOCALE_PARSE_EVENTS } from '@/src/types/events'
import { PARSE_EVENTS } from '@/src/types/events'
import { BABEL_PARSER_OPTIONS } from '@/src/helpers/value'

export interface ILocaleParser extends EventEmitter {
Expand All @@ -40,15 +40,15 @@ export class LocalParser extends EventEmitter implements ILocaleParser {
public async parse(): Promise<ILocale[]> {
const localeFilepaths = await this.resourceMatcher.getLocaleFiles()

this.emit(LOCALE_PARSE_EVENTS.START, localeFilepaths)
this.emit(PARSE_EVENTS.START, localeFilepaths)

const localeFiles = flatten<string>(localeFilepaths)

const localeKeys = await Promise.all(
localeFiles.map(async l => {
const data = await parseFileToLocale(l)

this.emit(LOCALE_PARSE_EVENTS.PARSED, l)
this.emit(PARSE_EVENTS.PARSED, l)

return data
})
Expand Down
6 changes: 3 additions & 3 deletions src/services/sourceParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {

import { IResourceMatcherToken, IResourceMatcher } from '@/src/services/resourceMatcher'
import { ISource } from '@/src/types'
import { SOURCE_PARSE_EVENTS } from '@/src/types/events'
import { PARSE_EVENTS } from '@/src/types/events'
import { toILoc } from '@/src/helpers/object'
import { BABEL_PARSER_OPTIONS } from '@/src/helpers/value'

Expand All @@ -39,7 +39,7 @@ export class SourceParser extends EventEmitter implements ISourceParser {
public async parse(): Promise<ISource[]> {
const sourceFiles = await this.resourceMatcher.getSourceFiles()

this.emit(SOURCE_PARSE_EVENTS.START, sourceFiles)
this.emit(PARSE_EVENTS.START, sourceFiles)

const sources: ISource[] = await this.parseRest(sourceFiles)
return sources.filter(s => s.keys.length)
Expand Down Expand Up @@ -102,7 +102,7 @@ export class SourceParser extends EventEmitter implements ISourceParser {
}
})

this.emit(SOURCE_PARSE_EVENTS.PARSED, filePath)
this.emit(PARSE_EVENTS.PARSED, filePath)

return [source].concat(await this.parseRest(filePaths.slice(1)))
}
Expand Down
11 changes: 3 additions & 8 deletions src/types/events.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
export enum LOCALE_PARSE_EVENTS {
START = 'LOCALE_PARSE_START',
PARSED = 'LOCALE_PARSE_PARSED'
}

export enum SOURCE_PARSE_EVENTS {
START = 'SOURCE_PARSE_START',
PARSED = 'SOURCE_PARSE_PARSED'
export enum PARSE_EVENTS {
START = 'PARSE_START',
PARSED = 'PARSE_PARSED'
}
10 changes: 5 additions & 5 deletions src/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { flatten } from '@/src/helpers/object'
import { ILocaleParserToken } from '@/src/services/localesParser'
import { ISourceParserToken } from '@/src/services/sourceParser'
import { ILocale, IUnUsedWarning, IUndefinedWarning, ISource, IKey } from '@/src/types'
import { LOCALE_PARSE_EVENTS, SOURCE_PARSE_EVENTS } from '@/src/types/events'
import { PARSE_EVENTS } from '@/src/types/events'
import { toPercent } from '@/src/helpers/cal'

export async function validate() {
Expand All @@ -35,12 +35,12 @@ function progress(localeEmitter: EventEmitter, sourceEmitter: EventEmitter) {
let localeSpinnerCounted: number = 0
let sourceSpinnerTotal: number = null
let sourceSpinnerCounted: number = 0
localeEmitter.on(LOCALE_PARSE_EVENTS.START, (localeFilepaths: string[]) => {
localeEmitter.on(PARSE_EVENTS.START, (localeFilepaths: string[]) => {
localeSpinner = ora('Parsing locales 0%').start()
localeSpinnerTotal = localeFilepaths.length
})

localeEmitter.on(LOCALE_PARSE_EVENTS.PARSED, (localeFilepath: string) => {
localeEmitter.on(PARSE_EVENTS.PARSED, (localeFilepath: string) => {
localeSpinnerCounted++
localeSpinner.text = `Parsing locales ${toPercent(
localeSpinnerTotal,
Expand All @@ -52,12 +52,12 @@ function progress(localeEmitter: EventEmitter, sourceEmitter: EventEmitter) {
}
})

sourceEmitter.on(SOURCE_PARSE_EVENTS.START, (sourceFilepaths: string[]) => {
sourceEmitter.on(PARSE_EVENTS.START, (sourceFilepaths: string[]) => {
sourceSpinner = ora('Parsing locales 0%').start()
sourceSpinnerTotal = sourceFilepaths.length
})

sourceEmitter.on(SOURCE_PARSE_EVENTS.PARSED, (sourceFilepath: string) => {
sourceEmitter.on(PARSE_EVENTS.PARSED, (sourceFilepath: string) => {
sourceSpinnerCounted++
sourceSpinner.text = `Parsing sources ${toPercent(
sourceSpinnerTotal,
Expand Down

0 comments on commit 75f4741

Please sign in to comment.