Developer Automation With NPX and Commander

Developer Automation With NPX and Commander thumbnail

The more I be taught about JavaScript model, the more I’m exposed to JavaScript originate and config instruments. The instruments equivalent to babel and webpack, frustrate me. The config instruments, the magic tool of enjoyment that objects up all of your originate instruments for you thru magic pixie-dust or something so that you just would perhaps perhaps presumably well presumably also gorgeous write code, these fascinate me.

I desire we had more for WordPress and ones that did issues precisely the scheme I maintain they needs to be performed. I if truth be told disfavor the spend of templating languages for templating code. That’s what’s stopped me from constructing my own scaffolding instruments earlier than. I continually originate a plugin that did issues the scheme I wished my template to enact issues, then copied the plugin into the templates directory of my generated and then manually added substitution strings. Debugging modified into once a priority.

My unusual goal is to defend a plugin that has a reference case for every order I’d desire a plugin to enact — composer.json, Gutenberg blocks, PHP train injection container, and many others. — and a scaffolding tool that can perhaps presumably replica from that plugin.

I determined to write it in Node so as that I could presumably well without issues portion it thru npm, and be succesful of traipse it within the community, in a Docker container or even in a Serverless app. It modified into once a objective correct excuse to be taught more about Node’s file system module and npx.

Getting Gay With NPX

NPX is a tool that allows you to traipse Node applications from anywhere without installing them first. It’s perfect for utility scaffolding instruments that you just objective every so frequently spend. In online page online of conserving a replica installed within the community, you traipse the version on npm. As an illustration, to make a react app, that you just would perhaps install make-react-app globally on your computer, otherwise you would perhaps perhaps presumably well presumably also spend NPX make-react-app to create the identical order and by no methodology anxiety about conserving it up to this level or eating up storage dwelling.

While you’re no longer accustomed to NPX, which is integrated with npm, I suggest reading the introductory publish about NPX.

Within the introductory publish, Kat shows the approach to enact JavaScript saved in a Github gist. As she notes, here is some distance away code execution, which is potentially extremely unsafe. So the first step for working this script is, be taught the source of the code you’re about to enact, step two is take whenever you bear to bear to enact it and step three is to motive some distance away code execution.

Let’s enact that. First, spin to and survey at the script. Final time I looked it gorgeous precipitated a console message to be logged. Guarantee it hasn’t modified. If so it hasn’t:

Take into memoir the code on Gist.

That will motive that message to log.

Running the kit with a local course. So here is icy, we can create a script saved as a gist for our automation. However the skills of atmosphere up goes to be contemptible. If we bear to push and pull code from the gist, then test, that will likely be sluggish. I’d somewhat test the code within the community.

Fine, I could presumably well traipse the code abruptly with node, ie. change to the native directory and traipse node index.js or whatever. What I if truth be told desire is to make spend of the model version in context. What if I could presumably well spend npx ~/npx-is-icy from anywhere on my computer to make spend of the native model version of npx-is-icy?

Looks npx can spend a local file course, so as that is likely. Let’s stroll thru how this could occasionally likely perhaps presumably indicate you some basics of how npx works.

First, download a replica of that gist. Build it in your predominant user directory and exchange the name of the directory to “npx-is-icy”. Then initiate up the index.js and to boot you would perhaps perhaps presumably even see:

Take into memoir the code on Gist.

Right here’s a soft long-established script, exchange it’s output to something more fun savor “I’m the Batman” so that you just would perhaps perhaps presumably well presumably also level to your native version is being feeble and on memoir of that it’s good to be the Batman.

Take into memoir the code on Gist.

Now you would perhaps perhaps presumably well presumably also traipse this checklist from anywhere on your computer to make spend of your individual npx-is-icy

Take into memoir the code on Gist.

Now we bear a protracted-established mannequin for atmosphere up the tool. Gain the npx kit within the community, the spend of and debugging it in context. That final portion is indispensable, I’m making a model tool, I bear to make spend of it on the instruments I’m atmosphere up to see if it works or no longer and once it does so I even bear its capabilities on hand to me objective away after they work.

Fine, I even bear to push my changes to some distance away version management and get the kit on hand thru npm. But, after I’m atmosphere up, I enact no longer bear to assume managing releases.

Enter The Commander

Record that outputs gorgeous “Hi there Roy”

Take into memoir the code on Gist.

Developer Automation With Commander

The spend of Node To Work With Native And Far away Files

