How to create files using androidjs - javascript

I was wondering if I could create files using androidjs.
I Tried using fs in the main.js class but I couldn't find the file I was trying to create.
Is it possible to use fs or anything like it to create android apps using androidjs?

Related

How can I use functions and libraries from Node.js to JavaScript?

I'm trying to use functions from Node.js to JavaScript but I don't know how to make the connection between them
I'm trying to use the next library in JavaScript
const mongoose = require('mongoose')
But the browser doesn't support "require"
Can any one helps me?
You can't directly use Node.js packages inside a Javascript file without a node environment. But there is a cool way of using Node.js packages inside your Javascript file.
Visit Skypack.dev and use any npm package using the CDN.
For example, mongoose - https://www.skypack.dev/view/mongoose
Write your javascript code like this,
<script type="module">
import mongoose from 'https://cdn.skypack.dev/mongoose';
</script>
Note: Make sure you add type="module" attribute to your <script> tag.
Thank you!

__dirname ceases to work when using pkg to pack my NodeJs apps

I applied the code underneath in my NodeJs application:
console.log(__dirname);
I wish to pack the application using pkg. However, where normally the path to the .js file would have been printed (let's assume it is "F:\Files\apps\my_dir", I now only get a "F:\snapshot\my_dir" (the middle part has been replaced by "snapshot".
How can I fix that?
I finally worked out the problem. We should not be using __dirname. Instead, we should do it like this:
const process = require('process');
console.log(process.cwd())

JS - creating new worker causes 404

I'm using Worker API to create new worker, it's as simple as:
var myWorker = new Worker('src/worker.js');
Unfortunately I'm using webpack to build my project and linking to src/worker.js returns 404.
How can I use it to create my Worker?
import hardWorker from './src/hardWorker.js';
var myWorker = new Worker(HOW TO PUT HARDWORKER HERE?);
Try using the worker-loader plugin for webpack, which can be found here: https://github.com/webpack-contrib/worker-loader
This registers scripts loaded through it as a Web worker, and bundles all of your workers in the main bundle that webpack creates. Using it in your app is as simple as this:
import MyWorker from 'worker-loader!./Worker.js';
const worker = new MyWorker();
Or you can add loader configuration parameters to your webpack.config so all files matching a specific pattern are loaded and registered as workers. The worker-loader has an example of this.
The issue is that the worker API expects a URL to load the work script from and webpack tries to bundle everything into a single file, meaning the src/worker.js file isn't where it needs to be when the browser tries to load it.
There are a couple options.
You could manually copy the worker.js file to wherever the build is trying to load the file from.
You could set up webpack to copy the file to the right location using the CopyWebpackPlugin.
Depending on how complicated your worker is, you could just put the code in a string and create an object URL so the browser doesn't have to load anything from the server at all. This is an approach I've used in the past and have written a helper library for to Workers a little easier to use.
Hope that helps!

A very simple query - Loading plain old javascript file with webpack

Should be quite a common question for a webpack newbie but unfortunately couldn't find a solution -
My project uses webpack. I need to use a library but it needs to be used as the old way of adding script tag like
<script src="//messaging-public.realtime.co/js/2.1.0/ortc.js"></script>
However I am looking for some way through webpack (a loader or in some other way) such that I can use it like
import ortc from "realtime-framework"
or
import * as ortc from "realtime-framework"
You will need to either:
Install it from a package manager like npm;
Download the file locally and import it;
Or include it the normal way with a script tag, making sure it is included before your script.

How can I use a typescript file in multiple projects some using modules (node.js) and some don't?

I used typescript in the past mainly for client side code to run in the browser. Now I am trying to reuse some classes of my library for node.js. My setup looks like this:
SharedLibrary Project with multiple classes
Client Project using the SharedLibrary
Server Project with node.js using the SharedLibrary
Node.js seems to force me to use commonjs as module library for typescript. Thus my code on the server side will be forced to use modules as well. How can I include my SharedLibrary now in this project? It seems modules are unable to access anything outside of the module except if it is itself a module but I also can't change my SharedLibrary to a module as this would force me to change my whole code base. Is there any way out of this without having to change everything?
Example code:
Library file A.ts:
class A {
public call() {
console.log("class A");
}
}
Server file C.ts:
export class C {
public call(): void {
console.log("class c");
let a = new A(); //this will compile but crash during runtime
a.call();
}
}
node.js main file:
import * as myodule from "./C";
var s = new myodule.C();
s.call();
It will print "class c" and then crash as it can't find class A. It works just fine if I add export to "class A" and then import it but then my client side code stops working.
What I tried so far:
using the files directly doesn't work
using the export keyword to export class A outside of its .ts file doesn't seem to work either
C style defines might work that optionally define the shared library as export or not should work but I couldn't find anything like that in typescript
Is there any way out of this without having to change everything?
Just use commonjs everywhere and use a module loader/bundler like webpack. Here is a quickstart : https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html
Example
Here is a fairly large project that uses this method http://alm.tools/

Categories

Resources