Skip to content

Commit

Permalink
fix(cli): Split & refactor get-compiler.js from cli.js
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Aug 20, 2019
1 parent 7f4f480 commit 1ccc8db
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 54 deletions.
79 changes: 33 additions & 46 deletions packages/cli/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
#!/usr/bin/env node

import pluralData from 'cldr-core/supplemental/plurals.json'
import ordinalData from 'cldr-core/supplemental/ordinals.json'
import MakePluralCompiler from 'make-plural-compiler'
import yargs from 'yargs'
import getCompiler from './get-compiler'
import printCategoriesModule from './print-categories'
import printPluralsModule from './print-plurals'

function buildMakePlural({ cardinals, ordinals, width }) {
const MakePlural = MakePluralCompiler.load(pluralData, ordinalData)
MakePlural.cardinals = cardinals
MakePlural.ordinals = ordinals
if (width > 0) MakePlural.foldWidth = width
return MakePlural
}

function valueCommand(type) {
const valueDesc = `A numerical value. If left empty, all ${type} plural categories will be printed.`
return {
Expand All @@ -27,7 +17,7 @@ function valueCommand(type) {
type: 'string'
}),
handler({ locale, value }) {
const MakePlural = buildMakePlural({
const MakePlural = getCompiler({
cardinals: type === 'cardinal',
ordinals: type === 'ordinal'
})
Expand All @@ -39,54 +29,51 @@ function valueCommand(type) {
}
}

const moduleOptions = {
cardinals: {
default: true,
desc: 'Include cardinal plurals',
type: 'boolean'
},
ordinals: {
default: true,
desc: 'Include ordinal plurals',
type: 'boolean'
},
umd: {
desc: 'Output an UMD rather than an ES module',
type: 'boolean'
},
width: {
alias: 'w',
desc: 'Fold width for the output',
type: 'number'
}
}

const lcOpt = {
desc:
'Identifiers for locales to include in the module. If left empty, all available locales will be included.'
}
const moduleCommandBuilder = yargs =>
yargs
.positional('locale', {
desc:
'Identifiers for locales to include in the module. If left empty, all available locales will be included.'
})
.options({
cardinals: {
default: true,
desc: 'Include cardinal plurals',
type: 'boolean'
},
ordinals: {
default: true,
desc: 'Include ordinal plurals',
type: 'boolean'
},
umd: {
desc: 'Output an UMD rather than an ES module',
type: 'boolean'
},
width: {
alias: 'w',
desc: 'Fold width for the output',
type: 'number'
}
})

yargs
.command(valueCommand('cardinal'))
.command(valueCommand('ordinal'))
.command({
command: 'plurals [locale...]',
desc: 'Print the plural functions as the source of a JS module',
builder: yargs => yargs.positional('locale', lcOpt).options(moduleOptions),
builder: moduleCommandBuilder,
handler(args) {
const MakePlural = buildMakePlural(args)
const str = printPluralsModule(MakePlural, args)
process.stdout.write(str)
process.stdout.write(printPluralsModule(args))
}
})
.command({
command: 'categories [locale...]',
desc: 'Print the plural categories as the source of a JS module',
builder: yargs => yargs.positional('locale', lcOpt).options(moduleOptions),
builder: moduleCommandBuilder,
handler(args) {
const MakePlural = buildMakePlural(args)
const str = printCategoriesModule(MakePlural, args)
process.stdout.write(str)
process.stdout.write(printCategoriesModule(args))
}
})
.help()
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/get-compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pluralData from 'cldr-core/supplemental/plurals.json'
import ordinalData from 'cldr-core/supplemental/ordinals.json'
import MakePluralCompiler from 'make-plural-compiler'

export default function getCompiler({ cardinals, ordinals, width }) {
const MakePlural = MakePluralCompiler.load(pluralData, ordinalData)
MakePlural.cardinals = cardinals
MakePlural.ordinals = ordinals
if (width > 0) MakePlural.foldWidth = width
return MakePlural
}
9 changes: 5 additions & 4 deletions packages/cli/src/print-categories.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { identifier } from 'safe-identifier'
import * as common from './common'
import getCompiler from './get-compiler'
import printUMD from './print-umd'

export default function printCategoriesModule(
MakePlural,
{ locale, ordinals, umd }
) {
export default function printCategoriesModule(args) {
const MakePlural = getCompiler(args)
const { locale, ordinals, umd } = args
const locales =
locale.length === 0 ? Object.keys(MakePlural.rules.cardinal) : locale.sort()
const commonCategories = ordinals
? common.combined.categories
: common.cardinals.categories

const usedCommonCategories = {}
const categories = locales.map(lc => {
const mpc = new MakePlural(lc)
Expand Down
9 changes: 5 additions & 4 deletions packages/cli/src/print-plurals.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import aliases from 'cldr-core/supplemental/aliases.json'
import { identifier } from 'safe-identifier'
import * as common from './common'
import getCompiler from './get-compiler'
import printUMD from './print-umd'

function getAlias(MakePlural, lc) {
Expand All @@ -10,14 +11,14 @@ function getAlias(MakePlural, lc) {
return MakePlural.rules.cardinal[r] ? r : null // https://unicode-org.atlassian.net/browse/CLDR-13227
}

export default function printPluralsModule(
MakePlural,
{ cardinals, locale, ordinals, umd }
) {
export default function printPluralsModule(args) {
const MakePlural = getCompiler(args)
const { cardinals, locale, ordinals, umd } = args
const locales =
locale.length === 0 ? Object.keys(MakePlural.rules.cardinal) : locale.sort()
const commonPlurals =
cardinals && ordinals ? common.combined.plurals : common.cardinals.plurals

const aliased = []
const usedCommonPlurals = {}
const plurals = locales.map(lc => {
Expand Down

0 comments on commit 1ccc8db

Please sign in to comment.