Skip to content

Commit 837fbb7

Browse files
authored
Add .json and .ts deno-workspace file support (#11)
* refractor: Use testDenoXRun in scripts.test.ts * feat: Add support for .json deno-worspace file * feat: Add extname dep and assertThrowsAsync * bugfix: Export project interfaces * bugfix: If globals is unddefined use a empty hash * bugfix: Type argValue in build cli arguments * bugfix: Import missing types in deno_options/parse.ts * feat: Add support for ts file workspace * bugfix: Catch TypeError in deno_workspace ts loading * test: Add test for .deno-workspace.ts * refractor: separate the read and check if list of file exist logic * refractor: Error handling in load workspace * style: Lint files * test: Add tmate to github workflow * chore: Fix eslint installation in github workflow * style: Fix eslint error * style: Refractor methods that had more than 20 lines * style: Add linting rules * test: Add test for deno_options parsing * test: Add test for utils * test: Add test for validate deno options * test: Add test for file * test: Replace in fixture allow-net by seed * test: Add tests for false permissions * fix: Script options not passed properly * style: Remove no-undef eslint rule * chore: Remove node_modules from CI * chore: Limit deno test to src and test folder * bugfix: deno-workspace.ts file not loading on windows * test: Fix read file test * feat: Bump version * doc: Add documentation for JSON and TS workspace files
1 parent 72232d9 commit 837fbb7

File tree

75 files changed

+1603
-337
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1603
-337
lines changed

.eslintrc.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module.exports = {
2+
root: true,
3+
parser: "@typescript-eslint/parser",
4+
plugins: [
5+
"@typescript-eslint",
6+
],
7+
rules: {
8+
"@typescript-eslint/ban-ts-ignore": 0,
9+
"no-console": 0,
10+
"no-else-return": "error",
11+
"@typescript-eslint/no-use-before-define": 0,
12+
"@typescript-eslint/camelcase": 0,
13+
"no-prototype-builtins": 0,
14+
"max-lines-per-function": [
15+
"error",
16+
{
17+
"max": 20,
18+
"skipComments": true,
19+
},
20+
],
21+
"no-shadow": "error",
22+
"max-params": [
23+
"error",
24+
3,
25+
],
26+
"max-statements-per-line": [
27+
"error",
28+
{
29+
"max": 2,
30+
},
31+
],
32+
"complexity": [
33+
"error",
34+
15,
35+
],
36+
"no-undef": 0,
37+
},
38+
"overrides": [
39+
{
40+
"files": ["*.test.ts"],
41+
"rules": {
42+
"max-lines-per-function": 0,
43+
},
44+
},
45+
],
46+
extends: [
47+
"eslint:recommended",
48+
"plugin:@typescript-eslint/eslint-recommended",
49+
"plugin:@typescript-eslint/recommended",
50+
],
51+
};

.github/workflows/ci.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,10 @@ jobs:
2020
- uses: denolib/setup-deno@master
2121
with:
2222
deno-version: ${{ matrix.deno }}
23+
- uses: actions/setup-node@v1
24+
with:
25+
node-version: '12'
26+
- run: npm install typescript eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin tsc-silent --save-dev
27+
- run: ./node_modules/.bin/eslint **/**.ts --ext .ts
2328
- run: deno install -Af denox.ts
24-
- run: deno test -A --failfast
29+
- run: deno test -A --failfast src/ test/

README.md

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<img src="https://img.shields.io/github/v/tag/bentoumitech/denox?style=flat-square" alt="latest SemVer"/>
1111
<img src="https://img.shields.io/badge/contribution-welcome-brightgreen.svg?style=flat-square" alt="Contribution welcome"/>
1212
<img src="https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square" alt="Commitizen friendly"/>
13-
<img src="https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square" alt="Commitizen friendly"/>
1413
<img src="https://img.shields.io/badge/semantic-release-e10079.svg?style=flat-square" alt="Semantic release"/>
1514
<img src="https://img.shields.io/github/license/BentoumiTech/denox?logo=MIT&style=flat-square" alt="MIT License"/>
1615
<br />
@@ -35,7 +34,7 @@ $ deno install -Af -n denox https://denopkg.com/BentoumiTech/denox/denox.ts
3534

3635
## Overview
3736

38-
DenoX is a script runner and workspace wrapper for Deno
37+
DenoX is a cross platform script runner and workspace wrapper for Deno
3938

4039
Using DenoX, you can set up your workspace scripts permissions and options in declarative code.
4140

@@ -53,13 +52,14 @@ $ denox run start
5352

5453
- **DRY** Only write your permissions and options once.
5554
- **Packaged** Your code can run on a different machine with a short single command `denox run start`
55+
- **Cross Platform** DenoX support and is tested on Linux, Mac OS and Windows.
5656
- **Extensible** :soon:
5757

5858
## Usage
5959

6060
```bash
6161
$ denox --help
62-
denox v0.2.0
62+
denox v0.4.0
6363

6464
Usage:
6565
$ denox <command> [options]
@@ -80,13 +80,67 @@ denox run start
8080

8181
## Getting Started
8282

83-
Create a file named `deno-workspace.yml` at the root of your Deno project.
83+
### `deno-workspace` file
84+
85+
Scripts and options need to be defined in a `deno-workspace` file at the root of your project.
86+
87+
DenoX supports YAML, JSON and typescript as format for `deno-workspace`.
88+
89+
In the following examples running `$ denox run start` will execute main.ts file with `example.com` networking permissions
90+
91+
#### YAML
92+
93+
`deno-workspace.yml`
8494

85-
> Note: `deno-workspace.yml` format is YAML but it also supports JSON
95+
```
96+
scripts:
97+
start:
98+
file: main.ts
99+
deno_options:
100+
allow-net: example.com
101+
```
102+
103+
#### JSON
104+
105+
`deno-workspace.json`
106+
107+
```
108+
{
109+
"scripts": {
110+
"start": {
111+
"file": "main.ts",
112+
"deno_options": {
113+
"allow-net": "example.com"
114+
}
115+
}
116+
}
117+
}
118+
```
119+
120+
#### Typescript
121+
122+
`deno-workspace.ts`
123+
124+
```
125+
import { DenoWorkspace } from "https://denopkg.com/BentoumiTech/denox/src/interfaces.ts";
126+
127+
const workspace: DenoWorkspace = {
128+
"scripts": {
129+
"start": {
130+
"file": "main.ts",
131+
"deno_options": {
132+
"allow-net": "example.com"
133+
}
134+
},
135+
},
136+
};
137+
138+
export { workspace };
139+
```
86140

87141
## Scripts
88142

89-
You can easily run scripts using denox by adding them to the "scripts" field in `deno-workspace.yml` and run them with `denox run <script-name>`.
143+
You can easily run scripts using denox by adding them to the "scripts" field in `deno-workspace` and run them with `denox run <script-name>`.
90144

91145
### Example:
92146

@@ -103,6 +157,9 @@ scripts:
103157
deno_options:
104158
allow-net: localhost
105159
reload: true
160+
v8-flags:
161+
- --regexp-tier-up
162+
- --adjust-os-scheduling-parameters true
106163
```
107164
108165
## Options

denox.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ cli
1212
"Run a script",
1313
{ allowUnknownOptions: true, ignoreOptionDefaultValue: true },
1414
)
15-
.example("denox run start arg1 arg2")
15+
.example("denox run start arg1 arg2 --namedArg=value")
1616
.action(run);
1717

1818
// @ts-ignore

deps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export { parse as parseYaml } from "https://deno.land/[email protected]/encoding/yaml.ts";
22
export { YAMLError } from "https://deno.land/[email protected]/encoding/yaml/error.ts";
33
export { red } from "https://deno.land/[email protected]/fmt/colors.ts";
4+
export { extname, resolve } from "https://deno.land/[email protected]/path/mod.ts";
45

56
// @deno-types="https://unpkg.com/[email protected]/mod.d.ts"
67
export { cac } from "https://unpkg.com/[email protected]/mod.js";

dev_deps.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
export * from "./deps.ts";
2+
13
export {
24
assertEquals,
35
assertArrayContains,
46
assertThrows,
57
assertStrContains,
8+
assertThrowsAsync,
69
} from "https://deno.land/[email protected]/testing/asserts.ts";
710

8-
export { resolve, join } from "https://deno.land/[email protected]/path/mod.ts";
11+
export { join } from "https://deno.land/[email protected]/path/mod.ts";

src/const.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const CURRENT_VERSION = "0.3.0";
1+
const CURRENT_VERSION = "0.4.0";
22
const GITHUB_REPO_NAME = "BentoumiTech/denox";
33
const DENO_WORKSPACE_FILES = [
44
"deno-workspace.yml",
@@ -7,6 +7,10 @@ const DENO_WORKSPACE_FILES = [
77
".deno-workspace",
88
".deno-workspace.yml",
99
".deno-workspace.yaml",
10+
"deno-workspace.json",
11+
".deno-workspace.json",
12+
"deno-workspace.ts",
13+
".deno-workspace.ts",
1014
];
1115

1216
export { CURRENT_VERSION, GITHUB_REPO_NAME, DENO_WORKSPACE_FILES };
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { assertEquals, assertThrows } from "../../../dev_deps.ts";
2+
import { parseDenoOptions } from "../parse.ts";
3+
import { DenoOptionIncorrectType } from "../../utils/DenoXErrors.ts";
4+
5+
Deno.test("deno_options not defined", () => {
6+
const script = { file: "" };
7+
const global = {};
8+
const denoOptions = parseDenoOptions(global, script);
9+
10+
assertEquals(
11+
denoOptions,
12+
[],
13+
);
14+
});
15+
16+
Deno.test("deno_options only defined in globals", () => {
17+
const script = { file: "" };
18+
const global = { deno_options: { "allow-read": true } };
19+
const denoOptions = parseDenoOptions(global, script);
20+
21+
assertEquals(
22+
denoOptions,
23+
["--allow-read"],
24+
);
25+
});
26+
27+
Deno.test("deno_options only defined in script", () => {
28+
const script = { deno_options: { "allow-read": true }, file: "" };
29+
const global = {};
30+
const denoOptions = parseDenoOptions(global, script);
31+
32+
assertEquals(
33+
denoOptions,
34+
["--allow-read"],
35+
);
36+
});
37+
38+
Deno.test("deno_options only defined in script and globals", () => {
39+
const script = { deno_options: { "allow-read": true }, file: "" };
40+
const global = { deno_options: { "allow-write": true } };
41+
const denoOptions = parseDenoOptions(global, script);
42+
43+
assertEquals(
44+
denoOptions,
45+
["--allow-write", "--allow-read"],
46+
);
47+
});
48+
49+
Deno.test("deno_options script overwrite globals one", () => {
50+
const script = { deno_options: { "allow-read": false }, file: "" };
51+
const global = { deno_options: { "allow-read": true } };
52+
const denoOptions = parseDenoOptions(global, script);
53+
54+
assertEquals(
55+
denoOptions,
56+
[],
57+
);
58+
});
59+
60+
Deno.test("validation error get thrown", () => {
61+
const script = { deno_options: { "allow-read": false }, file: "" };
62+
const global = { deno_options: { "allow-hrtime": "string" } };
63+
64+
assertThrows(() => {
65+
parseDenoOptions(global, script);
66+
}, DenoOptionIncorrectType);
67+
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { assertEquals } from "../../../dev_deps.ts";
2+
import { getOptionType } from "../utils.ts";
3+
4+
Deno.test("string[]", () => {
5+
const optionType = getOptionType(["first", "second"]);
6+
7+
assertEquals(
8+
optionType,
9+
"string[]",
10+
);
11+
});
12+
13+
Deno.test("string[] with numbers", () => {
14+
const optionType = getOptionType(["first", 2]);
15+
16+
assertEquals(
17+
optionType,
18+
"string[]",
19+
);
20+
});
21+
22+
Deno.test("mixed[]", () => {
23+
const optionType = getOptionType(["first", { "test": "string" }]);
24+
25+
assertEquals(
26+
optionType,
27+
"mixed[]",
28+
);
29+
});
30+
31+
Deno.test("object", () => {
32+
const optionType = getOptionType({ "test": "string" });
33+
34+
assertEquals(
35+
optionType,
36+
"object",
37+
);
38+
});
39+
40+
Deno.test("string", () => {
41+
const optionType = getOptionType("string");
42+
43+
assertEquals(
44+
optionType,
45+
"string",
46+
);
47+
});
48+
49+
Deno.test("number", () => {
50+
const optionType = getOptionType(1);
51+
52+
assertEquals(
53+
optionType,
54+
"number",
55+
);
56+
});
57+
58+
Deno.test("boolean", () => {
59+
const optionType = getOptionType(true);
60+
61+
assertEquals(
62+
optionType,
63+
"boolean",
64+
);
65+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { assertThrows } from "../../../dev_deps.ts";
2+
import { validateOptions } from "../validate.ts";
3+
import {
4+
DenoOptionNotRecognized,
5+
DenoOptionIncorrectType,
6+
} from "../../utils/DenoXErrors.ts";
7+
8+
Deno.test("invalid option name error get thrown", () => {
9+
const options = { "invalid-option": false };
10+
11+
assertThrows(() => {
12+
validateOptions(options);
13+
}, DenoOptionNotRecognized);
14+
});
15+
16+
Deno.test("invalid option value error get thrown", () => {
17+
const options = { "allow-hrtime": "string" };
18+
19+
assertThrows(() => {
20+
validateOptions(options);
21+
}, DenoOptionIncorrectType);
22+
});
23+
24+
Deno.test("not throw for valid values", () => {
25+
const options = {
26+
"allow-hrtime": true,
27+
"allow-read": ".",
28+
"allow-write": true,
29+
"allow-net": ["google.com", "github.com"],
30+
};
31+
validateOptions(options);
32+
});

0 commit comments

Comments
 (0)