I was annoyed to manually start new projects each time so I made a Node CLI that automate the process. I'm really not comfortable yet with node, so I wanted some advice. It works but I don't know if I did it the node way or not.

Basically the code under here is my whole application. The file structure is like so:

- app.js
- seed 

It's not really complicated. What I do in the app is copy the seed and change the project name in package.json. The seed is a template project, with nothing in it beside a starter-repo. I could have upload it to git but this is more fun.

#!/usr/bin/env node

const spawn = require('child_process').spawn;
const fs = require("fs");
const path = require('path');
const through2 = require('through2');
const copydir = require('copy-dir');

const name = process.argv[2] || "unnamed project";
const projPath = path.join(process.cwd(), name);
// path to my seed project
const seed = path.join(__dirname, "seed");

if (!fs.existsSync(name)) {
} else {
    throw new Error(`directory ${name} already exist`);

copydir(seed, path.join(process.cwd(), name), function(err) {
    if (err) {
    const package = path.join(projPath, "package.json");
    // in my seed project package.json, I have name: "-your-app-name-here-"
    replaceInFile(package, "-your-app-name-here-", name, onCopyDone);

function onCopyDone(err) {
    if (!err)

function replaceInFile(someFile, rep, by, cb) {
    fs.readFile(someFile, 'utf8', function(err, data) {
        if (err) {
            return console.log(err);
        let result = data.replace(rep, by);

        fs.writeFile(someFile, result, 'utf8', cb);

function install(where) {
    console.log('installing dependencies...')
    const i = spawn('npm', ['install', '--prefix', where]);

    i.stdout.on('data', (data) => {
        console.log(`stdout: ${data}`);

Primarily what I'd like to know is how I can do it more the Node way, if that makes sense.

  • \$\begingroup\$ Do you use ECMAScript 5 or 6 for Node? \$\endgroup\$ – Phrancis Mar 10 '17 at 2:18
  • \$\begingroup\$ @Phrancis well, the first couple of things I tried to do I used typescript. Then when trying in pure js I had trouble using imports, so I decided to use the require. To answer you question ES6. I assume you asked that because of my use of require with const. \$\endgroup\$ – Ced Mar 10 '17 at 2:29
  • \$\begingroup\$ Yup, I added the ES6 tag. That will influence the type of answer you're likely to get, since if you can use new ES6 things then there is usually more that can be improved. :-) \$\endgroup\$ – Phrancis Mar 10 '17 at 3:04
  • 3
    \$\begingroup\$ You should check out yeoman.io \$\endgroup\$ – konijn Mar 10 '17 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.