How to use grunt-contrib-copy to copy to root AND change directory structure - express

I have an express app with my dev views in /assets/views. I figure I need to separate development and production views because in production I'll be editing the HTML when I used grunt-contrib-usemin to concat/uglify scripts.
So here's the problem. My current tree:
assets/views
├── 404.html
├── index.html
├── layout.html
├── question_ask.html
└── question_display.html
Ideally, I want my production-ready views to live on the same level as assets. Using grunt-contrib-copy, it seems to copy the whole tree. I currently am putting it into public since I'm not sure how to set my dest to the root of the project.
copy: {
views: {
src: ['assets/views/*.html'],
dest: 'public/'
}
So there are a few questions here:
Is it bad practice to have dev views and production views? If so, is there another way of producing a view that has references to concat/uglified scripts?
How the heck can I use grunt-contrib-copy to copy to the root of my project? I don't want assets/views obviously, I just want a views/* folder that has the contents of what's in assets/views/*.
Thanks!

You need to specify the flatten option which will remove the directory structure from the destination path. See my answer here: Copy all files from directory to another with Grunt.js copy

Related

vite - Subpage with relative asset path

I have a Vue project with multiple pages where I use rollupOptions.input to specify them as entry points:
rollupOptions: {
input: {
main: resolve(__dirname, "index.html"),
subpage1: resolve(__dirname, "subpage1/index.html"),
subpage2: resolve(__dirname, "subpage2/index.html")
}
},
The final dist folder will be deployed at a subdirectory in a server,
so I then set a base attribute as base: "", to make the assets work for the main index.html . This turns all the paths into something relative like this: <link rel="stylesheet" href="assets/main.35431485.css">. Works for the root index.html but for the subpages, the links look identical. This however doesn't work, because the folder structure is something like:
├── index.html
├── assets
├── main.35431485.css
└── ...
└── subpage1
└── index.html
As such, subpage1/assets/main.35431485.css will simply not work.
Is there a way to tell vite to relatively path its way to the asset folder, even for subpages?
Ideally not using a static parent directory (like with base: "/some/dir/"), but keeping it all relative?
This is a known issue that will be fixed in vite 3.0. There seems to be a fair amount of issues left for the 3.0 release but hopefully it will be released sometime in 2022. Right now the only way is to have a static base path, changing to some SPA approach, or using another build tool.
Edit: vite 3.0 is now released and relative base path is now properly supported. See the docs here.

Relative link from readme.md to another file in package, rendered in npmjs

I've created an npm module which is hosted on npmjs.com.
Due to the structure of the module, I've split the documentation into a general readme.md and then more specific files which are linked to from the main readme.md:
.
├──documentation
│ ├── doc1.md
│ └── doc2.md
└── readme.md
The links in readme.md look like this: [some text](documentation/doc1.md). They work perfectly fine locally and in their CodeCommit repo (and I suspect it would be fine in github as well).
However after publishing it to npm, the links become https://www.npmjs.com/package/documentation/doc1.md, which of course is an invalid URL and leads to a 404 error.
I've tried to figure out how to get a valid url to the file in question so I can create links that lead to it, but I can't even find out how to get a direct link to the file in the package. It may also be worth noting that the package itself is a private organisation package, but I've tried the same thing on a test public package and had the same issue there, so I don't think it's relevant.
Does anyone know how to link from the root readme.md file to another file in the same package, in a way that works in npmjs?
I've found the issue at npm community they say that it's solved, so I tried for my self to see what went wrong?
And they were right, I've missed some points so I'll write all the required steps:
Make sure your repository at github/gitlab is public
Make sure in your package.json file you have the proper repository configuration that point at your repo correctly
Example:
{
...
"repository": {
"url": "https://github.com/lalosh/js-library-boilerplate",
"type": "git"
}
...
}
When doing the relative linking you need to include the relative path from your repo root, including the .md suffix.
Example:
We can [link](documentation/sub-doc.md) to an inner relative file

What is the recommended way to point an npm package's imports to a specific directory?

I have an npm package structured as follows:
my-package
├── build
├── many.config.files
├── package.json
└── src
It made sense to put helper files such as config files and scripts into the root of my package. Then it made sense to have a src file so that my source files wouldn't mix with all my config files. Then it made sense to have a build directory where the built version of src would get populated (maybe some transpiling and minification happens, etc).
Now my src directory has various JS files and folders, all of which users of my library should be able to import.
src
├── file1.js
├── file2.js
└── utils
For example, import utils3 from "my-package/utils/file3.js" should work just fine for them. They should not need to type import utils3 from "my-package/build/utils/file3.js" to import the file.
What is the best practice for dealing with this situation?
I have tried/considered various approaches:
Setting "main": "./build" in my package.json. This did not resolve the import error for me.
Setting "directories": {"utils": "./build/utils"} in my package.json. This did not resolve the import error for me.
Copying package.json into my build directory during build. Then npm publishing the build directory instead of the entire project. This has the upside of only sharing the built code. This has the downside that the built package will have information in its package.json that doesn't make sense because files/directories are missing/mislocated.
Something along the lines of module.exports = require('./build') in an index.js file. This did not work either.

Use relative path in init function cannot be found in test

I'm running into an annoying problem when using relative path in the init function which cannot be found by the unit test.
Say I have a project structured like:
.
├── conf
│   └── blacklist
├── filter
│   ├── filter.go
│   └── filter_test.go
And in the init function of filter.go, I try to load the blacklist by using relative path conf/blacklist to avoid loading it multiple times.
Since the default working directory is exactly the project root directory, it works well with compiled binary. However filter_test.go will panic by
panic: open conf/blacklist: no such file or directory, becasue go test always uses the package directory as the working directory.
I also referred this solution, which could dynamically build the relative path to find the config file. It did work well until I add the covermode=set flag and try to do some coverage testing. It seems that the covermode would rewrite and generate some middle packages like _obj, which makes the dynamical relative path not working any more.
Has anyone run into the same problem before? I'm still new to golang for a couple of months. Any suggestions will be appreciated.
A simple solution is to run tests using something like SRC_ROOT=$PWD go test and then inside the test that want to access files use os.Getenv("SRC_ROOT").

GO in IntelliJ IDEA. Multiple File and Error Undefined: Data

I want use IntelliJ IDE Community Edition to write code in GO (GoLang). I instaled right plugin, and instaled all need tools to build application.
My application consists with two below file. Each is in direcytory ../EventServer.
Main.go
Data.go
If I want to run project from IntelliJ using function Run (Ctlr+Shift+F10) and I get below error
/usr/lib/go/bin/go build -o "/tmp/Build Main.go and run0go" -gcflags "-N -l" /my/home/blah/EventServer/Main.go
# command-line-arguments
./Main.go:11: undefined: Data
I can without any problem compiled code from terminal come in to direcytory with project and execution command
:~/Pulpit/EventServer$ go build
./EventServer
Hello
dane w strukturze someone
tree direcytory and files looks like
EventServer$ tree -a
.
├── Data.go
├── EventServer
├── EventServer.iml
├── .idea
│   ├── compiler.xml
│   ├── copyright
│   │   └── profiles_settings.xml
│   ├── libraries
│   │   └── GOPATH__EventServer_.xml
│   ├── misc.xml
│   ├── modules.xml
│   ├── .name
│   ├── vcs.xml
│   └── workspace.xml
└── Main.go
I suppose that command to run is bad, because compiler trying build program with only one file Main.go but not with all files. Right command should be
$ go run *.go
But I do not know where Can I set this.
I also set GOPATH to:
export GOPATH=$HOME/Pulpit/EventServer
This also hasn't help
CODE
Main.go
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello")
abcd := Data{"someone" , "1234567"}
fmt.Printf("dane w strukturze %s ", abcd.Name)
}
And Data.go
package main
type Data struct {
Name string
Phone string
}
SYSTEM: LINUX
----------------------SOLVED-------------------------------------------SOLVED---------------------
Steps
Project must be found in directory for/example/MyProject/src/HERE_DIRECTORY_WITH_YOUR_SOURCE_GO_FILE
sub direcytory src is important
Go to Run --> Edit Configurations
Find below position
Change Run Kind to Package
In Position Package write Your folder with Your code (Shold be Highlight it is correct)
Click On PLUS icon in left top corner, and Add Go Application
Apply changes
In the right top corner main window IDE You see small icon Play
Chose early defined Go Application my is Unamed
Click Play
An Joy :D
Let say you are having a project with src/ sub directory and two .go files inside: hello.go and typetest.go and both defines the same package "main". One of them (hello.go) also implements func main().
To make it compile as whole you need to make sure two things are configured properly: GOPATH and Run/Debug configuration.
Open the Project Libraries/GOPATH settings:
For Gogland
File -> Settings -> Go
For Intellij IDEA
File -> Settings -> Languages & Frameworks -> Go -> Go Libraries
Makes sure the GOPATH for the project looks something like that:
GOPATH settings
Next, open Run -> Edit Configurations and make sure your configuration looks similar to this:
Run/Debug configuration
You can add second file (in your case Data.go) as Go tool arguments field in Run/Debug Configurations. I read about this here: https://github.com/go-lang-plugin-org/go-lang-idea-plugin/issues/2013 near the end of discussion. Example below (I used 2 files HelGo.go and aaa.go) worked for me:
Go Run Configuration with 2 files
Intellij objected in these 2 files were in different folders, and so both of them has to be same package (main).
On the other side I couldn't make any advises on this page work at all.
I solved the same problem with a complete list of checking. Usually, there are multiple problems combined with each other.
First of all, check your GOPATH settings, there are two ways:
use the command line to check your go environment:
$ go env
you will get a global environment setting, make sure your GO folders are correct. If you are using some local packages, be aware of them too.
check your Build tool setting, whether you have added all your resources files into your dependencies. This is another way to set up your project GOPATH, and do not affects the global settings.
Secondly, check your Run/Debug configuration, and make sure your settings have located the main package or main file. No matter which kind of configuration you use, this is always the start.
Sarp Kaya, just follow Mbded steps. Additional step is, ensure that your additional GOPATH should be there.
For example, this is our ~/.profile GOPATH
export GOPATH=$HOME/lib/Go:$HOME/Development/Go
The first path used by go get processes etc, while your active go development directory goes to the next path.
According to our config, the RightApp exact path should be $HOME/Development/Go/src/RightApp.