Skip to content

Commit

Permalink
Refactor common to esm module (#1199)
Browse files Browse the repository at this point in the history
* Refactor common to esm module
  • Loading branch information
thekevinscott authored Oct 10, 2023
1 parent bd50cbf commit 954656f
Show file tree
Hide file tree
Showing 27 changed files with 191 additions and 155 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ jobs:
working-directory: ./packages/shared
run: pnpm test

internal-unit:
name: 'Internals / Unit'
runs-on: ubuntu-latest
steps:
- name: 'Checkout repository'
uses: actions/checkout@v2
with:
lfs: 'true'

- name: 'Setup PNPM with Node 16'
uses: ./.github/actions/setup-pnpm
with:
node-version: 16

- name: 'Unit Tests'
working-directory: ./internals
run: pnpm test

upload-to-codecov:
name: 'Upload to CodeCov'
runs-on: ubuntu-latest
Expand Down
16 changes: 12 additions & 4 deletions internals/common/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"name": "@internals/common",
"private": true,
"type": "module",
"main": "./dist/index.js",
"exports": {
".": "./dist/index.js",
"./models": "./dist/models.js",
"./package-json": "./dist/package-json.js",
"./tmp-dir": "./dist/tmp-dir.js",
"./constants": "./dist/constants.js",
"./logger": "./dist/logger.mjs",
"./logger": "./dist/logger.js",
"./git": "./dist/git.js",
"./packages": "./dist/packages.js",
"./npm": "./dist/npm.mjs",
"./npm": "./dist/npm.js",
"./get-hashed-name": "./dist/get-hashed-name.js",
"./tfjs-library": "./dist/tfjs-library.js",
"./types": "./dist/types.js",
Expand All @@ -32,16 +33,19 @@
"wireit": "^0.14.0"
},
"devDependencies": {
"shx": "^0.3.4",
"@types/ejs": "^3.1.2",
"vitest-mock-process": "^1.0.4",
"vitest": "^0.34.5"
},
"wireit": {
"build": {
"command": "tsc -p ./tsconfig.json",
"command": "shx rm -rf dist && tsc -p ./tsconfig.json",
"files": [
"src/**/*.ts",
"src/**/*.mts",
"!src/**/*.test.ts",
"!src/**/*.test.mts",
"package.json",
"vite.config.ts",
"tsconfig.json"
Expand All @@ -50,12 +54,16 @@
"dist/**"
]
},
"test:run": {
"command": "vitest run --config ./vite.config.ts"
},
"test": {
"command": "vitest --config vite.config.js"
"command": "vitest --config ./vite.config.ts"
}
},
"scripts": {
"build": "wireit",
"test:run": "wireit",
"test": "wireit"
},
"engines": {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import chalk from 'chalk';
import { vi } from 'vitest';
import { rimraf } from 'rimraf';
import { isLogLevel, log, parseMessage, setLogLevel, logTypes } from './logger.mjs';
import * as mockProcess from 'vitest-mock-process';
import { isLogLevel, log, parseMessage, setLogLevel, logTypes } from './logger.js';

vi.mock('rimraf', async () => {
const actual = await vi.importActual("rimraf") as typeof rimraf;
Expand Down Expand Up @@ -57,31 +56,31 @@ describe('logger', () => {
});
});

describe('log', () => {
let mockStdout: ReturnType<typeof mockProcess.mockProcessStdout>;
let mockStderr: ReturnType<typeof mockProcess.mockProcessStderr>;
// describe('log', () => {
// let mockStdout: ReturnType<typeof mockProcess.mockProcessStdout>;
// let mockStderr: ReturnType<typeof mockProcess.mockProcessStderr>;

beforeEach(() => {
mockStdout = mockProcess.mockProcessStdout();
mockStderr = mockProcess.mockProcessStderr();
});
// beforeEach(() => {
// mockStdout = mockProcess.mockProcessStdout();
// mockStderr = mockProcess.mockProcessStderr();
// });

it('logs if level is greater than valid', () => {
setLogLevel('info');
log('warn', ['foo']);
expect(mockStderr).toHaveBeenCalledWith(logTypes.warn('foo\n'));
});
// it('logs if level is greater than valid', () => {
// setLogLevel('info');
// log('warn', ['foo']);
// expect(mockStderr).toHaveBeenCalledWith(logTypes.warn('foo\n'));
// });

it('logs if level is equal to valid', () => {
setLogLevel('info');
log('info', ['foo']);
expect(mockStdout).toHaveBeenCalledWith(logTypes.info('foo\n'));
});
// it('logs if level is equal to valid', () => {
// setLogLevel('info');
// log('info', ['foo']);
// expect(mockStdout).toHaveBeenCalledWith(logTypes.info('foo\n'));
// });

it('ignores log if below current log level', () => {
setLogLevel('info');
log('verbose', ['foo']);
expect(mockStdout).toHaveBeenCalledTimes(0);
});
});
// it('ignores log if below current log level', () => {
// setLogLevel('info');
// log('verbose', ['foo']);
// expect(mockStdout).toHaveBeenCalledTimes(0);
// });
// });
});
File renamed without changes.
51 changes: 0 additions & 51 deletions internals/common/src/npm.test.mts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawn } from 'child_process';
import { getLogLevel, verbose } from './logger.mjs';
import { getLogLevel, verbose } from './logger.js';

