Build trash program by JS and make it npm pkg & publish it
Tags: javascript, npm, bash, node
Published: 2020-12-10 17:14:39

> Hi, in this post, we'll create trash program by `JS`,
after create it, this program will be [npm][npmUrl] package.

## Pre-requisites
before we move on, you'll need :

- [npm][npmUrl] account.

- [npm][npmUrl], [node][nodeUrl] installed.

## Let's go

We're going to create `package.json` 

The first command is `npm init`

```sh
$ npm init
```

I'll name it `manx`

so you should have like this ...

```json
{
  "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

```sh
$ touch .npmrc
```

> in `.npmrc`

```.npmrc
package-lock=false
```

> now we'll install [@abdfnx/hac_k][myPkg] & [trash][trashPkg]

```sh
$ npm i @abdfnx/hac_k trash
```

ok, let's create `cli.js`

> in `cli.js`

```js
#!/usr/bin/env node
"use strict";
```

now we're going to require the packages

```js
#!/usr/bin/env node
"use strict";

const hac_k = require("@abdfnx/hac_k");
const manx = require("trash");
```

nice, also create two variables

```js
// 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

```js
for (const flag of ignoredFlags) {
	ignoredFlagsConfig[flag] = {
		type: "boolean",
	};
}
```

the most importnant variable is `cli`

```js
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

```js
if (cli.input.length === 0) {
	console.error("Specify at least one path");
	process.exit(1);
}
```

at the end add

```js
manx(cli.input);
```

the final result of file

```js
#!/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

```json
{
  "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`

```sh
$ npm link
```

add file and folder for test

```sh
$ touch test_file && mkdir test_folder
```

Now the awaited moment, in the terminal

```sh
$ 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][npmUrl], 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`

```.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`

```.gitattributes
* text=auto eol=lf
```

`.gitignore`

```.gitignore
node_modules
yarn.lock
```

`.travis.yml`

```yml
language: node_js
node_js:
  - '14'
  - '12'
  - '10'
```

ok, type

```sh
$ npm unlink
```

and now we must login to npm

```sh
$ npm login
```

> publish

```sh
$ npm publish --access=public
```

and if you want to install it, you should install it globally

```sh
$ npm i -g YOUR_PKG
```
you can see your package in [npm][npmUrl]

visit https://www.npmjs.com/package/YOUR_PKG

Here you are, you have trash program and npm package...

enjoy, and see you next time.

[npmUrl]: https://npmjs.com
[nodeUrl]: https://nodejs.org
[myPkg]: https://www.npmjs.com/package/@abdfnx/hac_k
[trashPkg]: https://www.npmjs.com/package/trash