Skip to content

Commit

Permalink
refactor(*)!: better structure pkgs
Browse files Browse the repository at this point in the history
  • Loading branch information
denizenging committed Aug 1, 2023
1 parent b7fd3fd commit 9b0c77b
Show file tree
Hide file tree
Showing 42 changed files with 753 additions and 2,052 deletions.
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@
!/package.json
!/pnpm-lock.yaml
!/pnpm-workspace.yaml
!/tsconfig.json
!/jest.config.ts
!/.prettierrc
!/.prettierignore
# shared by pkgs
!/tsconfig.json
!/.npmignore

# useful stuff
!/.editorconfig
!/.cruft.json

# packages
pkg/*/node_modules/
!/pkg/*/src/**/*.ts
!/pkg/*/test/**/*.ts
!/pkg/*/package.json
!/pkg/*/pnpm-lock.yaml
!/pkg/**/*.ts
!/pkg/*/tsconfig.json
!/pkg/*/.npmignore
11 changes: 4 additions & 7 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# blacklist everything
*
*/
!*/

# useful stuff
!/package.json

# built files and types
!/.build/*/*.js
!/.build/*/*.js.map
!/.build/*/*.d.ts
!/build/src/**/*.js
!/build/src/**/*.js.map
!/build/src/**/*.d.ts
14 changes: 0 additions & 14 deletions jest.config.ts

This file was deleted.

21 changes: 9 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,24 @@
},
"license": "AGPL-3.0-only",
"scripts": {
"lint": "eslint --ext .ts pkg/ jest.config.ts",
"test": "jest",
"coverage": "jest --coverage",
"clean": "rm -rf .build .coverage",
"ts-node": "ts-node",
"build": "tsc",
"build-all": "pnpm build",
"format": "prettier --ignore-path .gitignore --write '**/*.+(js|ts|json|html)'"
"lint": "eslint --ext .ts pkg/*/src pkg/*/test",
"format": "prettier --ignore-path .gitignore --write '**/*.+(js|ts|json|html)'",
"test": "pnpm build && pnpm -F @slangroom/* exec ava",
"coverage": "c8 -o .coverage --exclude '**/test/' pnpm test",
"clean": "rm -rf .coverage && pnpm -F @slangroom/* exec -- rm -rf build",
"build": "pnpm -F @slangroom/* exec tsc",
"build-all": "pnpm build"
},
"devDependencies": {
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
"ava": "^5.3.1",
"c8": "^8.0.1",
"esbuild": "^0.18.4",
"eslint": "^8.43.0",
"eslint-config-prettier": "^8.8.0",
"jest": "29.5.0",
"jest-junit": "^16.0.0",
"prettier": "^2.8.8",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"tslib": "^2.5.3",
"typedoc": "^0.24.8",
Expand Down
1 change: 1 addition & 0 deletions pkg/deps/.npmignore
3 changes: 2 additions & 1 deletion pkg/deps/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "@slangroom/deps",
"exports": {
"*": "../../.build/deps/*.js"
".": "./build/src/index.js",
"./*": "./build/src/*.js"
},
"version": "1.0.0",
"type": "module",
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions pkg/deps/test/make-ava-happy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import test from 'ava';

test('ava is happy', (t) => t.true(true));
1 change: 1 addition & 0 deletions pkg/deps/tsconfig.json
1 change: 1 addition & 0 deletions pkg/fs/.npmignore
4 changes: 0 additions & 4 deletions pkg/fs/index.ts

This file was deleted.

65 changes: 0 additions & 65 deletions pkg/fs/lexer.test.ts

This file was deleted.

3 changes: 2 additions & 1 deletion pkg/fs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "@slangroom/fs",
"exports": {
"*": "../../.build/fs/*.js"
".": "./build/src/index.js",
"./*": "./build/src/*.js"
},
"version": "1.0.0",
"type": "module",
Expand Down
4 changes: 4 additions & 0 deletions pkg/fs/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from '@slangroom/fs/lexer';
export * from '@slangroom/fs/parser';
export * from '@slangroom/fs/tokens';
export * from '@slangroom/fs/visitor';
3 changes: 1 addition & 2 deletions pkg/fs/lexer.ts → pkg/fs/src/lexer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { vocab } from './tokens';

import { vocab } from '@slangroom/fs/tokens';
import { Lexer } from '@slangroom/deps/chevrotain';

const FsLexer = new Lexer(vocab);
Expand Down
5 changes: 2 additions & 3 deletions pkg/fs/parser.ts → pkg/fs/src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { IntoTheFile, SaveThe, vocab } from './tokens';
import { lex } from './lexer';

import { IntoTheFile, SaveThe, vocab } from '@slangroom/fs/tokens';
import { lex } from '@slangroom/fs/lexer';
import { CstParser, type IToken } from '@slangroom/deps/chevrotain';
import { Then, I, Identifier } from '@slangroom/shared/tokens';

Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions pkg/fs/visitor.ts → pkg/fs/src/visitor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BaseFsVisitor, parse, type FileOverrideStatementCtx } from './parser';

