Skip to content

Commit

Permalink
refactor: remove .cjs dependency (#5956)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Jul 22, 2024
1 parent 0cfab8e commit 0b9ea12
Show file tree
Hide file tree
Showing 29 changed files with 92 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
missing-file.txt
../../../src/app/app.ts
../../../src/app/app.mts
../../../README.md
6 changes: 3 additions & 3 deletions packages/cspell/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@
],
"scripts": {
"clean": "shx rm -rf dist temp coverage \"*.tsbuildInfo\"",
"build": "tsc -b . -f && ts2mjs dist/esm && pnpm run build:api",
"build": "tsc -p . && pnpm run build:api",
"build:api": "rollup -c rollup.config.mjs",
"build:esm": "tsc -b tsconfig.esm.json -f",
"build:esm:ts2mjs": "tsc -b tsconfig.esm.json && ts2mjs dist/esm",
"build:esm": "tsc -p .",
"build:lib": "tsc -b src/lib/tsconfig.json -f",
"build:readme": "pnpm build:readme:help",
"build:readme:help": "pnpm build:readme:help:lint && pnpm build:readme:help:trace && inject-markdown README.md && prettier -w README.md",
Expand All @@ -56,6 +55,7 @@
"watch": "tsc -b . -w -f",
"compile": "tsc -b . -f",
"test-watch": "vitest",
"version": "node ./tools/patch-version.mjs",
"prepublishOnly": "pnpm run clean-build",
"test:update-snapshot": "vitest run --update"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import type { Command } from 'commander';
import { Option as CommanderOption, program } from 'commander';
import { satisfies as semverSatisfies } from 'semver';

import { npmPackage } from '../lib/pkgInfo.cjs';
import { commandCheck } from './commandCheck.js';
import { commandLink } from './commandLink.js';
import { commandLint } from './commandLint.js';
import { commandSuggestion } from './commandSuggestion.js';
import { commandTrace } from './commandTrace.js';
import { npmPackage } from './pkgInfo.js';
import { ApplicationError } from './util/errors.js';

export { LinterCliOptions as Options } from './options.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell/src/app/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as Commander from 'commander';
import stripAnsi from 'strip-ansi';
import { afterEach, beforeEach, type Constructable, describe, expect, test, vi } from 'vitest';

import * as app from './app.js';
import * as app from './app.mjs';
import { console } from './console.js';
import * as Link from './link.js';
import { pathPackageRoot } from './test/test.helper.js';
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/cspell/src/app/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Issue, RunResult } from '@cspell/cspell-types';
import getStdin from 'get-stdin';
import { afterEach, describe, expect, test, vi } from 'vitest';

import * as App from './application.js';
import * as App from './application.mjs';
import type { LinterOptions, TraceOptions } from './options.js';
import { pathPackageRoot, pathSamples } from './test/test.helper.js';
import { asyncIterableToArray } from './util/async.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/cspell/src/app/commandCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import chalk from 'chalk';
import type { Command } from 'commander';
import { Option as CommanderOption } from 'commander';

import * as App from './application.js';
import { checkText } from './application.js';
import * as App from './application.mjs';
import { checkText } from './application.mjs';
import { console } from './console.js';
import type { BaseOptions } from './options.js';
import { CheckFailed } from './util/errors.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell/src/app/commandLint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AddHelpTextContext, Command } from 'commander';
import { Option as CommanderOption } from 'commander';

import * as App from './application.js';
import * as App from './application.mjs';
import type { LinterCliOptions } from './options.js';
import { DEFAULT_CACHE_LOCATION } from './util/cache/index.js';
import { CheckFailed } from './util/errors.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell/src/app/commandSuggestion.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Command } from 'commander';
import { Option as CommanderOption } from 'commander';

import * as App from './application.js';
import * as App from './application.mjs';
import { emitSuggestionResult } from './emitters/suggestionsEmitter.js';
import type { SuggestionOptions } from './options.js';
import { CheckFailed } from './util/errors.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell/src/app/commandTrace.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Command } from 'commander';
import { Option as CommanderOption } from 'commander';

