Kotlin Js - Access Resource Folder - kotlin-multiplatform

I am starting a project in Kotlin-Multiplatform. Accessing the resource folder via Java is quite easy, but I don't know how to access it via JS targeting Node.
During testing, I found out that the resource file is stored in a separate folder. If I'm not mistaken:
build/js
|-- package
| |--project
| |--project-test \\tests are executed here via calling __dirname in node
|
|--processedResources
|-- testfile.txt \\ files in the resource folder are stored here
I would like to know: how is it done via JS/Node?

You can try to use fs:
external fun require(name: String): dynamic
external val __dirname: dynamic
val fs = require("fs")
val path = require("path");
fun main() {
val path = path.join(
__dirname,
"..\\..\\..\\..",
"processedResources",
"js",
"main",
"test.txt"
)
println(fs.readFileSync(path, "utf8"))
}

Related

I'm getting an error building a sveltekit project

I'm building a sveltekit project. One thing I've done is created a custom type of file which is converted to a *.svelte file upon building or running the development server. By default, sveltekit includes the rollup extension rollup-plugin-dynamic-import-variables which is trying to parse my custom file (who knows why?) and throwing an "unexpected token" error. I'm trying to configure that extension to ignore my custom files, but so far without success. Here is my attempted svelte.config.js file:
// #type {import('#sveltejs/kit').Config}
var config;
import adapter from '#sveltejs/adapter-static';
import dynamicImportVariables from 'rollup-plugin-dynamic-import-variables';
config = {
kit: {
// --- hydrate the <div id="svelte"> element in src/app.html
target: '#svelte',
adapter: adapter({
pages: 'build',
assets: 'build',
fallback: null
}),
vite: {
plugins: [
dynamicImportVariables({
warnOnError: true,
exclude: '**'
})
]
}
}
};
export default config;
To be honest about it, I don't use dynamic imports anywhere and therefore would accept as a solution the complete disabling of the extension. But anything that would get it to ignore my custom files would also work.
UPDATE: SvelteKit 1.0.0-beta now requires pages/endpoints to follow a specific naming pattern, so explicit file exclusion should no longer be needed.
SvelteKit specially handles files in the routes/ directory with the following filenames (note the leading + in each filename):
+page.svelte
+page.js
+page.server.js
+error.js
+layout.svelte
+layout.js
+layout.server.js
+server.js
All other files are ignored and can be colocated in the routes/ directory.
If, for some reason, you need to have a file that has a special name shown above, it's currently not possible to exclude that file from special processing.
Original outdated answer:
The rollup-plugin-dynamic-import-variables is actually included by Vite. To configure Vite's plugin, set the build.dynamicImportVarsOptions property:
// svelte.config.js
/** #type {import('#sveltejs/kit').Config} */
const config = {
kit: {
// hydrate the <div id="svelte"> element in src/app.html
target: "#svelte",
vite: {
build: {
dynamicImportVarsOptions: {
exclude: [/node_modules/, /\.starbucks$/],
},
},
},
},
}
export default config
But that's not going to fix the problem...
SvelteKit processes all files under src/routes/ so that they're automatically imported in the output application (in .svelte-kit/build/app.js), which will result in the same error.
Option 1: Private modules
You could exclude a src/routes/*.starbucks file by making it a private module, which has a leading underscore in the filename:
src/routes/_home.starbucks 👈
src/routes/_index.starbucks 👈
src/routes/index.svelte
Option 2: Move files outside src/routes
Alternatively, move those *.starbucks files outside of src/routes/ (e.g., into src/starbucks/ or src/lib/):
src/routes/index.svelte
src/starbucks/home.starbucks 👈
src/starbucks/index.starbucks 👈
src/lib/home.starbucks 👈
src/lib/index.starbucks 👈

How to open Modules from other files in Fable-F#

I'm brand new to Fable and I'm having some issues opening a custom module from a different file.
Here's my basic file setup
node_modules
|
public
|
src
|_ App.fsx
|_ OtherFile.fsx
Inside the App.fsx file:
open CustomModule
Inside the OtherFile.fsx file
module CustomModule =
let greeting =
printfn "hello from CustomModule"
Here is my fableconfig.json file:
{
"projFile": "./src/App.fsx",
"outDir": "./public",
"scripts": {
"postbuild": "./node_modules/.bin/webpack"
}
}
Whenever I try to reference CustomModule, I get a The namespace or module 'CustomModule' is not defined. error. Any ideas?
Turns out there was a similar question here, whose answer solved my issue.
Just call the following to load other modules
#load "OtherFile.fsx"

How to get the original path of a portable Electron app?

I have an Portable Electron App (packed with: electron-builder + asar, portable build) on Windows. I try to get the application path but it returns a path within the user\temp folder rather than the actual '.exe' file
Is there any way to get the original app.exe path?
I've tried the following:
app.getAppPath()
__dirname
require.main.filename
app-root-path
and a few node modules
The path I'm getting from my tests:
C:\Users\xxx\AppData\Local\Temp\xxxxxx.tmp\app
the actual .exe Path (where the app launched from, and what i need):
C:\Users\XXX\Documents\test\dist
I'm just starting with Electron.
I found a solution:
Use the Environment Variable (created by Electron-Builder)
process.env.PORTABLE_EXECUTABLE_DIR
to show the real Path of the App.exe.
Works only packed with Electron-Builder
From the main process:
// If not already defined...
const { app } = require ('electron');
const path = require ('path');
let execPath;
execPath = path.dirname (app.getPath ('exe'));
// or
execPath = path.dirname (process.execPath);
From a renderer process:
// If not already defined...
const { remote } = require ('electron');
const path = require ('path');
let execPath;
execPath = path.dirname (remote.app.getPath ('exe'));
// or
execPath = path.dirname (remote.process.execPath);
I had a lot of trouble with this and was finally able to solve the issue by replacing __dirname by '.', see working example below :
const path = require('path')
const myAppPath = path.resolve('.', 'myapp.exe');
Seems like PORTABLE_EXECUTABLE_FILE only works under certain Electron-Builder configurations; In particular, it won't work in apps deployed as a portable directory.
In such cases, the following will get you the application root path:
const path = require('path')
import { app } from 'electron'
let rootDir = app.getAppPath()
let last = path.basename(rootDir)
if (last == 'app.asar') {
rootDir = Path.dirname(app.getPath('exe'))
}
None of the above answers worked for me on Windows 10.
process.env.PORTABLE_EXECUTABLE_DIR returns the Temp directory path.
I got it to work using:
process.env.INIT_CWD
process.env.PORTABLE_EXECUTABLE_FILE
will give you the full path to the file.
As none of the methods above worked and I don't want to use an external lib for this, i tried following:
if (!fs.existsSync("../images")) {
fs.mkdirSync("./../images");
}
return path.resolve("./../images/") + "/";
You can use any directory that should exist at the top level or anywhere else. In my case I know that one level higher in the directory there must be a directory called "images".
This solution worked in my case for DEV build and prod (packaged).
Are there any drawbacks when using this approach?

How do we refer to etc package from NixOS configuration?

I want to get a path, which leads to nixos /etc location (any one of /run/current-system/etc or /nix/store/hashhere-etc-1.0). I use this path to configure pppd connect script, some kind of the following,
environment.etc."huawei" =
{ text = ''
/dev/ttyUSB0
38400
lock
crtscts
nodetach
noipdefault
# Below here what I've struggled
connect ${pkgs.etc}/${environment.etc."huawei-script".target}
'';
mode = "0777";
target = "ppp/peers/huawei"; };
I have tried to write ${pkgs.etc} or ${system.build.etc} or even ${environment.etc} resulting errors.
The directory structure is actually relative, but I think it's safer to use absolute path.
/nix/store/...etc.../ppp/peers
|- huawei
|- huawei.d
|- huawei.sh
|- huawei.chat
You can refer to path to file in /nix/store/...etc... like this:
{ config, pkgs, lib, ... }:
{
environment.etc."test".text = "helo";
environment.etc."test2".text = "${config.environment.etc."test".source.outPath}";
}
Now I have in /etc/test2:
$ cat /etc/test2
/nix/store/1igc2rf011jmrr3cprsgbdp3hhm5d4l0-etc-test
If I understand correctly your problem is you simply need to pass the string value of the target attribute to the huawei.text connect directive. As per the description for the target attribute the value is a path relative to /etc so you should be able to either:
Make the value of the connect directive the string literal connect /etc/ppp/peers/huawei or
make the etc.huaweiattribute set a recursive one so that the attributes can refer to each other then do
environment.etc.huawei = rec {
target = "ppp/peers/huawei";
text = ''...
# Below here what I've struggled
connect ${target}
'';
};
Sorry, I was overlook a fact where NixOS actually map any files in /nix/store/...etc../ into the /etc itself.
So, to refer to a file, it is better to use /etc directly.
connect /etc/${environment.etc."huawei-script".target}

Yeoman copy directory not working

I am trying to copy entire directories as part of a Yeoman generator. I have multiple functions that call this.fs.copy and it works as expected (for the most part) except fonts and assetsdev. I cannot figure out why assetsdev task copies the source folder into the fonts directory rather than into the root where I have specified. Here are my three functions that wholesale copy a directory:
this.props.dotDest = './';
this.props.srcPath = './assets-dev';
this.props.destPath = './public/assets';
dotfiles: function () { // Works as expected. Copies to the root.
this.fs.copy(
this.templatePath('dot_files/.*'),
this.destinationRoot(this.props.dotDest)
);
},
fonts: function () { // Works as expected. Copies to the bootstrap directory.
var done = this.async();
this.log( chalk.magenta.bold('Copying /fonts') );
this.fs.copy(
this.templatePath('fonts'),
this.destinationRoot(this.props.destPath + '/fonts/bootstrap')
)
done();
},
assetsdev: function () { // Does NOT work. Copies to the Bootstrap directory rather than the root.
var done = this.async();
this.log( chalk.magenta.bold('Copy assets-dev') );
this.fs.copy(
this.templatePath('assets-dev'),
this.destinationRoot(this.props.srcPath)
);
done();
},
I expect the resulting folder structure to be
-assets-dev
-public
-assets
-fonts
-bootstrap
-fontfile
-fontfile
-fontfile
But instead it's generating...
-public
-assets
-fonts
-bootstrap
-assets-dev
-fontfile
-fontfile
-fontfile
...and I can't figure out why. I even tried using the async() method in case it was running asynchronously and happened to be in that directory when the assetsdev function runs.
Does anyone know why assetsdev is copying to the wrong folder? I hope I have explained clearly enough. Thank you in advance!
So apparently I cant use destinationRoot(). Instead I have to use destinationPath(). I am not sure why but using destinationPath() copies correctly now.

Resources