ppackage 1.0.3

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/.npmignore ADDED
@@ -0,0 +1,7 @@
1
+ /npm-debug.log
2
+ /node_modules
3
+ /coverage
4
+ /test
5
+ /disc
6
+ /.git*
7
+
package/bin/ddocker ADDED
@@ -0,0 +1 @@
1
+ cnyks ddocker --ir://run=$*
@@ -0,0 +1 @@
1
+ @cnyks ddocker --ir://run=%*
package/index.js ADDED
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+
3
+ const fs = require('fs');
4
+ const semver = require('semver');
5
+ const path = require('path');
6
+ const {spawn} = require('child_process');
7
+
8
+ const args = require('nyks/process/parseArgs')();
9
+ const passthru = require('nyks/child_process/passthru');
10
+ const wait = require('nyks/child_process/wait');
11
+
12
+ const Dockerfile = require('./lib/dockerfile');
13
+
14
+
15
+ class ddocker {
16
+
17
+ async version(version = false, notag = false, nonpm = false) {
18
+ let target_version = version || args.args.shift();
19
+
20
+ if(!nonpm && fs.existsSync('package.json') && process.env['npm_lifecycle_event'] != "version")
21
+ throw `Please change version using \`npm version ${target_version}\``;
22
+
23
+ let dirty = await wait(spawn('git', ["diff", "--quiet"])).catch(err => true);
24
+ if(dirty && !notag)
25
+ throw "Working directory not clean, aborting";
26
+
27
+
28
+ const LABEL_VERSION = "org.opencontainers.image.version";
29
+ let body = this._read();
30
+ let current = body.labels[LABEL_VERSION] || "0.0.0";
31
+
32
+ if(!semver.valid(target_version))
33
+ target_version = semver.inc(current, target_version);
34
+
35
+
36
+ if(!target_version)
37
+ throw `Invalid semver range`;
38
+
39
+ body.setLabel(LABEL_VERSION, target_version);
40
+ fs.writeFileSync('Dockerfile', body.toString());
41
+
42
+
43
+ await passthru('git', ['add', 'Dockerfile']);
44
+ if(!notag) {
45
+ await passthru('git', ['commit', '-m', `v${target_version}`, 'Dockerfile']);
46
+ await passthru('git', ['tag', `v${target_version}`]);
47
+ }
48
+ return target_version;
49
+ }
50
+
51
+ _read() {
52
+ let body = fs.readFileSync('Dockerfile', 'utf8');
53
+ let foo = Dockerfile.parse(body);
54
+ return foo;
55
+ }
56
+ }
57
+
58
+
59
+ module.exports = ddocker;
60
+
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+
3
+ const fs = require('fs');
4
+ const semver = require('semver');
5
+ const path = require('path');
6
+
7
+
8
+ const args = require('nyks/process/parseArgs')();
9
+ const passthru = require('nyks/child_process/passthru');
10
+ const escapeRegExp = require('mout/string/escapeRegExp');
11
+ const splitArgs = require('nyks/process/splitArgs');
12
+
13
+ const {tokenize} = require('./util');
14
+
15
+
16
+ const INSTRUCTION = /^\s*#\s+(.*?)\s*=(.*)\s*/;
17
+ const COMMENTS = /^\s*#\s?(.*)/;
18
+ const VERBS = /^\s*(\w+)\s*(.*)/;
19
+
20
+ class Dockerfile {
21
+ static parse(body) {
22
+ let lines = body.trim().split("\n");
23
+
24
+ let out = new Dockerfile();
25
+
26
+ let lookingForDirectives = true;
27
+
28
+ var remainder = '';
29
+ var remainder_raw = '';
30
+ var lineRaw = "";
31
+
32
+ for(let line of lines) {
33
+ let multiline = new RegExp(`(.*)${escapeRegExp(out.escape)}\s*$`);
34
+
35
+ if(lookingForDirectives && INSTRUCTION.test(line)) {
36
+ let [, instruction, value] = INSTRUCTION.exec(line);
37
+ if(instruction == "escape")
38
+ out.escape = value;
39
+ out.raw.push({
40
+ instruction, value,
41
+ toString : function() {
42
+ return `# ${this.instruction}=${this.value}`;
43
+ }
44
+ });
45
+ continue;
46
+ }
47
+
48
+ lookingForDirectives = false
49
+
50
+ if(multiline.test(line)) {
51
+ let raw = multiline.exec(line);
52
+ remainder += raw[1];
53
+ remainder_raw += raw[0] + "\n";
54
+ continue;
55
+ } else {
56
+ lineRaw = remainder_raw + line ;
57
+ line = remainder + line;
58
+ remainder = "";
59
+ remainder_raw = "";
60
+ }
61
+
62
+ let bloc = out.feedLine(line, lineRaw);
63
+ }
64
+ return out;
65
+ }
66
+
67
+ constructor() {
68
+ this.escape = "\\";
69
+ this.raw = [];
70
+ }
71
+
72
+ get labels() {
73
+ let labels = {};
74
+ for(let entry of this.raw) {
75
+ if(entry.VERB == "LABEL")
76
+ for(let k in entry.labels)
77
+ Object.defineProperty(labels, k, {enumerable : true, value : entry.labels[k]});
78
+ }
79
+
80
+ Object.seal(labels);
81
+ return labels;
82
+ }
83
+
84
+
85
+ feedLine(line, raw = undefined) {
86
+ if(!raw)
87
+ raw = line;
88
+
89
+ let what = {
90
+ raw,
91
+ touched : false,
92
+
93
+ toString : function() {
94
+ return this.raw;
95
+ }
96
+ };
97
+
98
+ if(COMMENTS.test(line)) {
99
+ let [, value] = COMMENTS.exec(line);
100
+
101
+
102
+ Object.defineProperties(what, {
103
+ type : {value : "COMMENT"},
104
+ _comment : {value, writable : true},
105
+ comment : {
106
+ get : function() {return this._comment },
107
+ set : function(value) {
108
+ this._comment = value;
109
+ this.raw = `# ${this.comment}`;
110
+ }
111
+ }
112
+ });
113
+ }
114
+
115
+ if(VERBS.test(line)) {
116
+ let [, verb, payload] = VERBS.exec(line);
117
+
118
+ Object.defineProperties(what, {
119
+ type : {value : "VERB"},
120
+ VERB : {value : verb},
121
+ payload : {value : payload},
122
+ });
123
+
124
+ if(verb == "LABEL") {
125
+ Object.defineProperties(what, {
126
+ labels : {value : tokenize(payload) },
127
+ toString : {value : function() {
128
+ if(!this.touched)
129
+ return this.raw; //preseve indent
130
+ let out = `${this.VERB}`;
131
+ for(let k in this.labels)
132
+ out += ` ${JSON.stringify(k)}=${JSON.stringify(this.labels[k])}`;
133
+ return out;
134
+ }}
135
+ });
136
+ }
137
+ }
138
+
139
+
140
+ this.raw.push(what);
141
+ return what;
142
+ }
143
+
144
+ setLabel(label, value) {
145
+ //search for existing lavel
146
+ let existing = this.raw.find(entry => (entry.VERB == "LABEL") && (label in entry.labels));
147
+ if(existing) {
148
+ existing.touched = true;
149
+ existing.labels[label] = value;
150
+ } else {
151
+ this.feedLine(`LABEL ${JSON.stringify(label)}=${JSON.stringify(value)}`);
152
+ }
153
+ }
154
+
155
+ toString() {
156
+ let body = "";
157
+ for(let line of this.raw)
158
+ body += line.toString() + "\n";
159
+
160
+ return body;
161
+ }
162
+ }
163
+
164
+
165
+ module.exports = Dockerfile;
166
+
package/lib/util.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+
3
+
4
+ const mask = "(\\s+=)|(=)|([^\\s\\\"'=]+)|\\\"([^\\\"]*)\\\"|'([^']*)'";
5
+
6
+ const tokenize = function(str) {
7
+ var r = new RegExp(mask, "g");
8
+ var step, sep, value, eql;
9
+ let dict = {};
10
+ let k;
11
+
12
+ while((step = r.exec(str || ""))) {
13
+ sep = step[1] !== undefined;
14
+ value = step[3] || step[4] || step[5] || "";
15
+ eql = step[2] !== undefined;
16
+
17
+ if(sep || eql)
18
+ continue;
19
+ if(!k) {
20
+ k = value;
21
+ } else {
22
+ dict[k] = value;
23
+ k = false;
24
+ }
25
+ }
26
+
27
+ return dict;
28
+
29
+ };
30
+
31
+
32
+ module.exports = {tokenize};
33
+
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "ppackage",
3
+ "version": "1.0.3",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "bin": {
7
+ "ddocker": "./bin/ddocker"
8
+ },
9
+ "dependencies": {
10
+ "mout": "^1.0.0",
11
+ "nyks": "^6.1.8",
12
+ "semver": "^5.3.0"
13
+ },
14
+ "scripts": {
15
+ "test": "echo \"Error: no test specified\" && exit 1"
16
+ },
17
+ "author": "",
18
+ "license": "ISC"
19
+ }