Editing package.js file - javascript

i'm having a hard time to understand how to run all of my .js files using package.js files
i have almost 2000.js scripts i need to run them one by one, i'm using a api made by gameflip
in the folder i found package.js, but i don't know how to use it ,
can anyone tell me how to do that ? thank you
here the script :
{
"name": "gfapi",
"version": "0.1.1",
"description": "Gameflip API",
"keywords": "Gameflip",
"homepage": "https://github.com/iJJi/gfapi",
"bugs": "https://github.com/iJJi/gfapi/issues",
"author": {
"name": "Eng-Shien Wu",
"email": "engshien.wu#ijji.com"
},
"license": "MIT",
"private": true,
"files": [
"index.js"
],
"repository": "iJJi/gfapi",
"engines": {
"node": ">=8.5.0"
},
"scripts": {
"bulk_listing": "node src/samples/bulk_listing.js",
"test": "ENVIRONMENT=mocha mocha src/test --recursive",
"docs": "jsdoc -c jsdoc_conf.js -d docs -P package.json index.js; docco -o docs/samples src/samples/*.js src/samples/*.rb"
},
"dependencies": {
"base-64": "^0.1.0",
"bluebird": "^3.5.0",
"bunyan": "^1.8.12",
"file-type": "^8.1.0",
"http-errors": "^1.6.2",
"node-rest-client-promise": "^3.1.1",
"promise-ratelimit": "^0.0.3",
"request": "^2.85.0",
"request-promise": "^4.2.2",
"speakeasy": "^2.0.0"
},
"devDependencies": {
"marked": "^0.3.19",
"docco": "^0.7.0",``
"jsdoc": "^3.5.5"
}
}

What you posted isn't a package.js (I don't even know if it exists), but a package.json. It's generated by NPM, the Node Package Manager. It's a list of all the project's dependencies. I think that what you're looking for are the npm scripts, they are in the script object of package.json.
npm run <script>
# For example :
npm run bulk_listing
npm run test
npm run docs
Each script will run its associated command in this package.json.
npm run bulk_listing
# Will do the same thing as:
node src/samples/bulk_listing.js
More about package.json.
The script I talked about below
If you want to run all the scripts, this should do the job :
const fileNames = ["path/to/fileA", "fileB"]; // I assume you have something to get all the files path. Isn't that npm run bulk_listing ?
fileNames.forEach(async (path, index) => {
// It's pretty much like 'node <path>'
await require(path);
// All the code here is executed AFTER the script has been launched
console.log(`LAUNCHED ${index} | ${path}`)
});

Related

Firebase Functions + monorepo - Deploying doesn't work + deps not installed locally

Looks like someone has the same problem here: https://github.com/firebase/firebase-functions/issues/1050
Problem
I have moved my project structure to a monorepo with the following structure:
/
|
| - node_modules/
|
| - packages/
| - - app/
| - - - - index.js
| - - - - package.json
| - - functions/
| - - - - src/
| - - - - - - helloWorld.function.js
| - - - - - - index.js
| - - - - package.json
|
| - .firebaserc
| - firebase.json
| - firestore.indexes.json
| - firestore.rules
| - package.json
| - yarn.lock
| - LICENSE.md
As far as I know, all code our functions depends on must be inside the functions directory. But... when I run yarn install, the node_modules of my functions are saved in the root node_modules folder.
Then, if I run firebase deploy from the root of my project, I get the error:
Error: Error occurred while parsing your function triggers.
The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.
Note: I haven't made any code change... before configuring the project to the monorepo structure, everything worked fine.
My attempt to solve this problem
I have tried to use nohoist when defining my workspaces inside my root package.json:
{
"private": true,
"name": "#company/project",
"version": "1.0.0",
"description": "Project monorepo",
"repository": "...",
"license": "MIT",
"author": {
"name": "Raul",
"email": "...
},
"scripts": {
"app": "yarn workspace #company/app start",
"documentation": "yarn workspace #company/documentation start",
"server": "yarn workspace #company/server start"
},
"workspaces": {
"packages": ["packages/*"],
"nohoist": ["**/#company/server"] <---- HERE
}
}
-Note: when I run yarn server the functions shell is started correctly.
But... for some reason, after reinstalling everything:
rm -rf node_modules (everywhere)
yarn cache clean
yarn install
The dependencies of the functions packages are installed in the root node_modules!
Any ideas?
This is how my functions node_modules looks like after installing the deps:
node_modules/
.bin/
eslint
eslint-config-prettier
firebase-functions
functions-framework
functions-framework-nodejs
image_size
uuid
qs/
uuid/
And these are the dependencies required (which are installed in the root package.json, which is not the behavior I expect):
"name": "#company/server",
...
"dependencies": {
"#google-cloud/functions-framework": "^3.1.1",
"#google-cloud/translate": "^6.3.1",
"#google-cloud/vision": "^2.4.2",
"#googlemaps/google-maps-services-js": "^3.3.3",
"axios": "^0.27.2",
"camelcase": "^6.3.0",
"dotenv": "^16.0.1",
"expo-server-sdk": "^3.6.0",
"firebase-admin": "^10.3.0",
"firebase-functions": "^3.21.2",
"glob": "^7.2.0",
"image-size": "^1.0.1",
"lodash": "^4.17.21",
"moment": "^2.29.3",
"qs": "^6.10.3",
"sharp": "^0.30.6",
"uuid": "^8.3.2"
},
"devDependencies": {
"eslint": "^8.3.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^4.3.1",
"firebase-functions-test": "^0.2.0"
}
UPDATE
Instead of nohoisting in the root, I have placed this segment in the packages/functions/package.json:
workspaces: { nohoist: ["**"] }
and all the deps are installed locally!
But... when I do firebase deploy, same error. If instead of running the command from the root folder I do it from packages/functions/package.json the deployment is successful.
Why? What is happening?
This is my firebase.json file:
{
"functions": {
"predeploy": ["yarn --cwd \"$RESOURCE_DIR\" lint"],
"source": "packages/functions"
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
}
}
Workaround (Not the correct solution, just a trick):
Instead of using nohoist to installing all the deps locally inside the packages/functions directory, what about excluding it from the yarn workspaces?
For this, update your root's package.json to:
"workspaces": [
"packages/app",
"packages/documentation"
]
(packages/functions is not included as a workspace.)
Test it:
Now, if you run (wherever you want in your project) firebase deploy, everything works fine... the deployment is successful and all our backend deps are installed locally (it is required, since all the cloud functions code must be inside the functions folder).
Also, this error is solved too!
Pros:
You can maintain the monorepository structure and deploy your cloud functions without problems.
Cons:
This is a tricky solution... when you run yarn install from the root of your project, packages/functions/package.json dependencies are not installed (as we have remove it from the workspace in order to install them locally.)
In order to solve this little issue, in your root package.json, write the following:
{
"scripts": {
...,
"postinstall": "cd path/to/your/functions && yarn install", // automatically executed after running `yarn install`
}
}
in my case:
{
"scripts": {
...,
"postinstall": "cd packages/functions && yarn install", // automatically executed after running `yarn install`
}
}

