Hi, in this post, we'll create trash program by
JS,
after create it, this program will be npm package.
Pre-requisites
before we move on, you'll need :
Let's go
We're going to create package.json
The first command is npm init
$ npm init
I'll name it manx
so you should have like this ...
{
"name": "@your_npm_user_name/your_proj_name",
"version": "1.0.0",
"description": "Cli app can move files/folders to the trash without any dangerous",
"main": "cli.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://Git_Repos_Site/Your_Git_Repo"
},
"keywords": [
"cli-app",
"cli",
"trash"
],
"author": "Your_Name",
"license": "ISC"
}
We don't want to have package-lock.json, so type
$ touch .npmrc
in
.npmrc
package-lock=false
now we'll install @abdfnx/hac_k & trash
$ npm i @abdfnx/hac_k trash
ok, let's create cli.js
in
cli.js
#!/usr/bin/env node
"use strict";
now we're going to require the packages
#!/usr/bin/env node
"use strict";
const hac_k = require("@abdfnx/hac_k");
const manx = require("trash");
nice, also create two variables
// Ignore all flags of `rm` program.
const ignoredFlags = ["r", "f", "i", "d", "P", "R", "v", "W"];
const ignoredFlagsConfig = {};
this variables're very important, so we'll make for loop
for (const flag of ignoredFlags) {
ignoredFlagsConfig[flag] = {
type: "boolean",
};
}
the most importnant variable is cli
const cli = hac_k(
`
Usage
$ manx <file/folder> […]
Examples
# file
$ manx xcode.tsx layout.tsx edge.tsx
$ manx '*.tsx' '!xcode.tsx'
# folder
$ manx app
`,
{
flags: {
...ignoredFlagsConfig,
},
}
);
But what if the user entered a space, we need if statement
if (cli.input.length === 0) {
console.error("Specify at least one path");
process.exit(1);
}
at the end add
manx(cli.input);
the final result of file
#!/usr/bin/env node
"use strict";
const hac_k = require("@abdfnx/hac_k");
const manx = require("trash");
// Ignore all flags of `rm` program.
const ignoredFlags = ["r", "f", "i", "d", "P", "R", "v", "W"];
const ignoredFlagsConfig = {};
for (const flag of ignoredFlags) {
ignoredFlagsConfig[flag] = {
type: "boolean",
};
}
const cli = hac_k(
`
Usage
$ manx <file/folder> […]
Examples
# file
$ manx xcode.tsx layout.tsx edge.tsx
$ manx '*.tsx' '!xcode.tsx'
# folder
$ manx app
`,
{
flags: {
...ignoredFlagsConfig,
},
}
);
if (cli.input.length === 0) {
console.error("Specify at least one path");
process.exit(1);
}
manx(cli.input);
you're good, now let's test it, go to package.json and add bin, engines, files props
{
"name": "@your_npm_user_name/your_proj_name",
"version": "1.0.0",
"description": "Cli app can move files/folders to the trash without any dangerous",
"main": "cli.js",
"bin": {
"manx": "cli.js"
},
"engines": {
"node": ">=10"
},
"files": [
"cli.js"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://Git_Repos_Site/Your_Git_Repo"
},
"keywords": [
"cli-app",
"cli",
"trash"
],
"author": "Your_Name",
"license": "ISC",
"dependencies": {
"@abdfnx/hac_k": "^1.0.2",
"trash": "^7.0.0"
}
}
type npm link
$ npm link
add file and folder for test
$ touch test_file && mkdir test_folder
Now the awaited moment, in the terminal
$ manx --help
Usage
$ manx <file/folder> […]
Examples
# file
$ manx xcode.tsx layout.tsx edge.tsx
$ manx '*.tsx' '!xcode.tsx'
# folder
$ manx app
$ manx test_file test_folder
Congratulations, you now have a great program...
npm publish (optional)
if you want to publish your awesome project to npm, follow me
before publish, add some files
.editorconfig, .gitattributes, .gitignore and .travis.yml
it's optional, but it's better to create it
in .editorconfig
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.yml]
indent_style = space
indent_size = 2
.gitattributes
* text=auto eol=lf
.gitignore
node_modules
yarn.lock
.travis.yml
language: node_js
node_js:
- '14'
- '12'
- '10'
ok, type
$ npm unlink
and now we must login to npm
$ npm login
publish
$ npm publish --access=public
and if you want to install it, you should install it globally
$ npm i -g YOUR_PKG
you can see your package in npm
visit https://www.npmjs.com/package/YOUR_PKG
Here you are, you have trash program and npm package...
enjoy, and see you next time.
Top comments (0)