Skip to content

Commit efeb321

Browse files
authored
breaking: change push interface (#44)
1 parent a75bd82 commit efeb321

File tree

10 files changed

+70
-159
lines changed

10 files changed

+70
-159
lines changed

README.md

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -182,58 +182,45 @@ type ProviderFactory<T = {}> = (configration: ProviderConfiguration<T>) => Provi
182182
*/
183183
interface Provider {
184184
/**
185-
* push the resource to localization service
186-
* @param resource the resource that push to localization service
187-
* @param dryRun whether the CLI run as dryRun mode
185+
* push the locale messsages to localization service
188186
*/
189-
push (resource: ProviderPushResource, dryRun: boolean): Promise<void>
187+
push (args: PushArguments): Promise<void>
190188
/**
191-
* pull the resource from localization service
192-
* @param locales locales that pull from localization service, if empty, you must pull the all locale messages
193-
* @param dryRun whether the CLI run as dryRun mode
194-
* @returns the resource of localization service
189+
* pull the locale messages from localization service
195190
*/
196-
pull (locales: Locale[], dryRun: boolean): Promise<ProviderPullResource>
191+
pull (args: PullArguments): Promise<LocaleMessages>
197192
}
198193

199-
/**
200-
* mode that can be processed with provider push
201-
* - 'file-path':
202-
* To use when the provider uses the locale message directly from the file.
203-
* for example, used for file uploading.
204-
* When specified that mode, `ProviderPushResource` are passed from the CLI as `files`.
205-
* - 'locale-message':
206-
* To use when the provider uses the locale message.
207-
* When specified that mode, `ProviderPushResource` are passed from the CLI as `messaegs`.
208-
*/
209-
type ProviderPushMode = 'file-path' | 'locale-message'
210-
211-
type ProviderPushFileInfo = {
212-
locale: Locale
213-
path: string
194+
type CommonArguments = {
195+
dryRun: boolean // whether the CLI run as dryRun mode
196+
normalize?: string // normalization ways for locale messages or resource
214197
}
215198

216-
type ProviderPushResource = {
217-
mode: ProviderPushMode
218-
files?: ProviderPushFileInfo[]
219-
messages?: LocaleMessages
220-
}
199+
/**
200+
* Provider Push Arguments
201+
*/
202+
type PushArguments = {
203+
messages: LocaleMessages // the locale messages that push to localization service
204+
} & CommonArguments
221205

222-
type ProviderPullResource = LocaleMessages
206+
/**
207+
* Provider Pull Arguments
208+
*/
209+
type PullArguments = {
210+
locales: Locale[] // locales that pull from localization service, if empty, you must pull the all locale messages
211+
} & CommonArguments
223212

224213
/**
225214
* ProviderConfiguration provider fields structure
226215
* e.g.
227216
* {
228217
* "provider": {
229218
* "token": "xxx"
230-
* },
231-
* "pushMode": "file-path"
219+
* }
232220
* }
233221
*/
234222
interface ProviderConfiguration<T = {}> {
235223
provider: { [key in keyof ProviderConfigurationValue<T>]: ProviderConfigurationValue<T>[key] }
236-
pushMode: ProviderPushMode
237224
}
238225

239226
type ProviderConfigurationValue<T = {}> = T & { [prop: string]: unknown }

src/commands/pull.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
loadProviderConf,
1616
DEFUALT_CONF
1717
} from '../utils'
18-
import { ProviderPullResource, Locale, LocaleMessage } from '../../types'
18+
import { LocaleMessages, Locale, LocaleMessage } from '../../types'
1919

2020
type PullOptions = {
2121
provider: string
@@ -91,7 +91,7 @@ export const handler = async (args: Arguments<PullOptions>): Promise<unknown> =>
9191
const locales = args.locales?.split(',').filter(p => p) as Locale[] || []
9292
const provider = ProviderFactory(conf)
9393
const resource = await provider.pull({ locales, dryRun, normalize })
94-
await applyPullResource(args.output, resource, args.dryRun)
94+
await applyPullLocaleMessages(args.output, resource, args.dryRun)
9595
// TODO: should refactor console message
9696
console.log('pull success')
9797
} catch (e) {
@@ -100,9 +100,9 @@ export const handler = async (args: Arguments<PullOptions>): Promise<unknown> =>
100100
}
101101
}
102102

