Skip to content

Commit

Permalink
Add and configure TypeScript, migrate .d.ts files for metro-cache
Browse files Browse the repository at this point in the history
Summary:
Configures repository to support TypeScript definitions inside each package directory under `types/`.

This is an interim structure which preserves the current types and paths from DefinitelyTyped. In future, we may shift to inline definitions, or (more likely) auto-translated types when the [flow-api-translator](https://www.npmjs.com/package/flow-api-translator) tool is more mature.

- Add `typescript@4.1.3` and configure project file.
- Add step to `scripts/build.js` to copy TypeScript definitions (`.d.ts`) across to distributed code.
- Add `yarn typecheck-ts` script and add to CircleCI.
- Add and configure `typescript-eslint` against `types/` directories.
- Migrates TypeScript definitions for `metro-cache` (self-contained package types), from DefinitelyTyped, used to validate all changes.

Changelog: **[Types]** Add bundled TypeScript definitions (partial) for metro-cache

Reviewed By: motiz88

Differential Revision: D44133264

fbshipit-source-id: b9b56cd751670964b57400bd9c63155349f78e81
  • Loading branch information
huntie authored and facebook-github-bot committed Mar 30, 2023
1 parent b752459 commit c022c36
Show file tree
Hide file tree
Showing 16 changed files with 352 additions and 4 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
- checkout
- yarn_install
- run: yarn typecheck
- run: yarn typecheck-ts
- run: yarn lint
- run: yarn test-smoke

Expand Down
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ docs/
examples/react-native
flow-typed/**
packages/*/build/**
types/**
website/
**/third-party/**
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

'use strict';

/** @type {import('eslint').Linter.Config} */
module.exports = {
extends: './scripts/eslint/base',
overrides: [
Expand All @@ -21,6 +20,10 @@ module.exports = {
'lint/flow-function-shape': 'off',
},
},
{
files: ['packages/*/types/**/*.d.ts'],
extends: './scripts/eslint/typescript',
},
{
files: ['packages/metro-source-map/**/*.js'],
rules: {
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"@babel/plugin-syntax-class-properties": "^7.0.0",
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
"@babel/plugin-transform-modules-commonjs": "^7.0.0",
"@tsconfig/node16": "1.0.1",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"acorn": "^8.7.1",
"babel-jest": "^29.2.1",
"chalk": "^4.0.0",
Expand Down Expand Up @@ -34,7 +37,8 @@
"micromatch": "^4.0.4",
"prettier": "2.7.1",
"progress": "^2.0.0",
"promise": "^8.3.0"
"promise": "^8.3.0",
"typescript": "4.1.3"
},
"scripts": {
"build-clean": "rm -rf ./packages/*/build",
Expand All @@ -46,10 +50,11 @@
"postpublish": "yarn workspaces run cleanup-release",
"publish": "yarn run build-clean && yarn run build && yarn workspaces run prepare-release && npm publish --workspaces",
"start": "node packages/metro/src/cli",
"test": "yarn run typecheck && yarn run lint && yarn run build && yarn run jest",
"test-coverage": "yarn run build && yarn run jest --coverage -i && node scripts/mapCoverage.js",
"test-smoke": "yarn start build --config packages/metro/src/integration_tests/metro.config.js TestBundle.js --out /tmp/TestBundle",
"test": "yarn run typecheck && yarn run lint && yarn run build && yarn run jest",
"typecheck": "flow check",
"typecheck-ts": "tsc --project tsconfig.json",
"update-version": "node ./scripts/updateVersion"
},
"workspaces": [
Expand Down
25 changes: 25 additions & 0 deletions packages/metro-cache/types/Cache.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall react_native
*/

import {CacheStore} from './types';

/**
* Main cache class. Receives an array of cache instances, and sequentially
* traverses them to return a previously stored value. It also ensures setting
* the value in all instances.
*
* All get/set operations are logged via Metro's logger.
*/
export default class Cache<T> {
constructor(stores: ReadonlyArray<CacheStore<T>>);
get(key: Buffer): Promise<T | null>;
set(key: Buffer, value: T): void;
get isDisabled(): boolean;
}
40 changes: 40 additions & 0 deletions packages/metro-cache/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall react_native
*/

// <reference types="node" />

import AutoCleanFileStore from './stores/AutoCleanFileStore';
import FileStore from './stores/FileStore';
import HttpGetStore from './stores/HttpGetStore';
import HttpStore from './stores/HttpStore';
import Cache from './Cache';
import stableHash from './stableHash';

export type {Options as FileOptions} from './stores/FileStore';
export type {Options as HttpOptions} from './stores/HttpStore';
export type {CacheStore} from './types';

export interface MetroCache {
AutoCleanFileStore: typeof AutoCleanFileStore;
Cache: typeof Cache;
FileStore: typeof FileStore;
HttpGetStore: typeof HttpGetStore;
HttpStore: typeof HttpStore;
stableHash: typeof stableHash;
}

export {
AutoCleanFileStore,
Cache,
FileStore,
HttpGetStore,
HttpStore,
stableHash,
};
11 changes: 11 additions & 0 deletions packages/metro-cache/types/stableHash.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall react_native
*/

export default function stableHash(value: unknown): Buffer;
13 changes: 13 additions & 0 deletions packages/metro-cache/types/stores/AutoCleanFileStore.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall react_native
*/

import type FileStore from './FileStore';

export default class AutoCleanFileStore<T> extends FileStore<T> {}
20 changes: 20 additions & 0 deletions packages/metro-cache/types/stores/FileStore.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall react_native
*/

export interface Options {
root: string;
}

export default class FileStore<T> {
constructor(options: Options);
get(key: Buffer): Promise<T | null>;
set(key: Buffer, value: T): Promise<void>;
clear(): void;
}
18 changes: 18 additions & 0 deletions packages/metro-cache/types/stores/HttpGetStore.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall react_native
*/

import type {Options} from './HttpStore';

export default class HttpGetStore<T> {
constructor(options: Options);
get(key: Buffer): Promise<T | null>;
set(key: Buffer, value: T): Promise<void>;
clear(): void;
}
25 changes: 25 additions & 0 deletions packages/metro-cache/types/stores/HttpStore.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall react_native
*/

export interface Options {
endpoint: string;
family?: 4 | 6;
timeout?: number;
key?: string | ReadonlyArray<string> | Buffer | ReadonlyArray<Buffer>;
cert?: string | ReadonlyArray<string> | Buffer | ReadonlyArray<Buffer>;
ca?: string | ReadonlyArray<string> | Buffer | ReadonlyArray<Buffer>;
}

export default class HttpStore<T> {
constructor(options: Options);
get(key: Buffer): Promise<T | null>;
set(key: Buffer, value: T): Promise<void>;
clear(): void;
}
15 changes: 15 additions & 0 deletions packages/metro-cache/types/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall react_native
*/

export interface CacheStore<T> {
get(key: Buffer): T | undefined | Promise<T> | Promise<undefined>;
set(key: Buffer, value: T): void | Promise<void>;
clear(): void | Promise<void>;
}
8 changes: 8 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const path = require('path');
const prettier = require('prettier');

const SRC_DIR = 'src';
const TYPES_DIR = 'types';
const BUILD_DIR = 'build';
const JS_FILES_PATTERN = '**/*.js';
const IGNORE_PATTERN = '**/__tests__/**';
Expand Down Expand Up @@ -68,12 +69,19 @@ function getBuildPath(file /*: string */, buildFolder /*: string */) {

function buildPackage(p /*: string */) {
const srcDir = path.resolve(p, SRC_DIR);
const typesDir = path.resolve(p, TYPES_DIR);
const buildDir = path.resolve(p, BUILD_DIR);
const pattern = path.resolve(srcDir, '**/*');
const files = glob.sync(pattern, {nodir: true});
const typescriptDefs = glob.sync(path.join(typesDir, '**/*.d.ts'));

process.stdout.write(fixedWidth(`${path.basename(p)}\n`));

files.forEach(file => buildFile(file, true));
typescriptDefs.forEach(
file => fs.copyFileSync(file, file.replace(typesDir, buildDir))
);

process.stdout.write(`[ ${chalk.green('OK')} ]\n`);
}

Expand Down
30 changes: 30 additions & 0 deletions scripts/eslint/typescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall react_native
*/

'use strict';

const path = require('path');

require('eslint-plugin-lint').load(path.join(__dirname, 'rules'));

/**
* ESLint config for TypeScript definition files (.d.ts).
*
* @type {import('eslint').Linter.Config}
*/
module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
plugins: ['@typescript-eslint', 'prettier'],
parser: '@typescript-eslint/parser',
};
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "@tsconfig/node16/tsconfig.json",
"include": ["./packages/*/types/**/*.d.ts"],
"compilerOptions": {
"noEmit": true,
"baseUrl": ".",
"skipLibCheck": false,
"paths": {
"metro": ["./packages/metro/types"],
"metro/*": ["./packages/metro/types/*"],
"metro-*": ["./packages/metro-*/types"]
}
}
}
Loading

0 comments on commit c022c36

Please sign in to comment.