Skip to content

Commit 2d8c4fd

Browse files
committed
Use Node Path API to resolve paths
The script is not always run in root of this project
1 parent 3493ed6 commit 2d8c4fd

1 file changed

Lines changed: 32 additions & 14 deletions

File tree

build/build.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,33 @@
99

1010
var fs = require('fs');
1111
var Handlebars = require('handlebars');
12+
var path = require('path');
1213

13-
/* @type { string } - Default template file with path */
14-
var templateFile = './build/template.hbs.xml';
14+
/**
15+
* @typedef {Object} Paths
16+
* @description Collection of paths for this npm package
17+
* @property {string} package - Root directory of this npm package
18+
* @property {string} template - File path of Handlebars template
19+
*/
20+
/** @type {Paths} paths */
21+
var paths = {
22+
package: path.resolve(__dirname, '../'),
23+
template: path.resolve(__dirname, 'template.hbs.xml')
24+
};
1525

16-
/* @function template - Create a hbs template function. Requires templateFile to run. */
26+
/**
27+
* Create a function of Handlebars-template. Requires paths object to run.
28+
* @function template
29+
*/
1730
var template = Handlebars.compile(
1831
// Read template file synchronously
1932
// Making it a string is required by Handlebars
20-
fs.readFileSync(templateFile).toString()
33+
fs.readFileSync(paths.template).toString()
2134
);
2235

2336
/**
2437
* Render Handlebars template in Node 4 or above. Requires template() to run
25-
* @param {Object} files
26-
* @param {string} files.data - Full path for fs.readfile to read a config file
27-
* @param {string} files.output - Full path for fs.writeFile to write a udl.xml file
38+
* @param {Files} files - The {@link Files} object for Handlebars rendering
2839
*/
2940
var render = function (files) {
3041
/* @function readFile - Read the config file asynchronously */
@@ -49,15 +60,22 @@ var render = function (files) {
4960
};
5061

5162
/**
52-
* Create a Files object
53-
* @param {string} themeName
54-
* @return {Object} - Paths of Handlebars data source and expected output
63+
* @typedef {Object} Files
64+
* @description Full paths of config data, expected output UDL files and etc.
65+
* @property {string} data - Full path of config data
66+
* @property {string} output - Full path of expected UDL file
67+
*/
68+
/**
69+
* Create a Files object in format of {@link Files}
70+
* @param {string} themeName - In format of dash-lower-case-theme-name
71+
* @param {Paths} paths - The {@link Paths} object for Handlebars rendering
72+
* @return {Files}
5573
*/
56-
var createFilesObj = function (themeName) {
74+
var createFilesObj = function (themeName, paths) {
5775
return {
58-
data: './config/markdown.' + themeName + '.config.json',
59-
output: './udl/markdown.' + themeName + '.udl.xml'
76+
data: paths.package + '/config/markdown.' + themeName + '.config.json',
77+
output: paths.package + '/udl/markdown.' + themeName + '.udl.xml'
6078
};
6179
};
6280

63-
render(createFilesObj('default'));
81+
render(createFilesObj('default', paths));

0 commit comments

Comments
 (0)