testilo 4.0.1 → 4.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.
Files changed (3) hide show
  1. package/aim.js +57 -0
  2. package/merge.js +25 -24
  3. package/package.json +1 -1
package/aim.js ADDED
@@ -0,0 +1,57 @@
1
+ /*
2
+ aim.js
3
+ Returns a script aimed at a host.
4
+ Arguments:
5
+ 0. base of name of script located in process.env.SCRIPTDIR.
6
+ 1. ID of the host.
7
+ 2. URL of the host.
8
+ 3. Name of the host.
9
+ 4. ID of the requester.
10
+ Usage example:
11
+ node aim tp18 w3c https://www.w3.org/ 'World Wide Web Consortium'
12
+ */
13
+
14
+ // ########## IMPORTS
15
+
16
+ // Module to keep secrets.
17
+ require('dotenv').config();
18
+ // Module to read and write files.
19
+ const fs = require('fs/promises');
20
+
21
+ // ########## CONSTANTS
22
+
23
+ const scriptDir = process.env.SCRIPTDIR || 'scripts';
24
+ const scriptName = process.argv[2];
25
+ const host = {
26
+ id: process.argv[3],
27
+ which: process.argv[4],
28
+ what: process.argv[5]
29
+ };
30
+ const requester = process.argv[6];
31
+
32
+ // ########## FUNCTIONS
33
+
34
+ // Returns a script, aimed at a host.
35
+ exports.aim = async () => {
36
+ // Copy the script.
37
+ const scriptFile = await fs.readFile(`${scriptDir}/${scriptName}.json`, 'utf8');
38
+ const script = JSON.parse(scriptFile);
39
+ const newScript = JSON.parse(JSON.stringify(script));
40
+ // In the copy, make all url commands visit the host.
41
+ newScript.commands.forEach(command => {
42
+ if (command.type === 'url') {
43
+ command.id = host.id;
44
+ command.which = host.which;
45
+ command.what = host.what;
46
+ }
47
+ });
48
+ // Add source information to the script.
49
+ newScript.source = {
50
+ script: script.id,
51
+ requester
52
+ }
53
+ // Change the script id property to include the host ID.
54
+ newScript.id += `-${host.id}`;
55
+ // Return the host-specific script.
56
+ return newScript;
57
+ };
package/merge.js CHANGED
@@ -1,13 +1,12 @@
1
1
  /*
2
2
  merge.js
3
- Merges a batch and a script to produce jobs.
3
+ Merges a batch and a script to produce host-specific scripts.
4
4
  Arguments:
5
5
  0. base of name of script located in process.env.SCRIPTDIR.
6
6
  1. base of name of batch located in process.env.BATCHDIR.
7
- 2. name of subdirectory of process.env.JOBDIR into which to write host scripts.
8
7
  Usage example:
9
- node merge tp18 weborgs tp18-weborgs-1
10
- Note: The subdirectory for host scripts will be created if it does not exist.
8
+ node merge tp18 weborgs
9
+ Note: The subdirectory for host-specific scripts will be created if it does not exist.
11
10
  */
12
11
 
13
12
  // ########## IMPORTS
@@ -21,40 +20,44 @@ const fs = require('fs/promises');
21
20
 
22
21
  const scriptDir = process.env.SCRIPTDIR || 'scripts';
23
22
  const batchDir = process.env.BATCHDIR || 'batches';
24
- const jobDir = process.env.JOBDIR || 'jobs';
23
+ const watchDir = process.env.WATCHDIR || 'watch';
25
24
  const scriptName = process.argv[2];
26
25
  const batchName = process.argv[3];
27
- const jobSubdir = process.argv[4];
28
26
 
29
27
  // ########## FUNCTIONS
30
28
 
31
- // Merges a batch into a script and writes host scripts.
32
- const merge = async (script, batch, timeStamp, outDir) => {
33
- await fs.mkdir(outDir, {recursive: true});
34
- const {hosts} = batch;
29
+ // Merges a batch into a script and writes host-specific scripts.
30
+ const merge = async (script, batch) => {
31
+ // Create the watch directory if it does not exist.
32
+ await fs.mkdir(watchDir, {recursive: true});
35
33
  // For each host in the batch:
36
- const jobs = hosts.map(host => {
34
+ const {hosts} = batch;
35
+ const newScripts = hosts.map(host => {
37
36
  // Copy the script.
38
37
  const newScript = JSON.parse(JSON.stringify(script));
39
38
  // In the copy, make all url commands visit the host.
40
39
  newScript.commands.forEach(command => {
41
40
  if (command.type === 'url') {
41
+ command.id = host.id;
42
42
  command.which = host.which;
43
43
  command.what = host.what;
44
44
  }
45
45
  });
46
- // Create a host script.
47
- return {
48
- id: `${timeStamp}-${host.id}`,
49
- host,
50
- script: newScript
51
- };
46
+ // Add source information to the script.
47
+ newScript.sources = {
48
+ script: script.id,
49
+ batch: batch.id
50
+ }
51
+ // Change the script id property to include the host ID.
52
+ newScript.id += `-${host.id}`;
53
+ // Return the host-specific script.
54
+ return newScript;
52
55
  });
53
- // Write the host scripts.
54
- for (const job of jobs) {
55
- await fs.writeFile(`${outDir}/${job.id}.json`, JSON.stringify(job, null, 2));
56
+ // Write the host-specific scripts.
57
+ for (const newScript of newScripts) {
58
+ await fs.writeFile(`${watchDir}/${newScript.id}.json`, JSON.stringify(newScript, null, 2));
56
59
  };
57
- console.log(`Merger completed. Count of jobs created: ${hosts.length}`);
60
+ console.log(`Merger completed. Count of scripts created: ${hosts.length}`);
58
61
  };
59
62
 
60
63
  // ########## OPERATION
@@ -65,8 +68,6 @@ fs.readFile(`${scriptDir}/${scriptName}.json`, 'utf8')
65
68
  .then(async batchFile => {
66
69
  const script = JSON.parse(scriptFile);
67
70
  const batch = JSON.parse(batchFile);
68
- const timeStamp = Math.floor((Date.now() - Date.UTC(2022, 1)) / 2000).toString(36);
69
- const outDir = `${jobDir}/${jobSubdir}`;
70
- await merge(script, batch, timeStamp, outDir);
71
+ await merge(script, batch);
71
72
  });
72
73
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "testilo",
3
- "version": "4.0.1",
3
+ "version": "4.1.0",
4
4
  "description": "Client that scores and digests Testaro reports",
5
5
  "main": "index.js",
6
6
  "scripts": {