I am passing around 9 parameter via command line to Node JS script.
Here is my Command:
node awsInvokeDelete.js DELETE https://Test1234.execute-api.us-west-2.amazonaws.com us-west-2 /qa/transit-connectivity/api/v1/sites/tdcloudtsttd03 AKIAJ4Y5DGqwewqeqw CFdAgsdtqweqwe/SKqDezdqweewofWrUXXBbQoMy '{\"change_request\":\"chg0123456\"}'
I am passing query parameter as JSON in command line argument which is process.argv[9] in node JS script. It works perfectly If I pass value to all parameters but in some cases process.argv[8] will be empty. When I am passing empty value in process.argv[8], its actually takes argv[9] as argv[8].
how Can I pass empty argument value in command line for below script.
var apigClientFactory = require('aws-api-gateway-client').default;
let awsMethod = process.argv[2],
awsEndpoint = process.argv[3],
awsRegion = process.argv[4],
awsPathTemplate = process.argv[5],
awsAccessKey = process.argv[6],
awsSecreteKey = process.argv[7],
awsPathParams = process.argv[8],
awsAdditionalParams = JSON.parse(process.argv[9] || '{}');
var apigClient = apigClientFactory.newClient({
invokeUrl: awsEndpoint, // REQUIRED
accessKey: awsAccessKey, // REQUIRED
secretKey: awsSecreteKey, // REQUIRED
region: awsRegion, // REQUIRED: The region where the API is deployed.
retryCondition: (err) => { // OPTIONAL: Callback to further control if
request should be retried. Uses axon-retry plugin.
return err.response && err.response.status === 500;
}
});
var param = awsPathParams;
// Template syntax follows url-template https://www.npmjs.com/package/url-template
var pathTemplate = awsPathTemplate;
var method = awsMethod;
var additionalParams = { queryParams: awsAdditionalParams, };
console.log(additionalParams);
var body = {};
apigClient.invokeApi(param, pathTemplate, method, additionalParams, body)
.then(function(result) {
//console.log(result.data + ": " +result)
console.log(result.response.data)
}).catch(function(result) {
console.log(result.response.data)
});
Here is output: args[8]'s value should be displayed as args[9]
args[8]: {"change_request":"chg0123456"}
args[9]: [object Object]
Your script is not working on the input that you have provided (if you actually add the missing argument) because '{\"change_request\":\"chg0123456\"}' is not something that JS can parse as a JSON string. Furthermore, you are not passing any empty value in your input to the script (just an empty space is not considered as an actual input).
You need to change it to this '{"change_request":"chg0123456"}' and pass empty value as an empty string ''.
This input works correctly.
node index.js DELETE https://Test1234.execute-api.us-west-2.amazonaws.com us-west-2 /qa/transit-connectivity/api/v1/sites/tdcloudtsttd03 AKIAJ4Y5DGqwewqeqw CFdAgsdtqweqwe/SKqDezdqweewofWrUXXBbQoMy '' '{"change_request":"chg0123456"}'
If you really need the object in that format, then you need to remove \ characters from it before you can call JSON.parse on it.
awsAdditionalParams = JSON.parse(
process.argv[9].split('\\').join('') || '{}'
);
If you first want to check whether the last argument is not empty and only then run the code above, the you can use ternary operator like this.
awsAdditionalParams = process.argv[9]
? JSON.parse(process.argv[9].split('\\').join('') || '{}')
: '';
3 options:
change the script to switch around arguments 8 and 9. then you always have the same number even if 9 is empty.
pass the argument as "" instead of nothing.
change the script so you parse your own command line and change things around whichever way you like.
Related
I'm trying to write a node script that identifies unused translation strings in my React project.
First, I want to get a list of all the translations that are used. To do this, I am getting a list of each JS file in my /src/components folder and then reading the file.
My translation strings look like this: t('some.translation.key'), so basically, I want to identify each instance of t('...') using RegEx and then get the key in between those parentheses (i.e. "some.translation.key"). From there, I should be able to compare the keys to the ones in my translation JSON file and remove the ones that aren't being used.
unused.js
const path = require('path');
const fs = require('fs');
let files = [];
// https://stackoverflow.com/a/63111390/2262604
function getFiles(dir) {
fs.readdirSync(dir).forEach(file => {
const absolute = path.join(dir, file);
if (fs.statSync(absolute).isDirectory()) {
getFiles(absolute);
} else {
if (absolute.includes('.js')) {
files.push(absolute);
}
}
});
return files;
}
function getTranslations() {
const pathComponents = path.join(__dirname, '../../src/components');
// get all js files in components directory
const files = getFiles(pathComponents);
const translationKeys = [];
// for each js file
for(let i = 0; i < files.length; i++) {
// read contents of file
const contents = fs.readFileSync(files[i]).toString();
// search contents for all instances of t('...')
// and get the key between the parentheses
}
}
getTranslations();
How can I use RegEx to find all instances of t('...') in contents and then extract the ... string between the parentheses?
Yes, you could use a regular expression:
for (const [, str] of contents.matchAll(/\bt\(['"](.*?)['"]\)/g)) {
console.log('t called with string argument:', str)
}
However, with regular expressions the problem will be that they don't understand the code and would cause trouble with matching strings that contain ( ) or \' themselves, have issues with concatenated strings or extra whitespace, etc., and you'd then also get the contents literally, including possible escape sequences.
A more robust way would be to create an AST (abstract syntax tree) from the code and look for calls to t in it.
A popular AST parser would be acorn. There is also the supplementary module acorn-walk that helps walking through the whole syntax tree without building your own recursive algorithm.
import acorn from 'acorn'
import walk from 'acorn-walk'
// Example
const contents = "function a () { if (123) { t('hello') } return t('world') }"
// The arguments to acorn.parse would have to be adjusted based
// on what kind of syntax your files can use.
const result = acorn.parse(contents, {ecmaVersion: 2020})
walk.full(result, node => {
if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === 't') {
if (node.arguments.length === 1 && node.arguments[0].type === 'Literal' && typeof node.arguments[0].value === 'string') {
// This is for the case `t` is called with a single string
// literal as argument.
console.log('t called with string argument:', node.arguments[0].value)
} else {
// In case you have things like template literals as well,
// or multiple arguments, you'd need to handle them here too.
console.log('t called with unknown arguments:', node.arguments)
}
}
})
// Will output:
// t called with string argument: hello
// t called with string argument: world
Consider the following.
node file1.js && react-scripts start
I am trying to make an API call to the GCP Secret Manager in file1.js. After the request is received, I want to set them as environment variables under process.env. After that, I want to access them in the frontend. The browser can't make a call to that Secret Manager without OAuth. Is there any way I can share process.env between these two scripts?
File1 code
const {SecretManagerServiceClient} = require('#google-cloud/secret-manager');
// Instantiates a client
const client = new SecretManagerServiceClient();
const firebaseKeysResourceId = 'URL'
const getFireBaseKeys=async()=> {
const [version] = await client.accessSecretVersion({
name: firebaseKeysResourceId,
});
// Extract the payload as a string.
const payload = JSON.parse(version?.payload?.data?.toString() || '');
process.env.TEST= payload.TEST
return payload
}
getFireBaseKeys()
Expanding on my comment
Method 1 - kind of neat but unneccessary
Supposing you had these vars you wanted in the environment:
const passAlong = {
FOO: 'bar',
OAUTH: 'easy-crack',
N: 'eat'
}
Then at the end of file1.js you would do this
console.log(JSON.stringify(passAlong));
Note you cannot print anything else in file1.js
Then you would call your script like this
PASSALONG=$(node file1.js) react-script start
And at the beginning of react-script you would do this to populate the passed along variables into the environment.
const passAlong = JSON.parse(process.env.PASSALONG);
Object.assign(process.env,passAlong);
Method 2 - what I would do
Using the spawn method would involve just setting process.env how you like in file1.js and then adding something like this at the end of file1.js
// somewhere along the way
process.env.FOO = 'bar';
process.env.OAUTH = 'easy-crack';
process.env.N = 'eat';
// at the end of the script
require('child_process').spawnSync(
'node', // Calling a node script is really calling node
[ // with the script path as the first argument
'/path/to/react-script', // Using relative path will be relative
'start' // to where you call this from
],
{ stdio: 'inherit' }
);
I'm building a CLI app with nodejs with native REPL. A dumb sample is:
const repl = require('repl')
const myApp = require('myApp')
repl.start({
eval: (input, context, filename, callback) => {
const result = myApp(input)
callback(null, result)
},
writer: (result) => result
? 'my result: ' + result
: '' // <--- empty result
})
Everything is fine, but a newline is created for every response of the app. I'd like to override this behavior, and decide whether to create a new line or not.
Example
Let's pretend the app to be a calculator:
$ node index.js
add 5
add 3
result
8
Because add command doesn't have any result, a new command should be inserted in the very next line. However, what I'm getting from the above code is:
$ node index.js
add 5
add 3
result
8
with an empty line added after any 0add call.
How can I prevent this?
So I found out there's a parameter I could set, ignoreUndefined. If true, when you call callback(null, undefined) (empty result case), it won't add a new line.
Thus, in the above example:
const repl = require('repl')
const myApp = require('myApp')
repl.start({
ignoreUndefined: true,
eval: (input, context, filename, callback) => {
const result = myApp(input)
callback(null, result)
},
writer: (result) => result
? 'my result: ' + result
: undefined // <--- empty result
})
docs: https://nodejs.org/api/repl.html
similar question: node.js REPL "undefined"
Using gulp 3.9.1
I am attempting to return a bunch of files and perform a task that requires a var to be passed between two pipes.
I'm using node uuid to create a v3 UUID for each file path to
ultimately end up with a uuid for each page. I'm grabbing the file path with gulp-print.
I want to store that uuid value as a var. In the next pipe Im using
gulp-inject-string to write it into the page during the build.
Help: Either I need help getting the file path inside the gulp-inject-string pipe or I need to pass the var between the two different pipes. If I globally set a var with a default value outside the src it gets passed easily to the pipe(inject).
Super simplified code below:
// test code
var gulp = require('gulp');
var print = require('gulp-print');
var inject = require('gulp-inject-string');
var reload = browserSync.reload;
const uuidv3 = require('uuid/v3');
var uuid;
gulp.task('uuid', function() {
return gulp.src('**/*.html'])
// create uuid
.pipe(print(function(filepath) {
uuid = uuidv3(filepath, uuidv3.URL);
return "compiled: " + filepath + ' uuid: ' + uuid;
}))
// need to to add UUIDv3 to each page
.pipe(inject.before('</head>', '<meta name="dc.identifier" content="' + uuid + '">'))
.pipe(gulp.dest('/prod/./'))
.pipe(reload({ stream: true }));
});
It's worth noting that I need a cross platform way to get the file path starting in the root of the project and including forward slashes. The gulp(print) does this perfectly starting at the root of the project and ignoring anything upstream from that point. The format of the path is important because it's one half of the equation in creating the uuid and the uuid's must match on Mac or PC platforms.
examples:
/index.html
/dir1/file.html
/dir1/dir2/dir3/file.html
var gulp = require('gulp');
var print = require('gulp-print');
var inject = require('gulp-inject-string');
const uuidv3 = require('uuid/v3');
var tap = require('gulp-tap');
// you can declare here
var uuid;
gulp.task('pages', function() {
// or you can declare here
var uuid;
return gulp.src('**/*.html')
// bunch of stuff happens here involving templating/minifying
// create uuid
.pipe(print(function(filepath) {
// then set it here and use it further below
// it will be available
uuid = uuidv3(filepath, uuidv3.URL);
return "compiled: " + filepath + ' uuid: ' + uuid;
}))
// need to to add UUIDv3 to each page
//.pipe(inject.before('</head>', '<meta name="dc.identifier" content="' + uuid + '">\n'))
.pipe(tap(function(file, t) {
return t.through(inject.before('</head>', '<meta name="dc.identifier" content="' + uuid + '">\n');
})
.pipe(gulp.dest('/prod/./'))
.pipe(reload({stream:true}));
});
You are just creating a variable at a higher scope that you can set and refer to later. If you need a bunch of them create an array with filepath as an index. But I would try it first as just a simple value.
I solved the problem. It was an amateur mistake. I returned the statement where the var was set so the var was essentially killed. Updated code that allows the var to pass through the pipes.
var gulp = require('gulp');
var print = require('gulp-print');
var replace = require('gulp-replace');
const uuidv3 = require('uuid/v3');
var uuid;
gulp.task('build', function() {
return gulp.src('**/*.html')
// get a cross-platform filepath and create a uuid
.pipe(print(function(filepath) {
uuid = uuidv3(filepath, uuidv3.URL);
}))
// inject uuid
.pipe(replace('dc.identifier" content=""', function() {
return 'dc.identifier" content="' + uuid + '"';
}))
.pipe(gulp.dest('/prod/./'));
});
The var uuid passes through the pipes just fine now. This code creates a UUID based on a cross-platform file path and injects it into an empty dc.identifier meta tag.
i am using https://github.com/gf3/sandbox#readme but i am not getting that how can i pass some data from my code to javascript code through this sandbox. for example
var s = new sandBox();
s.run("(function(name) { return 'Hi there, ' + name + '!'; })('Fabio')", function(output) {
console.log("Example 2: " + output.result + "\n")
})
now i want to pass some data to this function how can i do that?
It does not have any clean way to pass arguments. However, since you are passing code as a string anyway, you can simply add the arguments directly to the string:
var arg = 'Hello';
var code = "(function(name) { return 'Hi there, ' + name + '!'; })("+ JSON.stringify(arg) +")"
s.run(code, ...);
I'm using JSON.stringify to ensure that the string is a valid JS expression.