hdoc-tools 0.19.7 → 0.20.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/hdoc-init.js CHANGED
@@ -1,147 +1,181 @@
1
- (function () {
2
- 'use strict';
3
-
4
- // Required modules
5
- const prompt = require('prompt');
6
- const fs = require('fs-extra');
7
- const path = require('path');
8
-
9
- // Configure prompt module preferences
10
- prompt.message = false;
11
- const promptProps = [{
12
- name: 'id',
13
- description: 'Document ID',
14
- validator: /^[a-z][a-z0-9-]+[a-z0-9]$/,
15
- warning: 'Document ID must only contain lower case letters, decimal digits and dashes.',
16
- required: true
17
- },
18
- {
19
- name: 'title',
20
- description: 'Title',
21
- required: true
22
- },
23
- {
24
- name: 'description',
25
- description: 'Description',
26
- required: true
27
- },
28
- {
29
- name: 'version',
30
- description: 'Initial Version',
31
- validator: /^[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}$/,
32
- warning: 'Version must formatted using semantic versioning - major.minor.patch, eg: 0.1.2',
33
- required: true
34
- },
35
- {
36
- name: 'author',
37
- description: 'Package Author',
38
- required: true
39
- }
40
- ];
41
-
42
- const createBook = function (server_path, source_path, docProps) {
43
- console.log('\r\nCreating book with the following properties:\r\n');
44
- console.log(' Doc ID:', docProps.id);
45
- console.log(' Title:', docProps.title);
46
- console.log(' Description:', docProps.description);
47
- console.log(' Author:', docProps.author);
48
- console.log(' Initial Version:', docProps.version, '\r\n');
49
-
50
- // Now copy files over
51
- const templatePath = path.join(server_path, 'templates', 'init');
52
- console.log('Copying template from:', templatePath);
53
-
54
- if (fs.existsSync(templatePath)) {
55
- // If template path exists, do sync copy into book path
56
- try {
57
- fs.copySync(templatePath, source_path);
58
- } catch (e) {
59
- console.error('Error copying template:\r\n', e);
60
- process.exit(1);
61
- }
62
-
63
- // Rename _hdocbook folder to docId, sync as we need to wait
64
- // until this in complete for the next tasks to be successful
65
- const bookContentRoot = path.join(source_path, docProps.id);
66
- try {
67
- fs.renameSync(path.join(source_path, '_hdocbook'), bookContentRoot);
68
- } catch (e) {
69
- console.error('Error renaming template folder:\r\n', e);
70
- process.exit(1);
71
- }
72
-
73
- // Update hdocbook-project.json
74
- const hdocBookProjectFilePath = path.join(source_path, 'hdocbook-project.json');
75
- const hdocBookProjectFile = require(hdocBookProjectFilePath);
76
- hdocBookProjectFile.docId = docProps.id;
77
- fs.writeFile(hdocBookProjectFilePath, JSON.stringify(hdocBookProjectFile, null, 2), function writeJSON(err) {
78
- if (err) return console.error('Error updating:', hdocBookProjectFilePath, '\r\n', err);
79
- console.log('Updated:', hdocBookProjectFilePath);
80
- });
81
-
82
- // Update root/hdocbook.json
83
- const hdocBookFilePath = path.join(bookContentRoot, 'hdocbook.json');
84
- const hdocbookFile = require(hdocBookFilePath);
85
- hdocbookFile.docId = docProps.id;
86
- hdocbookFile.title = docProps.title;
87
- hdocbookFile.description = docProps.description;
88
- hdocbookFile.version = docProps.version;
89
- hdocbookFile.publicSource = `https://github.com/Hornbill-Docs/${docProps.id}`;
90
- hdocbookFile.navigation.items[0].items = [{
91
- "text": "Welcome",
92
- "link": docProps.id + '/index'
93
- }];
94
- fs.writeFile(hdocBookFilePath, JSON.stringify(hdocbookFile, null, 2), function writeJSON(err) {
95
- if (err) return console.error('Error updating:', hdocBookFilePath, '\r\n', err);
96
- console.log('Updated:', hdocBookFilePath);
97
- });
98
-
99
- // Update package.json
100
- const packageFilePath = path.join(source_path, 'package.json');
101
- const packageFile = require(packageFilePath);
102
- packageFile.name = docProps.id;
103
- packageFile.version = docProps.version;
104
- hdocbookFile.description = docProps.description;
105
- hdocbookFile.version = docProps.version;
106
- hdocbookFile.author = docProps.author;
107
- fs.writeFile(packageFilePath, JSON.stringify(packageFile, null, 2), function writeJSON(err) {
108
- if (err) return console.error('Error updating:', packageFilePath, '\r\n', err);
109
- console.log('Updated:', packageFilePath);
110
- });
111
-
112
- // Rename gitignore to .gitignore
113
- const gitignorePath = path.join(source_path, 'gitignore');
114
- const newGitignorePath = path.join(source_path, '.gitignore');
115
- fs.renameSync(gitignorePath, newGitignorePath);
116
-
117
- } else {
118
- console.error('Template path does not exist:', templatePath);
119
- process.exit(1);
120
- }
121
- };
122
-
123
- exports.run = function (server_path, source_path, md) {
124
- // GERRY: The init function should create a new starting point HDocBook folder structure
125
- // ready to run the preview server and start editing.
126
- //
127
- // The init function should prompt for the ID of the doc book, title and initial version, then
128
- // create the required files and folders. Its possible to just create a template and copy
129
- // those files into place, with a few variable replacements.
130
-
131
- console.log('Hornbill HDocBook Init', '\r\n');
132
-
133
- const curr_dirs = source_path.split(path.sep);
134
- let doc_id = curr_dirs[curr_dirs.length - 1];
135
-
136
- prompt.start();
137
- promptProps[0].default = doc_id;
138
- prompt.get(promptProps, function (err, result) {
139
- if (err) {
140
- console.error(err);
141
- return err;
142
- }
143
- createBook(server_path, source_path, result);
144
- });
145
-
146
- };
147
- })();
1
+ (() => {
2
+ // Required modules
3
+ const prompt = require("prompt");
4
+ const fs = require("fs-extra");
5
+ const path = require("node:path");
6
+
7
+ // Configure prompt module preferences
8
+ prompt.message = false;
9
+ const promptProps = [
10
+ {
11
+ name: "id",
12
+ description: "Document ID",
13
+ validator: /^[a-z][a-z0-9-]+[a-z0-9]$/,
14
+ warning:
15
+ "Document ID must only contain lower case letters, decimal digits and dashes.",
16
+ required: true,
17
+ },
18
+ {
19
+ name: "title",
20
+ description: "Title",
21
+ required: true,
22
+ },
23
+ {
24
+ name: "description",
25
+ description: "Description",
26
+ required: true,
27
+ },
28
+ {
29
+ name: "version",
30
+ description: "Initial Version",
31
+ validator: /^[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}$/,
32
+ warning:
33
+ "Version must formatted using semantic versioning - major.minor.patch, eg: 0.1.2",
34
+ required: true,
35
+ },
36
+ {
37
+ name: "author",
38
+ description: "Package Author",
39
+ required: true,
40
+ },
41
+ ];
42
+
43
+ const createBook = (server_path, source_path, docProps) => {
44
+ console.log("\r\nCreating book with the following properties:\r\n");
45
+ console.log(" Doc ID:", docProps.id);
46
+ console.log(" Title:", docProps.title);
47
+ console.log(" Description:", docProps.description);
48
+ console.log(" Author:", docProps.author);
49
+ console.log(" Initial Version:", docProps.version, "\r\n");
50
+
51
+ // Now copy files over
52
+ const templatePath = path.join(server_path, "templates", "init");
53
+ console.log("Copying template from:", templatePath);
54
+
55
+ if (fs.existsSync(templatePath)) {
56
+ // If template path exists, do sync copy into book path
57
+ try {
58
+ fs.copySync(templatePath, source_path);
59
+ } catch (e) {
60
+ console.error("Error copying template:\r\n", e);
61
+ process.exit(1);
62
+ }
63
+
64
+ // Rename _hdocbook folder to docId, sync as we need to wait
65
+ // until this in complete for the next tasks to be successful
66
+ const bookContentRoot = path.join(source_path, docProps.id);
67
+ try {
68
+ fs.renameSync(path.join(source_path, "_hdocbook"), bookContentRoot);
69
+ } catch (e) {
70
+ console.error("Error renaming template folder:\r\n", e);
71
+ process.exit(1);
72
+ }
73
+
74
+ // Update hdocbook-project.json
75
+ const hdocBookProjectFilePath = path.join(
76
+ source_path,
77
+ "hdocbook-project.json",
78
+ );
79
+ const hdocBookProjectFile = require(hdocBookProjectFilePath);
80
+ hdocBookProjectFile.docId = docProps.id;
81
+ fs.writeFile(
82
+ hdocBookProjectFilePath,
83
+ JSON.stringify(hdocBookProjectFile, null, 2),
84
+ function writeJSON(err) {
85
+ if (err)
86
+ return console.error(
87
+ "Error updating:",
88
+ hdocBookProjectFilePath,
89
+ "\r\n",
90
+ err,
91
+ );
92
+ console.log("Updated:", hdocBookProjectFilePath);
93
+ },
94
+ );
95
+
96
+ // Update root/hdocbook.json
97
+ const hdocBookFilePath = path.join(bookContentRoot, "hdocbook.json");
98
+ const hdocbookFile = require(hdocBookFilePath);
99
+ hdocbookFile.docId = docProps.id;
100
+ hdocbookFile.title = docProps.title;
101
+ hdocbookFile.description = docProps.description;
102
+ hdocbookFile.version = docProps.version;
103
+ hdocbookFile.publicSource = `https://github.com/Hornbill-Docs/${docProps.id}`;
104
+ hdocbookFile.navigation.items[0].items = [
105
+ {
106
+ text: "Welcome",
107
+ link: `${docProps.id}/index`,
108
+ },
109
+ ];
110
+ fs.writeFile(
111
+ hdocBookFilePath,
112
+ JSON.stringify(hdocbookFile, null, 2),
113
+ function writeJSON(err) {
114
+ if (err)
115
+ return console.error(
116
+ "Error updating:",
117
+ hdocBookFilePath,
118
+ "\r\n",
119
+ err,
120
+ );
121
+ console.log("Updated:", hdocBookFilePath);
122
+ },
123
+ );
124
+
125
+ // Update package.json
126
+ const packageFilePath = path.join(source_path, "package.json");
127
+ const packageFile = require(packageFilePath);
128
+ packageFile.name = docProps.id;
129
+ packageFile.version = docProps.version;
130
+ hdocbookFile.description = docProps.description;
131
+ hdocbookFile.version = docProps.version;
132
+ hdocbookFile.author = docProps.author;
133
+ fs.writeFile(
134
+ packageFilePath,
135
+ JSON.stringify(packageFile, null, 2),
136
+ function writeJSON(err) {
137
+ if (err)
138
+ return console.error(
139
+ "Error updating:",
140
+ packageFilePath,
141
+ "\r\n",
142
+ err,
143
+ );
144
+ console.log("Updated:", packageFilePath);
145
+ },
146
+ );
147
+
148
+ // Rename gitignore to .gitignore
149
+ const gitignorePath = path.join(source_path, "gitignore");
150
+ const newGitignorePath = path.join(source_path, ".gitignore");
151
+ fs.renameSync(gitignorePath, newGitignorePath);
152
+ } else {
153
+ console.error("Template path does not exist:", templatePath);
154
+ process.exit(1);
155
+ }
156
+ };
157
+
158
+ exports.run = (server_path, source_path, md) => {
159
+ // GERRY: The init function should create a new starting point HDocBook folder structure
160
+ // ready to run the preview server and start editing.
161
+ //
162
+ // The init function should prompt for the ID of the doc book, title and initial version, then
163
+ // create the required files and folders. Its possible to just create a template and copy
164
+ // those files into place, with a few variable replacements.
165
+
166
+ console.log("Hornbill HDocBook Init", "\r\n");
167
+
168
+ const curr_dirs = source_path.split(path.sep);
169
+ const doc_id = curr_dirs[curr_dirs.length - 1];
170
+
171
+ prompt.start();
172
+ promptProps[0].default = doc_id;
173
+ prompt.get(promptProps, (err, result) => {
174
+ if (err) {
175
+ console.error(err);
176
+ return err;
177
+ }
178
+ createBook(server_path, source_path, result);
179
+ });
180
+ };
181
+ })();