Travis CI fails on tsc

So I have a typescript project hosted on github.
I'm trying to set up Travis CI for tests but when I get to the step to compile the code to javascript (tsc command) it fails.
The error looks like this:
> node node_modules/typescript/bin/tsc
src/DbHandler.ts:2:32 - error TS2307: Cannot find module './models/User'.
2 import { User, UserInit } from './models/User';
~~~~~~~~~~~~~~~
src/User.ts:2:22 - error TS2307: Cannot find module './models/User'.
2 import { User } from "./models/User";
~~~~~~~~~~~~~~~
Found 2 errors.
npm ERR! code ELIFECYCLE
The paths to the files ARE correct and it compiles just fine locally..
In the project I have this package.json(cleaned):
{
"name": "project",
"version": "0.0.1",
"description": "",
"main": "dist/server.js",
"scripts": {
"build": "node node_modules/typescript/bin/tsc",
"prod": "node dist/server.js",
"dev": "ts-node src/server.ts",
"dev:watch": "nodemon",
"test:watch": "ts-node node_modules/jasmine/bin/jasmine",
"test": "node node_modules/jasmine/bin/jasmine.js --config=jasmine.json"
},
"author": "myname",
"license": "MIT",
"devDependencies": {
"#types/express": "~4.17.1",
"#types/jasmine": "~3.4.4",
"jasmine": "~3.5.0",
"nodemon": "~1.19.4",
"ts-node": "~8.4.1",
"tslint": "~5.20.0",
"typescript": "~3.6.4"
},
"dependencies": {
"express": "~4.17.1"
}
}
And my .travis.yml looks like:
language: node_js
cache: npm
node_js:
- "stable"
install:
- npm ci
- npm run build
script:
- npm run test
And my file structure is something like this:
package.json
.travis.yml
src/
models/
User.ts
DbHandler.ts
User.ts
server.ts
test/
files_for_tests_with_ts_extension

webpack + babel not bundling the js properly

