@cloud-cli/dns 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,7 +1,4 @@
1
1
  import { init } from '@cloud-cli/cli';
2
- interface DNSConfig {
3
- defaultTarget?: string;
4
- }
5
2
  interface DomainAndTarget {
6
3
  domain: string;
7
4
  target?: string;
@@ -13,7 +10,7 @@ declare function get(options: {
13
10
  domain: string;
14
11
  }): DomainAndTarget | null;
15
12
  declare function reload(): Promise<true>;
16
- declare function addDnsConfig(options: DNSConfig): void;
13
+ declare function addDnsConfig(): void;
17
14
  export declare function parseDNSLine(line: string): DomainAndTarget[];
18
15
  declare const _default: {
19
16
  add: typeof add;
package/dist/index.js CHANGED
@@ -1,24 +1,24 @@
1
1
  import fs from 'node:fs';
2
2
  import { join } from 'node:path';
3
3
  import { exec } from '@cloud-cli/exec';
4
- import { init } from '@cloud-cli/cli';
4
+ import { getConfig, init } from '@cloud-cli/cli';
5
5
  const filePath = join(process.cwd(), 'configuration', 'hosts.conf');
6
6
  const dnsConfig = {
7
- defaultTarget: '127.0.0.1'
7
+ defaultTarget: '127.0.0.1',
8
8
  };
9
9
  function add(input) {
10
10
  let current = list();
11
11
  if (!input.target) {
12
12
  input.target = dnsConfig.defaultTarget;
13
13
  }
14
- current = current.filter(item => item.domain !== input.domain);
14
+ current = current.filter((item) => item.domain !== input.domain);
15
15
  current.push(input);
16
16
  save(current);
17
17
  return true;
18
18
  }
19
19
  function remove(input) {
20
20
  const current = list();
21
- const newList = current.filter(item => item.domain !== input.domain);
21
+ const newList = current.filter((item) => item.domain !== input.domain);
22
22
  save(newList);
23
23
  return true;
24
24
  }
@@ -27,25 +27,19 @@ function list() {
27
27
  return [];
28
28
  }
29
29
  const input = fs.readFileSync(filePath, 'utf8');
30
- const entries = input
31
- .trim()
32
- .split('\n')
33
- .filter(Boolean)
34
- .flatMap(parseDNSLine);
30
+ const entries = input.trim().split('\n').filter(Boolean).flatMap(parseDNSLine);
35
31
  return entries;
36
32
  }
37
33
  function get(options) {
38
- return list().filter(d => d.domain === options.domain)[0] || null;
34
+ return list().filter((d) => d.domain === options.domain)[0] || null;
39
35
  }
40
36
  async function reload() {
41
37
  const getPid = await exec('pidof', ['dnsmasq']);
42
38
  const cmd = await exec('kill', ['-s', 'HUP', getPid.stdout.trim()]);
43
39
  return cmd.ok || Promise.reject(new Error('Failed to reload'));
44
40
  }
45
- function addDnsConfig(options) {
46
- if (options && options.defaultTarget) {
47
- dnsConfig.defaultTarget = options.defaultTarget;
48
- }
41
+ function addDnsConfig() {
42
+ Object.assign(dnsConfig, getConfig('dns'));
49
43
  }
50
44
  function save(list) {
51
45
  const targets = list.reduce((all, next) => {
@@ -59,6 +53,6 @@ function save(list) {
59
53
  }
60
54
  export function parseDNSLine(line) {
61
55
  const [ip, ...domains] = line.split(/\s+/);
62
- return domains.map(d => ({ domain: d, target: ip }));
56
+ return domains.map((d) => ({ domain: d, target: ip }));
63
57
  }
64
58
  export default { add, remove, list, reload, get, [init]: addDnsConfig };
@@ -4,9 +4,17 @@ import { init } from '@cloud-cli/cli';
4
4
  import { describe, it, expect, vi, beforeEach } from 'vitest';
5
5
  const execMocks = vi.hoisted(() => ({
6
6
  exec: vi.fn(),
7
+ getConfig: vi.fn(),
7
8
  }));
8
9
  vi.mock('get-port', () => ({ default: vi.fn().mockReturnValue(1234) }));
9
10
  vi.mock('@cloud-cli/exec', () => ({ exec: execMocks.exec }));
11
+ vi.mock('@cloud-cli/cli', async (original) => {
12
+ const mod = await original();
13
+ return {
14
+ ...mod,
15
+ getConfig: execMocks.getConfig,
16
+ };
17
+ });
10
18
  beforeEach(() => {
11
19
  vi.spyOn(fs, 'writeFileSync').mockImplementation(() => { });
12
20
  vi.spyOn(fs, 'readFileSync').mockImplementation(() => '');
@@ -74,7 +82,8 @@ describe('dns', () => {
74
82
  });
75
83
  describe('dns configuration', () => {
76
84
  it('should configure the default target', () => {
77
- dns[init]({ defaultTarget: '1.1.2.2' });
85
+ execMocks.getConfig.mockReturnValue({ defaultTarget: '1.1.2.2' });
86
+ dns[init]();
78
87
  dns.add({ domain: 'bar' });
79
88
  expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '1.1.2.2 bar');
80
89
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/dns",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",
package/src/index.spec.ts CHANGED
@@ -5,10 +5,18 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
5
5
 
6
6
  const execMocks = vi.hoisted(() => ({
7
7
  exec: vi.fn(),
8
+ getConfig: vi.fn(),
8
9
  }));
9
10
 
10
11
  vi.mock('get-port', () => ({ default: vi.fn().mockReturnValue(1234) }));
11
12
  vi.mock('@cloud-cli/exec', () => ({ exec: execMocks.exec }));
13
+ vi.mock('@cloud-cli/cli', async (original) => {
14
+ const mod: any = await original();
15
+ return {
16
+ ...mod,
17
+ getConfig: execMocks.getConfig,
18
+ };
19
+ });
12
20
 
13
21
  beforeEach(() => {
14
22
  vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {});
@@ -95,7 +103,8 @@ describe('dns', () => {
95
103
 
96
104
  describe('dns configuration', () => {
97
105
  it('should configure the default target', () => {
98
- dns[init]({ defaultTarget: '1.1.2.2' });
106
+ execMocks.getConfig.mockReturnValue({ defaultTarget: '1.1.2.2' });
107
+ dns[init]();
99
108
  dns.add({ domain: 'bar' });
100
109
 
101
110
  expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), '1.1.2.2 bar');
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import fs from 'node:fs';
2
2
  import { join } from 'node:path';
3
3
  import { exec } from '@cloud-cli/exec';
4
- import { init } from '@cloud-cli/cli';
4
+ import { getConfig, init } from '@cloud-cli/cli';
5
5
 
6
6
  interface DNSConfig {
7
7
  defaultTarget?: string;
@@ -9,7 +9,7 @@ interface DNSConfig {
9
9
 
10
10
  const filePath = join(process.cwd(), 'configuration', 'hosts.conf');
11
11
  const dnsConfig: DNSConfig = {
12
- defaultTarget: '127.0.0.1'
12
+ defaultTarget: '127.0.0.1',
13
13
  };
14
14
 
15
15
  interface DomainAndTarget {
@@ -24,7 +24,7 @@ function add(input: DomainAndTarget) {
24
24
  input.target = dnsConfig.defaultTarget;
25
25
  }
26
26
 
27
- current = current.filter(item => item.domain !== input.domain);
27
+ current = current.filter((item) => item.domain !== input.domain);
28
28
  current.push(input);
29
29
  save(current);
30
30
 
@@ -33,7 +33,7 @@ function add(input: DomainAndTarget) {
33
33
 
34
34
  function remove(input: DomainAndTarget) {
35
35
  const current = list();
36
- const newList = current.filter(item => item.domain !== input.domain);
36
+ const newList = current.filter((item) => item.domain !== input.domain);
37
37
  save(newList);
38
38
  return true;
39
39
  }
@@ -44,17 +44,13 @@ function list(): DomainAndTarget[] {
44
44
  }
45
45
 
46
46
  const input = fs.readFileSync(filePath, 'utf8');
47
- const entries = input
48
- .trim()
49
- .split('\n')
50
- .filter(Boolean)
51
- .flatMap(parseDNSLine)
47
+ const entries = input.trim().split('\n').filter(Boolean).flatMap(parseDNSLine);
52
48
 
53
49
  return entries;
54
50
  }
55
51
 
56
- function get(options: { domain: string; }): DomainAndTarget | null {
57
- return list().filter(d => d.domain === options.domain)[0] || null;
52
+ function get(options: { domain: string }): DomainAndTarget | null {
53
+ return list().filter((d) => d.domain === options.domain)[0] || null;
58
54
  }
59
55
 
60
56
  async function reload() {
@@ -63,10 +59,8 @@ async function reload() {
63
59
  return cmd.ok || Promise.reject(new Error('Failed to reload'));
64
60
  }
65
61
 
66
- function addDnsConfig(options: DNSConfig) {
67
- if (options && options.defaultTarget) {
68
- dnsConfig.defaultTarget = options.defaultTarget;
69
- }
62
+ function addDnsConfig() {
63
+ Object.assign(dnsConfig, getConfig('dns'));
70
64
  }
71
65
 
72
66
  function save(list: DomainAndTarget[]) {
@@ -83,7 +77,7 @@ function save(list: DomainAndTarget[]) {
83
77
 
84
78
  export function parseDNSLine(line: string): DomainAndTarget[] {
85
79
  const [ip, ...domains] = line.split(/\s+/);
86
- return domains.map(d => ({ domain: d, target: ip }));
80
+ return domains.map((d) => ({ domain: d, target: ip }));
87
81
  }
88
82
 
89
- export default { add, remove, list, reload, get, [init]: addDnsConfig }
83
+ export default { add, remove, list, reload, get, [init]: addDnsConfig };