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 +21 -0
- package/README.md +122 -0
- package/clean.d.ts +14 -0
- package/clean.js +97 -0
- package/deparser.d.ts +127 -0
- package/deparser.js +441 -0
- package/esm/clean.js +91 -0
- package/esm/deparser.js +436 -0
- package/esm/index.js +10 -0
- package/esm/lexer.js +412 -0
- package/esm/parser.js +943 -0
- package/esm/types.js +1 -0
- package/index.d.ts +5 -0
- package/index.js +37 -0
- package/lexer.d.ts +117 -0
- package/lexer.js +416 -0
- package/package.json +45 -0
- package/parser.d.ts +150 -0
- package/parser.js +948 -0
- package/types.d.ts +284 -0
- package/types.js +2 -0
package/esm/deparser.js
ADDED
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dockerfile Deparser - converts AST back to source
|
|
3
|
+
*/
|
|
4
|
+
export class Deparser {
|
|
5
|
+
options;
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
this.options = {
|
|
8
|
+
newline: '\n',
|
|
9
|
+
indent: '',
|
|
10
|
+
...options
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Deparse Dockerfile AST to source string
|
|
15
|
+
*/
|
|
16
|
+
deparse(node) {
|
|
17
|
+
if ('type' in node) {
|
|
18
|
+
switch (node.type) {
|
|
19
|
+
case 'Dockerfile':
|
|
20
|
+
return this.deparseDockerfile(node);
|
|
21
|
+
case 'ParserDirective':
|
|
22
|
+
return this.deparseDirective(node);
|
|
23
|
+
case 'Stage':
|
|
24
|
+
return this.deparseStage(node);
|
|
25
|
+
case 'Comment':
|
|
26
|
+
return this.deparseComment(node);
|
|
27
|
+
case 'FromInstruction':
|
|
28
|
+
return this.deparseFrom(node);
|
|
29
|
+
case 'RunInstruction':
|
|
30
|
+
return this.deparseRun(node);
|
|
31
|
+
case 'CmdInstruction':
|
|
32
|
+
return this.deparseCmd(node);
|
|
33
|
+
case 'EntrypointInstruction':
|
|
34
|
+
return this.deparseEntrypoint(node);
|
|
35
|
+
case 'CopyInstruction':
|
|
36
|
+
return this.deparseCopy(node);
|
|
37
|
+
case 'AddInstruction':
|
|
38
|
+
return this.deparseAdd(node);
|
|
39
|
+
case 'EnvInstruction':
|
|
40
|
+
return this.deparseEnv(node);
|
|
41
|
+
case 'ArgInstruction':
|
|
42
|
+
return this.deparseArg(node);
|
|
43
|
+
case 'WorkdirInstruction':
|
|
44
|
+
return this.deparseWorkdir(node);
|
|
45
|
+
case 'UserInstruction':
|
|
46
|
+
return this.deparseUser(node);
|
|
47
|
+
case 'ExposeInstruction':
|
|
48
|
+
return this.deparseExpose(node);
|
|
49
|
+
case 'VolumeInstruction':
|
|
50
|
+
return this.deparseVolume(node);
|
|
51
|
+
case 'LabelInstruction':
|
|
52
|
+
return this.deparseLabel(node);
|
|
53
|
+
case 'ShellInstruction':
|
|
54
|
+
return this.deparseShell(node);
|
|
55
|
+
case 'HealthcheckInstruction':
|
|
56
|
+
return this.deparseHealthcheck(node);
|
|
57
|
+
case 'StopsignalInstruction':
|
|
58
|
+
return this.deparseStopsignal(node);
|
|
59
|
+
case 'OnbuildInstruction':
|
|
60
|
+
return this.deparseOnbuild(node);
|
|
61
|
+
case 'MaintainerInstruction':
|
|
62
|
+
return this.deparseMaintainer(node);
|
|
63
|
+
default:
|
|
64
|
+
throw new Error(`Unknown node type: ${node.type}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
throw new Error('Invalid node');
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Deparse entire Dockerfile
|
|
71
|
+
*/
|
|
72
|
+
deparseDockerfile(dockerfile) {
|
|
73
|
+
const lines = [];
|
|
74
|
+
// Directives first
|
|
75
|
+
for (const directive of dockerfile.directives) {
|
|
76
|
+
lines.push(this.deparseDirective(directive));
|
|
77
|
+
}
|
|
78
|
+
// Then stages
|
|
79
|
+
for (const stage of dockerfile.stages) {
|
|
80
|
+
if (lines.length > 0) {
|
|
81
|
+
lines.push('');
|
|
82
|
+
}
|
|
83
|
+
lines.push(this.deparseStage(stage));
|
|
84
|
+
}
|
|
85
|
+
return lines.join(this.options.newline);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Deparse parser directive
|
|
89
|
+
*/
|
|
90
|
+
deparseDirective(directive) {
|
|
91
|
+
return `# ${directive.directive}=${directive.value}`;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Deparse build stage
|
|
95
|
+
*/
|
|
96
|
+
deparseStage(stage) {
|
|
97
|
+
const lines = [];
|
|
98
|
+
// FROM instruction
|
|
99
|
+
lines.push(this.deparseFrom(stage.from));
|
|
100
|
+
// Other instructions
|
|
101
|
+
for (const instruction of stage.instructions) {
|
|
102
|
+
lines.push(this.deparseInstruction(instruction));
|
|
103
|
+
}
|
|
104
|
+
return lines.join(this.options.newline);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Deparse comment
|
|
108
|
+
*/
|
|
109
|
+
deparseComment(comment) {
|
|
110
|
+
return `# ${comment.value}`;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Deparse any instruction
|
|
114
|
+
*/
|
|
115
|
+
deparseInstruction(instruction) {
|
|
116
|
+
switch (instruction.type) {
|
|
117
|
+
case 'FromInstruction':
|
|
118
|
+
return this.deparseFrom(instruction);
|
|
119
|
+
case 'RunInstruction':
|
|
120
|
+
return this.deparseRun(instruction);
|
|
121
|
+
case 'CmdInstruction':
|
|
122
|
+
return this.deparseCmd(instruction);
|
|
123
|
+
case 'EntrypointInstruction':
|
|
124
|
+
return this.deparseEntrypoint(instruction);
|
|
125
|
+
case 'CopyInstruction':
|
|
126
|
+
return this.deparseCopy(instruction);
|
|
127
|
+
case 'AddInstruction':
|
|
128
|
+
return this.deparseAdd(instruction);
|
|
129
|
+
case 'EnvInstruction':
|
|
130
|
+
return this.deparseEnv(instruction);
|
|
131
|
+
case 'ArgInstruction':
|
|
132
|
+
return this.deparseArg(instruction);
|
|
133
|
+
case 'WorkdirInstruction':
|
|
134
|
+
return this.deparseWorkdir(instruction);
|
|
135
|
+
case 'UserInstruction':
|
|
136
|
+
return this.deparseUser(instruction);
|
|
137
|
+
case 'ExposeInstruction':
|
|
138
|
+
return this.deparseExpose(instruction);
|
|
139
|
+
case 'VolumeInstruction':
|
|
140
|
+
return this.deparseVolume(instruction);
|
|
141
|
+
case 'LabelInstruction':
|
|
142
|
+
return this.deparseLabel(instruction);
|
|
143
|
+
case 'ShellInstruction':
|
|
144
|
+
return this.deparseShell(instruction);
|
|
145
|
+
case 'HealthcheckInstruction':
|
|
146
|
+
return this.deparseHealthcheck(instruction);
|
|
147
|
+
case 'StopsignalInstruction':
|
|
148
|
+
return this.deparseStopsignal(instruction);
|
|
149
|
+
case 'OnbuildInstruction':
|
|
150
|
+
return this.deparseOnbuild(instruction);
|
|
151
|
+
case 'MaintainerInstruction':
|
|
152
|
+
return this.deparseMaintainer(instruction);
|
|
153
|
+
default:
|
|
154
|
+
throw new Error(`Unknown instruction type: ${instruction.type}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Deparse FROM instruction
|
|
159
|
+
*/
|
|
160
|
+
deparseFrom(from) {
|
|
161
|
+
const parts = ['FROM'];
|
|
162
|
+
if (from.platform) {
|
|
163
|
+
parts.push(`--platform=${from.platform}`);
|
|
164
|
+
}
|
|
165
|
+
let imageRef = from.image;
|
|
166
|
+
if (from.tag) {
|
|
167
|
+
imageRef += `:${from.tag}`;
|
|
168
|
+
}
|
|
169
|
+
if (from.digest) {
|
|
170
|
+
imageRef += `@${from.digest}`;
|
|
171
|
+
}
|
|
172
|
+
parts.push(imageRef);
|
|
173
|
+
if (from.name) {
|
|
174
|
+
parts.push('AS', from.name);
|
|
175
|
+
}
|
|
176
|
+
return parts.join(' ');
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Deparse RUN instruction
|
|
180
|
+
*/
|
|
181
|
+
deparseRun(run) {
|
|
182
|
+
const parts = ['RUN'];
|
|
183
|
+
// Mount flags
|
|
184
|
+
if (run.mount) {
|
|
185
|
+
for (const mount of run.mount) {
|
|
186
|
+
parts.push(this.deparseMountFlag(mount));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
// Network flag
|
|
190
|
+
if (run.network) {
|
|
191
|
+
parts.push(`--network=${run.network}`);
|
|
192
|
+
}
|
|
193
|
+
// Security flag
|
|
194
|
+
if (run.security) {
|
|
195
|
+
parts.push(`--security=${run.security}`);
|
|
196
|
+
}
|
|
197
|
+
// Command
|
|
198
|
+
if (run.isExec && Array.isArray(run.command)) {
|
|
199
|
+
parts.push(JSON.stringify(run.command));
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
parts.push(run.command);
|
|
203
|
+
}
|
|
204
|
+
return parts.join(' ');
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Deparse mount flag
|
|
208
|
+
*/
|
|
209
|
+
deparseMountFlag(mount) {
|
|
210
|
+
const opts = [];
|
|
211
|
+
if (mount.type)
|
|
212
|
+
opts.push(`type=${mount.type}`);
|
|
213
|
+
if (mount.target)
|
|
214
|
+
opts.push(`target=${mount.target}`);
|
|
215
|
+
if (mount.source)
|
|
216
|
+
opts.push(`source=${mount.source}`);
|
|
217
|
+
if (mount.from)
|
|
218
|
+
opts.push(`from=${mount.from}`);
|
|
219
|
+
if (mount.id)
|
|
220
|
+
opts.push(`id=${mount.id}`);
|
|
221
|
+
if (mount.sharing)
|
|
222
|
+
opts.push(`sharing=${mount.sharing}`);
|
|
223
|
+
if (mount.readonly)
|
|
224
|
+
opts.push('readonly');
|
|
225
|
+
if (mount.required)
|
|
226
|
+
opts.push('required');
|
|
227
|
+
if (mount.mode)
|
|
228
|
+
opts.push(`mode=${mount.mode}`);
|
|
229
|
+
if (mount.uid)
|
|
230
|
+
opts.push(`uid=${mount.uid}`);
|
|
231
|
+
if (mount.gid)
|
|
232
|
+
opts.push(`gid=${mount.gid}`);
|
|
233
|
+
return `--mount=${opts.join(',')}`;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Deparse CMD instruction
|
|
237
|
+
*/
|
|
238
|
+
deparseCmd(cmd) {
|
|
239
|
+
if (cmd.isExec && Array.isArray(cmd.command)) {
|
|
240
|
+
return `CMD ${JSON.stringify(cmd.command)}`;
|
|
241
|
+
}
|
|
242
|
+
return `CMD ${cmd.command}`;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Deparse ENTRYPOINT instruction
|
|
246
|
+
*/
|
|
247
|
+
deparseEntrypoint(entrypoint) {
|
|
248
|
+
if (entrypoint.isExec && Array.isArray(entrypoint.command)) {
|
|
249
|
+
return `ENTRYPOINT ${JSON.stringify(entrypoint.command)}`;
|
|
250
|
+
}
|
|
251
|
+
return `ENTRYPOINT ${entrypoint.command}`;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Deparse COPY instruction
|
|
255
|
+
*/
|
|
256
|
+
deparseCopy(copy) {
|
|
257
|
+
const parts = ['COPY'];
|
|
258
|
+
if (copy.from)
|
|
259
|
+
parts.push(`--from=${copy.from}`);
|
|
260
|
+
if (copy.chown)
|
|
261
|
+
parts.push(`--chown=${copy.chown}`);
|
|
262
|
+
if (copy.chmod)
|
|
263
|
+
parts.push(`--chmod=${copy.chmod}`);
|
|
264
|
+
if (copy.link)
|
|
265
|
+
parts.push('--link');
|
|
266
|
+
if (copy.parents)
|
|
267
|
+
parts.push('--parents');
|
|
268
|
+
if (copy.exclude) {
|
|
269
|
+
for (const exc of copy.exclude) {
|
|
270
|
+
parts.push(`--exclude=${exc}`);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
parts.push(...copy.sources);
|
|
274
|
+
parts.push(copy.destination);
|
|
275
|
+
return parts.join(' ');
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Deparse ADD instruction
|
|
279
|
+
*/
|
|
280
|
+
deparseAdd(add) {
|
|
281
|
+
const parts = ['ADD'];
|
|
282
|
+
if (add.chown)
|
|
283
|
+
parts.push(`--chown=${add.chown}`);
|
|
284
|
+
if (add.chmod)
|
|
285
|
+
parts.push(`--chmod=${add.chmod}`);
|
|
286
|
+
if (add.link)
|
|
287
|
+
parts.push('--link');
|
|
288
|
+
if (add.checksum)
|
|
289
|
+
parts.push(`--checksum=${add.checksum}`);
|
|
290
|
+
if (add.keepGitDir)
|
|
291
|
+
parts.push('--keep-git-dir');
|
|
292
|
+
if (add.exclude) {
|
|
293
|
+
for (const exc of add.exclude) {
|
|
294
|
+
parts.push(`--exclude=${exc}`);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
parts.push(...add.sources);
|
|
298
|
+
parts.push(add.destination);
|
|
299
|
+
return parts.join(' ');
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Deparse ENV instruction
|
|
303
|
+
*/
|
|
304
|
+
deparseEnv(env) {
|
|
305
|
+
const parts = ['ENV'];
|
|
306
|
+
for (const variable of env.variables) {
|
|
307
|
+
const value = this.needsQuotes(variable.value)
|
|
308
|
+
? `"${variable.value}"`
|
|
309
|
+
: variable.value;
|
|
310
|
+
parts.push(`${variable.key}=${value}`);
|
|
311
|
+
}
|
|
312
|
+
return parts.join(' ');
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Deparse ARG instruction
|
|
316
|
+
*/
|
|
317
|
+
deparseArg(arg) {
|
|
318
|
+
if (arg.defaultValue !== undefined) {
|
|
319
|
+
return `ARG ${arg.name}=${arg.defaultValue}`;
|
|
320
|
+
}
|
|
321
|
+
return `ARG ${arg.name}`;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Deparse WORKDIR instruction
|
|
325
|
+
*/
|
|
326
|
+
deparseWorkdir(workdir) {
|
|
327
|
+
return `WORKDIR ${workdir.path}`;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Deparse USER instruction
|
|
331
|
+
*/
|
|
332
|
+
deparseUser(user) {
|
|
333
|
+
if (user.group) {
|
|
334
|
+
return `USER ${user.user}:${user.group}`;
|
|
335
|
+
}
|
|
336
|
+
return `USER ${user.user}`;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Deparse EXPOSE instruction
|
|
340
|
+
*/
|
|
341
|
+
deparseExpose(expose) {
|
|
342
|
+
const ports = expose.ports.map(p => {
|
|
343
|
+
if (p.protocol) {
|
|
344
|
+
return `${p.port}/${p.protocol}`;
|
|
345
|
+
}
|
|
346
|
+
return String(p.port);
|
|
347
|
+
});
|
|
348
|
+
return `EXPOSE ${ports.join(' ')}`;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Deparse VOLUME instruction
|
|
352
|
+
*/
|
|
353
|
+
deparseVolume(volume) {
|
|
354
|
+
if (volume.paths.length === 1) {
|
|
355
|
+
return `VOLUME ${volume.paths[0]}`;
|
|
356
|
+
}
|
|
357
|
+
return `VOLUME ${JSON.stringify(volume.paths)}`;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Deparse LABEL instruction
|
|
361
|
+
*/
|
|
362
|
+
deparseLabel(label) {
|
|
363
|
+
const parts = ['LABEL'];
|
|
364
|
+
for (const entry of label.labels) {
|
|
365
|
+
const value = this.needsQuotes(entry.value)
|
|
366
|
+
? `"${entry.value}"`
|
|
367
|
+
: entry.value;
|
|
368
|
+
parts.push(`${entry.key}=${value}`);
|
|
369
|
+
}
|
|
370
|
+
return parts.join(' ');
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Deparse SHELL instruction
|
|
374
|
+
*/
|
|
375
|
+
deparseShell(shell) {
|
|
376
|
+
return `SHELL ${JSON.stringify(shell.shell)}`;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Deparse HEALTHCHECK instruction
|
|
380
|
+
*/
|
|
381
|
+
deparseHealthcheck(healthcheck) {
|
|
382
|
+
if (healthcheck.none) {
|
|
383
|
+
return 'HEALTHCHECK NONE';
|
|
384
|
+
}
|
|
385
|
+
const parts = ['HEALTHCHECK'];
|
|
386
|
+
if (healthcheck.interval)
|
|
387
|
+
parts.push(`--interval=${healthcheck.interval}`);
|
|
388
|
+
if (healthcheck.timeout)
|
|
389
|
+
parts.push(`--timeout=${healthcheck.timeout}`);
|
|
390
|
+
if (healthcheck.startPeriod)
|
|
391
|
+
parts.push(`--start-period=${healthcheck.startPeriod}`);
|
|
392
|
+
if (healthcheck.startInterval)
|
|
393
|
+
parts.push(`--start-interval=${healthcheck.startInterval}`);
|
|
394
|
+
if (healthcheck.retries !== undefined)
|
|
395
|
+
parts.push(`--retries=${healthcheck.retries}`);
|
|
396
|
+
if (healthcheck.command) {
|
|
397
|
+
parts.push(this.deparseCmd(healthcheck.command));
|
|
398
|
+
}
|
|
399
|
+
return parts.join(' ');
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Deparse STOPSIGNAL instruction
|
|
403
|
+
*/
|
|
404
|
+
deparseStopsignal(stopsignal) {
|
|
405
|
+
return `STOPSIGNAL ${stopsignal.signal}`;
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Deparse ONBUILD instruction
|
|
409
|
+
*/
|
|
410
|
+
deparseOnbuild(onbuild) {
|
|
411
|
+
return `ONBUILD ${this.deparseInstruction(onbuild.trigger)}`;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Deparse MAINTAINER instruction
|
|
415
|
+
*/
|
|
416
|
+
deparseMaintainer(maintainer) {
|
|
417
|
+
return `MAINTAINER ${maintainer.maintainer}`;
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Check if value needs quotes
|
|
421
|
+
*/
|
|
422
|
+
needsQuotes(value) {
|
|
423
|
+
return value.includes(' ') || value.includes('\t') || value.includes('"') || value.includes("'");
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Deparse Dockerfile AST to source string
|
|
428
|
+
*/
|
|
429
|
+
export function deparse(node, options) {
|
|
430
|
+
const deparser = new Deparser(options);
|
|
431
|
+
return deparser.deparse(node);
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Synchronous deparse (same as deparse, for API consistency)
|
|
435
|
+
*/
|
|
436
|
+
export const deparseSync = deparse;
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Types
|
|
2
|
+
export * from './types';
|
|
3
|
+
// Lexer
|
|
4
|
+
export { Lexer, TokenType } from './lexer';
|
|
5
|
+
// Parser
|
|
6
|
+
export { parse, Parser } from './parser';
|
|
7
|
+
// Deparser
|
|
8
|
+
export { deparse, Deparser, deparseSync } from './deparser';
|
|
9
|
+
// Clean utilities
|
|
10
|
+
export { cleanTree, compareAst, noop } from './clean';
|