Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
s3ththompson committed May 27, 2017
1 parent 476e75a commit 4b9e715
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 118 deletions.
277 changes: 168 additions & 109 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const view = require('./views/default');
module.exports = {
status: status,
render: render
}
};

function fetch(url, cb) {
request(url, (err, resp, html) => {
Expand All @@ -24,59 +24,80 @@ function fetch(url, cb) {
}

function crawl(sites, cb) {
async.mapLimit(sites, 10, (site, cb) => {
fetch(site.url, (err, lastModified, $) => {
if (err) return cb(err, null);
if (site.selector) {
site.content = $.html($(site.selector).first());
cb(null, site);
} else if (lastModified) {
site.lastModified = new Date(lastModified);
cb(null, site);
} else {
cb(new Error(`${site.url}: no 'last-modified' header, specify selector instead`), null)
}
});
}, cb);
async.mapLimit(
sites,
10,
(site, cb) => {
fetch(site.url, (err, lastModified, $) => {
if (err) return cb(err, null);
if (site.selector) {
site.content = $.html($(site.selector).first());
cb(null, site);
} else if (lastModified) {
site.lastModified = new Date(lastModified);
cb(null, site);
} else {
cb(
new Error(
`${site.url}: no 'last-modified' header, specify selector instead`
),
null
);
}
});
},
cb
);
}

function diff(lock, sites) {
for (var site of sites) {
var baseline = _.find(lock, ['url', site.url]);
if (site.selector && baseline && baseline.selector) {
site.lastModified = (site.content !== baseline.content) ? new Date() : baseline.lastModified;
site.lastModified = site.content !== baseline.content
? new Date()
: baseline.lastModified;
} else if (!site.lastModified) {
site.lastModified = new Date();
}
}
return sites
return sites;
}

function status(sites, lockfile, opts, cb) {
var cb = cb || opts;
if (typeof opts === 'function' || !opts) opts = {};

var expiration = opts.expiration || (1000 * 60 * 60 * 24 * 30) ; // ms in one month
var expiration = opts.expiration || 1000 * 60 * 60 * 24 * 30; // ms in one month

yaml.read(lockfile, (err, lock) => {
if (err) return cb(err, null);
lock = lock.sites;
crawl(sites, (err, sites) => {
if (err) return cb(err, null);
sites = diff(lock, sites);
yaml.write(lockfile, {sites: sites}, {
preamble: "# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n# newwork lockfile v1\n\n"
}, (err) => {
if (err) return cb(err, null);
for (var site of sites) {
site.new = (site.lastModified.getTime() > ((new Date()).getTime() - expiration))
yaml.write(
lockfile,
{ sites: sites },
{
preamble: '# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n# newwork lockfile v1\n\n'
},
err => {
if (err) return cb(err, null);
for (var site of sites) {
site.new =
site.lastModified.getTime() > new Date().getTime() - expiration;
}
cb(
null,
sites.map(site => {
return _.pick(site, ['name', 'url', 'new', 'lastModified']);
})
);
}
cb(null, sites.map((site) => {
return _.pick(site, ['name', 'url', 'new', 'lastModified']);
}))
})
);
});
})
});
}

function render(sites, lockfile, opts, cb) {
Expand All @@ -94,10 +115,10 @@ function render(sites, lockfile, opts, cb) {
function addEntry(input, opts, cb) {
yaml.read(input, (err, data) => {
if (err) return cb(err);
data.sites.push(opts)
data.sites.push(opts);
data.sites = _.sortBy(data.sites, 'name');
yaml.write(input, data, cb);
})
});
}

function add(opts, cb) {
Expand All @@ -117,94 +138,127 @@ function add(opts, cb) {
return checkModified(prompt, new Date(lastModified), (err, y) => {
if (err) return console.log(err);
if (y) {
return addEntry(opts.input, {
name: title,
url: url
}, cb);
return addEntry(
opts.input,
{
name: title,
url: url
},
cb
);
} else {
return getSelector(prompt, $, (err, selector) => {
if (err) return console.log(err);
addEntry(opts.input, {
name: title,
url: url,
selector: selector
}, cb);
addEntry(
opts.input,
{
name: title,
url: url,
selector: selector
},
cb
);
});
}
})
});
}
getSelector(prompt, $, (err, selector) => {
if (err) return console.log(err);
addEntry(opts.input, {
name: title,
url: url,
selector: selector
}, cb);
addEntry(
opts.input,
{
name: title,
url: url,
selector: selector
},
cb
);
});
});
});
});

