Jump to content

User:Lupin/jscat

From Wikipedia, the free encyclopedia

These simple scripts join javascript files together and split them up. I use them for popups.js.

jscat

[edit]
#!/bin/bash

# usage:
# jscat *.js	   cat js files for later uncatting
# jscat -l *.js	   cat js files, stripping out NOLITE chunks

if [ "$1" == "-l" ]; then
    striplite=1
    shift
fi

function docat() {
    if [ "$striplite" == "1" ]; then
	out=$(sed -e '/^\/\/<NOLITE> *$/,/^\/\/<\/NOLITE> *$/d' "$1")
    else 
	out=$(cat "$1")
    fi
    echo "$out"
    t=$(echo "$out" | tail -1);
    if [ -n "$t" ]; then 
	echo
    fi
}

for i in "$@"; do 
    out=$(docat "$i")
    if [ $(echo "$out" | grep -c '[^[:space:]]') -eq '0' ]; then 
	echo Empty "$i" >&2;
	continue;
    fi
    echo // STARTFILE: "$i"
    echo "$out";
    echo // ENDFILE: "$i"
done

jsuncat

[edit]
#!/usr/bin/env perl

# usage: jsuncat < popupsdev.js

my $file='';
while (<>) {
  if ( m/^\/\/ STARTFILE: (.*)$/ ) {
    $file=$1;
    die "File $file exists; aborting." if -e "$file";
    print "Starting $file...";
    open(FILE, ">$file");
  } elsif ( m/^\/\/ ENDFILE: $file/ ) {
    close(FILE);
    print "done.\n";
    $file='';
  } else {
    print FILE if "$file" =~ /./;
  }
}

if ( "$file" =~ /./ ) {
  warn "\nWARNING: unclosed file $file\n";
  close(FILE);
}