Skip to content

Commit

Permalink
nls: rename command to extract localizations (#10324)
Browse files Browse the repository at this point in the history
Rename the `@theia/cli` command to extract localization from sources to
something that would make it explicit enough. `extract` alone is a bit
too generic.

Change `--pattern/-p` glob into an array of globs as `--files/-f`.

Minor changes.
  • Loading branch information
paul-marechal authored Oct 26, 2021
1 parent 1811b5e commit 2c63c96
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
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

0 comments on commit 2c63c96

Please sign in to comment.