-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13971b3
commit 1d94dcf
Showing
8 changed files
with
166 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"i18next-google-sheet": minor | ||
--- | ||
|
||
API를 활용한 실행 방법 추가 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,66 @@ | ||
#!/usr/bin/env node | ||
import '../lib/index.js'; | ||
import yargs from 'yargs'; | ||
import chalk from 'chalk'; | ||
import { i18nextGoogleSheet } from '../lib/index.js'; | ||
|
||
function getNamespacesToString(set) { | ||
const array = Array.from(set); | ||
if (array.length === 0) return ''; | ||
return ' (' + array.join(', ') + ')'; | ||
} | ||
|
||
async function main() { | ||
const argv = await yargs(process.argv.slice(2)) | ||
.option('path', { | ||
alias: 'p', | ||
describe: '로컬 locales 폴더 경로', | ||
type: 'string', | ||
}) | ||
.option('range', { | ||
describe: '구글 시트에서 스캔할 범위', | ||
default: '시트1', | ||
type: 'string', | ||
}) | ||
.option('spreadsheet-id', { | ||
describe: '구글 시트 문서 ID', | ||
type: 'string', | ||
}) | ||
.option('credentials-file', { | ||
describe: '구글 API 인증 파일', | ||
type: 'string', | ||
}) | ||
.option('credentials-json', { | ||
describe: '구글 API 인증 JSON', | ||
type: 'string', | ||
}) | ||
.demandOption(['path', 'range', 'spreadsheet-id']) | ||
.env('I18NEXT') | ||
.help('h') | ||
.alias('h', 'help') | ||
.argv; | ||
|
||
const stats = await i18nextGoogleSheet({ | ||
path: argv.path, | ||
range: argv.range, | ||
spreadsheet_id: argv.spreadsheetId, | ||
credentials_file: argv.credentialsFile, | ||
credentials_json: argv.credentialsJson, | ||
}); | ||
|
||
if (['added', 'updated', 'reused', 'pruned'].every((v) => stats[v].count === 0)) { | ||
console.log('No changes detected'); | ||
} else { | ||
console.log('Sync complete!'); | ||
console.log(chalk.green('Added: ' + stats.added.count + getNamespacesToString(stats.added.namespaces))); | ||
console.log(chalk.blue('Updated: ' + stats.updated.count + getNamespacesToString(stats.updated.namespaces))); | ||
console.log(chalk.yellow('Reused: ' + stats.reused.count + getNamespacesToString(stats.reused.namespaces))); | ||
console.log(chalk.red('Pruned: ' + stats.pruned.count + getNamespacesToString(stats.pruned.namespaces))); | ||
} | ||
} | ||
|
||
main() | ||
.catch((e) => { | ||
console.error(chalk.red('Sync failed')); | ||
console.error(e.stack); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,43 @@ | ||
import { google } from 'googleapis'; | ||
import { oraPromise } from 'ora'; | ||
import yargs from 'yargs'; | ||
import { loadFileLocale, saveFileLocale } from './fileLocaleIO.js'; | ||
import { createGoogleAuth } from './googleAuth.js'; | ||
import { pruneSheetLocale } from './prune.js'; | ||
import { loadSheetLocale, saveSheetLocale } from './sheetLocaleIO.js'; | ||
import { createProcessStats, ProcessStats } from './types.js'; | ||
import { visitLocale } from './visitor.js'; | ||
|
||
async function main() { | ||
const argv = await yargs(process.argv.slice(2)) | ||
.option('path', { | ||
alias: 'p', | ||
describe: '로컬 locales 폴더 경로', | ||
type: 'string', | ||
}) | ||
.option('range', { | ||
describe: '구글 시트에서 스캔할 범위', | ||
default: '시트1', | ||
type: 'string', | ||
}) | ||
.option('spreadsheet-id', { | ||
describe: '구글 시트 문서 ID', | ||
type: 'string', | ||
}) | ||
.option('credentials-file', { | ||
describe: '구글 API 인증 파일', | ||
type: 'string', | ||
}) | ||
.option('credentials-json', { | ||
describe: '구글 API 인증 JSON', | ||
type: 'string', | ||
}) | ||
.demandOption(['path', 'range', 'spreadsheet-id']) | ||
.env('I18NEXT') | ||
.help('h') | ||
.alias('h', 'help') | ||
.argv; | ||
const googleClient = await createGoogleAuth(argv.credentialsFile, argv.credentialsJson); | ||
export interface I18nextGoogleSheetOptions { | ||
path: string; | ||
range: string; | ||
spreadsheet_id: string; | ||
credentials_file?: string; | ||
credentials_json?: string; | ||
} | ||
|
||
export async function i18nextGoogleSheet(options: I18nextGoogleSheetOptions): Promise<ProcessStats> { | ||
const stats = createProcessStats(); | ||
const googleClient = await createGoogleAuth(options.credentials_file, options.credentials_json); | ||
const sheets = google.sheets({ version: 'v4', auth: googleClient }); | ||
const file_locale = await oraPromise( | ||
loadFileLocale(argv.path), | ||
loadFileLocale(options.path), | ||
'Loading file locales', | ||
); | ||
const { columns, locale: sheet_locale } = await oraPromise( | ||
loadSheetLocale(sheets, argv.spreadsheetId, argv.range), | ||
loadSheetLocale(sheets, options.spreadsheet_id, options.range), | ||
'Loading sheet locales', | ||
); | ||
visitLocale(file_locale, sheet_locale); | ||
pruneSheetLocale(sheet_locale); | ||
visitLocale(file_locale, sheet_locale, stats); | ||
pruneSheetLocale(sheet_locale, stats); | ||
await oraPromise( | ||
saveFileLocale(argv.path, file_locale), | ||
saveFileLocale(options.path, file_locale), | ||
'Saving file locales', | ||
); | ||
await oraPromise( | ||
saveSheetLocale(sheets, argv.spreadsheetId, argv.range, columns, sheet_locale), | ||
saveSheetLocale(sheets, options.spreadsheet_id, options.range, columns, sheet_locale), | ||
'Saving sheet locales', | ||
); | ||
return stats; | ||
} | ||
|
||
main(); | ||
export default i18nextGoogleSheet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import chalk from 'chalk'; | ||
import { SheetLocale } from './sheetLocale.js'; | ||
import { ProcessStats } from './types.js'; | ||
import { truncateKey } from './utils.js'; | ||
|
||
export function pruneSheetLocale(sheet_locale: SheetLocale) { | ||
export function pruneSheetLocale(sheet_locale: SheetLocale, stats: ProcessStats) { | ||
for (const entry of sheet_locale.entries) { | ||
if (!entry.has_visited && entry.values.used !== 'FALSE') { | ||
entry.values.used = 'FALSE'; | ||
entry.has_changed = true; | ||
console.log(chalk.red('- pruning'), truncateKey(sheet_locale.getIndexKey(entry))); | ||
stats.pruned.count += 1; | ||
stats.pruned.namespaces.add(entry.namespace); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export interface ProcessStatEntry { | ||
count: number; | ||
namespaces: Set<string>; | ||
} | ||
|
||
export interface ProcessStats { | ||
added: ProcessStatEntry; | ||
updated: ProcessStatEntry; | ||
reused: ProcessStatEntry; | ||
pruned: ProcessStatEntry; | ||
} | ||
|
||
export function createProcessStats(): ProcessStats { | ||
return { | ||
added: { | ||
count: 0, | ||
namespaces: new Set(), | ||
}, | ||
updated: { | ||
count: 0, | ||
namespaces: new Set(), | ||
}, | ||
reused: { | ||
count: 0, | ||
namespaces: new Set(), | ||
}, | ||
pruned: { | ||
count: 0, | ||
namespaces: new Set(), | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters