node.js - Creating an NPM package CLI - Issues with Path and Running Locally -


  1. ive created cli npm package. having issue determining users project path. package in node_modules, how find path of users project should 1 level higher node_modules?

  2. i want run cli using "scratch" command in local project folder installed, how can this?

package.json

{   "name": "scratch-cli",   "version": "0.0.4",   "description": "scratch cli",   "main": "index.js",   "bin": {     "scratch": "index.js"   },   "scripts": {     "test": ""   },   "author": {     "name": "kay khan",     "email": "kaykhaninbox@gmail.com"   },   "repository": {     "type": "git",     "url": "git@github.com/kaykhan/scratch-cli.git"   },   "license": "mit",   "dependencies": {     "commander": "^2.11.0",     "inquirer": "^3.2.3"   } } 

index.js

#!/usr/bin/env node 'use strict';  const program    = require('commander'); const exec       = require('child_process').exec; const fs         = require('fs'); const controller = require('./templates/controller.template'); const path       = require('path'); const dir        = path.dirname(require.main.filename); let p            = require('./config.json');  console.log(dir);  program.version('0.0.1')        .description('scratch command line interface (cli)');  program.option('-p, --path', 'the environment path');  program.command('set:path <path>')        .description('set project path <path>')        .action(function (path) {            p.path = path;            fs.writefilesync('./config.json', json.stringify(p));        });  program.command('make:controller <name>')        .description('add new controller called <name>')        .action(function (name) {            const file    = p.path + name + ".js";            const command = "touch " + file;            exec(command, function (error, stdout, stderr) {                const contents = controller.generate(name);                fs.writefile(file, contents, function (err) {                    if (err) console.error(err);                })            });        });  program.parse(process.argv);  if (program.path) console.log('path: ' + p.path); 


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -