From fd7ddb072ad6af85814f79feb35110d887af095e Mon Sep 17 00:00:00 2001 From: Brandon Croft Date: Fri, 17 May 2019 08:40:39 +0200 Subject: [PATCH] feat: allow loading specific configuration using --config arg (#501) --- docs/ref/cli.rst | 8 ++++ packages/cli/src/api/pseudoLocalize.test.js | 8 +++- packages/cli/src/lingui-add-locale.js | 1 + packages/cli/src/lingui-compile.js | 1 + packages/cli/src/lingui-extract.js | 1 + packages/conf/src/index.js | 25 ++++++++++- packages/conf/src/index.test.js | 47 +++++++++++++++++++++ 7 files changed, 87 insertions(+), 4 deletions(-) diff --git a/docs/ref/cli.rst b/docs/ref/cli.rst index e52498d77..c83373ea6 100644 --- a/docs/ref/cli.rst +++ b/docs/ref/cli.rst @@ -44,6 +44,14 @@ Install } } +Global options +============== + +.. lingui-cli-option:: --config + +Path to LinguiJS configuration file. If not set, the default file +is loaded as described in :doc:`LinguiJS configuration ` reference. + Commands ======== diff --git a/packages/cli/src/api/pseudoLocalize.test.js b/packages/cli/src/api/pseudoLocalize.test.js index eef9b0e8f..e354e919b 100644 --- a/packages/cli/src/api/pseudoLocalize.test.js +++ b/packages/cli/src/api/pseudoLocalize.test.js @@ -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", () => { diff --git a/packages/cli/src/lingui-add-locale.js b/packages/cli/src/lingui-add-locale.js index bcffa2188..3be4428a3 100644 --- a/packages/cli/src/lingui-add-locale.js +++ b/packages/cli/src/lingui-add-locale.js @@ -41,6 +41,7 @@ if (require.main === module) { "directory from your localeDir (e.g. ./locale/en)" ) .arguments("") + .option("--config ", "Path to the config file") .option("--format ", "Format of message catalog") .on("--help", function() { console.log("\n Examples:\n") diff --git a/packages/cli/src/lingui-compile.js b/packages/cli/src/lingui-compile.js index 1b4f29796..8ae5fe23f 100644 --- a/packages/cli/src/lingui-compile.js +++ b/packages/cli/src/lingui-compile.js @@ -117,6 +117,7 @@ if (require.main === module) { .description( "Add compile message catalogs and add language data (plurals) to compiled bundle." ) + .option("--config ", "Path to the config file") .option("--strict", "Disable defaults for missing translations") .option("--verbose", "Verbose output") .option("--format ", "Format of message catalog") diff --git a/packages/cli/src/lingui-extract.js b/packages/cli/src/lingui-extract.js index 371d91561..73907b508 100644 --- a/packages/cli/src/lingui-extract.js +++ b/packages/cli/src/lingui-extract.js @@ -134,6 +134,7 @@ export default function command( if (require.main === module) { program + .option("--config ", "Path to the config file") .option("--overwrite", "Overwrite translations for source locale") .option("--clean", "Remove obsolete translations") .option( diff --git a/packages/conf/src/index.js b/packages/conf/src/index.js index a11754037..6a688861a 100644 --- a/packages/conf/src/index.js +++ b/packages/conf/src/index.js @@ -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") @@ -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) diff --git a/packages/conf/src/index.test.js b/packages/conf/src/index.test.js index eda7a6475..8e8221399 100644 --- a/packages/conf/src/index.test.js +++ b/packages/conf/src/index.test.js @@ -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() { @@ -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") + }) + }) })