{"id":4209,"date":"2017-08-31T16:30:29","date_gmt":"2017-08-31T14:30:29","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=4209"},"modified":"2020-02-09T10:59:45","modified_gmt":"2020-02-09T09:59:45","slug":"run-npm-package-for-multiple-files","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/run-npm-package-for-multiple-files-4209\/","title":{"rendered":"Run NPM package for multiple files (using the example of csso-cli)"},"content":{"rendered":"\n

Some NPM<\/a> packages already offer the possibility to be applied to several files of a directory at the same time \u2013 for instance, node-sass can convert a whole directory of SCSS files into CSS files. The minifier tool csso-cli<\/a> does not offer an option to minify multiple files at once \u2013 here I show a workaround to do this anyway.<\/p>\n\n\n\n\n\n\n\n

The initial situation and the problem<\/h2>\n\n\n\n

First of all, the reason why I need this. Since a few days, I try to use NPM scripts instead of Gulp for the build process. For a WordPress theme, I create the main CSS file from multiple SCSS files and generate an RTL version of the styles. Afterwards, a minified version of both files should be created. There are minification tools, which can handle a directory as input (for example, Minifier<\/a>), but I would like to use csso-cli, to keep the source maps.<\/p>\n\n\n\n

Of course, I could simply run one csso-cli call for each file, but there are various issues:<\/p>\n\n\n\n

  1. Don\u2019t repeat yourself<\/em>.<\/li>
  2. If another CSS file is added which should be minified, I would need to modify the build script.<\/li><\/ol>\n\n\n\n

    So this would not be the best solution, and that was the reason I searched for a way to run csso-cli (or every NPM package or other commands) automatically for multiple files.<\/p>\n\n\n\n

    My solution<\/h2>\n\n\n\n

    The solution is based on the find<\/a><\/code> command from Linux. I came closer to the solution via different sites \u2013 I found the decisive answer on askubuntu. com<\/a>. And that is it (without a few csso-cli options which are not relevant here):<\/p>\n\n\n

    find assets\/css\/ -iname '*.css'<\/span> -and -not -iname '*.min.css'<\/span> -exec sh -c 'node_modules\/csso-cli\/bin\/csso {} --output ${0%.css}.min.css'<\/span> {} \\;<\/code><\/div>Code language:<\/span> JavaScript<\/span> (<\/span>javascript<\/span>)<\/span><\/small><\/pre>\n\n\n

    We take all .css<\/code> files from assets\/css<\/code> without files ending with .min.css<\/code>. Afterwards, we execute a shell script for each of the found files, namely the csso-cli. With {}<\/code> we get the file name with the path of the current CSS file \u2013 exactly what csso-cli needs as the first option.<\/p>\n\n\n\n

    We use ${0%.css}<\/code> for the --output<\/code> parameter to get the file\u2019s name and path without the file ending and add .min.css<\/code> (${0}<\/code> lets us access {}<\/code>, the first positional parameter<\/em> of the shell script, which contains the file name and path again). The end of the script is marked by \\;<\/code>.<\/p>\n\n\n\n

    If we use this line as a node script, we will get the following error:<\/p>\n\n\n

    npm ERR! Failed to parse json\nnpm ERR! Unexpected token ';'<\/span><\/code><\/div>Code language:<\/span> JavaScript<\/span> (<\/span>javascript<\/span>)<\/span><\/small><\/pre>\n\n\n

    To solve that, we change the ending to \\\\;<\/code>. This is how the script looks in the \"scripts\":<\/code> part of my package.json<\/code> (including all used options now):<\/p>\n\n\n

    \"minify-css\"<\/span>: \"find assets\/css\/ -iname '*.css' -and -not -iname '*.min.css' -exec sh -c 'node_modules\/csso-cli\/bin\/csso {} --input-map auto --map inline --output ${0%.css}.min.css' {} \\\\;\"<\/span><\/code><\/div>Code language:<\/span> JavaScript<\/span> (<\/span>javascript<\/span>)<\/span><\/small><\/pre>\n\n\n

    Via npm run minify-css<\/code> I now can minify all CSS files from assets\/css<\/code> without processing .min.css<\/code> files. Of course, you can change the script to use it with over NPM packages.<\/p>\n\n\n\n

    If you have any suggestions on how this can be improved, feel free to write a comment (also if you have something else to say, of course ?)!<\/p>\n","protected":false},"excerpt":{"rendered":"

    Some NPM packages already offer the possibility to be applied to several files of a directory at the same time \u2013 for instance, node-sass can convert a whole directory of SCSS files into CSS files. The minifier tool csso-cli does not offer an option to minify multiple files at once \u2013 here I show a […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","lazy_load_responsive_images_disabled":false},"categories":[6],"tags":[],"wp-worthy-pixel":{"ignored":false,"public":"bec9992970644c80803b1942bf3b2ff7","server":"vg07.met.vgwort.de","url":"https:\/\/vg07.met.vgwort.de\/na\/bec9992970644c80803b1942bf3b2ff7"},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4209"}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=4209"}],"version-history":[{"count":2,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4209\/revisions"}],"predecessor-version":[{"id":5854,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4209\/revisions\/5854"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=4209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=4209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=4209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}