Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Pug 3 #88

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ language: node_js
sudo: false

node_js:
- "0.10"
- "0.12"
- "4"
- "6"
- "10"
- "12"
- "14"

after_success:
- npm run coverage
Expand Down
43 changes: 18 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

var fs = require('fs');
var path = require('path');
var program = require('commander');
var { program } = require('commander');
var mkdirp = require('mkdirp');
var chalk = require('chalk');
var pug = require('pug');
Expand All @@ -16,10 +16,6 @@ var normalize = path.normalize;
var join = path.join;
var relative = path.relative;

// Pug options

var options = {};

// options

program
Expand Down Expand Up @@ -75,11 +71,8 @@ program.on('--help', function(){

program.parse(process.argv);

// options given, parse them

if (program.obj) {
options = parseObj(program.obj);
}
var args = program.opts();
var options = (args.obj) ? parseObj(args.obj) : {};

/**
* Parse object either in `input` or in the file called `input`. The latter is
Expand All @@ -91,9 +84,9 @@ function parseObj (input) {
} catch (e) {
var str;
try {
str = fs.readFileSync(program.obj, 'utf8');
str = fs.readFileSync(args.obj, 'utf8');
} catch (e) {
str = program.obj;
str = args.obj;
}
try {
return JSON.parse(str);
Expand All @@ -111,18 +104,18 @@ function parseObj (input) {
['basedir', 'basedir'], // --basedir
['doctype', 'doctype'], // --doctype
].forEach(function (o) {
options[o[1]] = program[o[0]] !== undefined ? program[o[0]] : options[o[1]];
options[o[1]] = args[o[0]] !== undefined ? args[o[0]] : options[o[1]];
});

// --name

if (typeof program.name === 'string') {
options.name = program.name;
if (typeof args.name === 'string') {
options.name = args.name;
}

// --silent

var consoleLog = program.silent ? function() {} : console.log;
var consoleLog = args.silent ? function() {} : console.log;

// left-over args are file paths

Expand All @@ -134,13 +127,13 @@ var files = program.args;
var watchList = {};

// function for rendering
var render = program.watch ? tryRender : renderFile;
var render = args.watch ? tryRender : renderFile;

// compile files

if (files.length) {
consoleLog();
if (program.watch) {
if (args.watch) {
process.on('SIGINT', function() {
process.exit(1);
});
Expand Down Expand Up @@ -249,14 +242,14 @@ function renderFile(path, rootPath) {
// Found pug file
if (stat.isFile() && isPug.test(path) && !isIgnored.test(path)) {
// Try to watch the file if needed. watchFile takes care of duplicates.
if (program.watch) watchFile(path, null, rootPath);
if (program.nameAfterFile) {
if (args.watch) watchFile(path, null, rootPath);
if (args.nameAfterFile) {
options.name = getNameFromFileName(path);
}
var fn = options.client
? pug.compileFileClient(path, options)
: pug.compileFile(path, options);
if (program.watch && fn.dependencies) {
if (args.watch && fn.dependencies) {
// watch dependencies, and recompile the base
fn.dependencies.forEach(function (dep) {
watchFile(dep, path, rootPath);
Expand All @@ -265,14 +258,14 @@ function renderFile(path, rootPath) {

// --extension
var extname;
if (program.extension) extname = '.' + program.extension;
if (args.extension) extname = '.' + args.extension;
else if (options.client) extname = '.js';
else if (program.extension === '') extname = '';
else if (args.extension === '') extname = '';
else extname = '.html';

// path: foo.pug -> foo.<ext>
path = path.replace(isPug, extname);
if (program.out) {
if (args.out) {
// prepend output directory
if (rootPath) {
// replace the rootPath of the resolved path with output directory
Expand All @@ -281,7 +274,7 @@ function renderFile(path, rootPath) {
// if no rootPath handling is needed
path = basename(path);
}
path = resolve(program.out, path);
path = resolve(args.out, path);
}
var dir = resolve(dirname(path));
mkdirp.sync(dir);
Expand Down