Skip to content

Commit

Permalink
feat: added ignore flag to vocab push (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanDroryAu authored Nov 15, 2024
1 parent 50f6151 commit b54de90
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-ravens-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vocab/phrase': minor
---

Added a new `ignore` option to the `@vocab/phrase` `push` function.
5 changes: 5 additions & 0 deletions .changeset/late-trainers-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vocab/cli': minor
---

Added a new `--ignore` flag on the cli.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,17 @@ referenced in the upload. These keys can be deleted from Phrase by providing the
vocab push --branch my-branch --delete-unused-keys
```

#### Ignoring Files

The `ignore` key in your [Vocab config](#configuration) allows you to ignore certain files from being validated, compiled and uploaded.
However, in some cases you may only want certain files to be compiled and validated, but not uploaded, such as those present in a build output directory.
This can be accomplished by providing the `--ignore` flag to `vocab push`.
This flag accepts an array of glob patterns to ignore.

```sh
vocab push --branch my-branch --ignore "**/dist/**" "**/another_ignored_directory/**"
```

[phrase]: https://developers.phrase.com/api/

#### [Tags]
Expand Down
5 changes: 5 additions & 0 deletions fixtures/phrase/src/ignore.vocab/translations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"excluded": {
"message": "this is excluded"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"lint:manypkg": "manypkg check",
"lint:prettier": "prettier --cache --check .",
"lint:tsc": "tsc",
"changeset": "changeset",
"release": "pnpm build && pnpm copy-readme-to-packages && changeset publish",
"version": "changeset version && pnpm install --lockfile-only",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ const branchDefinition = {
default: branch || 'local-development',
} as const;

const ignorePathDefinition = {
type: 'string',
array: true,
describe: 'Array of glob paths to ignore when searching for keys to push',
default: [] as string[],
} as const;

let config: UserConfig | null = null;

// eslint-disable-next-line @typescript-eslint/no-unused-expressions
Expand All @@ -36,6 +43,7 @@ yargsCli(process.argv.slice(2))
describe: 'Whether or not to delete unused keys after pushing',
default: false,
},
ignore: ignorePathDefinition,
}),
handler: async (options) => {
await push(options, config!);
Expand Down
20 changes: 17 additions & 3 deletions packages/phrase/src/pull-translations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('pull translations', () => {
it('should resolve', async () => {
await expect(runPhrase(options)).resolves.toBeUndefined();

expect(jest.mocked(writeFile)).toHaveBeenCalledTimes(2);
expect(jest.mocked(writeFile)).toHaveBeenCalledTimes(4);
});

it('should update keys', async () => {
Expand All @@ -91,6 +91,13 @@ describe('pull translations', () => {
),
).toMatchInlineSnapshot(`
[
{
"_meta": {},
"excluded": {
"message": "this is excluded",
},
},
{},
{
"_meta": {
"tags": [
Expand Down Expand Up @@ -174,7 +181,7 @@ describe('pull translations', () => {
it('should resolve', async () => {
await expect(runPhrase(options)).resolves.toBeUndefined();

expect(jest.mocked(writeFile)).toHaveBeenCalledTimes(2);
expect(jest.mocked(writeFile)).toHaveBeenCalledTimes(4);
});

it('should update keys', async () => {
Expand All @@ -188,6 +195,13 @@ describe('pull translations', () => {
),
).toMatchInlineSnapshot(`
[
{
"_meta": {},
"excluded": {
"message": "this is excluded",
},
},
{},
{
"_meta": {
"tags": [
Expand Down Expand Up @@ -310,7 +324,7 @@ describe('pull translations', () => {
new Error(`Missing translation for global key thanks in language fr`),
);

expect(jest.mocked(writeFile)).toHaveBeenCalledTimes(1);
expect(jest.mocked(writeFile)).toHaveBeenCalledTimes(3);
});
});
});
105 changes: 103 additions & 2 deletions packages/phrase/src/push-translations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ jest.mock('./phrase-api', () => ({

const devLanguageUploadId = '1234';

function runPhrase(config: { deleteUnusedKeys: boolean }) {
function runPhrase(config: { deleteUnusedKeys: boolean; ignore?: string[] }) {
return push(
{ branch: 'tester', deleteUnusedKeys: config.deleteUnusedKeys },
{
branch: 'tester',
deleteUnusedKeys: config.deleteUnusedKeys,
ignore: config.ignore || [],
},
{
devLanguage: 'en',
languages: [{ name: 'en' }, { name: 'fr' }],
Expand Down Expand Up @@ -74,6 +78,10 @@ describe('push', () => {
"tags",
],
},
"excluded.ignore": {
"message": "this is excluded",
"tags": [],
},
"hello.mytranslations": {
"message": "Hello",
"tags": [
Expand Down Expand Up @@ -170,6 +178,10 @@ describe('push', () => {
"tags",
],
},
"excluded.ignore": {
"message": "this is excluded",
"tags": [],
},
"hello.mytranslations": {
"message": "Hello",
"tags": [
Expand Down Expand Up @@ -245,4 +257,93 @@ describe('push', () => {
});
});
});

describe('when ignore is ["**/ignore.vocab/**"]', () => {
const config = { deleteUnusedKeys: false, ignore: ['**/ignore.vocab/**'] };

beforeEach(() => {
jest.mocked(pushTranslations).mockClear();
jest.mocked(writeFile).mockClear();
jest.mocked(deleteUnusedKeys).mockClear();

jest
.mocked(pushTranslations)
.mockImplementation(() => Promise.resolve({ devLanguageUploadId }));
});

it('should resolve', async () => {
await expect(runPhrase(config)).resolves.toBeUndefined();

expect(jest.mocked(pushTranslations)).toHaveBeenCalledTimes(1);
});

it('should update keys', async () => {
await expect(runPhrase(config)).resolves.toBeUndefined();

expect(jest.mocked(pushTranslations).mock.calls[0][0])
.toMatchInlineSnapshot(`
{
"en": {
"app.thanks.label": {
"globalKey": "app.thanks.label",
"message": "Thanks",
"tags": [
"every",
"key",
"gets",
"these",
"tags",
],
},
"hello.mytranslations": {
"message": "Hello",
"tags": [
"only for this key",
"greeting",
"every",
"key",
"gets",
"these",
"tags",
],
},
"profile.mytranslations": {
"message": "profil",
"tags": [
"every",
"key",
"gets",
"these",
"tags",
],
},
"world.mytranslations": {
"message": "world",
"tags": [
"every",
"key",
"gets",
"these",
"tags",
],
},
},
"fr": {
"hello.mytranslations": {
"description": undefined,
"message": "Bonjour",
},
"profile.mytranslations": {
"description": undefined,
"message": "profil",
},
"world.mytranslations": {
"description": undefined,
"message": "monde",
},
},
}
`);
});
});
});
11 changes: 9 additions & 2 deletions packages/phrase/src/push-translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,26 @@ import { trace } from './logger';
interface PushOptions {
branch: string;
deleteUnusedKeys?: boolean;
ignore?: string[];
}

/**
* Uploads translations to the Phrase API for each language.
* A unique namespace is appended to each key using the file path the key came from.
*/
export async function push(
{ branch, deleteUnusedKeys }: PushOptions,
{ branch, deleteUnusedKeys, ignore }: PushOptions,
config: UserConfig,
) {
if (ignore) {
trace(`ignoring files on paths: ${ignore.join(', ')}`);
}
const allLanguageTranslations = await loadAllTranslations(
{ fallbacks: 'none', includeNodeModules: false, withTags: true },
config,
{
...config,
ignore: [...(config.ignore || []), ...(ignore || [])],
},
);
trace(`Pushing translations to branch ${branch}`);
const allLanguages = config.languages.map((v) => v.name);
Expand Down

0 comments on commit b54de90

Please sign in to comment.