Getting started with Faker.js with Meteorite - javascript

I am trying to user Faker.js with Meteorite. I have added this package:
https://atmosphere.meteor.com/package/Faker.js
I am trying to add fake users on server startup:
Meteor.startup(function () {
// code to run on server at startup
// add 100 fake tips and 10 fake articles and 50 fake users
for (var i=0; i<50; i++) {
// Accounts.createUser(Faker.Internet.userName(), Faker.Internet.email(), "1234");
}
});
but I get an error:
ReferenceError: Faker is not defined
Not sure how to fix this...

I'd recommend installing the npm package, and adding Faker as an NPM module. It's quite simple:
mrt add npm
Create a packages.json file in your project root, and add { "Faker": "0.5.11" }
mrt update
Use Faker in your code by doing Faker = Meteor.require('Faker');
Now you can use it like normal, e.g. Faker.Internet.email()

The author of Faker.js on atmosphere needs to update the package for it to work properly on Meteor 0.6.5.
In meteor 0.6.5 packages need to explicitly expose their variables. This was done so that packages variables don't conflict.
For the 'fakerjs' package the edits look pretty minor because it just interfaces the npm module.
You need to export 'Faker' in the package.on_use method in the package.js with an api.export, if you contact the author or send him a push request you could push it through faster. More details on how to expose the variables can be found in the namespacing section on the meteor docs.

Related

Fetching all private npm modules from an npmjs user

I'm working on a task to migrate all of our npm modules from npmjs.com.
Problem is, all of our modules are stored under an npm user as private packages, and I can't find a way to consume all packages the user owns programmatically.
I found many examples that explain how to migrate a single package or a list of packages by name, but I've yet to understand how can I fetch every package a user owns to create that list.
There are a few modules that allow interacting with the NPM registry API, but I couldn't find out how to make them consume private scoped packages. I'm currently working with the npm-registry-fetch to try and get an example query working with no luck.
This is the code I'm trying to run -
var npmFetch = require('npm-registry-fetch')
async function run() {
var response = await npmFetch.json('#<scope redacted>/<package name redacted>',{token: '<token redacted>'})
}
run()
This code results in this error -
HttpErrorGeneral: 404 Not Found - GET https://registry.npmjs.org/#<scope redacted>/<package name redacted> - Not found
How can I successfully query these type of packages?
Thanks.
Updating that this SO answer gave me the tools I've needed, performing the request as a curl command worked, so I'll just use that instead of using fancy registry clients. Thanks again internet stranger!

How to create a JavaScript Library

I've created some functions in JavaScript.
I've found out that I reuse them in many projects.
So I decided to create a small JavaScript Library for my coding.
A Library like react, react-dom, jquery which I can install with npm:
npm install <my-personal-library>
I searched the net. I learned that I can use npm publish <my-personal-library, but I do not know how to format my library and functions to use and install them like an npm package.
Also, I have no idea about creating type definitions for my functions and library.
like #types/react
Any guidence?
About how to publish your library
You can read the official npm documentation on how to create nodejs package modules:
https://docs.npmjs.com/creating-node-js-modules
Basically, what you need is a JS module file (IE test.js) to be exported with exports keyword like:
exports.printMsg = function() {
console.log("This is a message from the demo package");
}
Then publish the module with npm publish (add --access public if you want it to be public)
Finally import the package in the project you need it with npm install <your-module-name>
About the type definitions
I understand that you are using typescript? Then the following link is a good read:
https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html
To install your package in a pc you have to set up a cli package inside npm module.
import { Command } from "commander";
import open from "open";
// [] indicates that this is optional
// <> indicates that this is a required value
export const serveCommand = new Command()
.command("serve [filename]")
// when user enters node index.js --help, it sees description
.description("ADd a description")
.option("-p, --port <number>", "port to run server on", "4005")
.option("-v, --version", "show version", version, "")
// first arg will be the arg that passed in command() SO filename
// second arg is all other options
// THIS IS WE TELL WHAT TO DO
.action(async (filename = "main.js", options: { port: string }) => {
try {
// this is where you add logic about what to do when enterd the command
open("http://localhost:4005");
} catch (error: any) {
if (error.code === "EADDRINUSE") {
console.error("Port is in use. Try runnng on a different port ");
} else {
console.log("Issue is :", error.message);
}
process.exit(1);
}
});
To make cli run the code, in your main file
//whenever anyone runs cli from command line, this file will be executed.
!/usr/bin/env node
import { program } from "commander";
// this is the above command
import { serveCommand } from "./commands/serve";
// you could chain other commands .addCommand(otherCommand)
program.addCommand(serveCommand);
// parse this and run the aprropriate command that you put together
program.parse(process.argv);
as you see you might have different sub packages and each sub packages will have their own package.json. To communicate between those sub packages, you add them in their dependencies inside package.json. for example, you have to use cli package inside your main package. so in package.json
"dependencies": {
"#my-npm-package/cli": "^1.0.15",
// other dependencies
"express": "^4.17.1",
}
Since you have different sub packages and you have to group them together. Assgin those pacakges into an "organization". another term is to "create scoped package". "#types/cors" and "#types/express" are scoped packages.
Declaring Scoped Packages
in npm page, on the right, cick on "Add organization"
organization name should be unique
update name of the dependencies in package.json of each package.
Manage packages
Use Lerna to manage all of those npm packages. it is for managing multi project packages. - Lerna is one tool out there in the wild that we can use for managing a multi package project. Yarn and Npm are similar to lerna. there are also Bolt and Luigi.

