Skip to content

Commit

Permalink
Bye node, hello deno.
Browse files Browse the repository at this point in the history
  • Loading branch information
elgs committed Jan 26, 2022
1 parent f20ae33 commit dcaf812
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 73 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ parser and generator for Node.js and browser.
## Deno

```typescript
import { generate, parse } from 'https://deno.land/x/zonefile@0.2.10/lib/zonefile-es.js';
import zonefile from 'https://deno.land/x/zonefile@0.3.0/lib/zonefile.js';
```

## Standalone
Expand Down Expand Up @@ -222,9 +222,11 @@ _dns-zonefile_ can also be used as a module. Simply use `require()` to include
it, then invoke its `generate()` function as shown in the following example:

```javascript
var zonefile = require('dns-zonefile');
var options = require('./zonefile_forward.json');
var output = zonefile.generate(options);
import fs from 'fs';
import zonefile from 'dns-zonefile';
const json = fs.readFileSync('./zonefile_forward.json', 'utf8');
const options = JSON.parse(json);
const output = zonefile.generate(options);
console.log(output);
```

Expand All @@ -235,7 +237,8 @@ It is also possible to parse a zone file to JSON by invoking its `parse()`
function as shown in the following example:

```javascript
var zonefile = require('dns-zonefile');
import fs from 'fs';
import zonefile from 'dns-zonefile';
var text = fs.readFileSync('./zonefile_forward.txt', 'utf8');
output = zonefile.parse(text);
console.log(JSON.stringify(output));
Expand Down
53 changes: 0 additions & 53 deletions bin/zonefile

This file was deleted.

52 changes: 52 additions & 0 deletions bin/zonefile-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env node

import fs from 'fs';
import zonefile from '../lib/zonefile.js';

import { createRequire } from "module";
const require = createRequire(import.meta.url);

var args = function () {
var ret = [];
process.argv.forEach(function (val, index, array) {
if (index >= 2) {
ret.push(val);
}
});
return ret;
};

var input = args();

var pjson = require('../package.json');
if (input.length === 0) {
console.log('dns-zonefile version: ', pjson.version);
console.log('Usage:');
console.log(' "zonefile [-v|--version]" to show version information.');
console.log(' "zonefile -p zonefile.txt" to parse a zonefile to JSON.');
console.log(' "zonefile -g zonefile.json" to generate a zonefile from JSON.');
console.log(' "zonefile zonefile.json" to generate a zonefile from JSON if the file name ' +
'\n contains ".json", case insensitively, otherwise to parse the file as a zonefile to JSON.');
} else if (input[0] === '-v' || input[0] === '--version') {
console.log('dns-zonefile version: ', pjson.version);
} else if (input[0] === '-g') {
var src = input[1];
var options = fs.readFileSync(src, 'utf8');
var output = zonefile.generate(JSON.parse(options));
console.log(output);
} else if (input[0] === '-p') {
var src = input[1];
var options = fs.readFileSync(src, 'utf8');
var output = zonefile.parse(options);
console.log(JSON.stringify(output));
} else {
var src = input[0];
var options = fs.readFileSync(src, 'utf8');
if (src.toLocaleLowerCase().indexOf('.json') > 0) {
var output = zonefile.generate(JSON.parse(options));
} else {
var output = zonefile.parse(options);
output = JSON.stringify(output);
}
console.log(output);
}
13 changes: 11 additions & 2 deletions lib/zonefile-es.js → lib/zonefile-node-legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const defaultTemplate = `; Zone: {zone}
// $$$$$$/ //
// //
/////////////////////////////////////////////////////////////////////////////////////
export let generate = function (options, template) {
let generate = function (options, template) {
const json = JSON.parse(JSON.stringify(options));
template = template || defaultTemplate;
template = process$ORIGIN(json['$origin'], template);
Expand Down Expand Up @@ -229,6 +229,7 @@ let processValues = function (options, template) {
return template.replace('{time}', Math.round(Date.now() / 1000));
};


////////////////////////////////////////////////////////
// ______ ______ ______ _______ ______ //
// / \ / \ / \ / | / \ //
Expand All @@ -242,7 +243,7 @@ let processValues = function (options, template) {
// $$/ //
// //
////////////////////////////////////////////////////////
export let parse = function (text) {
let parse = function (text) {
text = removeComments(text);
text = flatten(text);
return parseRRs(text);
Expand Down Expand Up @@ -711,3 +712,11 @@ let splitArgs = function (input, sep, keepQuotes) {
}
return ret;
};

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
exports.generate = generate;
exports.parse = parse;
} else {
window.zonefile_generate = generate;
window.zonefile_parse = parse;
}
12 changes: 4 additions & 8 deletions lib/zonefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ let processValues = function (options, template) {
return template.replace('{time}', Math.round(Date.now() / 1000));
};


////////////////////////////////////////////////////////
// ______ ______ ______ _______ ______ //
// / \ / \ / \ / | / \ //
Expand Down Expand Up @@ -713,10 +712,7 @@ let splitArgs = function (input, sep, keepQuotes) {
return ret;
};

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
exports.generate = generate;
exports.parse = parse;
} else {
window.zonefile_generate = generate;
window.zonefile_parse = parse;
}
export default {
generate,
parse,
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"name": "dns-zonefile",
"version": "0.2.10",
"version": "0.3.0",
"description": "A DNS zone file parser and generator.",
"main": "./lib/zonefile.js",
"types": "./lib/zonefile.d.ts",
"bin": {
"zonefile": "./bin/zonefile"
"zonefile": "./bin/zonefile-cli.js"
},
"scripts": {
"test": "npx jasmine tests/tests.js"
},
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/elgs/dns-zonefile.git"
Expand Down
14 changes: 11 additions & 3 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
const fs = require('fs');
const zonefile = require('../lib/zonefile.js');
const deepEqual = require('deep-equal');
import fs from 'fs';
import zonefile from '../lib/zonefile.js';
import deepEqual from 'deep-equal';

import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

import { createRequire } from "module";
const require = createRequire(import.meta.url);

/////////////////////////////////////////////////////////////////////////////////
// _______ //
Expand Down

0 comments on commit dcaf812

Please sign in to comment.