Skip to content

Commit

Permalink
refactor: working through the refactor
Browse files Browse the repository at this point in the history
See #43
  • Loading branch information
ifiokjr committed Jun 2, 2022
1 parent 6f4f7f2 commit 039b4f5
Show file tree
Hide file tree
Showing 63 changed files with 1,433 additions and 429 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ yarn.lock
/.lintstagedrc
/vite.config.ts
/vitest.config.ts
/tsconfig.json
/tsconfig.references.json
/.markdownlint.json
/.vscode
Expand Down
37 changes: 22 additions & 15 deletions .monots/symlink/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
/// <reference types="vitest" />
import * as path from 'node:path';
import { defineConfig } from 'vite';
import { baseDir } from '../scripts/helpers.js';
import { loadJsonFile } from 'load-json-file';
import tsconfigPaths from 'vite-tsconfig-paths';

const config = defineConfig(async () => {
return {
test: {
open: true,
// rename `__snapshots__` to `snapshots`
resolveSnapshotPath(testPath, snapExtension) {
return path.join(
path.dirname(testPath),
'snapshots',
`${path.basename(testPath)}${snapExtension}`,
);
},
const referenceTsConfig = await loadJsonFile<{ references: Array<{ path: string }> }>(
baseDir('tsconfig.json'),
);

const projects = referenceTsConfig.references.map((reference) =>
path.join(reference.path, 'tsconfig.json'),
);

export default defineConfig({
plugins: [tsconfigPaths({ projects })],
test: {
// rename `__snapshots__` to `snapshots`
resolveSnapshotPath(testPath, snapExtension) {
return path.join(
path.dirname(testPath),
'snapshots',
`${path.basename(testPath)}${snapExtension}`,
);
},
};
},
});

export default config;
4 changes: 3 additions & 1 deletion .monots/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "../packages/monots__tsconfig/tsconfig.json",
"compilerOptions": {
"skipLibCheck": true
"skipLibCheck": true,
"allowJs": true,
"checkJs": true
}
}
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,26 @@
"@manypkg/get-packages": "^1.1.3",
"@milahu/patch-package": "^6.4.14",
"@monots/tsconfig": "^0.2.0",
"@types/node": "^17.0.35",
"@types/node": "^17.0.38",
"@types/ws": "^8.5.3",
"@vitest/ui": "^0.13.1",
"browserslist": "^4.20.3",
"eslint": "^8.16.0",
"eslint-config-monots": "^0.11.0",
"fast-glob": "^3.2.11",
"find-up": "^6.3.0",
"got": "^12.1.0",
"husky": "^8.0.1",
"lint-staged": "^12.4.3",
"lint-staged": "^13.0.0",
"load-json-file": "^7.0.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.6.2",
"prettier-config-monots": "^0.2.1",
"tsx": "^3.4.2",
"typescript": "^4.7.2",
"vite": "^2.9.9",
"vitest": "^0.13.0"
"vite-tsconfig-paths": "^3.5.0",
"vitest": "^0.13.1"
},
"packageManager": "pnpm@7.1.7",
"engines": {
Expand All @@ -85,7 +88,7 @@
},
"monots": {
"baseTsconfig": "./.monots/tsconfig.base.json",
"tsconfigPath": "./.monots/symlink/tsconfig.json",
"tsconfigPath": "./tsconfig.json",
"packages": [
"packages/*",
".monots/scripts",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-monots/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@monots/utils": "^0.5.0",
"@types/async-retry": "^1.4.4",
"@types/cross-spawn": "^6.0.2",
"@types/node": "^17.0.35",
"@types/node": "^17.0.38",
"@types/prompts": "^2.0.14",
"@types/rimraf": "^3.0.2",
"@types/tar": "^6.1.0",
Expand Down
10 changes: 5 additions & 5 deletions packages/eslint-config-monots/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@
],
"dependencies": {
"@rushstack/eslint-patch": "^1.1.3",
"@typescript-eslint/eslint-plugin": "^5.26.0",
"@typescript-eslint/parser": "^5.26.0",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-node": "^0.3.6",
"eslint-import-resolver-typescript": "^2.7.1",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-file-progress": "^1.2.0",
"eslint-plugin-file-progress": "^1.2.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.4.5",
"eslint-plugin-jest": "^26.4.6",
"eslint-plugin-jest-dom": "^4.0.2",
"eslint-plugin-jest-formatting": "^3.1.0",
"eslint-plugin-jsx-a11y": "^6.5.1",
Expand All @@ -112,7 +112,7 @@
},
"devDependencies": {
"@types/eslint": "^8.4.2",
"@types/node": "^17.0.35",
"@types/node": "^17.0.38",
"eslint": "^8.16.0",
"react": "^18.1.0",
"react-dom": "^18.1.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/load-esm-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"normalize-path": "^3.0.0",
"read-pkg-up": "^9.1.0",
"tiny-invariant": "^1.2.0",
"ts-extras": "^0.10.2",
"tsconfck": "^2.0.1",
"type-fest": "^2.13.0"
},
"devDependencies": {
Expand Down
34 changes: 25 additions & 9 deletions packages/load-esm-config/src/load-esm-file.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { objectKeys } from 'ts-extras';
import { build } from 'esbuild';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
Expand All @@ -9,6 +10,7 @@ import { debug } from './constants.js';
import { loadFromBundledFile } from './load-from-bundled-file';
import type { BundleConfigFile } from './types.js';
import { isCommonJsFile, isEsModuleFile, isTypeScriptFile } from './utils';
import { parse } from 'tsconfck';

export interface LoadEsmFileResult {
/**
Expand Down Expand Up @@ -66,7 +68,7 @@ export async function loadEsmFile(filepath: string): Promise<LoadEsmFileResult |

if (isEsModule) {
const fileUrl = pathToFileURL(filepath);
const bundled = await bundleConfigFile({ fileName: filepath, isEsModule });
const bundled = await bundleConfigFile({ fileName: filepath, isEsModule, cwd });
const now = Date.now();
dependencies = bundled.dependencies;

Expand All @@ -76,10 +78,13 @@ export async function loadEsmFile(filepath: string): Promise<LoadEsmFileResult |
// with --experimental-loader themselves, we have to do a hack here:
// bundle the config file w/ ts transforms first, write it to disk,
// load it with native Node ESM, then delete the file.
await fs.writeFile(tmpFile, bundled.code);
exported = await import(`${fileUrl}.js?t=${now}`);
await fs.unlink(tmpFile);
debug(`TS + native esm config loaded in ${getDuration()}`, fileUrl);
try {
await fs.writeFile(tmpFile, bundled.code);
exported = await import(`${fileUrl}.js?t=${now}`);
debug(`TS + native esm config loaded in ${getDuration()}`, fileUrl);
} finally {
await fs.unlink(tmpFile);
}
} else {
exported = await import(`${fileUrl}?t=${now}`);
debug(`native esm config loaded in ${getDuration()}`, fileUrl);
Expand All @@ -88,7 +93,7 @@ export async function loadEsmFile(filepath: string): Promise<LoadEsmFileResult |

if (!exported) {
// bundle config file and transpile to cjs using esbuild
const bundled = await bundleConfigFile({ fileName: filepath });
const bundled = await bundleConfigFile({ fileName: filepath, cwd });
dependencies = bundled.dependencies;
exported = await loadFromBundledFile(filepath, bundled.code);
debug(`bundled config file loaded in ${getDuration()}`);
Expand All @@ -107,14 +112,21 @@ export async function loadEsmFile(filepath: string): Promise<LoadEsmFileResult |
async function bundleConfigFile(
options: BundleConfigFile,
): Promise<{ code: string; dependencies: string[] }> {
const { fileName, isEsModule = false } = options;
const { fileName, cwd, isEsModule = false } = options;
const { tsconfig } = await parse(fileName);
const tsconfigPaths = objectKeys(tsconfig?.compilerOptions?.paths ?? {}).map(
(path) => new RegExp(`^${path.replace('*', '[a-zA-Z-_\\$\\[\\]]*')}`),
);

const result = await build({
absWorkingDir: process.cwd(),
absWorkingDir: cwd,
entryPoints: [fileName],
outfile: 'out.js',
write: false,
platform: 'node',
bundle: true,

// logLevel: 'verbose',
format: isEsModule ? 'esm' : 'cjs',
sourcemap: 'inline',
metafile: true,
Expand All @@ -125,7 +137,11 @@ async function bundleConfigFile(
name: 'externalize-deps',
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
if (args.path[0] === '.' || path.isAbsolute(args.path)) {
if (
args.path[0] === '.' ||
path.isAbsolute(args.path) ||
tsconfigPaths.some((regex) => regex.test(args.path ?? ''))
) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions packages/load-esm-config/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export interface LoadEsmConfigResult<Config extends object = any> {
export interface BundleConfigFile {
fileName: string;
isEsModule?: boolean;
cwd: string;
}

export interface GenerateLookupFiles {
Expand Down
2 changes: 1 addition & 1 deletion packages/load-esm-config/tests/load-esm-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { afterAll, expect, test } from 'vitest';

import { loadEsmConfig } from '../';

export const setupFixtures = createSetupFixtures({ context: {}, fileUrl: import.meta.url });
const setupFixtures = createSetupFixtures({ context: {}, fileUrl: import.meta.url });

afterAll(async () => {
await setupFixtures.cleanup();
Expand Down
4 changes: 2 additions & 2 deletions packages/monots__cli/src/commands/base-command.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type {
BaseCommandProps,
CommandBoolean,
CommandContext,
CommandString,
MonotsCommandProps,
} from '@monots/types';
import { Command, Option } from 'clipanion';
import * as path from 'node:path';

export abstract class BaseCommand extends Command<CommandContext> implements BaseCommandProps {
export abstract class BaseCommand extends Command<CommandContext> implements MonotsCommandProps {
/**
* Set the current working directory from the command line.
*/
Expand Down
37 changes: 37 additions & 0 deletions packages/monots__cli/src/setup-next.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { loadConfig, NAME } from '@monots/core';
import type { CommandContext } from '@monots/types';
import { getPackageJsonSync } from '@monots/utils';
import { Builtins, Cli } from 'clipanion';

const { version, description = '', name } = getPackageJsonSync();

/**
* Loads the `monots` configuration and uses it to register all commands.
*/
export async function createCli() {
const result = await loadConfig();

if (!result) {
throw new Error('No configuration found for `monots`');
}

const cli = new Cli<CommandContext>({
binaryLabel: description,
binaryName: NAME,
binaryVersion: version,
});

cli.register(Builtins.HelpCommand);
cli.register(Builtins.VersionCommand);
}

export const context: CommandContext = {
internal: false,
version,
description,
name,
cwd: process.cwd(),
stdin: process.stdin,
stdout: process.stdout,
stderr: process.stderr,
};
6 changes: 4 additions & 2 deletions packages/monots__cli/src/setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NAME } from '@monots/core';
import type { CommandContext } from '@monots/types';
import { getPackageJson } from '@monots/utils';
import { getPackageJsonSync } from '@monots/utils';
import { Builtins, Cli } from 'clipanion';

import {
Expand All @@ -12,7 +12,7 @@ import {
PrepareCommand,
} from './commands/index.js';

const { version, description = '', name } = getPackageJson();
const { version, description = '', name } = getPackageJsonSync();

export const cli = new Cli<CommandContext>({
binaryLabel: description,
Expand All @@ -39,6 +39,8 @@ export const context: CommandContext = {
version,
description,
name,
colorDepth: 256,
env: process.env,
cwd: process.cwd(),
stdin: process.stdin,
stdout: process.stdout,
Expand Down
6 changes: 3 additions & 3 deletions packages/monots__cli/tests/build-command.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { createSetupFixtures } from '@monots/test';
import { afterAll, expect, test } from 'vitest';
import { afterAll, test } from 'vitest';

import { cli, context } from '../src/setup';
import { cli, context } from '../src/setup.js';

const setupFixtures = createSetupFixtures({ context, fileUrl: import.meta.url });

afterAll(async () => {
setupFixtures.cleanup();
});

test('`monots build` should create build files', async () => {
test.concurrent('`monots build` should create build files', async ({ expect }) => {
const { context } = await setupFixtures('pnpm-to-build');
await cli.run(['prepare'], context);
await cli.run(['fix'], context);
Expand Down
6 changes: 3 additions & 3 deletions packages/monots__cli/tests/create-command.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSetupFixtures } from '@monots/test';
import type { PackageJson } from 'type-fest';
import { afterAll, expect, test } from 'vitest';
import { afterAll, test } from 'vitest';

import { cli, context } from '../src/setup';

Expand All @@ -10,7 +10,7 @@ afterAll(async () => {
setupFixtures.cleanup();
});

test('`monots create` should create package with a description', async () => {
test.concurrent('`monots create` should create package with a description', async ({ expect }) => {
const { context, loadJsonFile } = await setupFixtures('pnpm-with-packages');
const result = await cli.run(['create', '--description', 'DDD', '@scoped/d'], context);
const json = await loadJsonFile<PackageJson>('packages/scoped__d/package.json');
Expand All @@ -34,7 +34,7 @@ test('`monots create` should create package with a description', async () => {
});
});

test('`monots create` should not overwrite existing packages', async () => {
test.concurrent('`monots create` should not overwrite existing packages', async ({ expect }) => {
const { context } = await setupFixtures('pnpm-with-packages');
const result = await cli.run(['create', '--description', 'C', '@scoped/c'], context);

Expand Down
Loading

0 comments on commit 039b4f5

Please sign in to comment.