Skip to content

Commit be1028a

Browse files
authored
feat: Add app directory flag (#36)
* Add flag * fix tests * Lock major version
1 parent 21cb881 commit be1028a

File tree

7 files changed

+73
-36
lines changed

7 files changed

+73
-36
lines changed

.changeset/stupid-pianos-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@generaltranslation/translate-action': patch
3+
---
4+
5+
Add optional app_directory

__tests__/action.test.ts

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
22
import * as core from '@actions/core';
33
import { exec } from '@actions/exec';
4+
import { CLI_VERSION } from '../src/action';
45

56
vi.mock('@actions/core');
67
vi.mock('@actions/exec');
@@ -49,13 +50,13 @@ describe('GitHub Action', () => {
4950
expect(mockExec).toHaveBeenCalledWith('npm', [
5051
'install',
5152
'-g',
52-
'gtx-cli@latest',
53-
]);
54-
expect(mockExec).toHaveBeenCalledWith('gtx-cli', [
55-
'translate',
56-
'--api-key',
57-
'test-gt-api-key',
53+
`gtx-cli@${CLI_VERSION}`,
5854
]);
55+
expect(mockExec).toHaveBeenCalledWith(
56+
'gtx-cli',
57+
['translate', '--api-key', 'test-gt-api-key'],
58+
{}
59+
);
5960
});
6061

6162
it('should add config flag when config is provided', async () => {
@@ -68,13 +69,17 @@ describe('GitHub Action', () => {
6869
const { run } = await import('../src/action');
6970
await run();
7071

71-
expect(mockExec).toHaveBeenCalledWith('gtx-cli', [
72-
'translate',
73-
'--config',
74-
'custom.config.json',
75-
'--api-key',
76-
'test-gt-api-key',
77-
]);
72+
expect(mockExec).toHaveBeenCalledWith(
73+
'gtx-cli',
74+
[
75+
'translate',
76+
'--config',
77+
'custom.config.json',
78+
'--api-key',
79+
'test-gt-api-key',
80+
],
81+
{}
82+
);
7883
});
7984

8085
it('should add inline flag when inline is true', async () => {
@@ -90,12 +95,11 @@ describe('GitHub Action', () => {
9095
const { run } = await import('../src/action');
9196
await run();
9297

93-
expect(mockExec).toHaveBeenCalledWith('gtx-cli', [
94-
'translate',
95-
'--api-key',
96-
'test-gt-api-key',
97-
'--inline',
98-
]);
98+
expect(mockExec).toHaveBeenCalledWith(
99+
'gtx-cli',
100+
['translate', '--api-key', 'test-gt-api-key', '--inline'],
101+
{}
102+
);
99103
});
100104

101105
it('should add locales when provided', async () => {
@@ -108,13 +112,27 @@ describe('GitHub Action', () => {
108112
const { run } = await import('../src/action');
109113
await run();
110114

111-
expect(mockExec).toHaveBeenCalledWith('gtx-cli', [
112-
'translate',
113-
'--api-key',
114-
'test-gt-api-key',
115-
'--locales',
116-
'en fr es',
117-
]);
115+
expect(mockExec).toHaveBeenCalledWith(
116+
'gtx-cli',
117+
['translate', '--api-key', 'test-gt-api-key', '--locales', 'en fr es'],
118+
{}
119+
);
120+
});
121+
it('should run from app directory when provided', async () => {
122+
mockCore.getInput.mockImplementation((name) => {
123+
if (name === 'gt_api_key') return 'test-gt-api-key';
124+
if (name === 'app_directory') return 'app';
125+
return '';
126+
});
127+
128+
const { run } = await import('../src/action');
129+
await run();
130+
131+
expect(mockExec).toHaveBeenCalledWith(
132+
'gtx-cli',
133+
['translate', '--api-key', 'test-gt-api-key'],
134+
{ cwd: 'app' }
135+
);
118136
});
119137

120138
it('should handle errors and set failed status', async () => {

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ branding:
66
color: 'blue'
77

88
inputs:
9+
app_directory:
10+
description: 'Relative path to the directory containing the app source code'
11+
required: false
912
gt_api_key:
1013
description: 'API key for General Translation cloud service'
1114
required: true

dist/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31713,6 +31713,7 @@ module.exports = parseParams
3171331713
/************************************************************************/
3171431714
var __webpack_exports__ = {};
3171531715
/* harmony export */ __nccwpck_require__.d(__webpack_exports__, {
31716+
/* harmony export */ E: () => (/* binding */ CLI_VERSION),
3171631717
/* harmony export */ e: () => (/* binding */ run)
3171731718
/* harmony export */ });
3171831719
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(7484);
@@ -31724,6 +31725,7 @@ var __webpack_exports__ = {};
3172431725

3172531726

3172631727

31728+
const CLI_VERSION = '2';
3172731729
async function run() {
3172831730
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info('GT Translate action started');
3172931731
try {
@@ -31741,6 +31743,7 @@ async function run() {
3174131743
const ignoreErrors = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getBooleanInput('ignore_errors');
3174231744
const dryRun = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getBooleanInput('dry_run');
3174331745
const timeout = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('timeout');
31746+
const appDirectory = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('app_directory');
3174431747
const experimentalLocalizeStaticUrls = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getBooleanInput('experimental_localize_static_urls');
3174531748
const experimentalHideDefaultLocale = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getBooleanInput('experimental_hide_default_locale');
3174631749
const experimentalFlattenJsonFiles = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getBooleanInput('experimental_flatten_json_files');
@@ -31764,9 +31767,9 @@ async function run() {
3176431767
'npm',
3176531768
'install',
3176631769
'-g',
31767-
`gtx-cli@${version || 'latest'}`,
31770+
`gtx-cli@${version || CLI_VERSION}`,
3176831771
];
31769-
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Installing gtx-cli@${version || 'latest'}...`);
31772+
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Installing gtx-cli@${version || CLI_VERSION}...`);
3177031773
await (0,_actions_exec__WEBPACK_IMPORTED_MODULE_2__.exec)(installArgs[0], installArgs.slice(1));
3177131774
// Then run the gtx-cli translate command
3177231775
const args = ['gtx-cli', 'translate'];
@@ -31806,7 +31809,9 @@ async function run() {
3180631809
args.push('--experimental-localize-static-imports');
3180731810
_actions_core__WEBPACK_IMPORTED_MODULE_0__.info(`Running command: ${args.join(' ')}`);
3180831811
// Execute the command
31809-
const code = await (0,_actions_exec__WEBPACK_IMPORTED_MODULE_2__.exec)(args[0], args.slice(1));
31812+
const code = await (0,_actions_exec__WEBPACK_IMPORTED_MODULE_2__.exec)(args[0], args.slice(1), {
31813+
...(appDirectory && { cwd: appDirectory }),
31814+
});
3181031815
if (code !== 0) {
3181131816
throw new Error(`GT Translate failed with code ${code}`);
3181231817
}
@@ -31956,7 +31961,8 @@ async function createPR(githubToken, prBranch, prTitle, prBody, autoMerge = fals
3195631961
}
3195731962
run();
3195831963

31964+
var __webpack_exports__CLI_VERSION = __webpack_exports__.E;
3195931965
var __webpack_exports__run = __webpack_exports__.e;
31960-
export { __webpack_exports__run as run };
31966+
export { __webpack_exports__CLI_VERSION as CLI_VERSION, __webpack_exports__run as run };
3196131967

3196231968
//# sourceMappingURL=index.js.map

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/action.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import * as core from '@actions/core';
44
import * as github from '@actions/github';
55
import { exec, getExecOutput } from '@actions/exec';
66

7+
export const CLI_VERSION = '2';
8+
79
export async function run(): Promise<void> {
810
core.info('GT Translate action started');
911
try {
@@ -21,6 +23,7 @@ export async function run(): Promise<void> {
2123
const ignoreErrors = core.getBooleanInput('ignore_errors');
2224
const dryRun = core.getBooleanInput('dry_run');
2325
const timeout = core.getInput('timeout');
26+
const appDirectory = core.getInput('app_directory');
2427
const experimentalLocalizeStaticUrls = core.getBooleanInput(
2528
'experimental_localize_static_urls'
2629
);
@@ -55,9 +58,9 @@ export async function run(): Promise<void> {
5558
'npm',
5659
'install',
5760
'-g',
58-
`gtx-cli@${version || 'latest'}`,
61+
`gtx-cli@${version || CLI_VERSION}`,
5962
];
60-
core.info(`Installing gtx-cli@${version || 'latest'}...`);
63+
core.info(`Installing gtx-cli@${version || CLI_VERSION}...`);
6164
await exec(installArgs[0], installArgs.slice(1));
6265

6366
// Then run the gtx-cli translate command
@@ -88,7 +91,9 @@ export async function run(): Promise<void> {
8891
core.info(`Running command: ${args.join(' ')}`);
8992

9093
// Execute the command
91-
const code = await exec(args[0], args.slice(1));
94+
const code = await exec(args[0], args.slice(1), {
95+
...(appDirectory && { cwd: appDirectory }),
96+
});
9297
if (code !== 0) {
9398
throw new Error(`GT Translate failed with code ${code}`);
9499
} else {

0 commit comments

Comments
 (0)