Skip to content

Commit

Permalink
feat: allow loading specific configuration using --config arg (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonc authored and tricoder42 committed May 17, 2019
1 parent c91de56 commit fd7ddb0
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 4 deletions.
8 changes: 8 additions & 0 deletions docs/ref/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ Install
}
}
Global options
==============

.. lingui-cli-option:: --config <config>

Path to LinguiJS configuration file. If not set, the default file
is loaded as described in :doc:`LinguiJS configuration </ref/conf>` reference.

Commands
========

Expand Down
8 changes: 6 additions & 2 deletions packages/cli/src/api/pseudoLocalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ describe("PseudoLocalization", () => {
pseudoLocalize("{value, plural, one {# book} other {# books}}")
).toEqual("{value, plural, one {# ƀōōķ} other {# ƀōōķś}}")
expect(
pseudoLocalize("{count, plural, one {{countString} book} other {{countString} books}}")
).toEqual("{count, plural, one {{countString} ƀōōķ} other {{countString} ƀōōķś}}")
pseudoLocalize(
"{count, plural, one {{countString} book} other {{countString} books}}"
)
).toEqual(
"{count, plural, one {{countString} ƀōōķ} other {{countString} ƀōōķś}}"
)
})

it("shouldn't pseudolocalize variables", () => {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/lingui-add-locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ if (require.main === module) {
"directory from your localeDir (e.g. ./locale/en)"
)
.arguments("<locale...>")
.option("--config <path>", "Path to the config file")
.option("--format <format>", "Format of message catalog")
.on("--help", function() {
console.log("\n Examples:\n")
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/lingui-compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ if (require.main === module) {
.description(
"Add compile message catalogs and add language data (plurals) to compiled bundle."
)
.option("--config <path>", "Path to the config file")
.option("--strict", "Disable defaults for missing translations")
.option("--verbose", "Verbose output")
.option("--format <format>", "Format of message catalog")
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/lingui-extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export default function command(

if (require.main === module) {
program
.option("--config <path>", "Path to the config file")
.option("--overwrite", "Overwrite translations for source locale")
.option("--clean", "Remove obsolete translations")
.option(
Expand Down
25 changes: 23 additions & 2 deletions packages/conf/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require("path")
const fs = require("fs")
const chalk = require("chalk")
const cosmiconfig = require("cosmiconfig")
const { validate } = require("jest-validate")
Expand Down Expand Up @@ -74,10 +75,30 @@ const configValidation = {
comment: "See https://lingui.js.org/ref/conf.html for a list of valid options"
}

function configFilePathFromArgs() {
const configIndex = process.argv.indexOf("--config")

if (
configIndex >= 0 &&
process.argv.length > configIndex &&
fs.existsSync(process.argv[configIndex + 1])
) {
return process.argv[configIndex + 1]
}

return null
}

export function getConfig({ cwd } = {}) {
const defaultRootDir = cwd || process.cwd()
const configExplorer = cosmiconfig("lingui")
const result = configExplorer.searchSync(defaultRootDir)
const defaultRootDir = cwd || process.cwd()
const configPath = configFilePathFromArgs()

const result =
configPath == null
? configExplorer.searchSync(defaultRootDir)
: configExplorer.loadSync(configPath)

const raw = { ...defaultConfig, ...(result ? result.config : {}) }

validate(raw, configValidation)
Expand Down
47 changes: 47 additions & 0 deletions packages/conf/src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
import { getConfig, replaceRootDir } from "@lingui/conf"
import cosmiconfig from "cosmiconfig"

const mockExplorer = {
searchSync: jest.fn(),
loadSync: jest.fn()
}

jest.mock("cosmiconfig", function() {
return function() {
return mockExplorer
}
})

jest.mock("fs", function() {
return {
existsSync: function() {
return true
}
}
})

beforeEach(function() {
cosmiconfig().loadSync.mockClear()
cosmiconfig().searchSync.mockClear()
})

describe("lingui-conf", function() {
it("should return default config", function() {
Expand Down Expand Up @@ -27,4 +52,26 @@ describe("lingui-conf", function() {
expect(config.localeDir).toEqual("/Root")
expect(config.srcPathDirs).toEqual(["/Root", "rootDir"])
})

it("searches for a config file", function() {
getConfig()
expect(cosmiconfig().searchSync).toHaveBeenCalled()
})

describe("with --config command line argument", function() {
beforeEach(function() {
process.argv.push("--config")
process.argv.push("./lingui/myconfig")
})

afterEach(function() {
process.argv.splice(process.argv.length - 2, 2)
})

it("allows specific config file to be loaded", function() {
getConfig()
expect(cosmiconfig().searchSync).not.toHaveBeenCalled()
expect(cosmiconfig().loadSync).toHaveBeenCalledWith("./lingui/myconfig")
})
})
})

0 comments on commit fd7ddb0

Please sign in to comment.