const parseCommand = (_command: string | string[]) => {
const command = Array.isArray(_command) ? _command : _command.split(' ');
Expand Down
22 changes: 10 additions & 12 deletions internals/common/src/package-json.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { getPackageJSONExports } from './package-json.js';
import { vi } from 'vitest';
import * as fs from '@internals/common/fs';
import * as _fs from './fs.js';
import { readFile } from './fs.js';

const { readFile } = fs;

vi.mock('@internals/common/fs', async () => {
const actual = await vi.importActual("@internals/common/fs") as typeof fs;
vi.mock('./fs.js', async () => {
const actual = await vi.importActual("./fs.js") as typeof _fs;
return {
default: {
...actual,
// exists: vi.fn(),
// readdir: vi.fn().mockImplementation(() => Promise.resolve([])),
readFile: vi.fn(),
// stat: vi.fn(),
},
...actual,
readFile: vi.fn(),
}
});

Expand Down Expand Up @@ -74,6 +68,10 @@ describe('package-json', () => {
"require": "./dist/cjs/models/esrgan-thick/src/x2.js",
"import": "./dist/esm/models/esrgan-thick/src/x2.js"
}],
['.', {
"require": "./dist/cjs/models/esrgan-thick/src/index.js",
"import": "./dist/esm/models/esrgan-thick/src/index.js"
}],
]);
});

Expand Down
18 changes: 9 additions & 9 deletions internals/common/src/tfjs-library.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { vi } from 'vitest';
import fsExtra from "fs-extra";
import { readFile, } from "./fs.js";
import * as _fs from './fs.js';
import { TFJS_LIBRARY_TARGET_ERROR, getTFJSLibraryTargetFromPackageJSON } from './tfjs-library.js';
const { readFile } = fsExtra;

vi.mock('fs-extra', () => {
vi.mock('./fs.js', async () => {
const actual = await vi.importActual("./fs.js") as typeof _fs;
return {
default: {
readFile: vi.fn(),
},
...actual,
readFile: vi.fn(),
}
});

Expand All @@ -17,9 +17,9 @@ describe('getTFJSLibraryTarget', () => {
});

const makeMock = (dependencies: Record<string, string>) => {
vi.mocked(readFile).mockImplementation(() => Promise.resolve(Buffer.from(JSON.stringify({
vi.mocked(readFile).mockImplementation(() => Promise.resolve(JSON.stringify({
dependencies,
}))));
})));
}

it('loads the correct package json from the right directory', async () => {
Expand All @@ -28,7 +28,7 @@ describe('getTFJSLibraryTarget', () => {
});
expect(readFile).toHaveBeenCalledTimes(0);
expect(await getTFJSLibraryTargetFromPackageJSON('foo')).toBe('browser');
expect(readFile).toHaveBeenCalledWith(expect.stringContaining('foo/package.json'), expect.anything());
expect(readFile).toHaveBeenCalledWith(expect.stringContaining('foo/package.json'));
});

it('returns browser for @tensorflow/tfjs', async () => {
Expand Down
2 changes: 1 addition & 1 deletion internals/common/src/tfjs-library.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import { readFile } from "@internals/common/fs";
import { readFile } from "./fs.js";
import { Environment } from './types.js';

export type TFJSLibrary = 'browser' | 'node' | 'node-gpu';
Expand Down
17 changes: 7 additions & 10 deletions internals/common/src/tmp-dir.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import * as fs from '@internals/common/fs';
import * as _fs from './fs.js';
import { exists, mkdirp } from './fs.js';
import { vi } from 'vitest';
import { rimraf } from 'rimraf';
import { makeTmpDir, withTmpDir } from "./tmp-dir.js";

const { exists, mkdirp } = fs;

vi.mock("@internals/common/fs", async () => {
const actual = await vi.importActual("@internals/common/fs") as typeof fs;
vi.mock("./fs.js", async () => {
const actual = await vi.importActual("./fs.js") as typeof _fs;
return {
default: {
...actual,
exists: vi.fn(),
mkdirp: vi.fn(),
}
...actual,
exists: vi.fn(),
mkdirp: vi.fn(),
};
});

Expand Down
16 changes: 3 additions & 13 deletions internals/common/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"declaration": true,
"importHelpers": false,
"module": "es2022",
"rootDir": "./src",
"outDir": "./dist",
"rootDir": "src",
"strict": true,
"target": "es2022",
"allowSyntheticDefaultImports": true,
"moduleResolution": "node16",
"types": ["vitest/globals"]
},
"include": ["./src/**/*"],
"exclude": ["./src/**/*.test.ts"],
"ts-node": {
"esm": true
}
"include": ["./src/**/*.ts", "./src/**/*.mts"],
"exclude": ["./src/**/*.test.ts", "./src/**/*.test.mts"],
}
11 changes: 4 additions & 7 deletions internals/common/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { defineConfig } from 'vitest/config';
import { defineConfig, mergeConfig } from 'vitest/config';
import configShared from '../vite.config';

export default defineConfig({
test: {
include: ['**/*.test.ts'],
globals: true,
},
});
export default mergeConfig(configShared, defineConfig({
}));
10 changes: 8 additions & 2 deletions internals/http-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,27 @@
],
"files": [
"src/**/*.ts",
"src/**/*.mts",
"!src/**/*.test.ts",
"!src/**/*.test.mts",
"package.json",
"vitest.config.ts",
"vite.config.ts",
"tsconfig.json"
],
"output": [
"dist/**"
]
},
"test:run": {
"command": "vitest run --config ./vite.config.ts"
},
"test": {
"command": "vitest"
"command": "vitest --config ./vite.config.ts"
}
},
"scripts": {
"build": "wireit",
"test:run": "wireit",
"test": "wireit"
},
"engines": {
Expand Down
5 changes: 3 additions & 2 deletions internals/http-server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"declaration": true,
"rootDir": "./src",
"outDir": "./dist",
"types": ["vitest/globals"]
},
"include": ["./src/**/*.ts", "./src/**/*.mts"],
"exclude": ["./src/**/*.test.ts", "./src/**/*.test.mts"],
}
Loading

0 comments on commit 954656f

Please sign in to comment.