Using a task runner without package.json - gruntjs

I'm evaluating task runners, Grunt and Gulp in particular, but there's one thing I dislike about both of them: the fact that they require a package.json file for your project. This is even though your project might not even be an npm project in the first place. In my case, I'm already using composer.json, which basically does the exact same thing.
I ended up creating my package.json like this:
{
"name": "myproject",
"version": "0.0.0",
"devDependencies": {
"grunt": "~0.4.5",
"grunt-phpcs": "~0.2.3",
"grunt-phplint": "0.0.5",
"grunt-phpdocumentor": "~0.4.1"
}
}
Note that I'm not maintaining the version number, as that is unnecessary overhead. This works though, in the sense that I can run my grunt tasks after executing npm install. I feel that I should be able to do without this file though. I read that it's possible to use Grunt without a package.json, but my take is that you'd then have to install the node modules manually, which is more overhead. Gulp is no different, or at least I found no evidence to the contrary.
So the question is, are there any task runners that don't require you to define your project's metadata twice, need only a single file, and are not too bleeding edge?

Answering myself, the only thing I could find that seems to fit my requirements is bldr. It is PHP based, uses composer as package management backend, and does it without hijacking the composer.json you might already be using, as it uses bldr.json instead. It also does not require you to add metadata to the file that describes your bldr dependencies. Here's an example dependencies file (taken from http://docs.bldr.io/en/latest/blocks.html):
{
"require": {
"acme/demo-block": "#stable"
}
}
Then, when you run bldr install, your dependencies are installed and you can run your bldr tasks.

Related

How to load specific moment locales with browserify

I have an angular application built in Visual Studio 2017. I'm using npm, gulp, and browserify to help me manage dependencies, bundling, and minification. Everything had been going along well until I tried to pull in moment, moment-timezone, and angular-moment, when I started having trouble getting these libraries to play nice.
I'm assuming that the issue is related to the way these libraries are being included in my application due to some mistake or bug with the way I'm using npm, gulp, browserify, or the require('...') statements. So, it seems like it'd be helpful for me to explain how I'm doing that.
First, in VS, I added a node configuration file to the project (package.json) and it contains a list of all of my dependencies that will be installed through npm. So, for example, my package.json looks something like this:
{
"property": value,
"otherProperty": otherValue,
"dependencies": {
"angular": "1.5.8",
"angular-ui-router": "1.0.3",
"jquery": "3.1.0",
"moment": "2.18.1",
"moment-timezone": "0.5.13",
"angular-moment": "1.0.1"
}
}
Now, that makes npm go ahead and download everything and stick it in my node_modules folder, but it doesn't actually include anything in my application. So there's a gulp task similar to the following:
var dependencies = Object.keys(packageJson && packageJson.dependencies || {});
browserify({cache: {}, packageCache: {} }})
.require(dependencies)
.bundle()
.pipe(source(js/siteLibs.js))
.pipe(buffer())
.pipe(gulp.dest("."));
Ok, so if that gulp task works correctly, I'll have a file called siteLibs.js that contains all of the js from my npm dependencies, and then I can just make a single script tag to reference siteLibs.js.
The next part, I'm a little hazy on, but do I still have to have an actual require('...') statement in my app for moment, angular-moment, and moment-timezone? If it is required, why? What is it doing?
Now, once at this point, I should be able to go ahead and let my angular app take a dependency on moment, moment-timezone, or angular-moment, and, indeed I can. The issue is that when I call moment.locales(), which is supposed to return a list of all loaded locales, it has naught but 'en-US'. Ok, that's expected because I never loaded any locales. So if I go in my app and say:
require('moment/locales/en-gb');
require('moment/locales/en-au');
require('moment/locales/fr-ca');
It makes no difference. The only loaded locale is en-US. What is the right way to go about getting those additional locales loaded given that I'm using npm, gulp, and browserify?
Refer to this in moment's documentation
https://momentjs.com/docs/#/use-it/browserify/

phpunit composer install dependicies via symlink

