Skip to content

Commit

Permalink
feat(@formatjs/cli): switch from glob to fast-glob
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed Aug 12, 2020
1 parent 778ddfa commit 6fc7bfb
Show file tree
Hide file tree
Showing 27 changed files with 122 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .commitlintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {sync: globSync} = require('glob');
const {sync: globSync} = require('fast-glob');

const packages = globSync('./packages/*/package.json').map(
fn => require(fn).name
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@
"eslint-plugin-jest": "^23.8.2",
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-react": "^7.19.0",
"fast-glob": "^3.2.4",
"fast-memoize": "^2.5.2",
"fs-extra": "^9.0.0",
"glob": "^7.1.6",
"husky": "^4.2.5",
"jest": "26",
"json-schema-to-typescript": "^9.1.1",
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ SRC_DEPS = [
"@npm//@types/node",
"@npm//fs-extra",
"@npm//@types/fs-extra",
"@npm//glob",
"@npm//@types/glob",
"@npm//fast-glob",
"@npm//commander",
"@npm//loud-rejection",
"@npm//chalk",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"chalk": "^4.0.0",
"commander": "5.1.0",
"fs-extra": "^9.0.0",
"glob": "^7.1.6",
"fast-glob": "^3.2.4",
"intl-messageformat-parser": "^5.3.5",
"json-stable-stringify": "^1.0.1",
"lodash": "^4.17.15",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as loudRejection from 'loud-rejection';
import extract, {ExtractCLIOptions} from './extract';
import compile, {CompileCLIOpts, Opts} from './compile';
import compileFolder from './compile_folder';
import {sync as globSync} from 'glob';
import {sync as globSync} from 'fast-glob';
import {join} from 'path';

const KNOWN_COMMANDS = ['extract'];
Expand Down Expand Up @@ -98,7 +98,7 @@ See https://github.com/webpack/loader-utils#interpolatename for sample patterns`
true
)
.option(
'--ignore <files>',
'--ignore <files...>',
'List of glob paths to **not** extract translations from.'
)
.option(
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Opts,
MessageDescriptor,
} from '@formatjs/ts-transformer';
import {IOptions as GlobOptions} from 'glob';
import * as ts from 'typescript';
import {resolveBuiltinFormatter} from './formatters';
import * as stringify from 'json-stable-stringify';
Expand Down Expand Up @@ -43,7 +42,7 @@ export type ExtractCLIOptions = Omit<
/**
* Ignore file glob pattern
*/
ignore?: GlobOptions['ignore'];
ignore?: string[];
};

