Cannot load json file from repo in GitHub Action with Node - node.js

I'm trying to create a GitHub Action which :
browse a folder in the repo where the action is called
load the files that are in this folder as JSON schemas in an ajv instance
validate a file in the same repo against the instance
My problem is:
My Node script works well in a local environment, but shows an error on loading of JSON files :
Error: Cannot find module './GeoJSON_schemas/LineString.json'
Require stack:
- /home/runner/work/_actions/idrissad/jsonschema_validator/v1.0/dist/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
at Function.Module._load (internal/modules/cjs/loader.js:690:27)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at /home/runner/work/_actions/idrissad/jsonschema_validator/v1.0/dist/index.js:8105:19
at /home/runner/work/_actions/idrissad/jsonschema_validator/v1.0/dist/index.js:8118:3
at Object.<anonymous> (/home/runner/work/_actions/idrissad/jsonschema_validator/v1.0/dist/index.js:8121:12)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/runner/work/_actions/idrissad/jsonschema_validator/v1.0/dist/index.js'
]
}
Here is the Action code:
const core = require('#actions/core');
const fs = require('fs');
//const path = require('path');
const schema_path = core.getInput('main_schema_path');
const schemas_dir = core.getInput('additional_schemas_dir');
const data = core.getInput('data_path');
const Ajv = require("ajv");
const addFormats = require("ajv-formats");
const ajv = new Ajv({allErrors: true, strict: false});
addFormats(ajv);
let add_schemas = fs.readdirSync(schemas_dir);
for (let add_schema of add_schemas) {
add_schema_path = schemas_dir + add_schema
ajv.addSchema(require(add_schema_path), add_schema); //THIS REQUIRE IS THE ONE THAT DOESN'T WORK
};
const schema = require(schema_path);
const validate = ajv.compile(schema);
test(require(data_path));
function test(data) {
const valid = validate(data);
if (valid) core.setOutput('validity', "Valid!")
else core.setOutput('validity', "Invalid: " + ajv.errorsText(validate.errors))
};
Link of the GitHub Action repo: (https://github.com/IdrissaD/jsonschema_validator)
Link of the workflow error: (https://github.com/PnX-SI/schema_randonnee/runs/3144851785)
So the JSON file I'm trying to load is not a module (at least I believe so), and locally my script loads it perfectly with the "same" code. The difference is that I build the path with inputs from the yml workflow file.
I don't understand if the script just don't get to the right place, thus doesn't find any file called LineString.json, or if it gets to the right folder but doesn't manage to load the file as it takes it for a module?
I believe it can have something to do with the way I should write the fs.readDirSync in a GitHUb context (comparing to a local one), but I struggle to understand the solution.

Ok I got it, the directory in which the script was run was actually /home/runner/work/schema_randonnee/schema_randonnee/ (visible in the workflow logs of the actions/checkout#v2 run).
So by adding /home/runner/work/schema_randonnee/schema_randonnee/GeoJSON_schemas/ before all my input values (main_schema_path, additional_schemas_dir and data_path), the script managed to reach the files in the workflow repo!
Conclusion
The action checkout#v2 checks out the repository under $GITHUB_WORKSPACE (cf. README) and this env value is defined by default on /home/runner/work/my-repo-name/my-repo-name as visible in this GitHub doc. Without changement on the run of that checkout action, it's the directory to indicate to the GitHub Action you're calling (which is by default running at the same level than /home if I understand well).
Edit:
even better than the previous solution, I should use the generated$GITHUB_WORKSPACE variable that way:
steps:
- name: checking out repo
uses: actions/checkout#v2
- name: build the schema and validate the data
uses: idrissad/jsonschema_validator#main
id: validation
with:
main_schema_path: ${{ github.workspace }}/schema.json
additional_schemas_dir: ${{ github.workspace }}/GeoJSON_schemas/
data_path: ${{ github.workspace }}/exemple-valide.json

Related

Error "The specified module could not be found" on hyperledger Aries

after doing everything from https://aries.js.org/guides/getting-started/set-up , i'm trying to copy paste this code to initialize the agent
//imports for indy-sdk
import type { InitConfig } from '#aries-framework/core'
import { Agent } from '#aries-framework/core'
import { agentDependencies } from '#aries-framework/node'
import { HttpOutboundTransport, WsOutboundTransport } from '#aries-framework/core'
import { HttpInboundTransport } from '#aries-framework/node'
// The agent initialization configuration
const config: InitConfig = {
label: 'docs-nodejs-agent',
walletConfig: {
id: 'wallet-id',
key: 'testkey0000000000000000000000000',
},
}
// Creating an agent instance
const agent = new Agent(config, agentDependencies)
// Registering the required in- and outbound transports
agent.registerOutboundTransport(new HttpOutboundTransport())
agent.registerInboundTransport(new HttpInboundTransport({ port: 3000 }))
// Function to initialize the agent
const initialize = async () => await agent.initialize().catch(console.error)
but on visual studio 2022, it gives me this error:
C:\Users\Tosat\Desktop\Ladon\LadonHyperledgerAries\node_modules\bindings\bindings.js:121
throw e;
^
Error: The specified module could not be found.
\\?\C:\Users\Tosat\Desktop\Ladon\LadonHyperledgerAries\node_modules\indy-sdk\build\Release\indynodejs.node
at Module._extensions..node (node:internal/modules/cjs/loader:1243:18)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Module.require (node:internal/modules/cjs/loader:1061:19)
at require (node:internal/modules/cjs/helpers:103:18)
at bindings (C:\Users\Tosat\Desktop\Ladon\LadonHyperledgerAries\node_modules\bindings\bindings.js:112:48)
at Object.<anonymous> (C:\Users\Tosat\Desktop\Ladon\LadonHyperledgerAries\node_modules\indy-sdk\src\indyBinding.js:1:37)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32) {
code: 'ERR_DLOPEN_FAILED'
}
Node.js v19.0.1
the problem is that the file at C:\Users\Tosat\Desktop\Ladon\LadonHyperledgerAries\node_modules\indy-sdk\build\Release\indynodejs.node exists and is right....
Thanks in advance!
I had the same problem and i think i found the solution at least for windows so i hope you're also on windows otherwise i don't think this will help you a lot.
First step is: downloading the the prebuilt libraries of the indy-sdk at https://repo.sovrin.org/windows/libindy/master/1.16.0-1636/libindy_1.16.0.zip as stated in https://aries.js.org/guides/getting-started/installation/nodejs/windows
Second step is: unpacking these somewhere in a folder
Third step is: Setting these libraries in your Environment variables > System Variables as stated in the docs linked above as LD_LIBRARY_PATH and make sure the value points to the /lib folder of where you unpacked the prebuilt libraries
I did these steps above and still had the issue you had and somewhere i found a mention that the indy-sdk requires the libindy in your system PATH https://www.npmjs.com/package/indy-sdk. So i did exactly that as well.
So the fourth step is: Select Path under your System variables and click Edit. Then click new and paste the location of your extracted /lib so the same value that the LD_LIBRARY_PATH has.
Fifth Step is not forgetting to restart and retreading the installing of dependencies with yarn add ... as stated in the setup https://aries.js.org/guides/getting-started/set-up. This can be crucial since it took a restart for it to work after doing all of this
Let me know if this worked for you !

Error in constructing Graphql schema and resolvers in deployment of Next.js app on Vercel

I am using graphql-tools to merge Types and Schemas. My graphql server runs on the end point api/graphql
import { mergeTypeDefs, mergeResolvers } from "#graphql-tools/merge";
import { loadFilesSync } from "#graphql-tools/load-files";
import path from "path";
const typesArray = loadFilesSync(path.join(process.cwd(), "./**/*.graphql"));
export const types = mergeTypeDefs(typesArray);
const resolversArray = loadFilesSync(
path.join(process.cwd(), "./**/*.resolvers.*")
);
export const resolvers = mergeResolvers(resolversArray);
The graphql endpoint works locally, however upon deployment to Vercel, I get the following error with Serverless Function when an API call is made to api/graphql
ERROR Unhandled error during request: Error: Query root type must be provided.
at assertValidSchema (/var/task/node_modules/graphql/type/validate.js:69:11)
at assertValidExecutionArguments (/var/task/node_modules/graphql/execution/execute.js:150:35)
at executeImpl (/var/task/node_modules/graphql/execution/execute.js:98:3)
at Object.execute (/var/task/node_modules/graphql/execution/execute.js:60:63)
at Object.generateSchemaHash (/var/task/node_modules/apollo-server-core/dist/utils/schemaHash.js:15:32)
at ApolloServer.generateSchemaDerivedData (/var/task/node_modules/apollo-server-core/dist/ApolloServer.js:269:41)
at new ApolloServerBase (/var/task/node_modules/apollo-server-core/dist/ApolloServer.js:161:38)
at new ApolloServer (/var/task/node_modules/apollo-server-micro/dist/ApolloServer.js:18:1)
at Module.dCYn (/var/task/.next/serverless/pages/api/graphql.js:490:22)
at __webpack_require__ (/var/task/.next/serverless/pages/api/graphql.js:23:31)
at Module.GfcW (/var/task/.next/serverless/pages/api/graphql.js:251:23)
at __webpack_require__ (/var/task/.next/serverless/pages/api/graphql.js:23:31)
at /var/task/.next/serverless/pages/api/graphql.js:91:18
at Object.<anonymous> (/var/task/.next/serverless/pages/api/graphql.js:94:10)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
I believe it has to do with a mess up in the path for loadFilesSync. I tried replacing process.cwd() with __dirname, but it didn't work. I pretty much have scoured through every single resource on the internet but no luck. Any help would be greatly appreciated.

Error while trying to serialize a Qlik Sense app into a JSON object

I am trying to serialize a Qlik Sense app (.qvf file) into a JSON object.
For that I am passing the .qvf file in the below code as directed here - https://github.com/mindspank/serializeapp
The main reason behind this exercise is to save the JSON in Gitlab for version control since we cannot save .qvf in Git for version control as it is a binary file.
var qsocks = require('qsocks')
var serializeapp = require('serializeapp')
qsocks.Connect()
.then(global => global.openDoc('Executive D:\Users\ddas7071\Documents\Qlik\Sense\Apps\NewDeb.qvf'))
.then(app => serializeapp(app))
.then(result => console.log(result))
serializeapp = require('serializeapp')
const enigma = require('enigma.js')
const WebSocket = require('ws')
enigma.getService('qix', {
schema: require(`./node_modules/enigma.js/schemas/qix/12.67.2.json`),
session: {
host: 'localhost',
port: 4848,
secure: false
},
createSocket: (url) => new WebSocket(url)
})
.then(qix => qix.global.openDoc('Executive
D:\Users\ddas7071\Documents\Qlik\Sense\Apps\NewDeb.qvf'))
.then(app => serializeapp(app))
.then(result => console.log(result))
But while running the code (in windows), I am running into the below problem -
D:\Users\ddas7071\Desktop\Novartis_TechnicalDetails\myfile.js:12
enigma.getAttribute('qix', {
^
TypeError: enigma.getAttribute is not a function
at Object. (D:\Users\ddas7071\Desktop\Novartis_TechnicalDetails\myfile.js:12:8)
[90m at Module._compile (internal/modules/cjs/loader.js:1158:30)[39m
[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)[39m
[90m at Module.load (internal/modules/cjs/loader.js:1002:32)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:901:14)[39m
[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)[39m
[90m at internal/main/run_main_module.js:18:47[39m
Though I understand the error, but not sure how to resolve it.
Note - All the pre-requisites are being taken care of already.
qsocks is not supported from some time and serializeapp is using qsocks under the hood.
If you want to connect to Qlik Engine please the official package - enigma.js. But there is no JS package (as far as i know that can extract the objects information.
But ...
There is a CLI (official) that can do this for you (havent tested it myself) - corectl. Corectl have option to unbuild the app into separate json/yaml files which you can then put under version control
The description for the unbuild command
Extracts generic objects, dimensions, measures, variables, reload script and connections from an app in an engine into separate json and yaml files. In addition to the resources from the app a corectl.yml configuration file is generated that binds them all together. Passwords in the connection definitions can not be exported from the app and hence need to be handled manually. Generic Object trees (e.g. Qlik Sense sheets) are exported as a full property tree which means that child objects are found inside the parent´s json (the qChildren array).

In Node, how do I elegantly handle requiring modules that read files in their directories?

For instance, in my project directory, I have:
|--bar.js
|--dir
|--foo.txt
|--readfile.js
readfile.js:
const fs = require('fs');
var foo = fs.readFileSync('foo.txt', 'utf8');
console.log(foo);
module.exports = {foo};
Running node readfile.js, everything works perfectly.
bar.js:
const readfile = require('./dir/readfile');
console.log(read.foo);
Running node bar.js, I get:
fs.js:663
return binding.open(pathModule.toNamespacedPath(path),
^
Error: ENOENT: no such file or directory, open 'foo.txt'
at Object.fs.openSync (fs.js:663:18)
at Object.fs.readFileSync (fs.js:568:33)
at Object.<anonymous> (/Users/fterh/Documents/Projects/playground/dir/readfile.js:3:14)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
Fabians-MacBook-Pro:playground fterh$
I know it has to do with require('./dir/readfile') in bar.js, because Node then tries to search for "foo.txt" in the same directory as "bar.js". Currently, my fix is to use path.dirname(__filename) to get absolute paths, which would work regardless of whether I'm running the module directory or requiring it. I'm wondering if there is a more elegant way of doing things.
Use of require.resolve within readfile.js as follows:
const fs = require('fs');
let foo = fs.readFileSync(require.resolve('./foo.txt'), 'utf8');
console.log(foo);
module.exports = {foo};
Note: in the original question for bar.js it may have been intended to write: console.log(readfile.foo);.
require.resolve:
... return the resolved filename
Use __dirname to construct your path as that will always point to the directory where your module was loaded from, regardless of the current directory. This is one of the variables that is passed into a module so it has a unique value in the scope of each module and it's purpose is for exactly what you want (to do file operations relative to your module's directory).
const fs = require('fs');
const path = require('path');
var foo = fs.readFileSync(path.join(__dirname, 'foo.txt'), 'utf8');
console.log(foo);
module.exports = {foo};
Reference info for __dirname here.

'use-strict' enabled but not working in node

I have enabled use-strict mode in my .js file but when I run it, node keeps telling me that I don't have it enabled. PLEASE don't tell me to write "use-strict"; at the top of my file because I already tried that.
Here is my server.js file. I have been trying to see what is wrong but so far stack overflow has not been much help since most people seem to get this working on their first try.
require('use-strict')
'use-strict';
let util = require('util');
let http = require('http');
let Bot = require('#kikinteractive/kik');
var kik_username = process.env.KIK_USERNAME;
var kik_api_key = process.env.KIK_API_KEY;
var kik_baseUrl = process.env.KIK_BASEURL;
// Configure the bot API endpoint, details for your bot
let bot = new Bot({
username: kik_username,
apiKey: kik_api_key,
baseUrl: kik_baseUrl
});
bot.updateBotConfiguration();
bot.onTextMessage((message) => {
message.reply(message.body);
});
// Set up your server and start listening
let server = http.createServer(bot.incoming()).listen(8085);
Everything seems fine but when I run
$ node server.js
I keep getting this error
let util = require('util');
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:148:18)
at node.js:405:3
It tells me to enable strict mode BUT I ALREADY DID THAT. I even required an npm package to make sure I was doing it right! Can anyone make sense of what is happening?
No dash in 'use strict'
'use strict' // not 'use-strict'
Check out the documentation for further reference
You don't need to require an npm package. just put "use strict"; at the top of the js file.

Resources