docker-parser 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Constructive <developers@constructive.io>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # docker-parser
2
+
3
+ <p align="center" width="100%">
4
+ <img height="250" src="https://raw.githubusercontent.com/Safegres/brand/refs/heads/main/safegres.svg" />
5
+ </p>
6
+
7
+ Dockerfile parser and deparser for JavaScript/TypeScript.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install docker-parser
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Parsing
18
+
19
+ ```typescript
20
+ import { parse } from 'docker-parser';
21
+
22
+ const source = `
23
+ FROM node:18-alpine
24
+ WORKDIR /app
25
+ COPY package.json .
26
+ RUN npm install
27
+ COPY . .
28
+ EXPOSE 3000
29
+ CMD ["node", "server.js"]
30
+ `;
31
+
32
+ const ast = parse(source);
33
+ console.log(JSON.stringify(ast, null, 2));
34
+ ```
35
+
36
+ ### Deparsing
37
+
38
+ ```typescript
39
+ import { parse, deparse } from 'docker-parser';
40
+
41
+ const source = 'FROM node:18-alpine';
42
+ const ast = parse(source);
43
+ const output = deparse(ast);
44
+ console.log(output); // FROM node:18-alpine
45
+ ```
46
+
47
+ ### AST Comparison
48
+
49
+ ```typescript
50
+ import { parse, cleanTree } from 'docker-parser';
51
+
52
+ const ast1 = parse('FROM node:18');
53
+ const ast2 = parse('FROM node:18');
54
+
55
+ const clean1 = cleanTree(ast1);
56
+ const clean2 = cleanTree(ast2);
57
+
58
+ // Compare ASTs without position information
59
+ console.log(JSON.stringify(clean1) === JSON.stringify(clean2)); // true
60
+ ```
61
+
62
+ ## Supported Instructions
63
+
64
+ - FROM (with platform, AS alias, digest)
65
+ - RUN (shell and exec form, with mount flags)
66
+ - CMD (shell and exec form)
67
+ - ENTRYPOINT (shell and exec form)
68
+ - COPY (with --from, --chown, --chmod, --link)
69
+ - ADD (with --chown, --chmod, --checksum)
70
+ - ENV
71
+ - ARG
72
+ - WORKDIR
73
+ - USER
74
+ - EXPOSE
75
+ - VOLUME
76
+ - LABEL
77
+ - SHELL
78
+ - HEALTHCHECK
79
+ - STOPSIGNAL
80
+ - ONBUILD
81
+ - MAINTAINER (deprecated)
82
+
83
+ ## License
84
+
85
+ MIT
86
+
87
+ ---
88
+
89
+ ## Development
90
+
91
+ ### Setup
92
+
93
+ 1. Clone the repository:
94
+
95
+ ```bash
96
+ git clone https://github.com/constructive-io/dev-utils.git
97
+ ```
98
+
99
+ 2. Install dependencies:
100
+
101
+ ```bash
102
+ cd dev-utils
103
+ pnpm install
104
+ pnpm build
105
+ ```
106
+
107
+ 3. Test the package of interest:
108
+
109
+ ```bash
110
+ cd packages/<packagename>
111
+ pnpm test:watch
112
+ ```
113
+
114
+ ## Credits
115
+
116
+ **🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
117
+
118
+ ## Disclaimer
119
+
120
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
121
+
122
+ No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
package/clean.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { Dockerfile, Node } from './types';
2
+ /**
3
+ * No-op transform function - removes the property
4
+ */
5
+ export declare const noop: () => undefined;
6
+ /**
7
+ * Clean AST tree by removing location/position information
8
+ * This is used for comparing ASTs in round-trip testing
9
+ */
10
+ export declare function cleanTree<T extends Node | Dockerfile>(tree: T): T;
11
+ /**
12
+ * Compare two ASTs for equality after cleaning
13
+ */
14
+ export declare function compareAst(ast1: any, ast2: any): boolean;
package/clean.js ADDED
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.noop = void 0;
4
+ exports.cleanTree = cleanTree;
5
+ exports.compareAst = compareAst;
6
+ /**
7
+ * No-op transform function - removes the property
8
+ */
9
+ const noop = () => undefined;
10
+ exports.noop = noop;
11
+ /**
12
+ * Deep transform an object
13
+ */
14
+ function transform(obj, options) {
15
+ if (obj === null || obj === undefined) {
16
+ return obj;
17
+ }
18
+ if (Array.isArray(obj)) {
19
+ return obj.map(item => transform(item, options));
20
+ }
21
+ if (typeof obj === 'object') {
22
+ const result = {};
23
+ for (const key of Object.keys(obj)) {
24
+ const value = obj[key];
25
+ // Check if there's a transform function for this key
26
+ if (key in options) {
27
+ const transformed = options[key](value, key, obj);
28
+ if (transformed !== undefined) {
29
+ result[key] = transformed;
30
+ }
31
+ // If undefined, skip the property (remove it)
32
+ }
33
+ else {
34
+ // Recursively transform nested objects
35
+ result[key] = transform(value, options);
36
+ }
37
+ }
38
+ return result;
39
+ }
40
+ return obj;
41
+ }
42
+ /**
43
+ * Clean AST tree by removing location/position information
44
+ * This is used for comparing ASTs in round-trip testing
45
+ */
46
+ function cleanTree(tree) {
47
+ return transform(tree, {
48
+ // Remove range/position information
49
+ range: exports.noop,
50
+ // Normalize whitespace in command strings
51
+ command: (value) => {
52
+ if (typeof value === 'string') {
53
+ return value.trim().replace(/\s+/g, ' ');
54
+ }
55
+ if (Array.isArray(value)) {
56
+ return value.map(v => typeof v === 'string' ? v.trim() : v);
57
+ }
58
+ return value;
59
+ },
60
+ // Normalize path strings
61
+ path: (value) => {
62
+ if (typeof value === 'string') {
63
+ return value.trim();
64
+ }
65
+ return value;
66
+ },
67
+ // Normalize maintainer strings
68
+ maintainer: (value) => {
69
+ if (typeof value === 'string') {
70
+ return value.trim();
71
+ }
72
+ return value;
73
+ },
74
+ // Normalize signal strings
75
+ signal: (value) => {
76
+ if (typeof value === 'string') {
77
+ return value.trim();
78
+ }
79
+ return value;
80
+ },
81
+ // Normalize value strings in env/label
82
+ value: (value) => {
83
+ if (typeof value === 'string') {
84
+ return value.trim();
85
+ }
86
+ return value;
87
+ }
88
+ });
89
+ }
90
+ /**
91
+ * Compare two ASTs for equality after cleaning
92
+ */
93
+ function compareAst(ast1, ast2) {
94
+ const clean1 = cleanTree(ast1);
95
+ const clean2 = cleanTree(ast2);
96
+ return JSON.stringify(clean1) === JSON.stringify(clean2);
97
+ }
package/deparser.d.ts ADDED
@@ -0,0 +1,127 @@
1
+ import { Dockerfile, Node } from './types';
2
+ /**
3
+ * Deparser options
4
+ */
5
+ export interface DeparserOptions {
6
+ newline?: string;
7
+ indent?: string;
8
+ }
9
+ /**
10
+ * Dockerfile Deparser - converts AST back to source
11
+ */
12
+ export declare class Deparser {
13
+ private options;
14
+ constructor(options?: DeparserOptions);
15
+ /**
16
+ * Deparse Dockerfile AST to source string
17
+ */
18
+ deparse(node: Dockerfile | Node): string;
19
+ /**
20
+ * Deparse entire Dockerfile
21
+ */
22
+ private deparseDockerfile;
23
+ /**
24
+ * Deparse parser directive
25
+ */
26
+ private deparseDirective;
27
+ /**
28
+ * Deparse build stage
29
+ */
30
+ private deparseStage;
31
+ /**
32
+ * Deparse comment
33
+ */
34
+ private deparseComment;
35
+ /**
36
+ * Deparse any instruction
37
+ */
38
+ private deparseInstruction;
39
+ /**
40
+ * Deparse FROM instruction
41
+ */
42
+ private deparseFrom;
43
+ /**
44
+ * Deparse RUN instruction
45
+ */
46
+ private deparseRun;
47
+ /**
48
+ * Deparse mount flag
49
+ */
50
+ private deparseMountFlag;
51
+ /**
52
+ * Deparse CMD instruction
53
+ */
54
+ private deparseCmd;
55
+ /**
56
+ * Deparse ENTRYPOINT instruction
57
+ */
58
+ private deparseEntrypoint;
59
+ /**
60
+ * Deparse COPY instruction
61
+ */
62
+ private deparseCopy;
63
+ /**
64
+ * Deparse ADD instruction
65
+ */
66
+ private deparseAdd;
67
+ /**
68
+ * Deparse ENV instruction
69
+ */
70
+ private deparseEnv;
71
+ /**
72
+ * Deparse ARG instruction
73
+ */
74
+ private deparseArg;
75
+ /**
76
+ * Deparse WORKDIR instruction
77
+ */
78
+ private deparseWorkdir;
79
+ /**
80
+ * Deparse USER instruction
81
+ */
82
+ private deparseUser;
83
+ /**
84
+ * Deparse EXPOSE instruction
85
+ */
86
+ private deparseExpose;
87
+ /**
88
+ * Deparse VOLUME instruction
89
+ */
90
+ private deparseVolume;
91
+ /**
92
+ * Deparse LABEL instruction
93
+ */
94
+ private deparseLabel;
95
+ /**
96
+ * Deparse SHELL instruction
97
+ */
98
+ private deparseShell;
99
+ /**
100
+ * Deparse HEALTHCHECK instruction
101
+ */
102
+ private deparseHealthcheck;
103
+ /**
104
+ * Deparse STOPSIGNAL instruction
105
+ */
106
+ private deparseStopsignal;
107
+ /**
108
+ * Deparse ONBUILD instruction
109
+ */
110
+ private deparseOnbuild;
111
+ /**
112
+ * Deparse MAINTAINER instruction
113
+ */
114
+ private deparseMaintainer;
115
+ /**
116
+ * Check if value needs quotes
117
+ */
118
+ private needsQuotes;
119
+ }
120
+ /**
121
+ * Deparse Dockerfile AST to source string
122
+ */
123
+ export declare function deparse(node: Dockerfile | Node, options?: DeparserOptions): string;
124
+ /**
125
+ * Synchronous deparse (same as deparse, for API consistency)
126
+ */
127
+ export declare const deparseSync: typeof deparse;