How to add a custom library to a scratch file - intellij-idea

In PhpStorm/WebStorm how can I add a custom library so that the file compiles? In my case it would be Lodash?
I tried to add lodash to global libs but it did not help...

Using require and relative paths.
Here is a crazy example of using lodash in a scratch file:
const _ = require('./../../../DEV/node_modules/lodash');
var anything = [1, 2];
_.map(anything, function (item) {
console.log('Working >-P');
return item;
});

In fact, you can just add a package.json file to your scratches.
You could create one manually, but the following may be more practical:
Right click on any of your scratch files, then select "Open in terminal".
You'll notice that it will open the terminal directly in the folder of the scratches.
That's a folder such as C:\Users\ba\AppData\Roaming\JetBrains\WebStorm2020.2\scratches.
Since you are in the correct directory now, you can just run npm init from that terminal to create your package.json file. (You will be prompted with a bunch of questions, but you can just press enter for all questions if you are fine with the default values)
This file will just show up in your scratches along with your other files.
To answer your specific question, if you want to add lodash to those packages, you can open the terminal in the way mentioned above. This terminal will be in the correct folder. And then you can just run your npm install lodash from there.

var path = require('path');
_require = require;
require = function (p) {
var absPath = path.join(process.cwd(), p);
var relPath = path.relative(__dirname, absPath);
return _require(relPath);
}
var Query = require('./server/libs/query_builder.js');
A bit late to answer, but if you replace the require function as I show above you can use it normally.

Well I would suggest in this case to use plugin named Quokka.js available for Webstorm and other JetBrains tools.
It's recognise automatically the libraries from node_modules and have a lot of other features (work much better that pure scratch files).
When you will install it the default scratch Javascript and Typescript files will work with Quokka.
You will find the details on the official tool page: https://quokkajs.com/
Please select JET BRAINS tool on main page. Hope it helps.

Related

How to use Troisjs in Nuxt 3 project

I would like to use TroisJS (three.js wrapper for Vue) with Nuxt.js. According to the TroisJS documentation (https://troisjs.github.io/guide/install.html#existing-vuejs-3-project) I need to add it to my project like:
import { TroisJSVuePlugin } from 'troisjs';
app.use(TroisJSVuePlugin);
However, I don"t know how to figure out where I should put this code. I would expect the nuxt.config.js file, but I don't seem to quite get it where it should go.
I decided to use TroisJS and not three.js because I thought the former might be easier to import and use. If importing three.js directly is easier, I don't mind using it.
Thank you very much for any help!
In /plugins folder add new file named troisjs-plugin.js with the following content :
import { TroisJSVuePlugin } from 'troisjs';
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.use(TroisJSVuePlugin )
})
I found a repo with some testing with Trois and Nuxt 3, probably outdated and maybe some apis have changed, but if you wanna check it out: alvarosabu/nuxt3-trois
Also, there's an official repo from the Trois author with a Nuxt 3 custom plugin (probably outdated too) here

Webpack: How to compile all less files in a project

I use webpack for JS and now I want to use it for styles. I have a lot of styles in different folders and i want to compile them all without requiring each of them mannaully. The question is how to gather all the .less files in the folders and compile them via less-loader?
This isn't how webpack is meant to work, really. If you really want to do this, grunt/gulp is going to be a better choice.
Webpack's require mechanism ensures you build only the CSS you need for any given entry point, and gives you dependency management as well. If you do want to use webpack, but don't want to use the style-loader to insert them into the DOM etc., you can use the Extract Text plugin to build your compiled CSS into a separate file.
I found some workaround using require.context.
First you need to create a js file in the root of the styles folder if you don't have one.
Use this code if you use css or less and always extract them
require.context('./', true, /(\.less$)|(\.css$)/);
First argument is relative path to folder in which webpack should search for the files, second tells that it should search in subfolders and the last one is regexp of the extension of the files that webpack should require. Then you need to requre this file or use it as entry point. This works if you use extract-text-webpack-plugin but doesn't work otherwise.
Using styles without extracting them to style separate file
The example above doesn't work if you don't extract them because webpack generate modules with styles but doesn't execute them. This is complete example that works in both cases:
(function (requireContext) {
return requireContext.keys().map(requireContext);
} (require.context('../', true, /(\.less$)|(\.css$)/)));

NodeWebkit testing using grunt?

