Skip to content

Commit

Permalink
feat: Make cspell-io and cspell-service-bus pure ESM modules (#4752)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Aug 27, 2023
1 parent 41f1210 commit a113cf7
Show file tree
Hide file tree
Showing 47 changed files with 136 additions and 532 deletions.
19 changes: 10 additions & 9 deletions packages/cspell-eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-eslint-plugin#readme",
"license": "MIT",
"exports": {
"types": "./dist/plugin/index.d.ts",
"require": "./dist/plugin/index.js",
"default": "./dist/plugin/index.js"
"types": "./dist/plugin/index.d.cts",
"require": "./dist/plugin/index.cjs",
"import": "./dist/plugin/index.cjs",
"default": "./dist/plugin/index.cjs"
},
"type": "commonjs",
"main": "dist/plugin/index.js",
"types": "dist/plugin/index.d.ts",
"type": "module",
"main": "dist/plugin/index.cjs",
"types": "dist/plugin/index.d.cts",
"files": [
"assets",
"dist",
Expand All @@ -36,13 +37,13 @@
"scripts": {
"build": "pnpm build:schema && pnpm build:src",
"build:src": "tsc -b ./tsconfig.json",
"build:schema": "ts-json-schema-generator --no-top-ref --expose none --path src/common/options.ts --type Options -o ./assets/options.schema.json",
"build:schema": "ts-json-schema-generator --no-top-ref --expose none --path src/common/options.cts --type Options -o ./assets/options.schema.json",
"watch": "tsc -p . --watch",
"clean": "shx rm -rf dist temp coverage \"*.tsbuildInfo\"",
"clean-build": "pnpm run clean && pnpm run build",
"coverage": "echo coverage",
"test-watch": "pnpm run test -- --watch",
"test": "npx mocha --timeout 10000 \"dist/**/*.test.js\""
"test": "npx mocha --timeout 10000 \"dist/**/*.test.cjs\""
},
"repository": {
"type": "git",
Expand All @@ -65,7 +66,7 @@
"eslint-plugin-react": "^7.33.2",
"mocha": "^10.2.0",
"ts-json-schema-generator": "^1.3.0",
"typescript": "~5.1.6"
"typescript": "^5.2.2"
},
"dependencies": {
"@cspell/cspell-types": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { CSpellSettings } from '@cspell/cspell-types';
import assert from 'assert';
import { describe, test } from 'mocha';

import { defaultOptions, type Options } from './options.js';
import { defaultOptions, type Options } from './options.cjs';

describe('options', () => {
test('Options are compatible with cspell-types', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { readFileSync } from 'fs';
import { join as pathJoin } from 'path';
import { createSyncFn } from 'synckit';

import type { Issue, SpellCheckSyncFn } from '../worker/types';
import { normalizeOptions } from './defaultCheckOptions';
import type { Issue, SpellCheckSyncFn } from '../worker/types.cjs';
import { normalizeOptions } from './defaultCheckOptions.cjs';

const optionsSchema = JSON.parse(readFileSync(pathJoin(__dirname, '../../assets/options.schema.json'), 'utf8'));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Check, Options, RequiredOptions, WorkerOptions } from '../common/options';
import type { Check, Options, RequiredOptions, WorkerOptions } from '../common/options.cjs';

export const defaultCheckOptions: Required<Check> = {
checkComments: true,
Expand Down
2 changes: 2 additions & 0 deletions packages/cspell-eslint-plugin/src/plugin/index.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { Options } from '../common/options.cjs';
export { configs, rules } from './cspell-eslint-plugin.cjs';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RuleTester } from 'eslint';
import * as fs from 'fs';
import * as path from 'path';

import * as Rule from './index';
import * as Rule from './index.cjs';

const root = path.resolve(__dirname, '../..');
const fixturesDir = path.join(root, 'fixtures');
Expand Down
2 changes: 0 additions & 2 deletions packages/cspell-eslint-plugin/src/plugin/index.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/cspell-eslint-plugin/src/plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"moduleResolution": "node",
"module": "CommonJS",
"moduleResolution": "Node",
"tsBuildInfoFile": "../../dist/plugin/compile.tsbuildInfo",
"rootDir": ".",
"outDir": "../../dist/plugin"
Expand Down
6 changes: 3 additions & 3 deletions packages/cspell-eslint-plugin/src/worker/spellCheck.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import type { Comment, Identifier, ImportSpecifier, Literal, Node, TemplateEleme
import * as path from 'path';
import { format } from 'util';

import type { CustomWordListFile, WorkerOptions } from '../common/options.js';
import type { ASTNode, JSXText, NodeType } from './ASTNode.js';
import type { Issue, Suggestions } from './types.js';
import type { CustomWordListFile, WorkerOptions } from '../common/options.cjs';
import type { ASTNode, JSXText, NodeType } from './ASTNode.cjs';
import type { Issue, Suggestions } from './types.cjs';
import { walkTree } from './walkTree.mjs';

const defaultSettings: CSpellSettings = {
Expand Down
3 changes: 0 additions & 3 deletions packages/cspell-eslint-plugin/src/worker/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"tsBuildInfoFile": "../../dist/worker/compile.tsbuildInfo",
"target": "ES2020",
"module": "ES2020",
"allowJs": true,
"moduleResolution": "node16",
"rootDir": ".",
"outDir": "../../dist/worker"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Node } from 'estree';

import type { WorkerOptions } from '../common/options.js';
import type { NodeType } from './ASTNode.js';
import type { WorkerOptions } from '../common/options.cjs';
import type { NodeType } from './ASTNode.cjs';

interface ExtendedSuggestion {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-eslint-plugin/src/worker/walkTree.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Node } from 'estree-walker';
import { walk } from 'estree-walker';

import type { ASTNode } from './ASTNode.js';
import type { ASTNode } from './ASTNode.cjs';

type Key = string | number | symbol | null | undefined;

Expand Down
4 changes: 2 additions & 2 deletions packages/cspell-eslint-plugin/src/worker/worker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

/**
* @typedef {import('estree').Node} Node
* @typedef {import('./types.js').Issue} Issue
* @typedef {import('../common/options.js').WorkerOptions} WorkerOptions
* @typedef {import('./types.cjs').Issue} Issue
* @typedef {import('../common/options.cjs').WorkerOptions} WorkerOptions
*/

import { runAsWorker } from 'synckit';
Expand Down
5 changes: 2 additions & 3 deletions packages/cspell-eslint-plugin/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
"compilerOptions": {
"composite": true,
"incremental": true,
"target": "ES2020",
"lib": ["ES2020"],
"module": "CommonJS"
"module": "Node16",
"moduleResolution": "Node16"
},
"exclude": ["dist", "node_modules"]
}
14 changes: 6 additions & 8 deletions packages/cspell-io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
"name": "cspell-io",
"version": "7.0.1",
"description": "A library of useful I/O functions used across various cspell tools.",
"type": "commonjs",
"module": "dist/esm/index.mjs",
"main": "dist/cjs/index.js",
"typings": "dist/cjs/index.d.ts",
"type": "module",
"module": "dist/esm/index.js",
"exports": {
".": {
"import": "./dist/esm/index.mjs",
"require": "./dist/cjs/index.js"
"import": "./dist/esm/index.js"
}
},
"files": [
Expand All @@ -21,7 +18,7 @@
"!**/*.map"
],
"scripts": {
"build": "tsc -b . && ts2mjs dist/esm",
"build": "tsc -b .",
"build:esm": "tsc -p tsconfig.esm.json",
"build:watch": "tsc -b . -w",
"watch": "pnpm build:watch",
Expand Down Expand Up @@ -49,7 +46,8 @@
},
"devDependencies": {
"@types/node-fetch": "^2.6.4",
"lorem-ipsum": "^2.0.8"
"lorem-ipsum": "^2.0.8",
"typescript": "^5.2.2"
},
"dependencies": {
"@cspell/cspell-service-bus": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-io/src/handlers/node/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { gunzipSync, gzipSync } from 'zlib';
import { arrayBufferViewToBuffer } from '../../common/arrayBuffers.js';
import { decode } from '../../common/encode-decode.js';
import { toError } from '../../errors/index.js';
import type { BufferEncoding } from '../../models/BufferEncoding';
import type { BufferEncoding } from '../../models/BufferEncoding.ts';
import { decodeDataUrl } from '../../node/dataUrl.js';
import { fetchURL } from '../../node/file/fetch.js';
import { getStatHttp } from '../../node/file/stat.js';
Expand Down
11 changes: 8 additions & 3 deletions packages/cspell-io/src/node/file/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Headers } from 'node-fetch';
/* eslint-disable @typescript-eslint/ban-ts-comment */
import type { Headers, RequestInit, Response } from 'node-fetch';
import nodeFetch from 'node-fetch';

import { FetchUrlError } from './FetchError.js';

export const fetch = nodeFetch;

export async function fetchHead(request: string | URL): Promise<Headers> {
const r = await fetch(request, { method: 'HEAD' });
return r.headers;
Expand All @@ -17,3 +16,9 @@ export async function fetchURL(url: URL): Promise<Buffer> {
}
return Buffer.from(await response.arrayBuffer());
}

export function fetch(url: string | URL, init?: RequestInit): Promise<Response> {
/// This is a n issue with how TypeScript handles packages without `type` being set.
// @ts-ignore
return nodeFetch(url, init);
}
2 changes: 1 addition & 1 deletion packages/cspell-io/src/requests/RequestFsReadFile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ServiceRequestFactoryRequestType } from '@cspell/cspell-service-bus';
import { requestFactory } from '@cspell/cspell-service-bus';

import type { BufferEncoding } from '../models/BufferEncoding';
import type { BufferEncoding } from '../models/BufferEncoding.js';
import type { TextFileResource } from '../models/FileResource.js';

const RequestType = 'fs:readFile' as const;
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-io/src/requests/RequestFsReadFileSync.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ServiceRequestFactoryRequestType } from '@cspell/cspell-service-bus';
import { requestFactory } from '@cspell/cspell-service-bus';

import type { BufferEncoding } from '../models/BufferEncoding';
import type { BufferEncoding } from '../models/BufferEncoding.js';
import type { TextFileResource } from '../models/FileResource.js';

const RequestType = 'fs:readFileSync' as const;
Expand Down
13 changes: 0 additions & 13 deletions packages/cspell-io/tsconfig.cjs.json

This file was deleted.

2 changes: 0 additions & 2 deletions packages/cspell-io/tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"compilerOptions": {
"composite": true,
"tsBuildInfoFile": "dist/compile.esm.tsbuildInfo",
"module": "ES2020",
"moduleResolution": "node16",
"rootDir": "src",
"outDir": "dist/esm",
"sourceMap": false,
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-io/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"files": [],
"references": [{ "path": "./tsconfig.cjs.json" }, { "path": "./tsconfig.esm.json" }]
"references": [{ "path": "./tsconfig.esm.json" }]
}
14 changes: 6 additions & 8 deletions packages/cspell-service-bus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
"author": "Jason Dent <jason@streetsidesoftware.nl>",
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-service-bus#readme",
"license": "MIT",
"type": "commonjs",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.mjs",
"types": "dist/cjs/index.d.ts",
"type": "module",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"exports": {
".": {
"import": "./dist/esm/index.mjs",
"require": "./dist/cjs/index.js"
"import": "./dist/esm/index.js"
}
},
"files": [
Expand All @@ -31,9 +29,9 @@
"!**/*.map"
],
"scripts": {
"build": "tsc -b . && ts2mjs dist/esm",
"build": "tsc -b .",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build:esm": "tsc -p tsconfig.esm.json && ts2mjs dist/esm",
"build:esm": "tsc -p tsconfig.esm.json",
"build:clean": "pnpm run clean && pnpm run build",
"watch": "tsc -b . -w",
"clean": "shx rm -rf dist temp coverage \"*.tsbuildInfo\"",
Expand Down
11 changes: 0 additions & 11 deletions packages/cspell-service-bus/tsconfig.cjs.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/cspell-service-bus/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"files": [],
"references": [{ "path": "./tsconfig.cjs.json" }, { "path": "./tsconfig.esm.json" }]
"references": [{ "path": "./tsconfig.esm.json" }]
}
1 change: 1 addition & 0 deletions packages/cspell/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
}
],
"dictionaries": ["workspace"],
"noConfigSearch": true,
"ignoreWords": []
}
30 changes: 26 additions & 4 deletions packages/cspell/src/app/__snapshots__/app.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,29 @@ café - softwareTerms* node_modules/@cspell/.../dict/softwareTerms.txt
café - workspace* ../../cspell-dict.txt"
`;
exports[`Validate cli > app 'trace flavour' Expect Error: undefined 1`] = `[]`;
exports[`Validate cli > app 'trace flavour' Expect Error: undefined 2`] = `
"Word F Dictionary Dictionary Location
flavour - [flagWords]* From Settings \`flagWords\`
flavour - [ignoreWords]* From Settings \`ignoreWords\`
flavour - [suggestWords]* From Settings \`suggestWords\`
flavour - [words]* From Settings \`words\`
flavour - aws* node_modules/@cspell/dict-aws/dict/aws.txt
flavour - companies* node_modules/@cspell/.../dict/companies.txt
flavour - computing-acronyms* node_modules/.../dict/computing-acronyms.txt
flavour * cpp node_modules/@cspell/dict-cpp/dict/cpp.txt
flavour * cpp-legacy node_modules/@cspell/.../dict/cpp-legacy.txt
flavour - cryptocurrencies* node_modules/.../cryptocurrencies.txt.gz
flavour * en_us* node_modules/@cspell/dict-en_us/en_US.trie.gz
flavour - en-common-misspelling* node_modules/@cspell/.../dict-en.yaml
flavour * en-gb node_modules/@cspell/dict-en-gb/en_GB.trie.gz
flavour - filetypes* node_modules/@cspell/.../filetypes.txt.gz
flavour ! forbidden-words* samples/forbidden-words.txt
flavour - public-licenses* node_modules/@cspell/.../public-licenses.txt.gz
flavour - softwareTerms* node_modules/@cspell/.../dict/softwareTerms.txt"
`;
exports[`Validate cli > app 'trace hello' Expect Error: undefined 1`] = `[]`;
exports[`Validate cli > app 'trace hello' Expect Error: undefined 2`] = `
Expand Down Expand Up @@ -1024,10 +1047,9 @@ exports[`Validate cli > app 'with forbidden words' Expect Error: [Function Check
exports[`Validate cli > app 'with forbidden words' Expect Error: [Function CheckFailed] 2`] = `
"error 0.00ms X
log ./samples/src/sample-with-forbidden-words.md:3:3 - Forbidden word (behaviour)
log ./samples/src/sample-with-forbidden-words.md:4:3 - Forbidden word (flavour)
log ./samples/src/sample-with-forbidden-words.md:5:3 - Forbidden word (colour)
error CSpell: Files checked: 1, Issues found: 3 in 1 files"
log ./samples/src/sample-with-forbidden-words.md:3:3 - Unknown word (behaviour)
log ./samples/src/sample-with-forbidden-words.md:5:3 - Unknown word (colour)
error CSpell: Files checked: 1, Issues found: 2 in 1 files"
`;
exports[`Validate cli > app 'with forbidden words' Expect Error: [Function CheckFailed] 3`] = `
Expand Down
1 change: 1 addition & 0 deletions packages/cspell/src/app/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ describe('Validate cli', () => {
${'trace not-in-any-dictionary'} | ${['trace', 'not-in-any-dictionary']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'trace missing dictionary'} | ${['trace', 'hello', '-c', 'samples/cspell-missing-dict.json']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'with spelling errors --debug Dutch.txt'} | ${['--relative', '--debug', pathSamples('Dutch.txt')]} | ${app.CheckFailed} | ${true} | ${true} | ${true}
${'trace flavour'} | ${['trace', 'flavour', '-c', pathSamples('.cspell.json')]} | ${undefined} | ${false} | ${true} | ${false}
`('app $msg Expect Error: $errorCheck', async ({ testArgs, errorCheck, eError, eLog, eInfo }: TestCase) => {
chalk.level = 0;
const commander = getCommander();
Expand Down
Loading

0 comments on commit a113cf7

Please sign in to comment.