For something perfect, I created a checklist that copies the 2 recordsdata that copies the 2 PHP recordsdata I spend to load resources for a React app in a WordPress plugin and exchange there namespace.

To create this we’re going so that you just can want 4 issues:

  1. A characteristic to download a file from a miles away URL to the native.
  2. A characteristic that makes spend of the principle characteristic and then changes the namespace within the resulting file.
  3. A characteristic that calls these capabilities will the objective argument
  4. A checklist to call that characteristic.

Let’s buckle down and do the checklist. I enact no longer bear to write a stout tutorial on the node file system, however here’s your wreck course.

In Node, we spend the filesystem — be taught and write recordsdata for example — the spend of “fs”. As an illustration, to test if a file exists:

var fs = require('fs');
fs.existsSync(__dirname + '/hiRoy.txt' );

The fs module is soft readable. This characteristic is called “existsSync”. It assessments — synchronously — if a file exists. We are able to write a file with fs, synchronously the spend of fs.writeSync().

While you’re no longer feeble to require(). It brings the worth of the module.exports from the file you specify into scope. As an illustration, whenever you bear got the file “foo.js” and internal you bear got module.exports = characteristic foo(){}; Then whenever you happen to spend const foo = require( ‘./foo’ ); from a file within the identical directory that characteristic is now in saved within the const foo. We are able to leave off the file course — require( ‘react’ ) — to get right to use a module within the node_modules directory, in this case, the export of node_modules/react/index.js

Right here’s a module to handle downloading a file thru https — the spend of the https module — and writing it to file the spend of the fs module:

Take into memoir the code on Gist.

Look for that on the final line — module.exports = download. The download is a reference to the characteristic, so after we require this file, that characteristic is usable. That’s how we spend it in our next module.

Right here is the module to download a file and exchange its PHP namespace.

const download = require( './download' );
const substitute = require( 'substitute-in-recordsdata' );
const course = require( 'course' );

 * Get a file and exchange its namespace
 * @param {string} file Far away file to replica
 * @param {string } destPath Direction to write file to
 * @param {string} nameSpace Namespace to make spend of for unusual file
characteristic downloadPhpAndNameSpace(file,destPath,nameSpace){
	download(file,destPath, () => {
			from: " Website\WordPressPlugin/g",
			to: nameSpace,
			recordsdata: [destPath]
module.exports = downloadPhpAndNameSpace;

While you’re smooth getting feeble to required() survey at the adaptation between the primary for download and for substitute-in-recordsdata. The principle begins with a file course ‘./’, so node looks for the file. The other does no longer, so it looks in node_modules.

None of these capabilities so work with the negate recordsdata we desire to work with. They work with any recordsdata, which is fine, these are general needs. But let’s initiate up solving the concern at hand. We now bear to download two recordsdata and re-namespace them. With these two capabilities, we gorgeous want every other to location up the paths of the place the recordsdata spin.

Increasing The Record

Now it’s time to wrap all of this up in a checklist so we can form npx caldera-frail client-resources ~/my-plugin Vendor/Equipment and get our scripts copied to ~/my-plugin with the namespace Vendor/Equipment.

Right here’s a sub-checklist of our kit. I savor this sample vs having gorgeous one checklist with a good deal of alternatives. This could occasionally perhaps presumably get it more straightforward to add extra instructions and alternatives later.

Take into memoir the code on Gist.

Within the checklist() characteristic, that phrases in sq. brackets change into variables handed to the characteristic “action”. The characteristic action is lag to a easy callback that calls the characteristic created within the final step.

Now You Purchase Record

I’m taking half in studying more regarding the scheme of JavaScript that I enact no longer in most cases get to work with as a consequence of only the spend of JavaScript for entrance-discontinuance dev. Studying how Node modules are structured helped me heed more about what webpack is abstracting. I also learned more regarding the file system.

More importantly, I’ve been engaged on my npx app that ought to place me and my crew time. While you watched a tool savor this could occasionally likely perhaps presumably place your crew time, fork it and get your individual. At me on Twitter @josh412 or leave a observation with what you are making.

John Smith

Josh is a WordPress developer and educator. He’s the founding father of A Website, makers of righteous WordPress instruments including Caldera Kinds — a spin and descend, responsive WordPress create builder.

The publish Developer Automation With NPX and Commander appeared first on Torque.

Download the File

Download (12.5 MB)
Alternative Backup Mirror

Click here to download the archive from our server

Leave a Reply

Your email address will not be published. Required fields are marked *