Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle duplicate filenames of intermediate catalogs #246

Merged
merged 1 commit into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/babel-plugin-extract-messages/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,15 @@ export default function({ types: t }) {
*/
const localeDir = this.opts.localeDir || opts.localeDir
const { filename } = file.opts
const [basename] = fsPath.basename(filename).split(".", 2)
const baseDir = fsPath.dirname(fsPath.relative(optsBaseDir, filename))
const targetDir = fsPath.join(localeDir, "_build", baseDir)

const messages = file.get(MESSAGES)
const catalog = {}
const catalogFilename = fsPath.join(targetDir, `${basename}.json`)
const baseName = fsPath.basename(filename)
const catalogFilename = fsPath.join(targetDir, `${baseName}.json`)

mkdirp.sync(targetDir)

// no messages, skip file
if (!messages.size) {
Expand All @@ -235,7 +237,6 @@ export default function({ types: t }) {
catalog[key] = value
})

mkdirp.sync(targetDir)
fs.writeFileSync(catalogFilename, JSON.stringify(catalog, null, 2))
}
}
Expand Down
20 changes: 10 additions & 10 deletions packages/babel-plugin-extract-messages/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,21 @@ describe("babel-plugin-lingui-extract-messages", function() {
"shouldn't write catalog for files without translatable messages",
transform => {
expect(transform("empty.js")).not.toThrow()
expect(fs.existsSync(path.join(buildDir, "empty.json"))).toBeFalsy()
expect(fs.existsSync(path.join(buildDir, "empty.js.json"))).toBeFalsy()
}
)

testCase("should preserve path to file inside locale dir", transform => {
expect(transform("jsx/deep/all.js")).not.toThrow()
expect(
fs.existsSync(path.join(buildDir, "jsx", "deep", "all.json"))
fs.existsSync(path.join(buildDir, "jsx", "deep", "all.js.json"))
).toBeTruthy()
})

testCase("should ignore files without lingui import", transform => {
expect(transform("jsx/without-lingui.js")).not.toThrow()
expect(
fs.existsSync(path.join(buildDir, "jsx", "without-lingui.json"))
fs.existsSync(path.join(buildDir, "jsx", "without-lingui.js.json"))
).toBeFalsy()
})

Expand All @@ -117,7 +117,7 @@ describe("babel-plugin-lingui-extract-messages", function() {
)

const messages = JSON.parse(
fs.readFileSync(path.join(buildDir, "noop", "actual.json"))
fs.readFileSync(path.join(buildDir, "noop", "actual.js.json"))
)
expect(messages).toMatchSnapshot()
})
Expand All @@ -129,7 +129,7 @@ describe("babel-plugin-lingui-extract-messages", function() {
expect(transform("jsx/all.js")).not.toThrow()

const messages = JSON.parse(
fs.readFileSync(path.join(buildDir, "jsx", "all.json"))
fs.readFileSync(path.join(buildDir, "jsx", "all.js.json"))
)
expect(messages).toMatchSnapshot()
})
Expand All @@ -143,7 +143,7 @@ describe("babel-plugin-lingui-extract-messages", function() {
expect(transform("jsx/integration.js")).not.toThrow()

const messages = JSON.parse(
fs.readFileSync(path.join(buildDir, "jsx", "integration.json"))
fs.readFileSync(path.join(buildDir, "jsx", "integration.js.json"))
)
expect(messages).toMatchSnapshot()
}
Expand All @@ -159,7 +159,7 @@ describe("babel-plugin-lingui-extract-messages", function() {

const messages = JSON.parse(
fs.readFileSync(
path.join(buildDir, "jsx", "integration-with-aliases.json")
path.join(buildDir, "jsx", "integration-with-aliases.js.json")
)
)
expect(messages).toMatchSnapshot()
Expand All @@ -173,7 +173,7 @@ describe("babel-plugin-lingui-extract-messages", function() {
expect(transform("js/all.js", false)).not.toThrow()

const messages = JSON.parse(
fs.readFileSync(path.join(buildDir, "js", "all.json"))
fs.readFileSync(path.join(buildDir, "js", "all.js.json"))
)
expect(messages).toMatchSnapshot()
})
Expand All @@ -187,7 +187,7 @@ describe("babel-plugin-lingui-extract-messages", function() {
expect(transform("js/integration.js", false)).not.toThrow()

const messages = JSON.parse(
fs.readFileSync(path.join(buildDir, "js", "integration.json"))
fs.readFileSync(path.join(buildDir, "js", "integration.js.json"))
)
expect(messages).toMatchSnapshot()
}
Expand Down Expand Up @@ -215,7 +215,7 @@ describe("babel-plugin-lingui-extract-messages", function() {
).not.toThrow()

const messages = JSON.parse(
fs.readFileSync(path.join(buildDir, "jsx", "with-react.json"))
fs.readFileSync(path.join(buildDir, "jsx", "with-react.js.json"))
)
expect(messages).toMatchSnapshot()
})
Expand Down
25 changes: 25 additions & 0 deletions packages/cli/src/api/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import fs from "fs"
import path from "path"

export function removeDirectory(dir, keep = false) {
if (!fs.existsSync(dir)) return
const list = fs.readdirSync(dir)

for (let i = 0; i < list.length; i++) {
const filename = path.join(dir, list[i])
const stat = fs.statSync(filename)

if (filename === "." || filename === "..") {
// pass these files
} else if (stat.isDirectory()) {
// rmdir recursively
removeDirectory(filename)
} else {
fs.unlinkSync(filename)
}
}

if (!keep) {
fs.rmdirSync(dir)
}
}
6 changes: 5 additions & 1 deletion packages/cli/src/lingui-extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getConfig } from "@lingui/conf"
import type { LinguiConfig, CatalogFormat } from "./api/formats/types"
import { extract, collect, cleanObsolete, order } from "./api/extract"
import { printStats } from "./api/stats"
import { removeDirectory } from "./api/utils"

type ExtractOptions = {|
verbose: boolean,
Expand All @@ -36,7 +37,10 @@ export default function command(
}

const buildDir = path.join(config.localeDir, "_build")
if (!fs.existsSync(buildDir)) {
if (fs.existsSync(buildDir)) {
// remove only the content of build dir, not the directory itself
removeDirectory(buildDir, true)
} else {
mkdirp(buildDir)
}

Expand Down
36 changes: 11 additions & 25 deletions packages/cli/test/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
import fs from "fs"
import path from "path"
import extractor from "../src/api/extractors/typescript"
import { removeDirectory } from "../src/api/utils"

const LOCALE_DIR = "./locale"

const rmdir = dir => {
if (!fs.existsSync(dir)) return
const list = fs.readdirSync(dir)

for (let i = 0; i < list.length; i++) {
const filename = path.join(dir, list[i])
const stat = fs.statSync(filename)

if (filename === "." || filename === "..") {
// pass these files
} else if (stat.isDirectory()) {
// rmdir recursively
rmdir(filename)
} else {
// rm fiilename
fs.unlinkSync(filename)
}
}
fs.rmdirSync(dir)
}

describe("typescript-extractor", function() {
// CWD is root directory of repository, so origin of all messages is going to
// relative to root
Expand All @@ -39,11 +19,11 @@ describe("typescript-extractor", function() {
)

beforeAll(() => {
rmdir(LOCALE_DIR)
removeDirectory(LOCALE_DIR)
})

afterAll(() => {
rmdir(LOCALE_DIR)
removeDirectory(LOCALE_DIR)
})

it("should extract Typescript file", function() {
Expand All @@ -54,7 +34,10 @@ describe("typescript-extractor", function() {
)
).not.toThrow()

const contents = fs.readFileSync(path.join(buildDir, "core.json"), "utf8")
const contents = fs.readFileSync(
path.join(buildDir, "core.ts.json"),
"utf8"
)
const messages = JSON.parse(contents)
expect(Object.keys(messages).length).toBe(10)
})
Expand All @@ -67,7 +50,10 @@ describe("typescript-extractor", function() {
)
).not.toThrow()

const contents = fs.readFileSync(path.join(buildDir, "react.json"), "utf8")
const contents = fs.readFileSync(
path.join(buildDir, "react.tsx.json"),
"utf8"
)
const messages = JSON.parse(contents)
expect(Object.keys(messages).length).toBe(13)
})
Expand Down
4 changes: 2 additions & 2 deletions scripts/build/results.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"bundleSizes": {
"@lingui/core.development.js (NODE_DEV)": {
"size": 13213,
"gzip": 3506
"gzip": 3504
},
"@lingui/core.production.min.js (NODE_PROD)": {
"size": 5515,
"gzip": 1819
},
"@lingui/react.development.js (NODE_DEV)": {
"size": 18004,
"size": 18005,
"gzip": 5023
},
"@lingui/react.production.min.js (NODE_PROD)": {
Expand Down