export type ExtractOpts = Opts & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ Options:
--remove-default-message Remove \`defaultMessage\` field in generated js after extraction (default: false)
--additional-component-names <comma-separated-names> Additional component names to extract messages from, e.g: \`['FormattedFooBarMessage']\`. **NOTE**: By default we check for the fact that \`FormattedMessage\` is imported from \`moduleSourceName\` to make sure variable alias works. This option does not do that so it's less safe.
--extract-from-format-message-call Opt-in to extract from \`intl.formatMessage\` call with the same restrictions, e.g: has to be called with object literal such as \`intl.formatMessage({ id: 'foo', defaultMessage: 'bar', description: 'baz'})\` (default: true)
--ignore <files> List of glob paths to **not** extract translations from.
--ignore <files...> List of glob paths to **not** extract translations from.
--throws Whether to throw an exception when we fail to process any file in the batch.
--pragma <pragma> parse specific additional custom pragma. This allows you to tag certain file with metadata such as \`project\`. For example with this file:
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/tests/extract/__snapshots__/unit.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`it passes camelCase-converted arguments to typescript API 1`] = `
exports[`unit it passes camelCase-converted arguments to typescript API 1`] = `
Array [
Array [
";",
Expand Down
89 changes: 38 additions & 51 deletions packages/cli/tests/extract/unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,50 @@
import cliMain from '../../src/cli';
const glob = require('glob');
const glob = require('fast-glob');
const ts = require('typescript');
const transpileModule = jest.spyOn(ts, 'transpileModule');
// Commander.js will call this.
jest.spyOn(process, 'exit').mockImplementation((() => null) as any);

jest.mock('glob', () => ({
sync: jest.fn((p: string) => [p]),
}));
jest.spyOn(glob, 'sync').mockImplementation(p => [p]);

jest.mock('fs-extra', () => ({
outputJSONSync: () => Promise.resolve(),
readFile: () => Promise.resolve(';'),
}));

beforeEach(() => {
jest.clearAllMocks();
});

test('it passes camelCase-converted arguments to typescript API', async () => {
await cliMain([
'node',
'path/to/formatjs-cli',
'extract',
'--extract-source-location',
'--remove-default-message',
'--extract-from-format-message-call',
'--throws',
'--additional-component-names',
'Foo,Bar',
'--ignore=file3.ts',
'file1.js',
'file2.tsx',
]);

expect(transpileModule.mock.calls).toMatchSnapshot();
describe('unit', function () {
beforeEach(() => {
jest.clearAllMocks();
});

it('it passes camelCase-converted arguments to typescript API', async () => {
await cliMain([
'node',
'path/to/formatjs-cli',
'extract',
'--extract-source-location',
'--remove-default-message',
'--extract-from-format-message-call',
'--throws',
'--additional-component-names',
'Foo,Bar',
'--ignore=file3.ts',
'file1.js',
'file2.tsx',
]);

expect(transpileModule.mock.calls).toMatchSnapshot();
});

it('does not read from stdin when the glob pattern does NOT match anything', async () => {
// Does not match anything
jest.spyOn(glob, 'sync').mockImplementation(() => []);
// This should not hang
await cliMain([
'node',
'path/to/formatjs-cli',
'extract',
'*.doesnotexist',
]);
expect(transpileModule).not.toHaveBeenCalled();
}, 500); // 500ms timeout
});

test('it passes ignore argument to glob sync', () => {
cliMain([
'node',
'path/to/formatjs-cli',
'extract',
'--ignore=ignore-1.ts',
'include-1.js',
]);

expect(glob.sync).toHaveBeenCalled();
expect(glob.sync).toHaveBeenNthCalledWith(
1,
'include-1.js',
expect.objectContaining({ignore: 'ignore-1.ts'})
);
});

test('does not read from stdin when the glob pattern does NOT match anything', async () => {
// Does not match anything
jest.spyOn(glob, 'sync').mockImplementation(() => []);
// This should not hang
await cliMain(['node', 'path/to/formatjs-cli', 'extract', '*.doesnotexist']);
expect(transpileModule).not.toHaveBeenCalled();
}, 500); // 500ms timeout
6 changes: 2 additions & 4 deletions packages/intl-datetimeformat/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,10 @@ CLDR_DEPS = [
":cldr-raw",
"//:tsconfig.json",
"@npm//@types/fs-extra",
"@npm//@types/glob",
"@npm//@types/minimist",
"@npm//@types/node",
"@npm//fs-extra",
"@npm//glob",
"@npm//fast-glob",
"@npm//minimist",
]

Expand All @@ -126,14 +125,13 @@ ts_node(
"//packages/intl-utils:dist",
"//packages/intl-utils:types",
"@npm//@types/fs-extra",
"@npm//@types/glob",
"@npm//@types/minimist",
"@npm//@types/node",
"@npm//cldr-core",
"@npm//cldr-dates-full",
"@npm//cldr-numbers-full",
"@npm//fast-glob",
"@npm//fs-extra",
"@npm//glob",
"@npm//minimist",
],
output_dir = True,
Expand Down
2 changes: 1 addition & 1 deletion packages/intl-datetimeformat/scripts/cldr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {join, basename} from 'path';
import {outputFileSync, copyFileSync, readFileSync} from 'fs-extra';
import * as minimist from 'minimist';
import {sync as globSync} from 'glob';
import {sync as globSync} from 'fast-glob';

function main(args: minimist.ParsedArgs) {
const {
Expand Down
2 changes: 1 addition & 1 deletion packages/intl-datetimeformat/scripts/extract-dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import * as DateFields from 'cldr-dates-full/main/en/ca-gregorian.json';
import * as NumberFields from 'cldr-numbers-full/main/en/numbers.json';
import {sync as globSync} from 'glob';
import {sync as globSync} from 'fast-glob';
import {resolve, dirname} from 'path';
import * as AVAILABLE_LOCALES from 'cldr-core/availableLocales.json';
import {
Expand Down
6 changes: 2 additions & 4 deletions packages/intl-displaynames/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,10 @@ CLDR_DEPS = [
"scripts/cldr.ts",
"//:tsconfig.json",
"@npm//@types/fs-extra",
"@npm//@types/glob",
"@npm//@types/minimist",
"@npm//@types/node",
"@npm//fs-extra",
"@npm//glob",
"@npm//fast-glob",
"@npm//minimist",
]

Expand All @@ -115,14 +114,13 @@ ts_node(
"//packages/intl-utils:dist",
"//packages/intl-utils:types",
"@npm//@types/fs-extra",
"@npm//@types/glob",
"@npm//@types/minimist",
"@npm//@types/node",
"@npm//cldr-core",
"@npm//cldr-localenames-full",
"@npm//cldr-numbers-full",
"@npm//fast-glob",
"@npm//fs-extra",
"@npm//glob",
"@npm//minimist",
],
output_dir = True,
Expand Down
2 changes: 1 addition & 1 deletion packages/intl-displaynames/scripts/cldr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {join, basename} from 'path';
import {outputFileSync, readFileSync, copyFileSync} from 'fs-extra';
import * as minimist from 'minimist';
import {sync as globSync} from 'glob';
import {sync as globSync} from 'fast-glob';

function main(args: minimist.ParsedArgs) {
const {
Expand Down
2 changes: 1 addition & 1 deletion packages/intl-displaynames/scripts/extract-displaynames.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {sync as globSync} from 'glob';
import {sync as globSync} from 'fast-glob';
import {resolve, dirname} from 'path';
import {DisplayNamesData, invariant} from '@formatjs/intl-utils';
import * as AVAILABLE_LOCALES from 'cldr-core/availableLocales.json';
Expand Down
6 changes: 2 additions & 4 deletions packages/intl-listformat/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,10 @@ CLDR_DEPS = [
"scripts/cldr.ts",
"//:tsconfig.json",
"@npm//@types/fs-extra",
"@npm//@types/glob",
"@npm//@types/minimist",
"@npm//@types/node",
"@npm//fs-extra",
"@npm//glob",
"@npm//fast-glob",
"@npm//minimist",
]

Expand All @@ -120,14 +119,13 @@ ts_node(
"//packages/intl-utils:dist",
"//packages/intl-utils:types",
"@npm//@types/fs-extra",
"@npm//@types/glob",
"@npm//@types/lodash",
"@npm//@types/minimist",
"@npm//@types/node",
"@npm//cldr-core",
"@npm//cldr-misc-full",
"@npm//fast-glob",
"@npm//fs-extra",
"@npm//glob",
"@npm//lodash",
"@npm//minimist",
],
Expand Down
2 changes: 1 addition & 1 deletion packages/intl-listformat/scripts/cldr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {join, basename} from 'path';
import {outputFileSync, copyFileSync, readFileSync} from 'fs-extra';
import {sync as globSync} from 'glob';
import {sync as globSync} from 'fast-glob';
import * as minimist from 'minimist';

function main(args: minimist.ParsedArgs) {
Expand Down
2 changes: 1 addition & 1 deletion packages/intl-listformat/scripts/extract-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
'use strict';
import * as ListPatterns from 'cldr-misc-full/main/en/listPatterns.json';
import {sync as globSync} from 'glob';
import {sync as globSync} from 'fast-glob';
import {resolve, dirname} from 'path';
import {ListPatternFieldsData, ListPattern} from '@formatjs/intl-utils';

Expand Down
9 changes: 3 additions & 6 deletions packages/intl-numberformat/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,10 @@ CLDR_DEPS = [
"scripts/cldr.ts",
"//:tsconfig.json",
"@npm//@types/fs-extra",
"@npm//@types/glob",
"@npm//@types/minimist",
"@npm//@types/node",
"@npm//fs-extra",
"@npm//glob",
"@npm//fast-glob",
"@npm//minimist",
]

Expand All @@ -145,15 +144,14 @@ ts_node(
"//packages/intl-utils:dist",
"//packages/intl-utils:types",
"@npm//@types/fs-extra",
"@npm//@types/glob",
"@npm//@types/lodash",
"@npm//@types/minimist",
"@npm//@types/node",
"@npm//cldr-core",
"@npm//cldr-numbers-full",
"@npm//cldr-units-full",
"@npm//fast-glob",
"@npm//fs-extra",
"@npm//glob",
"@npm//lodash",
"@npm//minimist",
],
Expand Down Expand Up @@ -222,14 +220,13 @@ generate_src_file(
"//packages/intl-utils:dist",
"//packages/intl-utils:types",
"@npm//@types/fs-extra",
"@npm//@types/glob",
"@npm//@types/lodash",
"@npm//@types/minimist",
"@npm//@types/node",
"@npm//cldr-core",
"@npm//cldr-numbers-full",
"@npm//fast-glob",
"@npm//fs-extra",
"@npm//glob",
"@npm//lodash",
"@npm//minimist",
],
Expand Down
2 changes: 1 addition & 1 deletion packages/intl-numberformat/scripts/cldr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {join, basename} from 'path';
import {outputFileSync, copyFileSync, readFileSync} from 'fs-extra';
import * as minimist from 'minimist';
import {sync as globSync} from 'glob';
import {sync as globSync} from 'fast-glob';

function main(args: minimist.ParsedArgs) {
const {
Expand Down
2 changes: 1 addition & 1 deletion packages/intl-numberformat/scripts/extract-currencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';
import * as CurrenciesData from 'cldr-numbers-full/main/en/currencies.json';
import * as supplementalCurrencyData from 'cldr-core/supplemental/currencyData.json';
import {sync as globSync} from 'glob';
import {sync as globSync} from 'fast-glob';
import {resolve, dirname} from 'path';
import {CurrencyData, LDMLPluralRuleMap} from '@formatjs/intl-utils';
import * as AVAILABLE_LOCALES from 'cldr-core/availableLocales.json';
Expand Down
Loading

0 comments on commit 6fc7bfb

Please sign in to comment.