Saturday, October 21, 2017

Run Local Node.JS Dependancies

From Command Line


Install dependency locally

npm install live-server

Create folder public with index.html and file called run-server.js. Here's the structure:

node_modules/
public/
   index.html
run-server.js

Content of run-server.js:

var liveServer = require("live-server");

var params = {
 port: 8181, // Set the server port. Defaults to 8080.
 host: "0.0.0.0", // Set the address to bind to
 root: "./public", // Set root directory that's being served. Defaults to cwd.
 open: true, // When false, it won't load your browser by default.
 logLevel: 2, // 0 = errors only, 1 = some, 2 = lots
};

liveServer.start(params);

in console run live-server

node run-server.js
or on Linux
node node_modules/.bin/live-server --open=public/

Using package.json

in console
npm init
npm install -D live-server
npm install --save cowsay

this will generate file package.json:
{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "cowsay -b mooooooooo >> index.html && live-server --port=8085"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "live-server": "^1.2.0"
  },
  "dependencies": {
    "cowsay": "^1.2.1"
  }
}

to create index.html and run server
npm test

if you want to do clone a git repo and run in in live-server replace yellow line with:

"test": "git clone https://github.com/nikola-bodrozic/Bootstrap-Boilerplate-v3.3.7.git tb337 && live-server --watch=tb337 --open=tb337 --ignore=tb337/.git"

this will watch for file changes in tb337 folder but not in tb337/.git folder

another example
{
  "scripts": {
    "less-gen": "lessc -l public/css/style.less && lessc public/css/style.less public/css/style.css && exit 0",
    "lint-css": "csslint --ignore=ids,order-alphabetical  public/css && exit 0",
    "lint-js": "eslint public/js && exit 0",
    "test": "node run-w3cvalid.js && npm run less-gen && npm run lint-css && npm run lint-js && echo \"Tests finished\" && exit 0",
    "start": "node run-server.js"
  },
  "dependencies": {
    "csslint": "^1.0.5",
    "html-validator": "^2.2.3",
    "less": "^3.0.0-alpha.3",
    "live-server": "^1.2.0"
  },
  "devDependencies": {
    "eslint": "^4.10.0"
  }
}
Inside script.test and script.start we can run js files.

Friday, October 13, 2017

Usefull Shell Commands

Pack folder and backup database

Backup folder. Run as sudo, other users will change the original permissions
tar -zcf wp03.tar.gz wp03/
Backup database
mysqldump -u root -p -C albert > albert.sql.tgz

Create user with home folder, shell & password

sudo useradd -m -d /home/mike -s /bin/bash -c "Mike" -U mike
sudo passwd mike
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

Numeric permissions in console

$ ls -la | awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}'
775 drwxrwxr-x  9 3173 3173   4096 Oct 12 20:01 .
755 drwxr-xr-x  4 3173 3173   4096 Oct 12 21:54 ..
755 drwxr-xr-x  6 3173 3173   4096 Oct 12 19:08 app
755 drwxr-xr-x  2 3173 3173   4096 Oct  9 15:46 bin
644 -rw-r--r--  1 3173 3173   2111 Mar 11  2017 composer.json
664 -rw-rw-r--  1 3173 3173 102342 Oct 12 19:08 composer.lock

Search file names and file content

find file that begins with example case insensitive search
find . -iname "example*" -print

finds string `getcol` in js files in current folder and it`s subfolders, print file name, line number and the line
find . -type f -name "*.js" -exec grep -in --with-filename getcol {} \;

Set permissions for files and folders

sudo find /var/www -type d -exec chmod 775 {} \;  # rwxrwxr-x
sudo find /var/www -type f -exec chmod 664 {} \;  # rw-rw-r--

Saturday, October 7, 2017

Symfony 3 run console commands from controller

On shared hosting you don't have access to command line so you can use following script to clear cache:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

// Import the required classed
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;


class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction()
    {
        $kernel = $this->get('kernel');
        $application = new Application($kernel);
        $application->setAutoExit(false);
       
        /*
        // php bin/console doctrine:generate:entities AppBundle:Task
        $input = new ArrayInput(array(
            'command' => 'doctrine:generate:entities',
            'name' => 'AppBundle:Task'
        ));
        */
       
        // php bin/console cache:clear --env=prod --no-warmup
        $input = new ArrayInput(array(
            'command' => 'cache:clear',
            '--env' => 'prod',
            '--no-warmup' => null
        ));
       
        $output = new BufferedOutput();

        $application->run($input, $output);

        // return the output
        $content = $output->fetch();

        // Send the output of the console command as response
        return new Response($content);
    }
}