Power BI custom D3 chart Could not find plugin "proposal-numeric-separator"

I have tried for custom D3 Power BI ,but showing me below error .I also reinstall pbiviz and node but showing same error .Need little help.
I tried adding different version older but not got success.
my node version is v12.16.1. and pbiviz 3.1.10 Now
PS C:\Users\mahendra\Downloads\PowerBI-visuals-sampleBarChart-master\PowerBI-visuals-sampleBarChart-master\CircleCard> pbiviz package
info Building visual...
info Start preparing plugin template
info Finish preparing plugin template
Error parsing bundle asset "C:\Users\mahendra\Downloads\PowerBI-visuals-sampleBarChart-master\PowerBI-visuals-sampleBarChart-master\CircleCard\.tmp\drop\visual.js": no such file
No bundles were parsed. Analyzer will show only original module sizes from stats file.
Webpack Bundle Analyzer saved report to C:\Users\mahendra\Downloads\PowerBI-visuals-sampleBarChart-master\PowerBI-visuals-sampleBarChart-master\CircleCard\webpack.statistics.prod.html
ERROR Failed to compile with 1 errors 10:43:12 PM
error in ./.tmp/precompile/visualPlugin.ts
Module build failed (from C:/Users/mahendra/AppData/Roaming/npm/node_modules/powerbi-visuals-tools/node_modules/babel-loader/lib/index.js):
Error: [BABEL] C:\Users\mahendra\Downloads\PowerBI-visuals-sampleBarChart-master\PowerBI-visuals-sampleBarChart-master\CircleCard\.tmp\precompile\visualPlugin.ts: Could not find plugin "proposal-numeric-separator". Ensure there is an entry in ./available-plugins.js for it. (While processing: "C:\\Users\\mahendra\\AppData\\Roaming\\npm\\node_modules\\powerbi-visuals-tools\\node_modules\\#babel\\preset-env\\lib\\index.js")
at getPlugin (C:\Users\mahendra\AppData\Roaming\npm\node_modules\powerbi-visuals-tools\node_modules\#babel\preset-env\lib\index.js:67:11)
at C:\Users\mahendra\AppData\Roaming\npm\node_modules\powerbi-visuals-tools\node_modules\#babel\preset-env\lib\index.js:258:62
at Array.map (<anonymous>)
at C:\Users\mahendra\AppData\Roaming\npm\node_modules\powerbi-visuals-tools\node_modules\#babel\preset-env\lib\index.js:258:43
at C:\Users\mahendra\AppData\Roaming\npm\node_modules\powerbi-visuals-tools\node_modules\#babel\helper-plugin-utils\lib\index.js:19:12
at C:\Users\mahendra\AppData\Roaming\npm\node_modules\powerbi-visuals-tools\node_modules\#babel\core\lib\config\full.js:199:14
at Generator.next (<anonymous>)
at Function.<anonymous> (C:\Users\mahendra\AppData\Roaming\npm\node_modules\powerbi-visuals-tools\node_modules\#babel\core\lib\gensync-utils\async.js:26:3)
at Generator.next (<anonymous>)
at step (C:\Users\mahendra\AppData\Roaming\npm\node_modules\powerbi-visuals-tools\node_modules\gensync\index.js:254:32)
at evaluateAsync (C:\Users\mahendra\AppData\Roaming\npm\node_modules\powerbi-visuals-tools\node_modules\gensync\index.js:284:5)
at Function.errback (C:\Users\mahendra\AppData\Roaming\npm\node_modules\powerbi-visuals-tools\node_modules\gensync\index.js:108:7)
at errback (C:\Users\mahendra\AppData\Roaming\npm\node_modules\powerbi-visuals-tools\node_modules\#babel\core\lib\gensync-utils\async.js:70:18)
at async (C:\Users\mahendra\AppData\Roaming\npm\node_modules\powerbi-visuals-tools\node_modules\gensync\index.js:183:31)
at onFirstPause (C:\Users\mahendra\AppData\Roaming\npm\node_modules\powerbi-visuals-tools\node_modules\gensync\index.js:209:13)
at Generator.next (<anonymous>)
# multi ./.tmp/precompile/visualPlugin.ts
error Package wasn't created. 1 errors found
warn Please, make sure that the visual source code matches to requirements of certification:
info Visual must use API v2.5 and above
info The project repository must:
info Include package.json and package-lock.json;
info Not include node_modules folder
info Run npm install expect no errors
info Run pbiviz package expect no errors
info The compiled package of the Custom Visual should match submitted package.
info npm audit command must not return any alerts with high or moderate level.
info The project must include Tslint from Microsoft with no overridden configuration, and this command shouldn’t return any tslint errors.
info https://www.npmjs.com/package/tslint-microsoft-contrib
info Ensure no arbitrary/dynamic code is run (bad: eval(), unsafe use of settimeout(), requestAnimationFrame(), setinterval(some function with user input).. running user input/data etc.)
info Ensure DOM is manipulated safely (bad: innerHTML, D3.html(<some user/data input>), unsanitized user input/data directly added to DOM, etc.)
info Ensure no js errors/exceptions in browser console for any input data. As test dataset please use this sample report
info Full description of certification requirements you can find in documentation:
info https://learn.microsoft.com/en-us/power-bi/power-bi-custom-visuals-certified#certification-requirements
I sorted out after long trial and error.
Here are the steps. First, manually install the problematic dependencies:
npm install --save-dev #babel/core
npm install --save-dev #babel/preset-env
npm i #babel/plugin-proposal-numeric-separator
Second, go to file path in which the error is occurring (not in your code dir). In my case:
C:/Users/mahendra/AppData/Roaming/npm/node_modules/powerbi-visuals-tools/node_modules/#babel/preset-env/lib/available-plugins.js
Note that this was found by examining the error message and noticing this line:
Ensure there is an entry in ./available-plugins.js for it. (While processing: "C:/Users/mahendra/AppData/Roaming/npm/node_modules/powerbi-visuals-tools/node_modules/#babel/preset-env/lib/index.js")
Finally, add the following lines to the file:
var _pluginTransformNumericSeperator = _interopRequireDefault(require("#babel/plugin-proposal-numeric-separator")); (Import dependency; anywhere above the definition of the export object _default)
"proposal-numeric-separator": _pluginTransformNumericSeperator (Add the dependency as a new export in the export object _default by adding a new key value pair)
Note that you may have to replace the path argument to require() with an absolute path to the location of #babel/plugin-proposal-numeric-separator in step 1 above if you are unable to resolve this during your next build.
Enjoy!
The root issue seems to be a problematic version of #babel/plugin-proposal-numeric-separator that powerbi-visuals-tools is depending on.
An alternative to the accepted solution is to install a working version of #babel/plugin-proposal-numeric-separator as a global dependency and then modify the powerbi-visuals-tools to use this instead of its packaged version. See steps below:
npm i -g #babel/plugin-proposal-numeric-separator
Locate the file that lists dependencies for the failing module. See the accepted answer for instructions, but it seems to be this path for Windows: <userDir>/AppData/Roaming/npm/node_modules/powerbi-visuals-tools/node_modules/#babel/preset-env/lib/available-plugins.js
Add the following lines to this file:
var _pluginTransformNumericSeperator = _interopRequireDefault(require("#babel/plugin-proposal-numeric-separator"));
This imports our globally installed dependency: place anywhere above the definition of the export object _default
"proposal-numeric-separator": _pluginTransformNumericSeperator
Add the dependency as a new export in the export object _default by adding a new key value pair
You should now see the build succeed.
As a note, I'd generally avoid installing global dependencies like this. However, this may be the easiest workaround until the powerbi-visuals-tools package is updated to fix this issue. When this is done, I would recommend uninstalling the global dependency and reinstalling the powerbi-visuals-tools from scratch to undo our changes to the dependency file.

Import Stripe using Angular2 and stripe.js

I am using Angular2, Ionic2 and Stripe.js for payment processing. This thread here ionic2 with stripe payment gateway is using the plugin https://github.com/Telerik-Verified-Plugins/Stripe/blob/master/doc/index.md but it's not safe because you have to embed the Secret key inside the app. Even the page is telling people not to use this plugin.
I tried to use the node.js version here:
https://www.npmjs.com/package/stripe
However, I cannot figure out how to do the var stripe = require('stripe')(' your stripe API key '); when in TypeScript, you have to use import.
Finally, I did put <script type="text/javascript" src="https://js.stripe.com/v2/"></script> in index.html and the stripe variable shows globally inside each Component. However, I don't feel like this is the proper way to do it as the stripe object may not be ready by the time I use it inside each Component or Page.
What's the proper way to use Angular2 and Stripe.js? Ionic2 specifically would be nice but optional.
Thanks
UPDATE 1
I tried npm install stripe and then used import '../../node_modules/stripe/lib/stripe.js'; but still got error:
TypeScript error: /Users/username/Documents/StripePayment/app/pages/home/home.ts(16,23): Error TS2304: Cannot find name 'Stripe'.
Error: Cannot find module '../../node_modules/stripe/lib/stripe.js' from '/Users/username/Documents/StripePayment/app/pages/home'
Here is my VS Code screenshot with directory structure:
Add the scripttag in the index.html and then put a declaration after the imports in home.ts
declare var Stripe: any;
I believe this is the correct way to import external libs in ng2
Src: Nic Raboy
There are a some more info there; the better way to install an external lib is to download the typedefs from DefinitelyTyped and install with $ typings install
Then you should be able to import as usual
This is, of course, if there are typedefs in the DefinitelyTyped repo.
There does not seem to exist typedefs for the Stripe library, though.
Stripe seems to have type definitions now so alongside
npm install --save stripe
you can also run the following to get TypeScript definitions:
npm install --save #types/stripe
you should then be able to so something like:
import { Stripe } from 'stripe'
The above is psudo code as Ive not tested it but will will be something similar.
More info here:
https://www.npmjs.com/package/#types/stripe
The stripe.js library is intended for the server, requires the child_process module, and creates a server of its own. There is not a good way of importing this library directly to a browser environment.

Error: unknown package in top-level dependencies in Meteor app

I'm making a small Meteor package. It employs two other packages that are explicitly listed in its package.js. For test purposes, I add this package from local system (it's not published on Atmosphere). And I keep getting error messages after I run the app:
=> Started proxy.
=> Started MongoDB.
=> Errors prevented startup:
While selecting package versions:
error: unknown package in top-level dependencies: whoever:whatever
I even added required packages explicitly to the app but it didn't help.
The package.js:
Package.describe({
name: 'whoever:whatever',
version: '0.0.1',
summary: 'Whatever the summary is',
git: 'https://github.com/whoever/whatever',
documentation: 'README.md'
});
Package.onUse(function(api) {
api.versionsFrom('1.1.0.3');
api.use('http');
api.use('jparker:crypto-sha1', 'server');
api.use('simple:reactive-method', 'client');
api.addFiles('for-a-server.js', 'server');
api.addFiles([
'for-a-client.js',
'for-a-client.html'
], 'client');
});
What am I doing wrong? What should I look for next?
As was mentioned in your comments, it was due to a problem with symlinking. However, for googlers who come by developing their own meteor packages and also getting this message -- they need to check their env vars have $PACKAGE_DIRS defined in the terminal calling meteor to start their app.
I didn't and this caused the same problem!
Make sure to both init and update your submodules. This should work:
git submodule update --init --recursive
Can you please try and replace the single quotes with double quotes and try... something like below. Please type the quotes.
Package.describe({
name: "whoever:whatever",
version: "0.0.1",
summary: "Whatever the summary is",
git: "https://github.com/whoever/whatever",
documentation: "README.md"
});

Categories