streetsidesoftware / cspell-cli

CSpell command line spell checker.
MIT License
70 stars 8 forks source link

How to install npm dictionaries while using Docker cspell-cli? #454

Closed GitSparTV closed 4 months ago

GitSparTV commented 4 months ago

Hello. I'm using this program using Docker and haven't found a way to import additional dictionaries such as languages (the ones with @cspell at the beginning). How do deal with them? Currently I've put urls to dictionaries from the GitHub. Should I try to derive your Docker image and create my own?

Jason3S commented 4 months ago

@GitSparTV,

That is a good question. I have not tried it.

There are three approaches I can think of, two match yours.

  1. Install CSpell dictionaries locally. In the project you want to spell check. If you are already in a Node JS environment, that is pretty easy.
  2. As you suggested, derive the Docker image.
  3. As you are currently doing, import URLS: cspell.config.yaml
    import:
      - https://cdn.jsdelivr.net/npm/@cspell/dict-de-de@3.2.0/cspell-ext.json
Jason3S commented 4 months ago

@GitSparTV,

I created an example of how to extend the Docker image to include other dictionaries: PR Example

Extending the Docker Container to include German

The CSpell container has all the default dictionaries installed, but to include other dictionaries, it is necessary to extend the container.

Dockerfile

FROM ghcr.io/streetsidesoftware/cspell:8.4.1

WORKDIR /app
# Setting HOME is necessary to set the location of CSpell's global config.
ENV HOME=/home
# Install the dictionaries we want
RUN npm install @cspell/dict-de-de@3.2.0
# Link the dictionaries so that CSpell is aware of them by default.
RUN cspell-cli link add @cspell/dict-de-de

# Restore the working directory to the virtual directory.
WORKDIR /workdir

Building

docker build -t cspell-german:latest .

Running

Example of cspell trace command:

docker run -v $PWD:/workdir \
 cspell-german:latest \
 trace Strasse \
 --locale=de --dictionary-path=short

Result:

Word    F Dictionary           Dictionary Location
Strasse - [flagWords]*         From Settings `flagWords`
Strasse - [ignoreWords]*       From Settings `ignoreWords`
Strasse - [suggestWords]*      From Settings `suggestWords`
Strasse - [words]*             From Settings `words`
Strasse - aws*                 [node_modules]/aws.txt
Strasse - companies*           [node_modules]/companies.txt
Strasse - computing-acronyms*  [node_modules]/computing-acronyms.txt
Strasse - cryptocurrencies*    [node_modules]/cryptocurrencies.txt
Straße  * de-de*               [node_modules]/de_DE.trie.gz
Strasse - filetypes*           [node_modules]/filetypes.txt.gz
Strasse - public-licenses*     [node_modules]/public-licenses.txt.gz
Strasse - softwareTerms*       [node_modules]/softwareTerms.txt
Strasse - web-services*        [node_modules]/webServices.txt

CSpell Configuration

It is necessary to enable the locale for the dictionary to be used. On the command line, it is --locale=<lang>, i.e. --locale=de,en for German and English.

In a CSpell Configuration file:

cspell.config.yaml

language: de,en

cspell.json

{
  "language": "de,en"
}
GitSparTV commented 4 months ago

Incredible! Thank you. Meanwhile I've created a dev container and installed cspell manually. But this won't be useless for sure.