Skip to content

Commit 7d8dd51

Browse files
committed
feat: added ability to parse json5
1 parent 9c1acb9 commit 7d8dd51

File tree

16 files changed

+568
-404
lines changed

16 files changed

+568
-404
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $ npm install -g codify
1818
$ codify COMMAND
1919
running command...
2020
$ codify (--version)
21-
codify/0.7.1 darwin-arm64 node-v20.15.1
21+
codify/0.7.2 darwin-arm64 node-v20.15.1
2222
$ codify --help [COMMAND]
2323
USAGE
2424
$ codify COMMAND
@@ -74,7 +74,7 @@ EXAMPLES
7474
$ codify apply -S <sudo password>
7575
```
7676

77-
_See code: [src/commands/apply.ts](https://github.com/kevinwang5658/codify/blob/v0.7.1/src/commands/apply.ts)_
77+
_See code: [src/commands/apply.ts](https://github.com/kevinwang5658/codify/blob/v0.7.2/src/commands/apply.ts)_
7878

7979
## `codify destroy`
8080

@@ -113,7 +113,7 @@ EXAMPLES
113113
$ codify destroy
114114
```
115115

116-
_See code: [src/commands/destroy.ts](https://github.com/kevinwang5658/codify/blob/v0.7.1/src/commands/destroy.ts)_
116+
_See code: [src/commands/destroy.ts](https://github.com/kevinwang5658/codify/blob/v0.7.2/src/commands/destroy.ts)_
117117

118118
## `codify help [COMMAND]`
119119

@@ -189,7 +189,7 @@ EXAMPLES
189189
$ codify import \*
190190
```
191191

192-
_See code: [src/commands/import.ts](https://github.com/kevinwang5658/codify/blob/v0.7.1/src/commands/import.ts)_
192+
_See code: [src/commands/import.ts](https://github.com/kevinwang5658/codify/blob/v0.7.2/src/commands/import.ts)_
193193

194194
## `codify init`
195195

@@ -217,7 +217,7 @@ EXAMPLES
217217
$ codify init
218218
```
219219

220-
_See code: [src/commands/init.ts](https://github.com/kevinwang5658/codify/blob/v0.7.1/src/commands/init.ts)_
220+
_See code: [src/commands/init.ts](https://github.com/kevinwang5658/codify/blob/v0.7.2/src/commands/init.ts)_
221221

222222
## `codify plan`
223223

@@ -254,7 +254,7 @@ EXAMPLES
254254
$ codify plan -p ../
255255
```
256256

257-
_See code: [src/commands/plan.ts](https://github.com/kevinwang5658/codify/blob/v0.7.1/src/commands/plan.ts)_
257+
_See code: [src/commands/plan.ts](https://github.com/kevinwang5658/codify/blob/v0.7.2/src/commands/plan.ts)_
258258

259259
## `codify update [CHANNEL]`
260260

codify.json

Lines changed: 0 additions & 13 deletions
This file was deleted.

codify.json5

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"type": "project",
4+
"plugins": {
5+
"default": "../codify-homebrew-plugin/src/index.ts"
6+
}
7+
},
8+
{
9+
"type": "macports"
10+
},
11+
// This is a test comment
12+
{
13+
"type": "pnpm",
14+
"globalEnvNodeVersion": null
15+
},
16+
{
17+
"type": "homebrew"
18+
}
19+
]

package-lock.json

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

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
"parse-json": "^8.1.0",
3232
"react": "^18.3.1",
3333
"semver": "^7.5.4",
34-
"supports-color": "^9.4.0"
34+
"supports-color": "^9.4.0",
35+
"json5": "^2.2.3",
36+
"@mischnic/json-sourcemap": "^0.1.1"
3537
},
3638
"description": "Codify allows users to configure settings, install new packages, and automate their systems using code instead of the GUI. Get set up on a new laptop in one click, maintain a Codify file within your project so anyone can get started and never lose your cool apps or favourite settings again.",
3739
"devDependencies": {
@@ -47,6 +49,7 @@
4749
"@types/semver": "^7.5.4",
4850
"@types/strip-ansi": "^5.2.1",
4951
"@typescript-eslint/eslint-plugin": "^8.16.0",
52+
"@types/json5": "^2.2.0",
5053
"codify-plugin-lib": "^1.0.151",
5154
"esbuild": "^0.24.0",
5255
"esbuild-plugin-copy": "^2.1.1",

src/common/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class SyntaxError extends CodifyError {
138138
}
139139

140140
formattedMessage(): string {
141-
return `Syntax error: found in codify.json: ${this.message}`
141+
return `Syntax error: found in ${this.fileName}: ${this.message}`
142142
}
143143
}
144144

src/parser/entities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ export interface LanguageSpecificParser {
1818

1919
export enum FileType {
2020
JSON = 'json',
21-
YAML = 'yaml'
21+
YAML = 'yaml',
22+
JSON5 = 'json5'
2223
}

src/parser/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ import { Project } from '../entities/project.js';
77
import { ConfigFactory } from './config-factory.js';
88
import { FileType, InMemoryFile, ParsedConfig } from './entities.js';
99
import { JsonParser } from './json/json-parser.js';
10+
import { Json5Parser } from './json5/json-parser.js';
1011
import { FileReader } from './reader.js';
1112
import { SourceMapCache } from './source-maps.js';
1213
import { YamlParser } from './yaml/yaml-parser.js';
1314

14-
export const CODIFY_FILE_REGEX = /^(.*)?codify(.json|.yaml)$/;
15+
export const CODIFY_FILE_REGEX = /^(.*)?codify(.json|.yaml|.json5)$/;
1516

1617
class Parser {
1718
private readonly languageSpecificParsers= {
1819
[FileType.JSON]: new JsonParser(),
1920
[FileType.YAML]: new YamlParser(),
21+
[FileType.JSON5]: new Json5Parser()
2022
}
2123

2224
async parse(dirOrFile: string): Promise<Project> {

src/parser/json/json-source-map.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
declare module 'json-source-map' {
22
export interface JsonSourceMap {
3+
data: any;
34
pointers: JsonPointerObject;
45
}
56

src/parser/json/source-map.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { JsonSourceMap } from 'json-source-map';
2+
3+
import { SourceMap, SourceMapPointer } from '../source-maps.js';
4+
5+
export class JsonSourceMapAdapter implements SourceMap {
6+
7+
/**
8+
* {
9+
* "data": [
10+
* {
11+
* "type": "type1"
12+
* },
13+
* {
14+
* "type": "type2",
15+
* "propA": "a",
16+
* "propB": "b"
17+
* }
18+
* ],
19+
* "pointers": {
20+
* "": {
21+
* "value": { "line": 1, "column": 0, "pos": 1 },
22+
* "valueEnd": { "line": 10, "column": 1, "pos": 97 }
23+
* },
24+
* "/0": {
25+
* "value": { "line": 2, "column": 2, "pos": 5 },
26+
* "valueEnd": { "line": 4, "column": 3, "pos": 30 }
27+
* },
28+
* "/0/type": {
29+
* "key": { "line": 3, "column": 4, "pos": 11 },
30+
* "keyEnd": { "line": 3, "column": 10, "pos": 17 },
31+
* "value": { "line": 3, "column": 12, "pos": 19 },
32+
* "valueEnd": { "line": 3, "column": 19, "pos": 26 }
33+
* },
34+
* "/1": {
35+
* "value": { "line": 5, "column": 2, "pos": 34 },
36+
* "valueEnd": { "line": 9, "column": 3, "pos": 95 }
37+
* },
38+
* "/1/type": {
39+
* "key": { "line": 6, "column": 4, "pos": 40 },
40+
* "keyEnd": { "line": 6, "column": 10, "pos": 46 },
41+
* "value": { "line": 6, "column": 12, "pos": 48 },
42+
* "valueEnd": { "line": 6, "column": 19, "pos": 55 }
43+
* },
44+
* "/1/propA": {
45+
* "key": { "line": 7, "column": 4, "pos": 61 },
46+
* "keyEnd": { "line": 7, "column": 11, "pos": 68 },
47+
* "value": { "line": 7, "column": 13, "pos": 70 },
48+
* "valueEnd": { "line": 7, "column": 16, "pos": 73 }
49+
* },
50+
* "/1/propB": {
51+
* "key": { "line": 8, "column": 4, "pos": 79 },
52+
* "keyEnd": { "line": 8, "column": 11, "pos": 86 },
53+
* "value": { "line": 8, "column": 13, "pos": 88 },
54+
* "valueEnd": { "line": 8, "column": 16, "pos": 91 }
55+
* }
56+
* }
57+
* }
58+
*/
59+
private jsonSourceMap: JsonSourceMap;
60+
61+
constructor(jsonSourceMap: JsonSourceMap) {
62+
this.jsonSourceMap = jsonSourceMap;
63+
}
64+
65+
lookup(jsonKey: string): SourceMapPointer | null {
66+
const pointer = this.jsonSourceMap.pointers[jsonKey];
67+
if (!pointer) {
68+
return null;
69+
}
70+
71+
return {
72+
value: {
73+
line: pointer.value.line,
74+
column: pointer.value.column,
75+
position: pointer.value.pos,
76+
},
77+
valueEnd: {
78+
line: pointer.valueEnd.line,
79+
column: pointer.valueEnd.column,
80+
position: pointer.valueEnd.pos,
81+
}
82+
}
83+
}
84+
}
85+

0 commit comments

Comments
 (0)