Skip to content

Commit

Permalink
fix: cosmiconfig update to allow ESM config file (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpontvieux-systra authored Dec 19, 2023
1 parent 5ba9637 commit ee27c67
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 56 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: 18
- name: install dependencies
- name: install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y gettext
- name: install npm dependencies
run: npm ci
- name: run tests
run: npm run test
Expand Down
105 changes: 51 additions & 54 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"dependencies": {
"chalk": "^4.1.2",
"command-line-args": "^5.2.1",
"cosmiconfig": "^7.0.1",
"cosmiconfig": "^9.0.0",
"gettext-extractor": "^3.6.2",
"glob": "^7.2.0",
"parse5": "^6.0.1",
Expand Down
Empty file modified scripts/gettext_extract.ts
100755 → 100644
Empty file.
92 changes: 92 additions & 0 deletions tests/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { mkdir, mkdtemp, readFile, rm, symlink, writeFile } from "fs/promises";
import { join } from "path";
import { tmpdir } from "os";
import { cwd } from "process";
import { execSync } from "child_process";

describe("config format tests", () => {
type WithTempDirTest = (tmpDir: string) => Promise<any>;

const withTempDir = async (testFunc: WithTempDirTest) => {
let tmpDir;
try {
tmpDir = await mkdtemp(join(tmpdir(), "vue3-gettext-"));
await testFunc(tmpDir);
} finally {
if (tmpDir) {
await rm(tmpDir, { recursive: true, force: true });
}
}
};

const testConfigWithExtract = async (tmpDir: string, config: string, configFileName: string, isModule: boolean) => {
const packageJson = {
name: "test",
version: "0.0.1",
type: isModule ? "module" : "commonjs",
};
for (const d of ["src", "scripts", "node_modules"]) {
await symlink(join(cwd(), d), join(tmpDir, d));
}
await writeFile(join(tmpDir, "package.json"), JSON.stringify(packageJson));
await writeFile(join(tmpDir, configFileName), config);
await mkdir(join(tmpDir, "srctest", "lang"), { recursive: true });
await writeFile(
join(tmpDir, "srctest", "example.js"),
`
const { $gettext } = useGettext();
$gettext('Translate me');
`,
);
execSync(`sh -c 'cd ${tmpDir}; ts-node ./scripts/gettext_extract.ts'`);
const appEnPo = (await readFile(join(tmpDir, "srctest", "lang", "en", "app.po"))).toString();
const appEnPoLines = appEnPo.trim().split("\n");
expect(appEnPoLines).toContain('msgid "Translate me"');
expect(appEnPoLines[appEnPoLines.length - 1]).toEqual('msgstr "Translate me"');
const appFrPo = (await readFile(join(tmpDir, "srctest", "lang", "fr", "app.po"))).toString();
const appFrPoLines = appFrPo.trim().split("\n");
expect(appFrPoLines).toContain('msgid "Translate me"');
expect(appFrPoLines[appFrPoLines.length - 1]).toEqual('msgstr ""');
};

it("load a commonjs format", async () => {
await withTempDir(
async (tmpDir) =>
await testConfigWithExtract(
tmpDir,
`
module.exports = {
input: {
path: './srctest',
},
output: {
path: './srctest/lang',
locales: ['en', 'fr'],
},
};`,
"gettext.config.js",
false,
),
);
});
it("load an ESM format", async () => {
await withTempDir(
async (tmpDir) =>
await testConfigWithExtract(
tmpDir,
`
export default {
input: {
path: './srctest',
},
output: {
path: './srctest/lang',
locales: ['en', 'fr'],
},
};`,
"gettext.config.js",
true,
),
);
});
});

0 comments on commit ee27c67

Please sign in to comment.