Right now i have grunt setup to watch for file changes and feed them to mocha, and mocha runs the tests. The problem is when modules include something like "nw.gui" the test case cannot find them. Is there any way to get around that, some way that i can include it?
//indexeddb.spec.js
var assert = require("assert");
var IndexedDB = require("../scripts/indexeddb");
db = new IndexedDB();
console.log(db);
describe('IndexedDB', function(){
describe('initialize', function(){
it('Should throw an error when the paramaters are null', function(){
expect(db.initialize()).to.throwError();
});
});
});
//indexeddb.js
module.exports = exports = function(){
var indexedDB = require("nw.gui").Window.get().window.indexedDB;
var _ = require("../bower_components/underscore/underscore.js")
this.initialize = function(databaseName,version,schema) {
}
}
Should i do this differently? I did think about running the tests in a gui window in webkit, but that would require that i include all the spec file's on the page and reload the page every time i wanted to run the test. With grunt and watch i was trying to get it to run the test for each file when i edited either the spec or the src file.
I will also need to test the html/js/backbone pages that i open in a webkit window.
Thanks for any insight.
Code example at https://github.com/varunvairavan/node-webkit-unit-testing
Found a way to run test's on node webkit, with a watch to reload the page on file changes.
I was going through this post https://github.com/visionmedia/mocha/issues/960 when i found a link to https://github.com/BenoitZugmeyer/chwitt/tree/5f9bc6c0b8c0328f1dc06a554e9fdd3a969c36ae/tests
The way it works is, create a new node webkit app in your test directory, it then includes all the files in that directory that end with .spec.js and runs the tests in them. It also does a page reload on file changes in the source/test folder.
I had my files in sub directories in the test folder so i used the walk function from node.js fs.readdir recursive directory search to find all the files ending with ".spec.js".

Play framework 2.1 can't use custom modules