I am currently installing phpunit on a per project basis.
This works extremely well, however, the install takes time because I install the directories from cache each time. Is there a way to symlink the directories to one install, or some kind of clever trick I can use to make it do that.
If I do a path repository like this, with phpunit cloned and composer installed on my disk:
"require-dev": {
"phpunit/phpunit": "dev-master"
},
"repositories": [
{
"type": "path",
"url": "../phpunit",
"options": {
"symlink": true
}
}
],
This installs only links the phpunit/phpunit directory, and not the rest of the dependencies like:
"phpunit/php-code-coverage": "^5.2",
"phpunit/php-file-iterator": "^1.4",
"phpunit/php-text-template": "^1.2",etc
Composer can install packages globally and then you can refer to the files via it's path, or put the appropriate directory into your PATH variable (in ~/.composer/vendor/bin). Another, often more popular method is to download the phpunit.phar file, and use that to run PHPunit. This also has the advantage of being a single file that can also be committed to a project - though you will want to occasionally update it to the latest version (at least within the major version, 5.7+ or 6.1+). The .Phar file can also be installed globally on the server, in the same way that composer is suggested to.

VS Code SCSS auto compiling to CSS

I am total beginner in programming and just started to learn HTML/CSS.
For coding I started to use VS Code. And I really like it.
Only problem so far, what I got, is auto compiling of SCSS to CSS.
I have searched and read many solutions, and the best what I found was with ruby + sass + code in VS Code terminal sass --watch . It is watching my project and creating new CSS when new SCSS is created. And it is watching for changes in SCSS. But problem is that this code must be entered each time I am starting VS Code.
Tried also solution with Gulp file and package.json, but also could not make it start automatically. And it has to be made for each project separately.
I tried also Atom, and it has sass-autocompile package, and it works perfectly. So, simplest way for me would be to use Atom and forget. But I would like to use VS Code though.
So, generally question is if there would be possibility to create extension for VS Code to automate SCSS compilation to CSS (similar to Atom's package, which would be the best IMO). Or maybe somebody could explain me other way how to solve this problem.
You will need two things:
tasks.json file
Blade Runner extension for VS CODE
Start by creating .vscode folder in your project.
Then in it create tasks.json file with the following content:
{
"version": "0.1.0",
"command": "sass",
"isShellCommand": true,
"args": ["--watch", "."],
"showOutput": "always"
}
Now, after opening the project you can run the task by clicking Ctrl+Shift+B.
To automate the process use Blade Runner extension - https://marketplace.visualstudio.com/items?itemName=yukidoi.blade-runner
Blade Runner will run the task automatically after opening the project :)
A solution without additional extensions
With sass
Assuming you have sass installed globally with for instance:
npm install -g sass
Open the folder and create a task.json file under .vscode containing
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Watch Sass",
"type": "shell",
"command": "sass --watch src/style.sass styles/style.css --style=compressed",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
},
"runOptions": {
"runOn": "folderOpen"
}
}]
}
With node-sass
Replace sass with node-sass in the above.
In both cases make sure the source/destination filename, location and extension are correct (in my case src/style.scss and style/style.css)
With a Workspace file
Or copy the section in your .vscode-workspace file to avoid clutter of .json files.
Make sure to change the sass source and destination files to your personal needs.
Setup VSCode
[EDIT] whith the current version this is asked the first time you open the workspace file and the following steps are no longer needed.
To a llow automatic run tasks
Ctrl+Shift+P
select Manage automatic Tasks and
select Allow Automatic Tasks in Folder and
close and reopen your folder (or Workspace)
The sass compiler will be called and starts watching all your edits with a reassuring:
Compiled css\src\style.sass to css\style.css.
Sass is watching for changes. Press Ctrl-C to stop.
or with error messages when compilation failed.:
Error: semicolons aren't allowed in the indented syntax.
╷
7 │ padding: 0;
│ ^
╵
css\src\_base.sass 7:12 #import
css\src\style.sass 1:9 root stylesheet
Or use Easy Compile - it will auto compile on save.
https://marketplace.visualstudio.com/items?itemName=refgd.easy-compile
There already is an official document out there
https://code.visualstudio.com/docs/languages/css#_step-3-create-tasksjson
Only tip we can consider here is put an argument of --watch just not to build manually by hitting ctrl+shift+b every time.
// Sass configuration
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Sass Compile",
"type": "shell",
"command": "sass --watch styles.scss styles.css",
"group": "build"
}
]
}
Without any plugins, you can create .vscode folder in your project and just write some tasks.json
Easy Compile or Live SASS Compiler extensions for Visual Studio Code.
The Live SASS Compiler can recompile all sources, whereas Easy Compile just compiles a single file.
Easy Compile compiles when you save a file, whereas Live SASS Compiler can be made to watch your code and compile when it sees a change. You must manually start it every time, whereas Easy Compile runs out of the box.

