Skip to content

Commit f565c05

Browse files
committed
feat!: support browser
1 parent c940bd6 commit f565c05

File tree

10 files changed

+169
-11
lines changed

10 files changed

+169
-11
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"major": "pnpm build && generi log major"
4646
},
4747
"dependencies": {
48+
"c12": "^3.3.1",
4849
"destr": "^2.0.5",
4950
"fs-extra": "^11.3.2",
5051
"pathe": "^2.0.3"

pnpm-lock.yaml

Lines changed: 109 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ export const NULL_KEY = '__NULL_KEY__';
2222

2323
export const MIMETYPE = 'text/nvton';
2424
export const EXTENSION = '.nvton';
25+
26+
export const DEFAULT_CONFIG = {
27+
foo: 'foo',
28+
};

src/data.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
1+
import { loadConfig } from 'c12';
12
import { LexerKey, LexerResult, NvtonOptions } from './types';
2-
import { writeFile } from './utils';
3+
import { isBrowser, writeFile } from './utils';
4+
import { DEFAULT_CONFIG } from './constants';
35

46
export class NVTON {
57
// TODO: unknown deep type
68
private data: Map<LexerKey, unknown>;
7-
private options: NvtonOptions;
9+
private options: NvtonOptions = DEFAULT_CONFIG;
810

9-
constructor(lexeme: LexerResult) {
11+
private async loadDefaultConfig(options?: NvtonOptions) {
12+
try {
13+
if (!isBrowser) {
14+
// TODO: awaitable load fix config
15+
const { config } = await loadConfig({
16+
name: 'nvton',
17+
rcFile: false,
18+
envName: false,
19+
});
20+
21+
this.options = options ?? config ?? DEFAULT_CONFIG;
22+
} else {
23+
this.options = options ?? DEFAULT_CONFIG;
24+
}
25+
} catch (e) {
26+
this.options = options ?? DEFAULT_CONFIG;
27+
}
28+
}
29+
30+
constructor(lexeme: LexerResult, options?: NvtonOptions) {
1031
this.data = new Map();
11-
this.options = {};
32+
this.loadDefaultConfig(options);
1233
this.set(lexeme);
1334
}
1435

@@ -28,6 +49,10 @@ export class NVTON {
2849
}
2950

3051
public write(path: string) {
52+
if (isBrowser) {
53+
throw new Error(`Browser setups don't support write function!`);
54+
}
55+
3156
const filepath = path.endsWith('.nvton') ? path : `${path}.nvton`;
3257
// TODO: format .nvton file
3358
writeFile(filepath, '');

src/pipeline.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
import { getFile } from './utils';
1+
import { getFile, isBrowser } from './utils';
22
import { lex } from './lexer';
33
import { NVTON } from './data';
44

55
const get = (raw: string): NVTON => {
6-
const resolved = (raw.endsWith('.nvton') ? getFile(raw) : raw)?.trim();
6+
const isFilepath = raw.endsWith('.nvton');
77

8-
if (!resolved && resolved !== '') {
9-
throw new Error(`${raw} file not found!`);
8+
if (isFilepath && isBrowser) {
9+
throw new Error(
10+
'get() function only support read files in node, bun or deno environments.'
11+
);
12+
}
13+
14+
const resolved = (isFilepath ? getFile(raw) : raw)?.trim();
15+
16+
if (!resolved) {
17+
throw new Error(`${raw} file not found or datatype is wrong!`);
1018
}
1119

1220
const data = lex(resolved);

src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ export const getFile = (target: string): Maybe<string> => {
2020

2121
return readFileSync(path).toString('utf8') || undefined;
2222
};
23+
24+
export const isBrowser: boolean =
25+
typeof window !== 'undefined' && typeof window.document !== 'undefined';
26+
27+
export const isNode: boolean =
28+
typeof process !== 'undefined' &&
29+
process.versions != null &&
30+
process.versions.node != null;

test/nvton.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ describe('NVTON', () => {
88
expect(data.get('0')).toEqual(0);
99
});
1010
it('expect error in read', () => {
11-
expect(() => nvton('null.nvton')).toThrowError('null.nvton file not found!');
11+
expect(() => nvton('null.nvton')).toThrowError(
12+
'null.nvton file not found or datatype is wrong!'
13+
);
1214
});
1315
});

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"strict": true,
99
"resolveJsonModule": true,
1010
"esModuleInterop": true,
11-
"lib": ["es2015"],
11+
"lib": ["es2015", "dom"],
1212
},
1313
"include": ["src/**/*.ts"]
1414
}

tsdown.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineConfig } from 'tsdown';
33
export default defineConfig({
44
entry: 'src/index.ts',
55
format: ['esm', 'cjs'],
6+
target: ['es2015', 'node18'],
67
clean: true,
78
dts: true
89
})

0 commit comments

Comments
 (0)