I'm not able to use a module which I created by myself. I do everything like the following description:
http://java.dzone.com/articles/writing-modules-play-2-get
But there are little changes which shouldn't have any influence of the functionality: I don't change the project-struktur and i create separates projects in different folders. I think I should describe my steps for using and creating my own module and perhaps anyone see my mistake:
I create one project with play new testapp (workspace/testapp). Then i create an other projekt with play new testmodul (workspace/testmodul). Then i go to the testmodul and delete the route-file and the content of the application.conf. Then i create a class called "Tester" with the method test. (a simple system.out) After that i exceute "clean", "compile" and "publish-local" in the testmodul-folder.
In my understanding the modul ist published in the local repository of play and every application which want to use this module will have access to the local repository version of the module.
Now I go to the testapp and create the dependencies in the Build.scala. After that i go to the default-Application.class and add the line "Tester.test();". Then I excecute the dependencies command and try to compile.
After that i get the error, "can't find symbol". It seemed to be that the testapp don't know the Tester.class and that means that the testapp don't know the module.
But I don't know why.
Has anybody an idea?
Edit:
Perhaps I should display my dependencie config of the testapp:
val appDependencies = Seq(
// Add your project dependencies here,
javaCore
,javaJdbc
,javaEbean
,"testmodul" % "testmodul_2.10" % "1.0-SNAPSHOT"
)
val main = play.Project(appName, appVersion, appDependencies).settings(
// Add your own project settings here
resolvers += "Local Play Repository" at "file://C:/play-2.1.0/repository/local"
)
I'm not sure why the solution above wasn't right, but i found a solution. I found out that in the documentation of the playframework "modules" called "subprojects" there I found how to include modules. (http://www.playframework.com/documentation/2.1.0/SBTSubProjects)
But I think the other way of "global" modules should also be possible.
Change this line
resolvers += "Local Play Repository" at "file://C:/play-2.1.0/repository/local"
To
resolvers += "Local Play Repository" at "file://play-2.1.0/repository/local"
Just remove the C:/ .It worked for me. Check this http://www.objectify.be/wordpress/?p=363.

Adding Google Objective-C API 'GTL' to iPhone project

How to I add the Google Drive API to my iPhone project to I can use it?
So far, I have dragged the GTL project into my current app project (so that it is nested under my app project). Then, under my app target's build phases, I added GTL.framework, and then added GTL.framework to my 'Link binary with Libraries' (see attached pic). This throws the following error:
clang: error: no such file or directory: '/Users/xxx/Library/Developer/Xcode/DerivedData/Golf-hfbczyaemhyzgvbrtgdxqnlzeuaa/Build/Products/Debug-iphonesimulator/GTL/GTL'
How to I fix this?
I struggled with this exact issue for most of the day today, and I found it extremely frustrating. I have finally figured it all out so here is a straightforward step by step guide on how to add the Google API to an iOS7 project using XCode5, using ARC, without having to create Workspaces or any of that.
The answer provided by RawMean works well, but it gave me issues with ARC. I also didn't like the fact that you had to add project, create a workspace and then delete the project. So my solution will deal with both these issues.
Check out the code. To do this, you can just run svn checkout http://google-api-objectivec-client.googlecode.com/svn/trunk/ google-api-objectivec-client-read-only from your terminal. I will refer to this code as "Google's code".
Go to your project's Build Phases. Expand "Link Binary With Libraries" and add Security.framework and SystemConfiguration.framework. These two are required by Google's code.
Go to your project's Build Settings. Using the search box there, look for Other Linker Flags (make sure "All" is selected to the left of the search box). Add -ObjC -all_load.
Now search for User headers search path and add the full path to Goggle's /Source directory. Make sure you select recursive.
Using Finder, go to Google's /Source/OAuth2/Touch directory. Drag and drop GTMOAuth2ViewTouch.xib into your project.
Go back to Finder and go to Google's /Source directory. Drag and drop GTLCommon_Sources.m and GTLCommon_Networking.m into your project.
Now, you need to import the files for the services you want to use. In my case, I need to use Google Drive, so I'll add those. In finder, go to Google's /Source/Services/Drive/Generated directory. Drag and drop GTLDrive.h and GTLDrive_Sources.m into your project. If you want to use other services, go to their directory and import the appropriate .h and .m file.
For some reason, Google's code doesn't use ARC, so if you try to build right now, you will get ARC compile errors. So we need to disable ARC for Google's code only. To do this, go back to your project's Build Phases, but this time expand "Compile Sources". Make sure that GTLCommon_Sources.m and GTLCommon_Networking.m are there. Select them, press enter and type in -fno-objc-arc. This will disable ARC for both of them. Make sure you don't add this option for any other file (unless you know what you're doing).
You are done! Now, whenever you want to use Google's API, just import GTMOAuth2ViewControllerTouch.h and your service header. In my case, since I'm using Google Drive, I will also import GTLDrive.h.
I hope that helps and saves some people from pulling all their hair out.
I struggled with this error message as well. This is how I solved it:
Make sure you have added the folder for the service that you are using under GTLSource/Common/ (e.g., add the Drive folder for GoogleDrive).
Under GTL.xcodeproj (that you have already added to your workspace) find the GTLSource folder and drag it to your main project (Golf in your case). Done!
Now you can remove references to the GTL.xcodeproj that you have added to the workspace.
With this approach, you don't even need to add the libraries (so remove them from the list of linked libraries if you have added them).
The Google API documentation is nothing like Apple's documentation (it's not good).
I should also mention that I'm building an app for iOS and not MacOSX, but this should work for OSX as well.
Not only doing the above, but go under "[Project Name] Targets->Build Phases> Compile Sources" and click the + button. Then add all the .m files, for some reason most of the aren't automatically.
I also had to delete (the reference) to "GTLDrive_Souces.m" from the Drive folder, but I don't understand why i had to do that part.
Better to use Pod
How to install CocoaPods and setup with your Xcode project
for reference : [http://blogs.triffort.com/?p=309][1]
Open the pod file and add
pod 'Google-API-Client/Drive', '~> 1.0' save pod file and call pod install in terminal. Note:pod file you must give link_with
'Your_project_name', 'Your_project_nameTests' after this line only add
your library
this is does not really solve the problem of installing Google API's but in this repo I accessed Google Forms from an iOS app without using Google's API. https://github.com/goktugyil/QorumLogs
So you may skip the installing API part in some projects
Here is the tutorial of how to set it up:
https://github.com/goktugyil/QorumLogs/blob/master/Log%20To%20GoogleDocs.md
Heres the code to do it:
private static var googleFormLink: String!
private static var googleFormAppVersionField: String!
private static var googleFormUserInfoField: String!
private static var googleFormMethodInfoField: String!
private static var googleFormErrorTextField: String!
/// Setup Google Form links
static func setupOnlineLogs(#formLink: String, versionField: String, userInfoField: String, methodInfoField: String, textField: String) {
googleFormLink = formLink
googleFormAppVersionField = versionField
googleFormUserInfoField = userInfoField
googleFormMethodInfoField = methodInfoField
googleFormErrorTextField = textField
}
private static func sendError(#text: String) {
var url = NSURL(string: googleFormLink)
var postData = googleFormAppVersionField + "=" + text
postData += "&" + googleFormUserInfoField + "=" + "anothertext"
postData += "&" + googleFormMethodInfoField + "=" + "anothertext"
postData += "&" + googleFormErrorTextField + "=" + "anothertext"
var request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.HTTPBody = postData.dataUsingEncoding(NSUTF8StringEncoding)
var connection = NSURLConnection(request: request, delegate: nil, startImmediately: true)
}