Skip to content

Commit

Permalink
feat: create deno.jsonc filled with specific fields only (#85)
Browse files Browse the repository at this point in the history
* feat: create deno.jsonc filled with specific fields only

* chore: add comments

* fix: typo

* chore(docs): add readme info

* update example

* chore: version
  • Loading branch information
GJZwiers committed Apr 24, 2022
1 parent 17d359c commit a38f077
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
DENO_DIR: deno_dir

steps:
- name: Configure line-endings for Windows builds
- name: Configure line-endings for Windows build
if: matrix.os == 'windows-2019'
run: |
git config --system core.autocrlf false
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ this module is purely for generating config.
## Installation

```
deno install --allow-read --allow-write -fn deno-init https://deno.land/x/init@v2.4.4/mod.ts
deno install --allow-read --allow-write -fn deno-init https://deno.land/x/init@v2.5.0/mod.ts
```

## Usage
Expand Down Expand Up @@ -69,6 +69,13 @@ output of `tsc --init` for generating a `tsconfig.json`.
deno-init --fill
```

It is also possible to add only specific fields to the config file and fill them
with options in comments, for example:

```
deno-init --fmt --fill
```

`--fmt` or `-m` will add a `fmt` section only.

```
Expand Down
2 changes: 1 addition & 1 deletion egg.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"entry": "./mod.ts",
"description": "Generate a Deno configuration file.",
"unstable": false,
"version": "v2.4.4",
"version": "v2.5.0",
"files": [
"./**/*.ts",
"./README.md",
Expand Down
2 changes: 1 addition & 1 deletion init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ask } from "./ask.ts";

await new Command()
.name("deno-init")
.version("v2.4.4")
.version("v2.5.0")
.description("Generate a Deno configuration file.")
.help({
colors: (Deno.build.os === "windows") ? false : true,
Expand Down
34 changes: 33 additions & 1 deletion schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// deno-lint-ignore-file no-explicit-any
import { schema } from "./deps.ts";
import { Settings } from "./writeConfigFile.ts";

// TODO: better types
export function createFromSchema(properties: any, configFile: any) {
Expand All @@ -17,11 +18,42 @@ export function createFromSchema(properties: any, configFile: any) {
}
}

export function generateJsonc(): string {
const mapOfKeys: { [key: string]: string } = {
fmt: "fmt",
lint: "lint",
map: "importMap",
task: "tasks",
tsconfig: "compilerOptions",
};

export function generateJsonc(settings: Settings): string {
const configFile: any = {};

// Find which settings are true and map it to fields in the deno.jsonc file
// TODO: refactor
let opts = 0;
const keep = Object.entries(settings)
.filter((setting) => {
return setting[1] === true;
}).map((v) => {
if (mapOfKeys[v[0]]) {
opts += 1;
return mapOfKeys[v[0]];
}
});

createFromSchema(schema.properties, configFile);

// When e.g. both --fill and --fmt are true
if (opts > 0) {
for (const key in configFile) {
// Delete fields except the ones passed in the settings
if (keep.indexOf(key) === -1) {
delete configFile[key];
}
}
}

const jsonString = JSON.stringify(configFile, null, 2);

const optionMatcher = /^(\s*?)(".+?"): "(.+?)\|(.+?)\|(.+?)",?$/gm;
Expand Down
43 changes: 37 additions & 6 deletions writeConfigFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ Deno.test("writeConfigFile()", async (context) => {
Deno.chdir("..");
await Deno.remove(testDir, { recursive: true });
testSettings.name = "deno.json";
testSettings.jsonc = false;
testSettings.map = false;
testSettings.fill = false;
testSettings.fmt = false;
};

const test = async (
Expand Down Expand Up @@ -45,17 +46,47 @@ Deno.test("writeConfigFile()", async (context) => {
});

await test({
name: "create deno.jsonc",
name: "create filled deno.jsonc if --fill is used",
fn: async () => {
testSettings.jsonc = true;
testSettings.fill = true;
await inputHandler(testSettings);

const configFile = await Deno.readFile(
assertEquals(testSettings.name, "deno.jsonc");

const bytes = await Deno.readFile(
`${testSettings.name}`,
);

assertEquals(testSettings.name, "deno.jsonc");
assert(configFile);
const file = new TextDecoder().decode(bytes);

assert(file.indexOf("fmt") > 0);
assert(file.indexOf("lint") > 0);
assert(file.indexOf("importMap") > 0);
assert(file.indexOf("compilerOptions") > 0);
assert(file.indexOf("tasks") > 0);
},
});

await test({
name:
"create deno.jsonc with fmt section only if fill is used in combination with fmt",
fn: async () => {
testSettings.fill = true;
testSettings.fmt = true;
await inputHandler(testSettings);

const bytes = await Deno.readFile(
`${testSettings.name}`,
);

const file = new TextDecoder().decode(bytes);

assert(file.indexOf("fmt") > 0);

assert(file.indexOf("lint") === -1);
assert(file.indexOf("importMap") === -1);
assert(file.indexOf("compilerOptions") === -1);
assert(file.indexOf("tasks") === -1);
},
});

Expand Down
2 changes: 1 addition & 1 deletion writeConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function inputHandler(settings: Settings) {

return await writeFileSec(
settings.name,
new TextEncoder().encode(generateJsonc()),
new TextEncoder().encode(generateJsonc(settings)),
{
force: settings.force,
},
Expand Down

0 comments on commit a38f077

Please sign in to comment.