@cloud-cli/dns 1.0.2 → 1.1.0

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,8 +1,6 @@
1
- export declare class DNS {
2
- static parse(line: any): {
3
- domain: any;
4
- target: any;
5
- };
1
+ import { init } from '@cloud-cli/cli';
2
+ interface DNSConfig {
3
+ defaultTarget?: string;
6
4
  }
7
5
  interface DomainAndTarget {
8
6
  domain: string;
@@ -12,10 +10,16 @@ declare function add(input: DomainAndTarget): boolean;
12
10
  declare function remove(input: DomainAndTarget): boolean;
13
11
  declare function list(): DomainAndTarget[];
14
12
  declare function reload(): Promise<true>;
13
+ declare function addDnsConfig(options: DNSConfig): void;
14
+ export declare function parseDNSLine(line: string): {
15
+ domain: string;
16
+ target: string;
17
+ };
15
18
  declare const _default: {
16
19
  add: typeof add;
17
20
  remove: typeof remove;
18
21
  list: typeof list;
19
22
  reload: typeof reload;
23
+ [init]: typeof addDnsConfig;
20
24
  };
21
25
  export default _default;
package/dist/index.js CHANGED
@@ -1,18 +1,16 @@
1
1
  import fs from 'fs';
2
2
  import { join } from 'path';
3
3
  import { exec } from '@cloud-cli/exec';
4
+ import { init } from '@cloud-cli/cli';
4
5
  const lineParse = /^address=\/(.+)\/(.+)$/;
5
- const filePath = join(process.cwd(), 'configuration/apps.conf');
6
- export class DNS {
7
- static parse(line) {
8
- const parts = line.match(lineParse);
9
- return { domain: parts[1], target: parts[2] };
10
- }
11
- }
6
+ const filePath = join(process.cwd(), 'configuration', 'apps.conf');
7
+ const dnsConfig = {
8
+ defaultTarget: '127.0.0.1'
9
+ };
12
10
  function add(input) {
13
11
  let current = list();
14
12
  if (!input.target) {
15
- input.target = '127.0.0.1';
13
+ input.target = dnsConfig.defaultTarget;
16
14
  }
17
15
  current = current.filter(item => item.domain !== input.domain);
18
16
  current.push(input);
@@ -25,10 +23,6 @@ function remove(input) {
25
23
  save(newList);
26
24
  return true;
27
25
  }
28
- function save(list) {
29
- const txt = list.map(line => 'address=/' + line.domain + '/' + line.target);
30
- fs.writeFileSync(filePath, txt.join('\n'));
31
- }
32
26
  function list() {
33
27
  if (!fs.existsSync(filePath)) {
34
28
  return [];
@@ -38,11 +32,25 @@ function list() {
38
32
  .trim()
39
33
  .split('\n')
40
34
  .filter(Boolean)
41
- .map(line => DNS.parse(line));
35
+ .map(parseDNSLine);
42
36
  return entries;
43
37
  }
44
38
  async function reload() {
45
39
  const cmd = await exec('systemctl', ['restart', 'dnsmasq']);
46
40
  return cmd.ok || Promise.reject(new Error('Failed to reload'));
47
41
  }
48
- export default { add, remove, list, reload };
42
+ function addDnsConfig(options) {
43
+ if (options && options.defaultTarget) {
44
+ dnsConfig.defaultTarget = options.defaultTarget;
45
+ }
46
+ }
47
+ function save(list) {
48
+ const txt = list.map(line => `address=/${line.domain}/${line.target}`);
49
+ console.log('save', filePath, txt);
50
+ fs.writeFileSync(filePath, txt.join('\n'));
51
+ }
52
+ export function parseDNSLine(line) {
53
+ const parts = line.match(lineParse);
54
+ return { domain: parts[1], target: parts[2] };
55
+ }
56
+ export default { add, remove, list, reload, [init]: addDnsConfig };
@@ -1,11 +1,15 @@
1
- import { DNS } from './index';
2
- import dns from './index';
1
+ import dns, { parseDNSLine } from './index';
3
2
  import fs from 'fs';
4
3
  import * as exec from '@cloud-cli/exec';
4
+ import { init } from '@cloud-cli/cli';
5
+ beforeEach(() => {
6
+ jest.spyOn(fs, 'writeFileSync').mockImplementation();
7
+ jest.spyOn(fs, 'readFileSync').mockImplementation(() => '');
8
+ });
5
9
  describe('dns', () => {
6
10
  it('should parse a DNS configuration line', () => {
7
11
  const line = 'address=/foo/1.2.3.4';
8
- const output = DNS.parse(line);
12
+ const output = parseDNSLine(line);
9
13
  expect(output).toEqual({ target: '1.2.3.4', domain: 'foo' });
10
14
  });
11
15
  it('should load entries from file', () => {
@@ -44,4 +48,11 @@ describe('dns', () => {
44
48
  await expect(dns.reload()).rejects.toEqual(new Error('Failed to reload'));
45
49
  });
46
50
  });
51
+ describe('dns configuration', () => {
52
+ it('should configure the default target', () => {
53
+ dns[init]({ defaultTarget: '1.1.2.2' });
54
+ dns.add({ domain: 'bar' });
55
+ expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), 'address=/bar/1.1.2.2');
56
+ });
57
+ });
47
58
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloud-cli/dns",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",
@@ -23,6 +23,7 @@
23
23
  "@cloud-cli/typescript-config": "^1.0.0"
24
24
  },
25
25
  "dependencies": {
26
+ "@cloud-cli/cli": "^1.6.0",
26
27
  "@cloud-cli/exec": "^1.0.4"
27
28
  }
28
29
  }
package/src/index.spec.ts CHANGED
@@ -1,12 +1,18 @@
1
- import { DNS } from './index';
2
- import dns from './index';
1
+ import dns, { parseDNSLine } from './index';
3
2
  import fs from 'fs';
4
3
  import * as exec from '@cloud-cli/exec';
4
+ import { init } from '@cloud-cli/cli';
5
+
6
+ beforeEach(() => {
7
+ jest.spyOn(fs, 'writeFileSync').mockImplementation();
8
+ jest.spyOn(fs, 'readFileSync').mockImplementation(() => '');
9
+ });
5
10
 
6
11
  describe('dns', () => {
7
12
  it('should parse a DNS configuration line', () => {
8
13
  const line = 'address=/foo/1.2.3.4';
9
- const output = DNS.parse(line);
14
+ const output = parseDNSLine(line);
15
+
10
16
  expect(output).toEqual({ target: '1.2.3.4', domain: 'foo' });
11
17
  });
12
18
 
@@ -15,9 +21,9 @@ describe('dns', () => {
15
21
  const buffer = `address=/test/1.2.3.4\naddress=/foo/5.6.7.8`;
16
22
  jest.spyOn(fs, 'existsSync').mockImplementation(() => fileExists);
17
23
  jest.spyOn(fs, 'readFileSync').mockImplementation(() => buffer);
18
-
24
+
19
25
  expect(dns.list()).toEqual([]);
20
-
26
+
21
27
  fileExists = true;
22
28
  const list = dns.list();
23
29
  expect(list).toEqual([
@@ -57,4 +63,13 @@ describe('dns', () => {
57
63
  await expect(dns.reload()).rejects.toEqual(new Error('Failed to reload'));
58
64
  });
59
65
  });
66
+
67
+ describe('dns configuration', () => {
68
+ it('should configure the default target', () => {
69
+ dns[init]({ defaultTarget: '1.1.2.2' });
70
+ dns.add({ domain: 'bar' });
71
+
72
+ expect(fs.writeFileSync).toHaveBeenCalledWith(expect.any(String), 'address=/bar/1.1.2.2');
73
+ });
74
+ })
60
75
  });
package/src/index.ts CHANGED
@@ -1,17 +1,18 @@
1
1
  import fs from 'fs';
2
2
  import { join } from 'path';
3
3
  import { exec } from '@cloud-cli/exec';
4
+ import { init } from '@cloud-cli/cli';
4
5
 
5
- const lineParse = /^address=\/(.+)\/(.+)$/;
6
- const filePath = join(process.cwd(), 'configuration/apps.conf');
7
-
8
- export class DNS {
9
- static parse(line) {
10
- const parts = line.match(lineParse);
11
- return { domain: parts[1], target: parts[2] }
12
- }
6
+ interface DNSConfig {
7
+ defaultTarget?: string;
13
8
  }
14
9
 
10
+ const lineParse = /^address=\/(.+)\/(.+)$/;
11
+ const filePath = join(process.cwd(), 'configuration', 'apps.conf');
12
+ const dnsConfig: DNSConfig = {
13
+ defaultTarget: '127.0.0.1'
14
+ };
15
+
15
16
  interface DomainAndTarget {
16
17
  domain: string;
17
18
  target?: string;
@@ -21,7 +22,7 @@ function add(input: DomainAndTarget) {
21
22
  let current = list();
22
23
 
23
24
  if (!input.target) {
24
- input.target = '127.0.0.1';
25
+ input.target = dnsConfig.defaultTarget;
25
26
  }
26
27
 
27
28
  current = current.filter(item => item.domain !== input.domain);
@@ -33,26 +34,22 @@ function add(input: DomainAndTarget) {
33
34
 
34
35
  function remove(input: DomainAndTarget) {
35
36
  const current = list();
36
- const newList = current.filter(item => item.domain !== input.domain)
37
+ const newList = current.filter(item => item.domain !== input.domain);
37
38
  save(newList);
38
-
39
39
  return true;
40
40
  }
41
41
 
42
- function save(list: DomainAndTarget[]) {
43
- const txt = list.map(line => 'address=/' + line.domain + '/' + line.target);
44
- fs.writeFileSync(filePath, txt.join('\n'));
45
- }
46
-
47
42
  function list(): DomainAndTarget[] {
48
- if (!fs.existsSync(filePath)) { return []; }
43
+ if (!fs.existsSync(filePath)) {
44
+ return [];
45
+ }
49
46
 
50
47
  const input = fs.readFileSync(filePath, 'utf8');
51
48
  const entries = input
52
49
  .trim()
53
50
  .split('\n')
54
51
  .filter(Boolean)
55
- .map(line => DNS.parse(line))
52
+ .map(parseDNSLine)
56
53
 
57
54
  return entries;
58
55
  }
@@ -62,4 +59,21 @@ async function reload() {
62
59
  return cmd.ok || Promise.reject(new Error('Failed to reload'));
63
60
  }
64
61
 
65
- export default { add, remove, list, reload }
62
+ function addDnsConfig(options: DNSConfig) {
63
+ if (options && options.defaultTarget) {
64
+ dnsConfig.defaultTarget = options.defaultTarget;
65
+ }
66
+ }
67
+
68
+ function save(list: DomainAndTarget[]) {
69
+ const txt = list.map(line => `address=/${line.domain}/${line.target}`);
70
+ console.log('save', filePath, txt);
71
+ fs.writeFileSync(filePath, txt.join('\n'));
72
+ }
73
+
74
+ export function parseDNSLine(line: string) {
75
+ const parts = line.match(lineParse);
76
+ return { domain: parts[1], target: parts[2] }
77
+ }
78
+
79
+ export default { add, remove, list, reload, [init]: addDnsConfig }