import * as App from './application.js';
import * as App from './application.mjs';
import { console } from './console.js';
import { isDictionaryPathFormat } from './emitters/DictionaryPathFormat.js';
import { emitTraceResults } from './emitters/traceEmitter.js';
Expand Down
15 changes: 15 additions & 0 deletions packages/cspell/src/app/dirname.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This file is generated by tools/patch-version.mjs
import { fileURLToPath } from 'node:url';

declare const __dirname: string;

let _dirname: string;

try {
if (typeof import.meta.url !== 'string') throw new Error('assert');
_dirname = fileURLToPath(new URL('.', import.meta.url));
} catch {
_dirname = __dirname;
}

export const pkgDir = _dirname;
2 changes: 1 addition & 1 deletion packages/cspell/src/app/emitters/traceEmitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { posix, win32 } from 'node:path';
import strip from 'strip-ansi';
import { describe, expect, test, vi } from 'vitest';

import type { TraceResult } from '../application.js';
import type { TraceResult } from '../application.mjs';
import { console } from '../console.js';
import { __testing__, calcTraceResultsReport, emitTraceResults } from './traceEmitter.js';

Expand Down
2 changes: 1 addition & 1 deletion packages/cspell/src/app/emitters/traceEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as iPath from 'node:path';

import chalk from 'chalk';

import type { TraceResult } from '../application.js';
import type { TraceResult } from '../application.mjs';
import { console } from '../console.js';
import { TableRow, tableToLines } from '../util/table.js';
import type { DictionaryPathFormat } from './DictionaryPathFormat.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './application.js';
export * from './application.mjs';
export { getReporter as getDefaultReporter } from './cli-reporter.js';
export type { CSpellReporterConfiguration, CSpellReporterModule } from './models.js';
export type { BaseOptions, LinterCliOptions as CSpellApplicationOptions, TraceOptions } from './options.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/cspell/src/app/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, test } from 'vitest';

// Make sure the types are exported.
import type { CSpellApplicationOptions } from './index.js';
import * as index from './index.js';
import type { CSpellApplicationOptions } from './index.mjs';
import * as index from './index.mjs';
import { InMemoryReporter } from './util/InMemoryReporter.js';

describe('Validate index.ts', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell/src/app/lint/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from 'node:path';

import { describe, expect, test } from 'vitest';

import { CheckFailed } from '../app.js';
import { CheckFailed } from '../app.mjs';
import { pathPackageRoot } from '../test/test.helper.js';
import { InMemoryReporter } from '../util/InMemoryReporter.js';
import { runLint } from './lint.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell/src/app/lint/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import {
Text as cspellText,
} from 'cspell-lib';

import { npmPackage } from '../../lib/pkgInfo.cjs';
import { console } from '../console.js';
import { getFeatureFlags } from '../featureFlags/index.js';
import { CSpellReporterConfiguration } from '../models.js';
import { npmPackage } from '../pkgInfo.js';
import type { CreateCacheSettings, CSpellLintResultCache } from '../util/cache/index.js';
import { calcCacheSettings, createCache } from '../util/cache/index.js';
import { CheckFailed, toApplicationError, toError } from '../util/errors.js';
Expand Down
9 changes: 9 additions & 0 deletions packages/cspell/src/app/pkgInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This file is generated by tools/patch-version.mjs

export { pkgDir } from './dirname.js';

export const name = 'cspell';
export const version = '8.11.0';
export const engines = { node: '>=18' };

export const npmPackage = { name, version, engines };
4 changes: 2 additions & 2 deletions packages/cspell/src/app/util/cache/DiskCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import * as path from 'node:path';

import { afterEach, beforeEach, describe, expect, type Mock, test, vi } from 'vitest';

import { createFromFile } from '../../../lib/file-entry-cache.cjs';
import * as fileHelper from '../../util/fileHelper.js';
import type { CachedFileResult, CSpellCacheMeta } from './DiskCache.js';
import { __testing__, DiskCache } from './DiskCache.js';
import { createFromFile } from './file-entry-cache.mjs';

const { calcVersion } = __testing__;

