How to load a local file for a clojurescript test - clojurescript

I am writing some cljs.test tests for a clojurescript library and have some JSON test data I would like to load in.
I'm unsure what function I can use to load the data from files local to the project.
I'm using lein-doo as the test runner, initially with a nodejs configuration, ideally I would like to load files in an agnostic manner if possible, but I'd be fine with a nodejs-specific answer.

Following Piotrek's link I have come up with the following nodejs-only function that accomplishes this:
(defn node-slurp [path]
(let [fs (nodejs/require "fs")]
(.readFileSync fs path "utf8")))
(Node documentation: File System - readFileSync)

Related

How do you append to a file in ClojureScript (on Node)?

How can you append to a local file using ClojureScript (running on Node)?
Use the Node.js fs.appendFileSync method from the fs package, like so:
(.appendFileSync fs "/path/to/file.txt" "string to append")
This will also create the file if it doesn't exist yet.

Loading JSON files in React, without JSON extension

I'm trying to require some JSON files in my React app (based on CRA 3.01 with Typescript).
The normal const obj = require('./path/file.json') would work if my files had a .json extension - however, these files have .md for 'metadata' and a couple other extensions, and the standard require isn't working. The files are from a tool, so changing to .json isn't a practical option.
Doing some research, it seems the approach is to use the webpack json-loader module (the webpack json-loader docs says that working with different file extensions is the main reason for using the module). I found an example and am using this:
const context = require.context(
"json-loader!./metadata",
true,
/^\.\/.*\.md$/
);
const metadata = context("./foo.md");
I've got a minimum reproduction here (see App.tsx):
https://github.com/ericsolberg/testjson
It seems that this is correctly using the json-loader, and finding the file correctly. However, I'm getting a syntax error:
Error: Module build failed (from ./node_modules/json-loader/index.js):
SyntaxError: Unexpected token m in JSON at position 0
at JSON.parse (<anonymous>)
at Object.module.exports (/Users/***/projects/jsontest/node_modules/json-loader/index.js:4:49)
I did some research on this error, and believe the problem is that the file is being parsed twice - first by the loader configured by CreateCreactApp's default webpack config, then by the specified JSON loader.
I don't want to eject my CRA app to modify the webpack config, and would like to avoid a re-wire hack (and whatever other issues that introduces) ... does anyone know of a way to load JSON files in a CRA app, if these files don't have a JSON extension?
Here's the solution that ended up working for me.
I could eject my project, of course, and customize the webpack config to load JSON files with other extensions. It may be possible to make a rewire hack work as well.
But I realized that when I require a file that is not one of the extensions recognized by CRA's config, it instead copies that file into the build, and require('file.ext') returns the URL of the file. So I'm using axios to load the file. This means a trip to the server for something that could be done statically, but for where I'm taking this project that is actually OK (eventually it will load metadata from a server anyway).

Read and Write file using vs code extension

i am building an extension to parse json using vs code extension.
so my need is ,it should be able able to load .json file from a particular folder and iterate through content of the file.
Then it should allow user to select few keys from it make a new json file out of this and save it in any folder.
But i am not able to find any way to read and write files in "vs code extension".Could someone please help me.
If you want to read the current edit state of a file you can use the following API workspace function:
vscode.workspace.openTextDocument(uri).then((document) => {
let text = document.getText();
});
This will show you the current state of the file including unpersisted changes. document is of type TextDocument and has isDirty set to true if it has pending changes.
Since the extension runs in nodejs, you should be able to use any nodejs module built-in or installed by npm in the usual way.
For your purpose you will be OK with the built-in fs module: https://nodejs.org/dist/latest-v6.x/docs/api/fs.html
In your extension you will need to import the required module, so your code file should contain this:
let fs = require("fs");
and then use the methods in the usual way, eg. fs.fileReadSync( filename, encoding ) ...
Please not that there is one exception. If you install a nodejs module containing compiled, binary code, it will not run in the extension and instead you will see an error message saying something like %1 is not a valid Win32 application. Pure javascript modules are OK, though.
VSCode extensions are running in node.js. Therefore you can use any available node.js package/module within your extension. For instance, check out this question for reading JSON.
For JSON, you just need to require or import the JSON file, such as:
const jsonObject = require('./myJSONfile.json');
// do something
For JSON with comments, you can use node-jsonc-parser.
After the manipulation, you could use the fs module of nodej.js to write to the disk.

CasperJS and MySQL

I want to save data to a MySQL DB that was retrieved while using casperJS.
I have not been able to find any way to do this directly.
Is there a way to DIRECTLY connect the two?
Will node-mysql work from within Casper?
No, there is no way to do it directly.
You will need to do it indirectly. Remember that CasperJS is built on top of PhantomJS which has a different execution environment that node.js. Very few node.js modules actually work inside of PhantomJS/CasperJS without change. You will have to write a script (e.g. node.js script) which has the ability to read a file and write to MySQL.
CasperJS script scrapes data and stores the data in some (temporary) file (see PhantomJS' fs module),
Call the external script with the scraped data file (see PhantomJS' child_process module) and
if necessary, delete the temporary data file either in CasperJS (see PhantomJS' fs module) or the external script.

Load JSON file in Pig script inside Hortonworks Sandbox

I'm new to the whole Hadoop/Hortonworks/Pig stuff, so excuse me for the question.
I have installed the Hortonworks Sandbox. I'm trying to load a twitter JSON file and perform some queries on the file, but I'm currently stuck in the loading file part.
I know that I should use the Elephant-bird in order to load a JSON file (without specifying the JSON schema) with JsonLoader(), so I've downloaded the Elephant-bird from the git repo and I've included the jar file
Elephant-bird\repo\com\twitter\elephant-bird\2.2.3\elephant-bird-2.2.3.jar
inside the Hortonworks Sandbox. Here a screen shot with my Pig script:
REGISTER elephant-bird-2.2.3.jar;
Json1 = LOAD 'JSON/sample.tweets' JsonLoader();
DESCRIBE Json1;
STORE Json1 INTO 'tweeterOutput';
Unfortunately I cannot get any results from this script execution. I've tried with both STORE and DUMP commands.
Probably I'm doing many wrong things in this process flow, so any help will be appreciated!
You are missing the USING keyword:
Json1 = LOAD 'JSON/sample.tweets' USING JsonLoader();
Fix the below
You need to add few more jars: elephant-bird-core-4.4.jar, elephant-bird-pig-4.4.jar, elephant-bird-hadoop-compat-4.4.jar, json-simple-1.1.1.jar
Register all of them in the script
REGISTER elephant-bird-core-4.4.jar;
REGISTER elephant-bird-pig-4.4.jar;
REGISTER elephant-bird-hadoop-compat-4.4.jar;
REGISTER json-simple-1.1.1.jar;
LOAD 'JSON/sample.tweets' USING com.twitter.elephantbird.pig.load.JsonLoader('-nestedLoad');