function getUrl(prompt, cb) {
prompt.get([{
name: 'url',
description: 'url:',
required: true
}], (err, result) => {
if (err) return cb(err, null);
cb(null, result.url);
});
prompt.get(
[
{
name: 'url',
description: 'url:',
required: true
}
],
(err, result) => {
if (err) return cb(err, null);
cb(null, result.url);
}
);
}

function checkModified(prompt, lastModified, cb) {
prompt.get([{
name: 'lastModified',
description: `was ${lastModified} the last modification (y/n):`,
pattern: /^(?:y|n)$/,
message: 'answer must be y/n',
default: 'y',
required: false
}], (err, result) => {
if (err) return cb(err, null);
cb(null, (result.lastModified == 'y'));
});
prompt.get(
[
{
name: 'lastModified',
description: `was ${lastModified} the last modification (y/n):`,
pattern: /^(?:y|n)$/,
message: 'answer must be y/n',
default: 'y',
required: false
}
],
(err, result) => {
if (err) return cb(err, null);
cb(null, result.lastModified == 'y');
}
);
}

function getName(prompt, title, cb) {
prompt.get([{
name: 'name',
description: 'name:',
default: title,
required: !title
}], (err, result) => {
if (err) return cb(err, null);
cb(null, result.name);
});
prompt.get(
[
{
name: 'name',
description: 'name:',
default: title,
required: !title
}
],
(err, result) => {
if (err) return cb(err, null);
cb(null, result.name);
}
);
}

function getSelector(prompt, $, cb) {
prompt.get([{
name: 'selector',
description: 'selector:',
required: true
}], (err, result) => {
if (err) return cb(err, null);
if (!$.html($(result.selector).first())) {
console.log(`The selector ${result.selector} didn't return any elements, try again.`)
return getSelector(prompt, $, cb);
prompt.get(
[
{
name: 'selector',
description: 'selector:',
required: true
}
],
(err, result) => {
if (err) return cb(err, null);
if (!$.html($(result.selector).first())) {
console.log(
`The selector ${result.selector} didn't return any elements, try again.`
);
return getSelector(prompt, $, cb);
}
cb(null, result.selector);
}
cb(null, result.selector);
});
);
}

}

function build(opts, cb) {
if (!opts.input) return cb(new Error('please specify an input file.'));
if (!cb) cb = () => {};

var input = opts.input;
var file = input.substr(0, input.lastIndexOf("."));
var file = input.substr(0, input.lastIndexOf('.'));
var lockfile = opts.lockfile || file + '.lock';
var output = opts.output || file + '.html';
var opts = _.omit(opts, ['input', 'lockfile', 'output']);
Expand All @@ -216,27 +270,29 @@ function build(opts, cb) {
render(sites, lockfile, opts, (err, body) => {
if (err) return cb(err);

fs.readFile(path.join(__dirname, './views/default.css'), 'utf8', (err, css) => {
if (err) return cb(err);
var cssTag = `<style type="text/css">
fs.readFile(
path.join(__dirname, './views/default.css'),
'utf8',
(err, css) => {
if (err) return cb(err);
var cssTag = `<style type="text/css">
${css}
</style>
`
var html = createHTML({
title: 'New Work',
body: body,
head: cssTag,
lang: 'en'
});

fs.writeFile(output, html, function (err) {
if (err) return cb(err);
cb();
})
});
`;
var html = createHTML({
title: 'New Work',
body: body,
head: cssTag,
lang: 'en'
});

fs.writeFile(output, html, function(err) {
if (err) return cb(err);
cb();
});
}
);
});

});
}

Expand All @@ -246,8 +302,11 @@ function build(opts, cb) {
// if (err) console.log(err);
// });

build({
input: './sites.yaml'
}, (err) => {
if (err) return console.log(err);
});
build(
{
input: './sites.yaml'
},
err => {
if (err) return console.log(err);
}
);
Loading

0 comments on commit 4b9e715

Please sign in to comment.