vi.mock('../../../lib/file-entry-cache.cjs', () => ({
vi.mock('./file-entry-cache.mjs', () => ({
createFromFile: vi.fn().mockReturnValue({
getFileDescriptor: vi.fn(),
reconcile: vi.fn(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type { FileDescriptor, FileEntryCache } from 'file-entry-cache';
import type { FileEntryCache } from 'file-entry-cache';
/* This is to work around a bug in file-entry-cache that uses `this` */
import * as fileEntryCache from 'file-entry-cache';
import fileEntryCache from 'file-entry-cache';

export type { FileDescriptor, FileEntryCache } from 'file-entry-cache';

export function createFromFile(pathToCache: string, useChecksum?: boolean): FileEntryCache {
return fileEntryCache.createFromFile(pathToCache, useChecksum);
Expand Down
6 changes: 3 additions & 3 deletions packages/cspell/src/app/util/cache/fileEntryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* This is a wrapper for 'file-entry-cache'
*/

export type { FileDescriptor } from '../../../lib/file-entry-cache.cjs';
export type { FileDescriptor } from './file-entry-cache.mjs';
import { mkdirSync } from 'node:fs';
import * as path from 'node:path';
import { isMainThread } from 'node:worker_threads';

import type { FileEntryCache as FecFileEntryCache } from '../../../lib/file-entry-cache.cjs';
import * as fec from '../../../lib/file-entry-cache.cjs';
import type { FileEntryCache as FecFileEntryCache } from './file-entry-cache.mjs';
import * as fec from './file-entry-cache.mjs';

export type FileEntryCache = FecFileEntryCache;

Expand Down
2 changes: 1 addition & 1 deletion packages/cspell/src/app/util/reporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
} from '@cspell/cspell-types';
import { dynamicImport } from '@cspell/dynamic-import';

import { pkgDir } from '../../lib/pkgInfo.cjs';
import { pkgDir } from '../pkgInfo.js';
import { ApplicationError, toError } from './errors.js';

type StandardEmitters = Omit<CSpellReporter, 'result'>;
Expand Down
3 changes: 0 additions & 3 deletions packages/cspell/src/lib/pkgInfo.cts

This file was deleted.

14 changes: 0 additions & 14 deletions packages/cspell/src/lib/tsconfig.cjs.json

This file was deleted.

4 changes: 0 additions & 4 deletions packages/cspell/src/lib/tsconfig.json

This file was deleted.

14 changes: 0 additions & 14 deletions packages/cspell/src/lib/tsconfig.test.json

This file was deleted.

31 changes: 31 additions & 0 deletions packages/cspell/tools/patch-version.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { promises as fs } from 'node:fs';

const urlPackageJson = new URL('../package.json', import.meta.url);
const urlVersionFile = new URL('../src/app/pkgInfo.ts', import.meta.url);

async function readPackageJson() {
const pkgJson = await fs.readFile(urlPackageJson, 'utf8');
return JSON.parse(pkgJson);
}

async function writeVersionFile(pkgJson) {
const fileContent = `\
// This file is generated by tools/patch-version.mjs
export { pkgDir } from './dirname.js';
export const name = '${pkgJson.name}';
export const version = '${pkgJson.version}';
export const engines = { node: '${pkgJson.engines.node}' };
export const npmPackage = { name, version, engines };
`;
await fs.writeFile(urlVersionFile, fileContent);
}

async function run() {
const pkgJson = await readPackageJson();
await writeVersionFile(pkgJson);
}

await run();
14 changes: 0 additions & 14 deletions packages/cspell/tsconfig.esm.json

This file was deleted.

11 changes: 9 additions & 2 deletions packages/cspell/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"files": [],
"references": [{ "path": "./tsconfig.esm.json" }, { "path": "./src/lib/tsconfig.json" }]
"extends": "../../tsconfig.esm.json",
"compilerOptions": {
"rootDir": "src/app",
"outDir": "dist/esm",
"target": "es2022",
"lib": ["es2023"],
"types": ["node"]
},
"include": ["src/app"]
}

0 comments on commit 0b9ea12

Please sign in to comment.