Is it possible to update bower dependencies using grunt?

I’m using grunt to develop a website, and was wondering if it’s possible to update bower dependencies in my project within my 'build' grunt task—so that when I build a production version of my project, everything is up–to–date?
Obviously, I know I could just execute bower update before grunt build:prod, but it would be one less step every time.
Just curious!
Grunt has this task called grunt-bower-task which can help you manage bower dependencies. Use the official grunt documentation to go through the details.
Found one possible solution:
I guess I could use grunt-shell to automate running the bower update command in a shell…
Just wondering if this is the most logical/sophisticated way of doing it. Any other suggestions?
Found a good solution—I’ll post it in case it’s of use to someone…
The grunt-bower-install-simple plugin allows you to update bower dependencies from a grunt task by setting the command option to update like so:
"bower-install-simple": {
options: {
color: true,
directory: "src/bower_components"
}
"prod": {
options: {
command: update,
production: true
}
}

when to use gruntjs as JavaScript developer

First of all i have not really understood what gruntjs really does, but i have some idea. I am hoping that by seeing how and for what it is used i will come to see its purpose. So could anyone explain to me what is gruntjs, why it's used, and for what it's used.
Is it beneficial for indie developer or for team or both?
Is it only for big projects?
Is it just a trend/fad? And makes things uncomplicated for no reason?
So basically in short What are benefits of it and for whom?
Grunt is a task runner. That's all it does. If you're a Java guy, think Ant.
Developers use Grunt to automate the running of tasks, especially sequences of tasks. Some of those tasks are so prevalent, such as linting JavaScript, running unit tests, or minifying JavaScript, that they're packaged up as plugins and freely available for you to use. For example, grunt-contrib-clean is a plugin that contains a clean task. This task simply deletes the contents of a list of directories, a common step in a build process. To use it, you first pull the plugin into you Gruntfile.js using
grunt.loadNpmTasks('grunt-contrib-clean');
and then configure the clean task to clear your hypothetical minified directory using
grunt.initConfig({
clean: [ 'minified' ]
});
You can now run the clean task from the command line using
grunt clean
To visualise its potential, imagine a task that cleans a directory, then runs Jasmine unit tests using Karma, then lints and compiles LESS files, minifies and concatenates JS files, and packages them up for deployment, or outright deploys them.
So to answer your questions
it can be beneficial to anyone working on the project
the benefit is proportional to how many repetitive tasks you have to deal with
it's a tool, not a trend/fad, and it simplifies processes, not overcomplicates them
want to do pre required task before running project then grunt is best.
Means task required for running project
In grunt we can use add task .Common task like
browserify : In our project we crate multiple file by name convention for better understanding. But when we want to run the project you have to include all files rather than you can just combine all file in one file at the time for deploy project it will reduces your server time to include all files
just like include in Gruntfile.js
uglify:{
app: {
src: ['app/**/*.js'],//include all js file in app folder
dest: 'public/site.js'//one file
}
}
grunt.loadNpmTasks('grunt-browserify');//use npm task for browserify
Register Task : In project we use diff environment.If you want manage what thing to be run before the deploy by each environment.
Just like task required for only Env Dev watch task
grunt.loadNpmTasks('grunt-contrib-watch');
For this you register task dev
env: {
development: { NODE_ENV: 'development' },
staging: { NODE_ENV: 'staging' },
production: { NODE_ENV: 'production' }
}
grunt.registerTask('dev',['env:development','watch']);//list of task required only for dev env
grunt.registerTask('production',['env:production']);

Resources