I am using webpack and babel in my project. While the webpack is wokring fine but babel is somehow not doing it's job in polyfilling the ES6+ features. When I use the npm script, I get some error "Entrypoint undefined = index.html" in the command prompt. Kindly help !
Package.json
{
"name": "forkify",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack --mode development",
"build": "webpack --mode production",
"start": "webpack-dev-server --mode development --open"
},
"author": "neeraj",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^8.0.2",
"babel-preset-env": "^1.7.0",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.19.0",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.8"
},
"dependencies": {
"babel-polyfill": "^6.26.0"
}
}
webpack.config.js
const path=require('path');
const HtmlWebPackPlugin=require('html-webpack-plugin');
module.exports={
entry:['babel-polyfill','./src/js/index.js'],
output:{
path:path.resolve(__dirname,'dist'),
filename:'js/bundle.js'
},
devServer:{
contentBase:'./dist'
},
plugins:[
new HtmlWebPackPlugin({
filename:'index.html',
template:'./src/index.html'
})
],
module:{
rules:[
{
test:/\.js$/,
exclude:/node_modules/,
use:{
loader:'babel-loader'
}
}
]
}
};
.bablerc
{
"presets":[
["env",{
"targets":{
"browser":[
"last 5 versions",
"ie>=8"
]
}
}]
]
}
Error what I get:
Project Structure:
EDIT------------------
I started getting some new error now.
It is the html-webpack-plugin's problem, a fix was introduced in
3.0.7 but was removed again in 3.0.8. For more information, please click here.
So if you run npm install --save-dev html-webpack-plugin#3.0.7, and then npm run dev will output Entrypoint html-webpack-plugin for "index.html" = index.html. And personally, it is a trivail problem not that important.
There was some dependency issue. Deleted my node_modules folder and used this command instead: npm install -D babel-loader#7 babel-core babel-preset-env webpack
Follow this link for more information : https://github.com/babel/babel-loader
Now I no longer get any error and also babel is working fine.
FYI....you might have to install babel-polyfill & html-webpack-plugin seperately.

global cli npm package works on windows but not linux

I am wanting to create an npm cli tool, I dug around in bower's code and replicated their basic package.json along with the bin and lib/bin file.
On windows it works perfectly, but on linux I just get : No such file or directory
The npm package is: https://www.npmjs.com/package/quilk
Here is the code, package.json:
{
"name": "quilk",
"version": "0.1.3",
"description": "quilk.",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "John Carmichael",
"keywords": [
"builder", "watcher", "fast", "quilk"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/johnc1984/quilk"
},
"main": "lib",
"bin": {
"quilk": "bin/quilk"
},
"files": [
"bin",
"lib"
],
"homepage": "https://github.com/johnc1984/quilk",
"engines": {
"node": ">=0.10.0"
},
"dependencies": {
"browserify" : "13.1.0",
"concat-files" : "0.1.0",
"chokidar" : "1.6.0",
"fs.extra" : "1.3.2",
"javascript-natural-sort" : "0.7.1",
"jdc-node-cliarg-reader" : "1.0.0",
"less" : "2.7.1",
"node-notifier" : "4.6.1",
"node-minify" : "1.3.9",
"q" : "1.4.1",
"recursive-readdir" : "2.0.0"
}
}
bin/quilk:
#!/usr/bin/env node
require('../lib/bin/quilk');
lib/bin/quilk
process.bin = process.title = 'quilk';
console.log('This is the quilk script.');
The end result of installing this globally on windows is the console.log is run on the output is This is the quilk script. after i simply type quilk.
On Ubuntu 14lts though, trying to run the freshly, globally installed npm module results in the output of : No such file or directory
What am I missing to get this run on linux?
(when i run the command to list the globally installed npm modules npm ls -g quilk is listed)
Further info:
I get the exact same issue on the following builds:
Ubuntu 14lts, node 6.4.0, npm 3.10.3
Debian 6.0.10, node 0.12.7, npm 2.11.3
I found the answer.
It turns out to be some kind of bug in npm on windows 10.
After publishing the same package from a linux box, the npm package now works on both linux and windows leading me to believe that npm publish on windows has some bug in it somewhere.
Issue reported: https://github.com/npm/npm/issues/13808

node_modules is not recognized as an internal or external command

I'm trying to write a test automation script using appium, jasmine, and perfecto mobile. I'm using the project cloned from the following URL with my own configuration Appium Javascript Example
The problem is when I execute the npm test command I get the following error
node_modules is not recognized as an internal or external command
This is how the packages.json script looks like:
{
"name": "perfecto_appium_sample",
"version": "1.0.0",
"description": "The following sample shows how to Install an application and use WebDriverIO to automate and test it.<br/> It uses selendroid test application which can be downloaded from [here](https://github.com/PerfectoCode/AppsForSamples/tree/master/selendroid-test-app-0.17.0).",
"main": "perfectoSpec.js",
"scripts": {
"test": "node_modules/webdriverio/bin/wdio wdio.conf.js",
"start": "wdio wdio.conf.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"wdio": "^0.3.3",
"wdio-jasmine-framework": "^0.2.19",
"wdio-mocha-framework": "^0.5.12"
},
"dependencies": {
"wd": "^1.5.0",
"webdriverio": "^4.10.2"
},
"keywords": []
}
you need to provide relative path properly:
"scripts": {
"test": "node ./node_modules/webdriverio/bin/wdio wdio.conf.js",
"start": "wdio wdio.conf.js"
}
Just remove the paths "node_modules/webdriverio/bin/" and simply specify "wdio wdio.conf.js". It should work.

Categories

Resources