1.0.4 • Published 2 years ago
chamick7say v1.0.4
Basic Node Package 101
install Node.js
- install nvm for node.js version management
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.6/install.sh | bashexport NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm- install node.js for specific version
nvm install lts/iron- check node version
node -vcreate node package
- create project folder
mkdir <name>say
cd <name>say- init node package (package.json will be generated)
npm init -y- create file
.npmrcto config npm registry to default (incase default registry is other)
registry=https://registry.npmjs.org- install
yargslib for cli
npm install yargs- create bin folder and file for cli execution
mkdir bin & touch bin/say.js- edit file
bin/say.js
#! /usr/bin/env node
const fs = require("fs");
const yargs = require("yargs");
function readCharacter() {
return fs.readFileSync(`${__dirname}/../character/bear.ascii`, "utf-8"); //read character text file
}
function say(word) {
const character = readCharacter();
const output = character
.replace("$text", word) //replace $text with provided word
.replaceAll("$bubble", "-".repeat(word.length * 1.2)); // replace $bubble with - to create bubble
console.log(output); // print output to console
}
const command = yargs.argv.$0; // read command that use in command line
const args = yargs.argv._.join(" "); // join arguments in command
if (!args) {
return console.log(`Usage: ${command} text`);
}
say(args);- find
ascii artthat you like
- create character in text file ex.
bear.asciiin foldercharacter
▄▀▀▀▄▄▄▄▄▄▄▀▀▀▄ $bubble
█▒▒░░░░░░░░░▒▒█ < $text
█░░█░░░░░█░░█ / $bubble
▄▄ █░░░▀█▀░░░█ ▄▄
█░░█ ▀▄░░░░░░░▄▀ █░░█- config add field
binandfilesinpackage.json
//package.json
"bin": {
"<project-name>": "bin/say.js"
},
"files": [
"bin/",
"character/"
],- login to npm registry type in terminal
npm login- publish node package to npm registry
npm publish- try your node package by typing in terminal
npx <your-project-name> hello