Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nls: rename command that extract localizations #10324

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions dev-packages/cli/src/theia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ function theiaCli(): void {
merge: boolean,
exclude?: string,
logs?: string,
pattern?: string
files?: string[]
}>({
command: 'extract',
command: 'nls-extract',
describe: 'Extract translation key/value pairs from source code',
builder: {
'output': {
Expand All @@ -256,9 +256,10 @@ function theiaCli(): void {
alias: 'e',
describe: 'Allows to exclude translation keys starting with this value'
},
'pattern': {
alias: 'p',
describe: 'A glob pattern for filtering the files used for extraction'
'files': {
alias: 'f',
describe: 'Glob pattern matching the files to extract from (starting from --root).',
array: true
},
'logs': {
alias: 'l',
Expand Down
29 changes: 12 additions & 17 deletions dev-packages/localization-manager/src/localization-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ import * as fs from 'fs-extra';
import * as ts from 'typescript';
import * as os from 'os';
import * as path from 'path';
import { glob, IOptions } from 'glob';
import { glob } from 'glob';
import { promisify } from 'util';
import deepmerge = require('deepmerge');

const globPromise = promisify(glob);

export interface Localization {
[key: string]: string | Localization
}
Expand All @@ -30,7 +33,8 @@ export interface ExtractionOptions {
output: string
exclude?: string
logs?: string
pattern?: string
/** List of globs matching the files to extract from. */
files?: string[]
merge: boolean
}

Expand Down Expand Up @@ -69,31 +73,22 @@ const tsOptions: ts.CompilerOptions = {
allowJs: true
};

function globPromise(pattern: string, options: IOptions): Promise<string[]> {
return new Promise((resolve, reject) => {
glob(pattern, options, (err, matches) => {
if (err) {
reject(err);
} else {
resolve(matches);
}
});
});
}

export async function extract(options: ExtractionOptions): Promise<void> {
const cwd = path.resolve(process.cwd(), options.root);
const files = await globPromise(options.pattern || '**/src/**/*.ts', { cwd });
const files: string[] = [];
await Promise.all((options.files ?? ['**/src/**/*.ts']).map(
async pattern => files.push(...await globPromise(pattern, { cwd }))
));
let localization: Localization = {};
const errors: string[] = [];
for (const file of files) {
const filePath = path.resolve(cwd, file);
const content = await fs.promises.readFile(filePath, { encoding: 'utf8' });
const content = await fs.readFile(filePath, 'utf8');
const fileLocalization = await extractFromFile(file, content, errors, options);
localization = deepmerge(localization, fileLocalization);
}
if (errors.length > 0 && options.logs) {
await fs.promises.writeFile(options.logs, errors.join(os.EOL));
await fs.writeFile(options.logs, errors.join(os.EOL));
}
const output = path.resolve(process.cwd(), options.output);
if (options.merge && await fs.pathExists(output)) {
Expand Down