Skip to content

Commit 8b2afe6

Browse files
robinsondotnetkazupon
authored andcommitted
Add dryRun on infuse command (#61)
* Add dryRun option on infuse command * Remove unused variable
1 parent dfe3c20 commit 8b2afe6

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/commands/infuse.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type InfuseOptions = {
1616
target: string
1717
locales: string
1818
match?: string
19+
dryRun: boolean
1920
}
2021

2122
export const command = 'infuse'
@@ -41,6 +42,12 @@ export const builder = (args: Argv): Argv<InfuseOptions> => {
4142
alias: 'm',
4243
describe: 'option should be accepted a regex filenames, must be specified together --messages'
4344
})
45+
.option('dryRun', {
46+
type: 'boolean',
47+
alias: 'd',
48+
default: false,
49+
describe: 'run the infuse command, but do not apply them'
50+
})
4451
}
4552

4653
export const handler = (args: Arguments<InfuseOptions>): void => {
@@ -51,7 +58,7 @@ export const handler = (args: Arguments<InfuseOptions>): void => {
5158
const meta = squeeze(targetPath, sources)
5259
apply(messages, meta)
5360
const newSources = infuse(targetPath, sources, meta)
54-
writeSFC(newSources)
61+
if (!args.dryRun) writeSFC(newSources)
5562
}
5663

5764
function readLocaleMessages (targetPath: string, matchRegex?: string): LocaleMessages {

test/__snapshots__/cli.test.ts.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ Options:
1212
[string] [required]
1313
--locales, -l locale messages path to be infused [string] [required]
1414
--match, -m option should be accepted a regex filenames, must be specified
15-
together --messages [string]"
15+
together --messages [string]
16+
--dryRun, -d run the infuse command, but do not apply them
17+
[boolean] [default: false]"
1618
`;
1719

1820
exports[`squeeze command output help 1`] = `

test/commands/infuse.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,34 @@ test('relative path', async () => {
131131
}
132132
})
133133

134+
test('dryRun option', async () => {
135+
// setup mock'
136+
const mockUtils = utils as jest.Mocked<typeof utils>
137+
mockUtils.resolve
138+
.mockImplementationOnce(() => `${TARGET_PATH}/src`)
139+
.mockImplementationOnce((...paths) => `${TARGET_PATH}/${paths[0]}`)
140+
const mockFS = fs as jest.Mocked<typeof fs>
141+
mockFS.readFileSync.mockImplementation(path => {
142+
if (MOCK_FILES[path as string]) {
143+
return MOCK_FILES[path as string]
144+
} else {
145+
return JSON.stringify(json)
146+
}
147+
})
148+
149+
// run
150+
const infuse = await import('../../src/commands/infuse')
151+
const cmd = yargs.command(infuse)
152+
await new Promise(resolve => {
153+
cmd.parse(`infuse --target=./src --locales=locales-2.json --dry-run`, () => {
154+
resolve()
155+
})
156+
})
157+
158+
// check
159+
expect(mockFS.writeFileSync).not.toHaveBeenCalled()
160+
})
161+
134162
test('match option', async () => {
135163
// setup mocks
136164
const mockUtils = utils as jest.Mocked<typeof utils>

0 commit comments

Comments
 (0)