ppackage 2.1.1 → 2.2.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/.eslintrc ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "root": true,
3
+ "extends": [
4
+ "plugin:ivs/node"
5
+ ]
6
+ }
package/.npmignore ADDED
@@ -0,0 +1,7 @@
1
+ /npm-debug.log
2
+ /node_modules
3
+ /coverage
4
+ /test
5
+ /disc
6
+ /.git*
7
+
package/index.js CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  const fs = require('fs');
4
4
  const semver = require('semver');
5
- const path = require('path');
6
5
  const {spawn} = require('child_process');
7
6
 
8
7
  const args = require('nyks/process/parseArgs')();
@@ -10,6 +9,14 @@ const passthru = require('nyks/child_process/passthru');
10
9
  const wait = require('nyks/child_process/wait');
11
10
 
12
11
  const Dockerfile = require('./lib/dockerfile');
12
+ const {Parser, Composer} = require('yaml');
13
+
14
+
15
+ const laxParser = function(body) {
16
+ const tokens = new Parser().parse(body);
17
+ const docs = new Composer({merge : true, uniqueKeys : false}).compose(tokens);
18
+ return docs.next().value;
19
+ };
13
20
 
14
21
 
15
22
  class ppackage {
@@ -17,37 +24,86 @@ class ppackage {
17
24
  async version(version = false, notag = false) {
18
25
  let current_version;
19
26
 
27
+
28
+ const DOCKER_LABEL_VERSION = "org.opencontainers.image.version";
29
+ const GITLAB_PATH_VERSION = ".version";
30
+
20
31
  let modes = {
21
- composer : { enabled : fs.existsSync('composer.json') },
22
- docker : { enabled : fs.existsSync('Dockerfile') },
23
- npm : { enabled : fs.existsSync('package.json') },
32
+ gitlab : {
33
+ file : '.gitlab-ci.yml',
34
+ analyze : function() {
35
+ const body = fs.readFileSync(this.file, 'utf-8');
36
+ this.meta = laxParser(body);
37
+ current_version = this.meta.get(GITLAB_PATH_VERSION);
38
+ },
39
+ commit : function({target_version, files}) {
40
+ let opts = {lineWidth : 0};
41
+ this.meta.set(GITLAB_PATH_VERSION, target_version);
42
+ fs.writeFileSync(this.file, this.meta.toString(opts));
43
+ files.push(this.file);
44
+ }
45
+
46
+ },
47
+ composer : {
48
+ file : 'composer.json',
49
+ analyze : function() {
50
+ const body = fs.readFileSync(this.file, 'utf-8');
51
+ this.meta = JSON.parse(body);
52
+ current_version = this.meta.version;
53
+ },
54
+ commit : function({target_version, files}) {
55
+ this.meta.version = target_version;
56
+ fs.writeFileSync(this.file, JSON.stringify(this.meta, null, 2));
57
+ files.push(this.file);
58
+ }
59
+ },
60
+ docker : {
61
+ file : 'Dockerfile',
62
+ analyze : function () {
63
+ const body = fs.readFileSync(this.file, 'utf-8');
64
+ this.meta = Dockerfile.parse(body);
65
+ current_version = this.meta.labels[DOCKER_LABEL_VERSION];
66
+ },
67
+ commit : function(target_version) {
68
+ modes.docker.meta.setLabel(DOCKER_LABEL_VERSION, target_version);
69
+ fs.writeFileSync(this.file, this.meta.toString());
70
+ files.push(this.file);
71
+ }
72
+ },
73
+
74
+ npm : {
75
+ file : 'package.json',
76
+ analyze : function () {
77
+ const body = fs.readFileSync(this.file, 'utf-8');
78
+ this.meta = JSON.parse(body);
79
+ current_version = this.meta.version;
80
+ },
81
+ commit : function({target_version, files}) {
82
+ this.meta.version = target_version;
83
+ fs.writeFileSync(this.file, JSON.stringify(this.meta, null, 2));
84
+ files.push(this.file);
85
+ }
86
+
87
+ }
24
88
  };
25
89
 
26
- const DOCKER_LABEL_VERSION = "org.opencontainers.image.version";
90
+ // prepare modes
91
+ for(let [mode_n, mode] of Object.entries(modes)) {
92
+ if(!fs.existsSync(mode.file)) {
93
+ delete modes[mode_n];
94
+ continue;
95
+ }
96
+ mode.analyze.call(mode);
97
+ }
98
+
99
+
27
100
 
28
101
  let target_version = version || args.args.shift();
29
102
 
30
- let dirty = await wait(spawn('git', ["diff-index", "--quiet", "HEAD"])).catch(err => true);
103
+ let dirty = await wait(spawn('git', ["diff-index", "--quiet", "HEAD"])).catch(() => true);
31
104
  if(dirty && !notag)
32
105
  throw "Working directory not clean, aborting";
33
106
 
34
- if(modes.composer.enabled) {
35
- let body = fs.readFileSync('composer.json', 'utf-8');
36
- modes.composer.meta = JSON.parse(body);
37
- current_version = modes.composer.meta.version;
38
- }
39
-
40
- if(modes.npm.enabled) {
41
- let body = fs.readFileSync('package.json', 'utf-8');
42
- modes.npm.meta = JSON.parse(body);
43
- current_version = modes.npm.meta.version;
44
- }
45
-
46
- if(modes.docker.enabled) {
47
- let body = fs.readFileSync('Dockerfile', 'utf8');
48
- modes.docker.meta = Dockerfile.parse(body);
49
- current_version = modes.docker.meta.labels[DOCKER_LABEL_VERSION];
50
- }
51
107
 
52
108
  if(!current_version)
53
109
  current_version = "0.0.0";
@@ -58,32 +114,18 @@ class ppackage {
58
114
  if(!target_version)
59
115
  throw `Invalid semver range`;
60
116
 
61
- if(modes.npm.enabled && !process.env['npm_package_version']) {
62
- let version_hook = modes.npm.meta.scripts.version;
117
+ if(modes.npm && !process.env['npm_package_version']) {
118
+ let version_hook = modes.npm.meta.scripts && modes.npm.meta.scripts.version;
63
119
  if(version_hook) {
64
120
  console.log("Running version hook", version_hook);
65
121
  await passthru(version_hook, {shell : true, env : {npm_package_version : target_version}});
66
122
  }
67
123
  }
68
124
 
69
- let files = [];
70
- if(modes.docker.enabled) {
71
- modes.docker.meta.setLabel(DOCKER_LABEL_VERSION, target_version);
72
- fs.writeFileSync('Dockerfile', modes.docker.meta.toString());
73
- files.push('Dockerfile');
74
- }
75
-
76
- if(modes.composer.enabled) {
77
- modes.composer.meta.version = target_version;
78
- fs.writeFileSync('composer.json', JSON.stringify(modes.composer.meta, null, 2));
79
- files.push('composer.json');
80
- }
81
125
 
82
- if(modes.npm.enabled) {
83
- modes.npm.meta.version = target_version;
84
- fs.writeFileSync('package.json', JSON.stringify(modes.npm.meta, null, 2));
85
- files.push('package.json');
86
- }
126
+ let files = [];
127
+ for(let [, mode] of Object.entries(modes))
128
+ mode.commit.call(mode, {target_version, files});
87
129
 
88
130
  await passthru('git', ['add', ...files]);
89
131
 
package/lib/dockerfile.js CHANGED
@@ -1,14 +1,6 @@
1
1
  "use strict";
2
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
3
  const escapeRegExp = require('mout/string/escapeRegExp');
11
- const splitArgs = require('nyks/process/splitArgs');
12
4
 
13
5
  const {tokenize} = require('./util');
14
6
 
@@ -20,7 +12,7 @@ const VERBS = /^\s*(\w+)\s*(.*)/;
20
12
  class Dockerfile {
21
13
  static parse(body) {
22
14
  let lines = body.trim().split("\n");
23
-
15
+
24
16
  let out = new Dockerfile();
25
17
 
26
18
  let lookingForDirectives = true;
@@ -30,7 +22,7 @@ class Dockerfile {
30
22
  var lineRaw = "";
31
23
 
32
24
  for(let line of lines) {
33
- let multiline = new RegExp(`(.*)${escapeRegExp(out.escape)}\s*$`);
25
+ let multiline = new RegExp(`(.*)${escapeRegExp(out.escape)}\\s*$`);
34
26
 
35
27
  if(lookingForDirectives && INSTRUCTION.test(line)) {
36
28
  let [, instruction, value] = INSTRUCTION.exec(line);
@@ -45,7 +37,7 @@ class Dockerfile {
45
37
  continue;
46
38
  }
47
39
 
48
- lookingForDirectives = false
40
+ lookingForDirectives = false;
49
41
 
50
42
  if(multiline.test(line)) {
51
43
  let raw = multiline.exec(line);
@@ -53,13 +45,13 @@ class Dockerfile {
53
45
  remainder_raw += raw[0] + "\n";
54
46
  continue;
55
47
  } else {
56
- lineRaw = remainder_raw + line ;
48
+ lineRaw = remainder_raw + line;
57
49
  line = remainder + line;
58
50
  remainder = "";
59
51
  remainder_raw = "";
60
52
  }
61
53
 
62
- let bloc = out.feedLine(line, lineRaw);
54
+ out.feedLine(line, lineRaw);
63
55
  }
64
56
  return out;
65
57
  }
@@ -73,8 +65,8 @@ class Dockerfile {
73
65
  let labels = {};
74
66
  for(let entry of this.raw) {
75
67
  if(entry.VERB == "LABEL")
76
- for(let k in entry.labels)
77
- Object.defineProperty(labels, k, {enumerable : true, value : entry.labels[k]});
68
+ {for(let k in entry.labels)
69
+ Object.defineProperty(labels, k, {enumerable : true, value : entry.labels[k]});}
78
70
  }
79
71
 
80
72
  Object.seal(labels);
@@ -93,7 +85,7 @@ class Dockerfile {
93
85
  toString : function() {
94
86
  return this.raw;
95
87
  }
96
- };
88
+ };
97
89
 
98
90
  if(COMMENTS.test(line)) {
99
91
  let [, value] = COMMENTS.exec(line);
@@ -103,7 +95,7 @@ class Dockerfile {
103
95
  type : {value : "COMMENT"},
104
96
  _comment : {value, writable : true},
105
97
  comment : {
106
- get : function() {return this._comment },
98
+ get : function() {return this._comment; },
107
99
  set : function(value) {
108
100
  this._comment = value;
109
101
  this.raw = `# ${this.comment}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ppackage",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -9,10 +9,18 @@
9
9
  "dependencies": {
10
10
  "mout": "^1.0.0",
11
11
  "nyks": "^6.1.8",
12
- "semver": "^5.3.0"
12
+ "semver": "^5.3.0",
13
+ "yaml": "^2.4.2"
14
+ },
15
+ "devDependencies": {
16
+ "eslint": "^8.57.0",
17
+ "eslint-plugin-ivs": "^4.0.0",
18
+ "expect.js": "^0.3.1",
19
+ "mocha": "^3.1.2",
20
+ "nyc": "^15.0.1"
13
21
  },
14
22
  "scripts": {
15
- "test": "echo \"Error: no test specified\" && exit 1"
23
+ "eslint": "eslint ."
16
24
  },
17
25
  "author": "",
18
26
  "license": "ISC"