@ntlab/sipd-agr 3.2.0 → 3.4.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.
@@ -44,6 +44,16 @@ Cmd.addBool('help', '', 'Show program usage', false);
44
44
  */
45
45
  class Configuration {
46
46
 
47
+ defaults = {
48
+ mode: Sipd.DOWNLOAD,
49
+ url: 'https://sipd-ri.kemendagri.go.id',
50
+ year: new Date().getFullYear(),
51
+ username: null,
52
+ password: null,
53
+ dir: null,
54
+ skipDownload: ['no-download', false],
55
+ }
56
+
47
57
  /**
48
58
  * Constructor.
49
59
  *
@@ -55,25 +65,13 @@ class Configuration {
55
65
  if (fs.existsSync(filename)) {
56
66
  Object.assign(this, this.getConfig(filename));
57
67
  }
58
- for (const a of ['url', 'username', 'password', 'year', 'dir']) {
59
- const v = Cmd.get(a);
60
- if (v) {
61
- this[a] = v;
62
- }
63
- }
64
68
  if (fs.existsSync(filename)) {
65
69
  console.log('Configuration loaded from %s', filename);
66
70
  }
67
71
  if (!this.workdir) {
68
72
  this.workdir = rootDir;
69
73
  }
70
- if (Cmd.get('no-download')) {
71
- this.skipDownload = Cmd.get('no-download');
72
- }
73
- if (!this.mode) {
74
- this.mode = Cmd.get('mode') ? Cmd.get('mode') : Sipd.DOWNLOAD;
75
- }
76
-
74
+ this.checkDefaults();
77
75
  if (!this.username || !this.password) {
78
76
  console.log('Both username or password must be supplied!');
79
77
  return;
@@ -103,6 +101,22 @@ class Configuration {
103
101
  this.initialized = true;
104
102
  }
105
103
 
104
+ checkDefaults() {
105
+ for (const [k, v] of Object.entries(this.defaults)) {
106
+ const opt = Array.isArray(v) ? v[0] : k;
107
+ const defval = Array.isArray(v) ? v[1] : v;
108
+ // get value from command options
109
+ let value = Cmd.get(opt);
110
+ // fallback to default
111
+ if (value === null) {
112
+ value = defval;
113
+ }
114
+ if (value !== undefined && value !== null) {
115
+ this[k] = value;
116
+ }
117
+ }
118
+ }
119
+
106
120
  getConfig(filename) {
107
121
  let config = JSON.parse(fs.readFileSync(filename));
108
122
  if (config.ref) {
package/app/index.js CHANGED
@@ -66,20 +66,26 @@ class App {
66
66
  }
67
67
  const works = sipd.getWorks();
68
68
  if (works) {
69
- sipd.works(works, next => {
70
- setTimeout(() => next(), 500);
71
- }).then(() => {
69
+ sipd.works(works, {
70
+ callback(next) {
71
+ setTimeout(() => next(), 500);
72
+ }
73
+ })
74
+ .then(() => {
72
75
  sipd.app.showMessage('Information', 'The process has been completed! :)');
73
76
  console.log('Done');
74
- }).catch(err => {
77
+ })
78
+ .catch(err => {
79
+ process.exitCode = 2;
75
80
  if (err) {
76
- console.log(err);
81
+ console.error(err);
77
82
  } else {
78
- console.log('Unknown error, aborting!!!');
83
+ console.error('Unknown error, aborting!!!');
79
84
  }
80
85
  });
81
86
  } else {
82
- console.log('Unknown MODE %s!!!', this.config.mode);
87
+ process.exitCode = 1;
88
+ console.error('Unknown mode %s!!!', this.config.mode);
83
89
  }
84
90
  }
85
91
 
@@ -1,7 +1,5 @@
1
1
  {
2
- "url":"https://sipd-ri.kemendagri.go.id",
3
2
  "provinsi":"CHANGEME",
4
3
  "username":"CHANGEME",
5
- "password":"CHANGEME",
6
- "year":"2025"
4
+ "password":"CHANGEME"
7
5
  }
package/main.js CHANGED
@@ -29,7 +29,7 @@ const App = require('./app');
29
29
  const Cmd = require('@ntlab/ntlib/cmd');
30
30
 
31
31
  if (!Cmd.parse() || (Cmd.get('help') && usage())) {
32
- process.exit();
32
+ process.exit(process.exitCode !== undefined ? null : 1);
33
33
  }
34
34
 
35
35
  (function run() {
@@ -43,5 +43,6 @@ function usage() {
43
43
  console.log('Options:');
44
44
  console.log(Cmd.dump());
45
45
  console.log('');
46
+ process.exitCode = 0;
46
47
  return true;
47
48
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ntlab/sipd-agr",
3
- "version": "3.2.0",
3
+ "version": "3.4.0",
4
4
  "description": "SIPD RI Kemendagri Automation is a web automation using Selenium to help manage data such as budgeting download",
5
5
  "main": "main.js",
6
6
  "author": "Toha <tohenk@yahoo.com>",