{"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<p>Some <a href=\"https:\/\/www.npmjs.com\/\">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 <a href=\"https:\/\/www.npmjs.com\/package\/csso-cli\">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<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">The initial situation and the problem<\/h2>\n\n\n\n<p>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, <a href=\"https:\/\/www.npmjs.com\/package\/minifier\">Minifier<\/a>), but I would like to use csso-cli, to keep the source maps.<\/p>\n\n\n\n<p>Of course, I could simply run one csso-cli call for each file, but there are various issues:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><em>Don\u2019t repeat yourself<\/em>.<\/li><li>If another CSS file is added which should be minified, I would need to modify the build script.<\/li><\/ol>\n\n\n\n<p>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<h2 class=\"wp-block-heading\">My solution<\/h2>\n\n\n\n<p>The solution is based on the <code class=\"lang-bash\"><a href=\"https:\/\/help.ubuntu.com\/community\/find\">find<\/a><\/code> command from Linux. I came closer to the solution via different sites \u2013 I found the decisive answer on <a href=\"https:\/\/askubuntu.com\/a\/35994\">askubuntu. com<\/a>. And that is it (without a few csso-cli options which are not relevant here):<\/p>\n\n\n<pre class=\"wp-block-code lang-bash\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">find assets\/css\/ -iname <span class=\"hljs-string\">'*.css'<\/span> -and -not -iname <span class=\"hljs-string\">'*.min.css'<\/span> -exec sh -c <span class=\"hljs-string\">'node_modules\/csso-cli\/bin\/csso {} --output ${0%.css}.min.css'<\/span> {} \\;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>We take all <code class=\"lang-markup\">.css<\/code> files from <code class=\"lang-markup\">assets\/css<\/code> without files ending with <code class=\"lang-markup\">.min.css<\/code>. Afterwards, we execute a shell script for each of the found files, namely the csso-cli. With <code class=\"lang-bash\">{}<\/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<p>We use <code class=\"lang-bash\">${0%.css}<\/code> for the <code class=\"lang-bash\">--output<\/code> parameter to get the file\u2019s name and path without the file ending and add <code class=\"lang-bash\">.min.css<\/code> (<code class=\"lang-bash\">${0}<\/code> lets us access <code class=\"lang-bash\">{}<\/code>, the first <em>positional parameter<\/em> of the shell script, which contains the file name and path again). The end of the script is marked by <code class=\"lang-bash\">\\;<\/code>.<\/p>\n\n\n\n<p>If we use this line as a node script, we will get the following error:<\/p>\n\n\n<pre class=\"wp-block-code lang-bash\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">npm ERR! Failed to parse json\nnpm ERR! Unexpected token <span class=\"hljs-string\">';'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To solve that, we change the ending to <code class=\"lang-bash\">\\\\;<\/code>. This is how the script looks in the <code class=\"lang-js\">\"scripts\":<\/code> part of my <code class=\"lang-markup\">package.json<\/code> (including all used options now):<\/p>\n\n\n<pre class=\"wp-block-code Lang-js\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-string\">\"minify-css\"<\/span>: <span class=\"hljs-string\">\"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><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Via <code class=\"lang-bash\">npm run minify-css<\/code> I now can minify all CSS files from <code class=\"lang-markup\">assets\/css<\/code> without processing <code class=\"lang-bash\">.min.css<\/code> files. Of course, you can change the script to use it with over NPM packages.<\/p>\n\n\n\n<p>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":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-4209","post","type-post","status-publish","format-standard","hentry","category-web-development"],"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","targetHints":{"allow":["GET"]}}],"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}]}}