Skip to content

Commit

Permalink
Merge pull request #2 from tokilabs/anthony-tron/yaml-support
Browse files Browse the repository at this point in the history
Merge contributions from anthony-tron/yaml support
  • Loading branch information
svallory authored Apr 21, 2023
2 parents 4fa88a1 + 403c263 commit 98d7c28
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ input is taken from standard input and output to standard output.
```
-h, --help output usage information
-V, --version output the version number
-O, --obj <str|path> JSON/JavaScript options object or file
-O, --obj <str|path> JSON/JavaScript/YAML options object or file
-o, --out <dir> output the rendered HTML or compiled JavaScript to
<dir>
-p, --path <path> filename used to resolve includes
Expand Down Expand Up @@ -85,13 +85,15 @@ $ pug3 -O options.js foo.pug
# or, JSON works too
$ echo '{"doctype": "html"}' > options.json
$ pug3 -O options.json foo.pug
# YAML works as well
$ pug3 -O options.yaml foo.pug
```

## Installation

npm install @anduh/pug-cli -g

## Original
## Original
The original project this was forked from:
* [github.com/pugjs/pug-cli](https://github.com/pugjs/pug-cli)

Expand Down
38 changes: 34 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var { program } = require('commander');
var mkdirp = require('mkdirp');
var chalk = require('chalk');
var pug = require('pug');
var yaml = require('js-yaml')

var basename = path.basename;
var dirname = path.dirname;
Expand All @@ -24,7 +25,7 @@ program
'@anduh/pug-cli version: ' + require( './package.json').version
)
.usage('[options] [dir|file ...]')
.option('-O, --obj <str|path>', 'JSON/JavaScript options object or file')
.option('-O, --obj <str|path>', 'JSON/JavaScript/YAML options object or file')
.option('-o, --out <dir>', 'output the rendered HTML or compiled JavaScript to <dir>')
.option('-p, --path <path>', 'filename used to resolve includes')
.option('-b, --basedir <path>', 'path used as root directory to resolve absolute includes')
Expand Down Expand Up @@ -74,13 +75,19 @@ program.parse(process.argv);
var args = program.opts();
var options = (args.obj) ? parseObj(args.obj) : {};

if (program.obj) {
options = parseObj(program.obj);
}

/**
* Parse object either in `input` or in the file called `input`. The latter is
* searched first.
*/
function parseObj (input) {
try {
return require(path.resolve(input));
resolved = require.resolve(input);
delete require[resolved]; // delete cache
return require(resolved);
} catch (e) {
var str;
try {
Expand All @@ -91,7 +98,11 @@ function parseObj (input) {
try {
return JSON.parse(str);
} catch (e) {
return eval('(' + str + ')');
try {
return yaml.load(str, 'utf-8');
} catch(e) {
return eval('(' + str + ')');
}
}
}
}
Expand Down Expand Up @@ -133,7 +144,26 @@ var render = args.watch ? tryRender : renderFile;

if (files.length) {
consoleLog();
if (args.watch) {

if (program.watch) {
if (program.obj && fs.existsSync(program.obj)) {
fs.watchFile(program.obj, {persistent: true, interval: 200}, function() {
consoleLog(' ' + chalk.yellow(program.obj) + ' ' + chalk.gray('changed'));

// update object without losing previous data
Object.assign(options, parseObj(options));

// then update all files
for (const [path, bases] of Object.entries(watchList)) {
if (watchList.hasOwnProperty(path)) {
bases.forEach(render);
}
}
});

consoleLog(' ' + chalk.gray('watching') + ' ' + chalk.yellow(program.obj));
}

process.on('SIGINT', function() {
process.exit(1);
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"commander": "^7.2.0",
"pug": "^3.0.2",
"mkdirp": "^1.0.4"
"js-yaml": "^4.1.0",
},
"devDependencies": {
"istanbul": "*",
Expand Down

0 comments on commit 98d7c28

Please sign in to comment.