import { BaseFsVisitor, parse, type FileOverrideStatementCtx } from '@slangroom/fs/parser';
import type { CstNode } from '@slangroom/deps/chevrotain';

export type FileOverrideStatement = {
Expand Down
89 changes: 43 additions & 46 deletions pkg/fs/visitor.test.ts → pkg/fs/test/visitor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { visit } from './visitor';

import test from 'ava';
import { visit } from '@slangroom/fs/visitor';
import { getIgnoredStatements } from '@slangroom/ignored';
import { zencodeExec } from '@slangroom/shared';

import * as fs from 'node:fs/promises';
import * as path from 'node:path';

test('ast is correct with one statement', async () => {
test('ast is correct with one statement', async (t) => {
// Given I have a contract with one filesystems statement in it
const contract = `Rule unknown ignore
Given I have a 'string' named 'stringToWrite'
Expand All @@ -25,20 +24,19 @@ Then I save the 'stringToWrite' into the file 'nameOfTheFile'
// and I generate AST of each of them
const asts = ignoreds.map((x) => visit(x));
// Then the result must contain only one item
expect(asts).toHaveLength(1);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const ast = asts[0]!;
t.is(asts?.length, 1);
const ast = asts[0];
// and its content must be "stringToWrite"
expect(ast.content).toStrictEqual('stringToWrite');
t.is(ast?.content, 'stringToWrite');
// and its filename must be "nameOfTheFile"
expect(ast.filename).toStrictEqual('nameOfTheFile');
t.is(ast?.filename, 'nameOfTheFile');
// and the value indexed by its content in data must be data's stringToWrite
expect(data[ast.content as 'stringToWrite']).toStrictEqual(data.stringToWrite);
t.is(data[ast?.content as 'stringToWrite'], data.stringToWrite);
// and the value indexed by its filename in data must be data's nameOfTheFile
expect(data[ast.filename as 'nameOfTheFile']).toStrictEqual(data.nameOfTheFile);
t.is(data[ast?.filename as 'nameOfTheFile'], data.nameOfTheFile);
});

test('ast is correct with multiple statements', async () => {
test('ast is correct with multiple statements', async (t) => {
// Given I have a contract with multiple filesystems statements in it
const contract = `Rule unknown ignore
Given I have a 'string' named 'stringToWrite0'
Expand All @@ -64,46 +62,45 @@ Then I save the 'stringToWrite2' into the file 'nameOfTheFile2'
const ignoreds = await getIgnoredStatements(contract, {
data: data,
});
// and I generate AST of each of them
// And I generate AST of each of them
const asts = ignoreds.map((x) => visit(x));
// Then the result must contain 3 items
expect(asts).toHaveLength(3);
// and I get the first one
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const first = asts[0]!;
// and the its content must be "stringToWrite0"
expect(first.content).toStrictEqual('stringToWrite0');
// and the its filename must be "nameOfTheFile"
expect(first.filename).toStrictEqual('nameOfTheFile0');
// and the value indexed by its content in data must be data's stringToWrite0
expect(data[first.content as 'stringToWrite0']).toStrictEqual(data.stringToWrite0);
// and the value indexed by its filename in data must be data's nameOfTheFile0
expect(data[first.filename as 'nameOfTheFile0']).toStrictEqual(data.nameOfTheFile0);
// and I get the second one
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const second = asts[1]!;
t.is(asts.length, 3);
// And I get the first one
const first = asts[0];
// And the its content must be "stringToWrite0"
t.is(first?.content, 'stringToWrite0');
// And the its filename must be "nameOfTheFile"
t.is(first?.filename, 'nameOfTheFile0');
// And the value indexed by its content in data must be data's stringToWrite0
t.is(data[first?.content as 'stringToWrite0'], data.stringToWrite0);
// And the value indexed by its filename in data must be data's nameOfTheFile0
t.is(data[first?.filename as 'nameOfTheFile0'], data.nameOfTheFile0);

// Then get the second one
const second = asts[1];
// and the its content must be "stringToWrite0"
expect(second.content).toStrictEqual('stringToWrite1');
t.is(second?.content, 'stringToWrite1');
// and the its filename must be "nameOfTheFile"
expect(second.filename).toStrictEqual('nameOfTheFile1');
t.is(second?.filename, 'nameOfTheFile1');
// and the value indexed by its content in data must be data's stringToWrite1
expect(data[second.content as 'stringToWrite1']).toStrictEqual(data.stringToWrite1);
t.is(data[second?.content as 'stringToWrite1'], data.stringToWrite1);
// and the value indexed by its filename in data must be data's nameOfTheFile1
expect(data[second.filename as 'nameOfTheFile1']).toStrictEqual(data.nameOfTheFile1);
// and I get the third one
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const third = asts[2]!;
// and the its content must be "stringToWrite2"
expect(third.content).toStrictEqual('stringToWrite2');
// and the its filename must be "nameOfTheFile"
expect(third.filename).toStrictEqual('nameOfTheFile2');
// and the value indexed by its content in data must be data's stringToWrite2
expect(data[third.content as 'stringToWrite2']).toStrictEqual(data.stringToWrite2);
// and the value indexed by its filename in data must be data's nameOfTheFile2
expect(data[third.filename as 'nameOfTheFile2']).toStrictEqual(data.nameOfTheFile2);
t.is(data[second?.filename as 'nameOfTheFile1'], data.nameOfTheFile1);

// Then I get the third one
const third = asts[2];
// And the its content must be "stringToWrite2"
t.is(third?.content, 'stringToWrite2');
// And the its filename must be "nameOfTheFile"
t.is(third?.filename, 'nameOfTheFile2');
// And the value indexed by its content in data must be data's stringToWrite2
t.is(data[third?.content as 'stringToWrite2'], data.stringToWrite2);
// And the value indexed by its filename in data must be data's nameOfTheFile2
t.is(data[third?.filename as 'nameOfTheFile2'], data.nameOfTheFile2);
});

test('keyholder works', async () => {
test('keyholder works', async (t) => {
// Given I have a contract with one filesystems statement in it
const contract = `Rule unknown ignore
Given I have a 'string' named 'nameOfTheFile'
Expand All @@ -115,7 +112,7 @@ Then I save the 'stringToWrite' into the file 'nameOfTheFile'
Then I print the 'stringToWrite'
Then I print the 'nameOfTheFile'
`;
// and the params used in the contract
// And the params used in the contract
const params = { data: { nameOfTheFile: 'hello-world.txt' } };
// When I get the ignored statement of it
const ignoreds = await getIgnoredStatements(contract, params);
Expand All @@ -135,5 +132,5 @@ Then I print the 'nameOfTheFile'
// And write the value of stringToWrite to the file
const { buffer } = await fh.write(stringToWrite);
// Then the content of the file must be stringToWrite
expect(buffer).toStrictEqual(stringToWrite);
t.is(buffer, stringToWrite);
});
1 change: 1 addition & 0 deletions pkg/fs/tsconfig.json
1 change: 1 addition & 0 deletions pkg/ignored/.npmignore
3 changes: 2 additions & 1 deletion pkg/ignored/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "@slangroom/ignored",
"exports": {
"*": "../../.build/ignored/*.js"
".": "./build/src/index.js",
"./*": "./build/src/*.js"
},
"version": "1.0.0",
"type": "module",
Expand Down
5 changes: 2 additions & 3 deletions pkg/ignored/index.ts → pkg/ignored/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { vocab } from './tokens';

import { zencodeExec, type ZenroomParams } from '@slangroom/shared';
import { Lexer } from '@slangroom/deps/chevrotain';
import { vocab } from '@slangroom/ignored/tokens';
import { zencodeExec, type ZenroomParams } from '@slangroom/shared';

const IgnoredLexer = new Lexer(vocab);

Expand Down
File renamed without changes.
Loading

0 comments on commit 9b0c77b

Please sign in to comment.