103-
async function applyPullResource (output: string, resource: ProviderPullResource, dryRun: boolean) {
104-
const locales = Object.keys(resource) as Locale[]
105-
debug('applyPullResource', resource, locales, dryRun)
103+
async function applyPullLocaleMessages (output: string, messages: LocaleMessages, dryRun: boolean) {
104+
const locales = Object.keys(messages) as Locale[]
105+
debug('applyPullLocaleMessages', messages, locales, dryRun)
106106
// wrap mkdir with dryRun
107107
const mkdir = async (output: string) => {
108108
return !dryRun
@@ -122,7 +122,7 @@ async function applyPullResource (output: string, resource: ProviderPullResource
122122
// run!
123123
await mkdir(output)
124124
for (const locale of locales) {
125-
await writeFile(output, locale, resource[locale])
125+
await writeFile(output, locale, messages[locale])
126126
}
127127
}
128128

src/commands/push.ts

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ import glob from 'glob'
1313
import { debug as Debug } from 'debug'
1414
const debug = Debug('vue-i18n-locale-message:commands:push')
1515

16-
import {
17-
ProviderPushResource,
18-
ProviderPushMode
19-
} from '../../types'
16+
import { LocaleMessages } from '../../types'
2017

2118
type PushOptions = {
2219
provider: string
@@ -98,17 +95,17 @@ export const handler = async (args: Arguments<PushOptions>): Promise<unknown> =>
9895
const confPath = resolveProviderConf(args.provider, args.conf)
9996
const conf = loadProviderConf(confPath) || DEFUALT_CONF
10097

101-
let resource
98+
let messages
10299
try {
103-
resource = getProviderPushResource(args, conf.pushMode)
100+
messages = getLocaleMessages(args)
104101
} catch (e) {
105102
console.log(e.message)
106103
return
107104
}
108105

109106
try {
110107
const provider = ProviderFactory(conf)
111-
await provider.push({ resource, dryRun, normalize })
108+
await provider.push({ messages, dryRun, normalize })
112109
// TODO: should refactor console message
113110
console.log('push success')
114111
} catch (e) {
@@ -117,28 +114,14 @@ export const handler = async (args: Arguments<PushOptions>): Promise<unknown> =>
117114
}
118115
}
119116

120-
function getProviderPushResource (args: Arguments<PushOptions>, mode: ProviderPushMode): ProviderPushResource {
121-
const resource = { mode } as ProviderPushResource
122-
debug(`getProviderPushResource: mode=${mode}`)
123-
124-
if (mode === 'locale-message') {
125-
resource.messages = {}
126-
} else { // 'file-path'
127-
resource.files = []
128-
}
117+
function getLocaleMessages (args: Arguments<PushOptions>): LocaleMessages {
118+
let messages = {} as LocaleMessages
129119

130120
if (args.target) {
131121
const targetPath = resolve(args.target)
132122
const parsed = path.parse(targetPath)
133123
const locale = args.locale ? args.locale : parsed.name
134-
if (mode === 'locale-message') {
135-
resource.messages = Object.assign(resource.messages, { [locale]: require(targetPath) })
136-
} else { // 'file-path'
137-
resource.files?.push({
138-
locale,
139-
path: targetPath
140-
})
141-
}
124+
messages = Object.assign(messages, { [locale]: require(targetPath) })
142125
} else if (args.targetPaths) {
143126
const filenameMatch = args.filenameMatch
144127
if (!filenameMatch) {
@@ -155,14 +138,7 @@ function getProviderPushResource (args: Arguments<PushOptions>, mode: ProviderPu
155138
debug('regex match', match, fullPath)
156139
if (match && match[1]) {
157140
const locale = match[1]
158-
if (mode === 'locale-message') {
159-
resource.messages = Object.assign(resource.messages, { [locale]: require(fullPath) })
160-
} else { // 'file-path'
161-
resource.files?.push({
162-
locale,
163-
path: fullPath
164-
})
165-
}
141+
messages = Object.assign(messages, { [locale]: require(fullPath) })
166142
} else {
167143
// TODO: should refactor console message
168144
console.log(`${fullPath} is not matched with ${filenameMatch}`)
@@ -171,7 +147,7 @@ function getProviderPushResource (args: Arguments<PushOptions>, mode: ProviderPu
171147
})
172148
}
173149

174-
return resource
150+
return messages
175151
}
176152

177153
export default {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
throw new Error('cannot find module')
1+
throw new Error('cannot find module')

test/commands/__mocks__/l10n-service-provider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class L10nServiceProvider {
33
this._options = options
44
}
55

6-
async push (resource, dryRun) {
6+
async push (messsages, dryRun) {
77
return
88
}
99

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"provider": {
33
"token": "yyy"
4-
},
5-
"pushMode": "file-path"
4+
}
65
}
76

test/commands/pull.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ test('--conf option', async () => {
100100
})
101101

102102
expect(L10nServiceProvider).toHaveBeenCalledWith({
103-
provider: { token: 'xxx' },
104-
pushMode: 'file-path'
103+
provider: { token: 'xxx' }
105104
})
106105
})
107106

@@ -118,8 +117,7 @@ test('--conf option omit', async () => {
118117
})
119118

120119
expect(L10nOmitServiceProvider).toHaveBeenCalledWith({
121-
provider: { token: 'yyy' },
122-
pushMode: 'file-path'
120+
provider: { token: 'yyy' }
123121
})
124122
})
125123

test/commands/push.test.ts

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,8 @@ test('--target option', async () => {
9494
})
9595

9696
expect(mockPush).toHaveBeenCalledWith({
97-
resource: {
98-
messages: {
99-
en: { hello: 'world' }
100-
},
101-
mode: 'locale-message'
97+
messages: {
98+
en: { hello: 'world' }
10299
},
103100
dryRun: false,
104101
normalize: undefined
@@ -119,11 +116,8 @@ test('--locale option', async () => {
119116
})
120117

121118
expect(mockPush).toHaveBeenCalledWith({
122-
resource: {
123-
messages: {
124-
ja: { hello: '世界' }
125-
},
126-
mode: 'locale-message'
119+
messages: {
120+
ja: { hello: '世界' }
127121
},
128122
dryRun: false,
129123
normalize: undefined
@@ -145,16 +139,11 @@ test('--conf option', async () => {
145139
})
146140

147141
expect(L10nServiceProvider).toHaveBeenCalledWith({
148-
provider: { token: 'xxx' },
149-
pushMode: 'file-path'
142+
provider: { token: 'xxx' }
150143
})
151144
expect(mockPush).toHaveBeenCalledWith({
152-
resource: {
153-
files: [{
154-
locale: 'en',
155-
path: path.resolve(TARGET_LOCALE)
156-
}],
157-
mode: 'file-path'
145+
messages: {
146+
en: { hello: 'world' }
158147
},
159148
dryRun: false,
160149
normalize: undefined
@@ -173,8 +162,7 @@ test('--conf option omit', async () => {
173162
})
174163

175164
expect(L10nOmitServiceProvider).toHaveBeenCalledWith({
176-
provider: { token: 'yyy' },
177-
pushMode: 'file-path'
165+
provider: { token: 'yyy' }
178166
})
179167
})
180168

@@ -192,20 +180,17 @@ test('--target-paths option', async () => {
192180
})
193181

194182
expect(mockPush).toHaveBeenCalledWith({
195-
resource: {
196-
messages: {
197-
en: {
198-
hello: 'world'
199-
},
200-
lang: {
201-
hello: '世界'
202-
},
203-
ja: {
204-
hello: 'こんにちわわわ!',
205-
world: 'ザ・ワールド'
206-
}
183+
messages: {
184+
en: {
185+
hello: 'world'
186+
},
187+
lang: {
188+
hello: '世界'
207189
},
208-
mode: 'locale-message'
190+
ja: {
191+
hello: 'こんにちわわわ!',
192+
world: 'ザ・ワールド'
193+
}
209194
},
210195
dryRun: false,
211196
normalize: undefined
@@ -242,11 +227,8 @@ test('--dry-run option', async () => {
242227
})
243228

244229
expect(mockPush).toHaveBeenCalledWith({
245-
resource: {
246-
messages: {
247-
ja: { hello: '世界' }
248-
},
249-
mode: 'locale-message'
230+
messages: {
231+
ja: { hello: '世界' }
250232
},
251233
dryRun: true,
252234
normalize: undefined
@@ -267,11 +249,8 @@ test('--normalize option', async () => {
267249
})
268250

269251
expect(mockPush).toHaveBeenCalledWith({
270-
resource: {
271-
messages: {
272-
ja: { hello: '世界' }
273-
},
274-
mode: 'locale-message'
252+
messages: {
253+
ja: { hello: '世界' }
275254
},
276255
dryRun: false,
277256
normalize: 'flat'
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"provider": {
33
"token": "xxx"
4-
},
5-
"pushMode": "file-path"
4+
}
65
}

0 commit comments

Comments
 (0)