Skip to content

Commit

Permalink
feat: get rid of cjs module
Browse files Browse the repository at this point in the history
BREAKING CHANGE: only provide esm module
  • Loading branch information
arlac77 committed Jan 14, 2019
1 parent fe45223 commit 1a7e5c7
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 37 deletions.
1 change: 0 additions & 1 deletion .markdown-doctest-setup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module.exports = {
require: {
'config-expander': require('./dist/expander')
},
globals: {
'process' : process
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ Expands expressions in config files

## file.js

<!-- skip-example -->

```js
const { expand } = require('config-expander');
import { expand } = from 'config-expander';

// expanding hole expressions at the value position (result key is a number)
expand({ key: '${value + 1}' }, { constants: { value: 77 } }).then(r =>
Expand Down
17 changes: 0 additions & 17 deletions rollup.config.js

This file was deleted.

File renamed without changes.
13 changes: 6 additions & 7 deletions src/functions.js → src/functions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { expand } from "./expander";
import { promisify } from "util";
import { spawn } from "child_process";
import { dirname, resolve } from "path";
const { readFile } = require("fs").promises;
const { createCipher, createDecipher } = require("crypto");
//import { createCipher, createDecipher } from "crypto";
import fs from 'fs';
import crypto from "crypto";

/**
* @typedef {Object} Value
Expand Down Expand Up @@ -35,7 +34,7 @@ export const functions = {
arguments: ["string"],
returns: "buffer",
apply: (context, args) =>
createValue(readFile(resolve(context.constants.basedir, args[0].value)))
createValue(fs.promises.readFile(resolve(context.constants.basedir, args[0].value)))
},
resolve: {
arguments: ["string"],
Expand All @@ -56,7 +55,7 @@ export const functions = {
const file = resolve(context.constants.basedir, args[0].value);

return createValue(
readFile(file).then(data => {
fs.promises.readFile(file).then(data => {
const json = JSON.parse(data);
return expand(
json,
Expand Down Expand Up @@ -184,7 +183,7 @@ export const functions = {
returns: "string",
apply: (context, args) => {
const [key, plaintext] = args.map(a => a.value);
const encipher = createCipher("aes-256-cbc", key);
const encipher = crypto.createCipher("aes-256-cbc", key);
let encryptdata = encipher.update(plaintext, "utf8", "binary");
encryptdata += encipher.final("binary");
return createValue(Buffer.from(encryptdata, "binary").toString("base64"));
Expand All @@ -203,7 +202,7 @@ export const functions = {
apply: (context, args) => {
let [key, encryptdata] = args.map(a => a.value);
encryptdata = Buffer.from(encryptdata, "base64").toString("binary");
const decipher = createDecipher("aes-256-cbc", key);
const decipher = crypto.createDecipher("aes-256-cbc", key);
let decoded = decipher.update(encryptdata, "binary", "utf8");
decoded += decipher.final("utf8");
return createValue(decoded);
Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 8 additions & 4 deletions tests/basics-test.js → tests/basics-test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import test from "ava";
import { expand, createValue } from "../src/expander";
import { dirname } from "path";
import { fileURLToPath } from "url";

const here = dirname(fileURLToPath(import.meta.url));

test("null expansion", async t => {
t.deepEqual(
Expand Down Expand Up @@ -131,7 +135,7 @@ test("function promise arg", async t =>
"${substring(string(document('../tests/fixtures/short.txt')),0,4)}",
{
constants: {
basedir: __dirname
basedir: here
}
}
),
Expand All @@ -144,7 +148,7 @@ test("two promises binop", async t =>
"${document('../tests/fixtures/short.txt') + document('../tests/fixtures/short2.txt')}",
{
constants: {
basedir: __dirname
basedir: here
}
}
)).toString(),
Expand All @@ -155,7 +159,7 @@ test("left only promise binop", async t =>
t.is(
(await expand("${document('../tests/fixtures/short.txt') + 'XX'}", {
constants: {
basedir: __dirname
basedir: here
}
})).toString(),
"line 1\nXX"
Expand Down Expand Up @@ -197,7 +201,7 @@ test("object paths with promise", async t =>
t.deepEqual(
await expand("${include('../tests/fixtures/with_sub.json').sub}", {
constants: {
basedir: __dirname,
basedir: here,
c1: "vc1"
}
}),
Expand Down
File renamed without changes.
File renamed without changes.
17 changes: 10 additions & 7 deletions tests/files-test.js → tests/files-test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import test from "ava";
import { expand } from "../src/expander";
import { join } from "path";
import { join, dirname } from "path";
import { fileURLToPath } from "url";

const here = dirname(fileURLToPath(import.meta.url));

test("has file content", async t =>
t.deepEqual(
Expand All @@ -12,7 +15,7 @@ test("has file content", async t =>
},
{
constants: {
basedir: join(__dirname, "..", "tests", "fixtures")
basedir: join(here, "..", "tests", "fixtures")
}
}
)
Expand All @@ -24,17 +27,17 @@ test("has file content #2", async t =>
t.is(
await expand("${resolve('fixtures')}", {
constants: {
basedir: join(__dirname, "..", "tests")
basedir: join(here, "..", "tests")
}
}),
join(__dirname, "..", "tests", "fixtures")
join(here, "..", "tests", "fixtures")
));

test("can include", async t =>
t.deepEqual(
await expand("${include('../tests/fixtures/other.json')}", {
constants: {
basedir: __dirname,
basedir: here,
c1: "x"
}
}),
Expand All @@ -48,7 +51,7 @@ test("can nest includes", async t =>
(await expand("${include('../tests/fixtures/first.json')}", {
constants: {
nameOfTheOther: "other.json",
basedir: __dirname
basedir: here
}
})).first_key,
{
Expand All @@ -61,7 +64,7 @@ test("include missing", async t => {
async () => expand("${include('../tests/fixtures/missing.json')}"),
{
message: `ENOENT: no such file or directory, open '${join(
__dirname,
here,
"..",
"../tests/fixtures/missing.json"
)}'`
Expand Down

0 comments on commit 1a7e5c7

Please sign in to comment.