Search code examples
javapowershellwebbundler

Coding a small file bundler for HTML/JS/CSS


So my project has a lot of small "widget" like components with their own CSS and JS. What I need is a sort of automatism to "bundle" up the css and js of all the components into a single file with a press of a button (or cmd expression).

Something like this: bundleMeFiles.exe -header -footer -widget1 or if all: bundleMeFiles.exe -all.

I know there are tools and a lot of stuff that does that in a great scale but I'm also a DIY guy so I like doing my own stuff because it makes me improve.

My first guess is Powershell or even Java to build a fancy UI but any tips would be welcome.


Solution

  • You can do that in java by using the commons-io library

    public static void main(String[] args) throws IOException {
        File result = new File("C:/temp/result.txt");
        OutputStream os = new FileOutputStream(result);
        Collection<File> files = FileUtils.listFiles(
                new File("C:/temp/dir"),
                new RegexFileFilter("^(.*?)"),
                DirectoryFileFilter.DIRECTORY
        );
        for(File f: files){
            //path to file surrounded by ===
            IOUtils.writeLines(
                    Arrays.asList("===", f.getAbsolutePath(), "==="),
                    "\r\n",
                    os,
                    Charset.defaultCharset());
            //file contents
            IOUtils.copy(new FileInputStream(f), os);
        }
    }