[SOLVED] An example: link to an external CSON file in 'init.coffee'


#1

Hello.

I must be asking the wrong questions,
as Lady Google is currently not giving me the hints I am hoping for…

I hope to construct a little code piece in init.coffee that needs to use a look-up table.
The idea is to use an external CSON file for that.

Could someone please show me what to do to include the CSON file into the coffeescript code. A web search term would help too. I assume it is not needed to include external dependency as Atom already uses CSON files for their own internal configurations.

Thank you for your attention.
-Dan


#2

This package is what Atom uses. Your code would start something like:

CSON = require 'season'

atom.commands.add 'atom-workspace', 'custom:log-keys', ->
    CSON.readFile "C:/Users/<name>/.atom/keymap.cson", (err, data) ->
        console.log(data)

I tested this just now and it works perfectly, printing an object to my console with all of my keybindings.


#3

Thank you for providing a very helpful hint.
In my case I had to make a small change:

CSON = require './packages/language-markdown/node_modules/season'

The season package is not available to me in any other way.

I wonder… is it required to give the absolute file path for the file to be read? I have tried plainly keymap.cson, which did not work. This is strange as the above ./package/.... worked.

Cheers.


#4

Version 3 of the above example…

baseName = atom.getConfigDirPath() # try to overcome the custom position of the installation
CSON = require (baseName+'/packages/language-markdown/node_modules/season')

atom.commands.add 'atom-workspace', 'custom:log-keys', ->
  fileName = baseName + '/keymap.cson'
  CSON.readFile fileName, (err, data) ->
    console.log(data)

#5

Weird. I was able to get to it from init.coffee just using its name.

What about './keymap.cson'?


#6

Perhaps that has something to do with the fact that Atom is not loaded into my path.

Now that is the strange one… I tried that first earlier. That also does not work. Any way I can check what the working directory is at the moment the code is executed? (academical question)



Just for the sake of sharing…

# ./init.coffee
baseName = atom.getConfigDirPath() # try to overcome the custom position of the installation
CSON = require (baseName+'/packages/language-markdown/node_modules/season')

atom.commands.add 'atom-text-editor', 'test command', ->
  _string = "1 5 7 2 90 4 2"
  _fileName = baseName + "/customTools/dictionary.cson"
  CSON.readFile _fileName, (_err, _list) ->
    # console.log(_list)
    console.log _string.replace /(\w+)/g, (_get) ->
      x = _get
      for _key, _value of _list
        x = _value if _get == _key
      _get = x
# ./customTools/dictionary.cson
1 : 21
2 : 32
3 : 43
4 : 54
5 : 65
6 : 76
7 : 87
8 : 98
9 : 19

It is a trivialized example, but it is proof on concept for me. Coding I write for PLCs uses sequence numbers (integers). It happens regularly that I have to “move” stuff around. This will help with exchanging one number (or even a string word) from one to another.

The next step is to replace _string with the text buffer or use scan from Atom’s API.

@DamnedScholar - my sincere thanks for your attention.


#7

Try this in the console:

fs = require('fs')
fs.realpath('.', (err, path) => console.log(path))

#8

Answer:
C:\Users\Dan\AppData\Local\atom\app-1.20.0
:thinking:


#9

Two thoughts:

  1. Wouldn’t it be simpler to create a package that has logic and data in one place?

  2. Everything would be easier if you just used Node’s native format: JSON:

    const data = require('path/to/data.json')


#10

(1)
A valid point.

I have not considered this before as…
a) The text manipulation tasks needed are very variable… depending on the PLC project at hand.
b) I am still feeling my way through what can be done.
c) I am still a bit apprehensive in creating a project. not claiming any logic to that

Granted it does not invalidate your suggestion as my goals can be achieved within (an unpublished) project.

(2)
I am not a fan of JSON. In this case it would not matter though as the dictionary is very straight forward. A valuable suggestion you have made.

When you use const I assume you are putting down Javascript. I further assume that Coffeescript will do the same and would not need const. Interesting… I would think that Coffeescript would have the same for CSON. I will do some tests soon.

Thank you for taking the time to make your contribution.


ADDITION:
The Coffeescript works when leaving out the const.

# init.coffee
atom.commands.add 'atom-text-editor', 'test command', ->
  _list = require('dict.json')  # no 'const'    this is not the real file path 
  _string = "1 5 7 2 90 4 2"
  console.log _string.replace /(\w+)/g, (_get) ->
    x = _get
    for _key, _value of _list
      x = _value if _get == _key
    _get = x

Noting the " and , in the dict.json file

{
  "1": "21", "2": "32", "3": "43", "4": "54", "5": "65", "6": "76", "7": "87", "8": "98", "9": "19"
}

#11

CoffeeScript has to be compiled into JavaScript before it can be run. A CSON file can be compiled into a JSON file when CS runs, but then you won’t be able to change the stored data and have those changes reflected in your program without running CS again. With season, you can read from and write to CSON files directly.


#12

Hello.

Many thanks for voicing the pros of using season. Your notes are important to me.

Using the JSON file is more in line with the needs of current task. It will be configure once, read once and use once per task.

If the task was morphed to more than a code piece; into a project, the season add-in becomes of advantage. It is especially the error checking that would make it attractive. Thereafter comes the ability to write values to the CSON file from some user input.

Bottom line… I am using the JSON file now, but is thankful for knowing what to do with the CSON file when the next phase comes up.

Best regards.