Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>UPDATE</strong>: Please check the short version of this script below if you are in a hurry.</p> <p>A simple Bash script does the Job for ya:</p> <pre><code>#!/bin/bash for FILE in `find . -name "*.js" -type f -o -path './node_modules' -prune -o -path './components' -prune` do if [ -e $FILE ] ; then COFFEE=${FILE//.js/.coffee} echo "converting ${FILE} to ${COFFEE}" js2coffee "$FILE" &gt; "$COFFEE" else echo "File: {$1} does not exist!" fi done </code></pre> <p>make a file, for example all2coffee, put it in /usr/local/bin, add an <code>chmod + x</code> flag it in terminal </p> <p><strong>REQUIREMENTS</strong></p> <p><code>js2coffee</code> installed globally, if not yet instaleld do: <code>npm install -g js2coffee</code></p> <p><strong>SCRIPT EXPLAINED</strong></p> <p><strong>for loop:</strong></p> <p><code>for FILE in `find arguments` ....</code> means:</p> <p>find output is assigned to <code>FILE</code> string every time <code>find</code> stumbles upon a .js file</p> <p><strong>find parameters:</strong></p> <p><code>-name "*.js"</code> grab all files with .js ending</p> <p><code>-type f</code> must be of type <strong>file</strong> since we don't want .js dir's but file's only</p> <p><code>-o -path './node_modules' -prune</code> </p> <p>excludes files in dir's <strong><code>./node_modules</code></strong> adding <strong><code>-prune</code></strong> is crucial, otherwise find will descend into the dir and print <strong><code>*.js</code></strong> files found in the directory</p> <p><strong>do block:</strong> </p> <p><code>if [ -e ${FILE} ] ; then</code> </p> <p><strong>-e</strong> flag checks if the string from <strong><code>FILE</code></strong> is a existing file on the filesystem, otherwise <code>else</code> is executed.</p> <p><strong>string manipulation:</strong></p> <p><code>COFFEE=${FILE//.js/.coffee}</code> </p> <p>we assing thte <code>COFFEE</code> variable a string where we replace <strong>.js</strong> with <strong>.coffee</strong> through the bash string manipulation: <code>${STRING//match_this/replace_with}</code></p> <p><strong>conversion:</strong></p> <p><code>js2coffee "$FILE" &gt; "$COFFEE"</code> we feed js2coffee with <code>FILE</code> and <code>COFFEE</code> as <strong>strings</strong> </p> <p><strong>EXTRA:</strong></p> <p>You like to move all of your converted .coffee files to a new directory, but keep the structure? </p> <p>Use <strong>find</strong> with <strong><code>rsync</code></strong> in Linux or <strong><code>ditto</code></strong> on Os X since cp won't create directories needed by this command. Here a little script to execute in the main dir that will do the job </p> <p>all .coffee files will in the /coffee dir copying the .js files hierarchy</p> <pre><code>for FILE in `find . -name "*.coffee"` do ditto .${FILE/./} coffee${FILE/./} done </code></pre> <p><em>execute this <strong>after</strong> you converted your files to .coffee</em></p> <p><strong>UPDATE</strong></p> <p>you can swap <code>ditto</code> or <code>rsync</code> with <code>mv</code> after the first run to move the files since <code>mv</code> like <code>cp</code> does not create sub dirs.</p> <p><strong>UPDATE 2</strong></p> <p>added an one liner for those on time, see my second answer below!</p> <p><strong>UPDATE 3</strong></p> <p>added an option to exclude <code>./node_modules</code> directory from conversion, for those who don't want to convert their dependencies</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload