A few node utilities for working with your hosts file, inspired by Hostile
Note
If you're looking for a CLI frontend, raise an issue and I'll write one.
I wanted to programatically work with my hosts file while maintaining formatting. Hostile doesn't seem to be maintained anymore and lacks features I was looking for.
Because I haven't worked much with hosts files and can't be confident in the code until it's used in the real world.
The hosts file seems simple, but in my experience, real-world implementations of anything are quirky.
npm install hosts-utilitiesAssume a hosts file /etc/hosts
1.2.3.4 hostname1We can add hostname2 using assignHostnames
import * as hosts from 'hosts-utilities'
await hosts.assignHostnames('1.2.3.4', ['hostname2'])
// updates /etc/hosts to
// 1.2.3.4 hostname1 hostname2Note
Both named and sub-path exports are available. Paths are kebab-cased.
Example:
import { assignHostnames } from 'hosts-utilities'
// or
import assignHostnames from 'hosts-utilities/assign-hostnames'Add or update host entries
Signature
- async (ip: string, hostnames: string[], options: AssignOptions = {}) =>
undefined- ip and hostnames must match
/^[^#\s]+$/
- ip and hostnames must match
Examples
Assume a file /etc/hosts with
1.2.3.4 hostname1 hostname2await assignHostnames('1.2.3.4', ['hostname1', 'hostname3'])
// updates /etc/hosts to
// 1.2.3.4 hostname1 hostname2 hostname3
await assignHostnames('5.6.7.8', ['hostname4'])
// adds the line
// 5.6.7.8 hostname4A string
- On windows:
C:\Windows\System32\drivers\etc\hosts - On not windows:
/etc/hosts
Parse the hosts file
Why use it?
- This let's you perform your own logic on the hosts file. For example, you
could parse it, modify the parsed lines, then call
write(updatedParsedLines)
Signature
- async (options: ParseOptions = {}) => ParsedLine[]
Examples
- here's an example of a parsed file
const parsedLines1 = await parse()
const parsedLines2 = await parse({ filePath: '/path/to/custom/hosts' })Signature
- async (hostnames: string[], options: RemoveOptions = {}) =>
undefined- hostnames must match
/^[^#\s]+$/
- hostnames must match
Examples
Let's assume a file /etc/hosts with
1.2.3.4 hostname1
5.6.7.8 hostname2 hostname3await removeHostnames(['hostname1', 'hostname2'])
// updates /etc/hosts to
// 5.6.7.8 hostname3Write parsed lines to the hosts file
Why use it?
- Like parse, this lets you perform your own logic on the hosts file. For example, you could parse it, modify the parsedLines, then write them.
Signature
- async (parsedLines: ParsedLine[], options: WriteOptions = {}) =>
undefined
Examples
- here are some examples of writing your own parsedLines
const parsedLines = await parse()
const updatedLines = doSomethingTo(parsedLines)
await write(updatedLines){
// default: hostsPath
// validation: minimum of one character
filePath?: string
}Includes FormatOptions
FormatOptions & {
// default: hostsPath
// validation: minimum of one character
filePath?: string
// default: undefined (removes hostnames for all ips)
// validation: matches /^[^#\s]+$/
withIp?: string
}Warning
The option withIp is provided because Hostile supports it,
meaning I assume someone found it helpful. Keep in mind hosts shouldn't be
mapped to more than one IP address, so withIp should be unnecessary.
Includes FormatOptions
FormatOptions & {
// default: hostsPath
// validation: minimum of one character
filePath?: string
}Includes FormatOptions
FormatOptions & {
// default: hostsPath
// validation: minimum of one character
filePath?: string
}Note
Due to complexity, these options may not behave as you expect.
At a glance
{
preserveFormatting?: boolean = true
separatorParts?: string = '\t'
separatorHostname?: string = ' '
}Note
Due to complexity, this structure may not work how you expect.
At a glance
{
original: string
data: {
ip: string
hostnamesWithSpace: string[]
comment: string
space: {
beforeIp: string
afterIp: string
beforeComment: string
}
}
}