Skip to content
This repository has been archived by the owner on Nov 23, 2018. It is now read-only.

Setting up spreadsheet CMS for 2019. #13

Open
wants to merge 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ node_modules/
/.wwwtfrc
/service-account-credentials.json
/secrets.tar
/schedule.json
/contents/schedule-json.txt
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ node_js:

script:
- npm run-script generate-locals
# - npm run-script generate-redirects
- npm run-script generate-redirects
- npm run-script build
# Because we build again, move the non-deploy build out of the way.
# - mv build test-build
- mv build test-build

before_deploy:
# - openssl aes-256-cbc -K $encrypted_92a5b1b18fab_key -iv $encrypted_92a5b1b18fab_iv -in secrets.tar.enc -out secrets.tar -d
# - tar xvf secrets.tar
# - npm run-script ci:import
# - npm run-script build
# - node tests/post-build.js
- openssl aes-256-cbc -in secrets.tar.enc -out secrets.tar.out -d -pass "env:encrypted_client_secrets_password"
- tar xvf secrets.tar
- npm run-script ci:import
- npm run-script build
- node tests/post-build.js

deploy:
provider: pages
Expand Down
10 changes: 7 additions & 3 deletions plugins/nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function(env, callback) {
extensions: {}
};
Object.assign(env.config.locals, require('../locals-generated.json'));
env.config.locals.schedule = require('../schedule.json');
// env.config.locals.schedule = require('../schedule.json');
env.config.locals.Date = Date;

// Load the new nunjucks environment.
Expand Down Expand Up @@ -47,14 +47,18 @@ module.exports = function(env, callback) {
nenv.opts.autoescape = options.autoescape;

class NunjucksTemplate extends env.TemplatePlugin {
constructor(template) {
constructor(template, filename) {
super();
this.template = template;
this.filename = filename;
}

render(locals, callback) {
try {
let html = this.template.render(locals);
if (!html) {
throw new Error('Template render failed' + this.filename);
}
html = minify(html, {
removeAttributeQuotes: true,
collapseWhitespace: true,
Expand All @@ -69,7 +73,7 @@ module.exports = function(env, callback) {
}

static fromFile(filepath, callback) {
callback(null, new NunjucksTemplate(nenv.getTemplate(filepath.relative)));
callback(null, new NunjucksTemplate(nenv.getTemplate(filepath.relative), filepath.relative));
}
}

Expand Down
1 change: 0 additions & 1 deletion schedule.json

This file was deleted.

86 changes: 86 additions & 0 deletions scripts/spreadsheet-import/credentials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const path = require('path');
const fs = require('fs');
const credentialsCache = {};

function getCredentialsPath() {
return (
process.env.WWWTF_CREDENTIALS_PATH ||
path.resolve(process.env.HOME, '.wwwtf18')
);
}

/**
* Loads a private credentials-file.
* Files with sensitive information are stored in the directory ~/.wwwtf18
* in JSON-Format. An alternative directory can be specified using the
* env-variable `WWWTF_CREDENTIALS_PATH`, but that is mostly intended
* to be used for CI/CD-servers and similar environments.
* @param {string} filename json-filename without any path.
* @param {boolean} ignoreMissing true to ignore missing files and return
* no content.
* @returns {*} the parsed content of the json-file
*/
function loadCredentials(filename, ignoreMissing = false) {
if (!credentialsCache[filename]) {
const credentialsFile = path.resolve(getCredentialsPath(), filename);

if (ignoreMissing && !fs.existsSync(credentialsFile)) {
return null;
}

try {
credentialsCache[filename] = require(credentialsFile);
} catch (err) {
console.error(
"🔐 It appears that you don't have your credentials setup yet.\n" +
' Please copy the file ' +
filename +
' to\n ' +
getCredentialsPath() +
'\n to continue. Ask your coworkers if you never heard of that file.'
);

throw new Error(`failed to load ${credentialsFile}: ${err}`);
}
}

return credentialsCache[filename];
}

/**
* Checks if credentials with the given filename exist.
* @param {string} filename file basename
* @return {boolean} -
*/
function hasCredentials(filename) {
const credentialsFile = path.resolve(getCredentialsPath(), filename);

return fs.existsSync(credentialsFile);
}

/**
* Stores credentials to a file in the credentials-store.
* @param {string} filename the file basename.
* @param {object} data the data to store.
*/
function storeCredentials(filename, data) {
credentialsCache[filename] = data;

const credentialsFile = path.resolve(getCredentialsPath(), filename);
try {
fs.writeFileSync(credentialsFile, JSON.stringify(data));
} catch (err) {
console.error(
`🔐 Failed to write credentials to file ${credentialsFile}.`
);

throw new Error(`failed to write credentials: ${err}`);
}
}

module.exports = {
getCredentialsPath,
loadCredentials,
hasCredentials,
storeCredentials
};
84 changes: 84 additions & 0 deletions scripts/spreadsheet-import/image-utils/speaker-image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch');
const chalk = require('chalk');
const imageType = require('image-type');
const imageSize = require('image-size');

function getImageFilename(speaker, ext) {
let filename = speaker.firstname + '-' + speaker.lastname;
filename = filename.replace(/[^\w]/g, '-');
filename = filename.replace(/--/g, '-').toLowerCase();

return filename + '.' + ext;
}

function getLocalSpeakerImage(imagePath, speaker) {
if (!imagePath) {
return null;
}

const filename = getImageFilename(speaker, 'jpg');
const srcFilename = path.join(imagePath, filename);
const destFilename = path.join('contents/images/speaker', filename);

if (fs.existsSync(srcFilename)) {
console.log(` --> image found in image-path:`, filename);
const buffer = fs.readFileSync(srcFilename);
const size = imageSize(buffer);
fs.writeFileSync(destFilename, buffer);

return {
filename,
width: size.width,
height: size.height
};
}

return null;
}

async function downloadSpeakerImage(speaker) {
const url = speaker.potraitImageUrl;
console.log('downloadImage', url);
if (!url) {
console.error(chalk.yellow('no image specified for ' + speaker.id));
return {};
}

try {
const res = await fetch(url);

if (!res.headers.get('content-type').startsWith('image')) {
console.error(chalk.red.bold(' !!! url is not an image', url));
return {};
}

const buffer = await res.buffer();

const info = imageType(buffer);
if (!info) {
console.error(chalk.red.bold(' !!! no type-imformation for image', url));
return {};
}

const size = imageSize(buffer);
const filename = getImageFilename(speaker, info.ext);
const fullPath = 'contents/images/speaker/' + filename;

console.info(' --> image downloaded ', chalk.green(fullPath));
fs.writeFileSync(fullPath, buffer);

return {
filename,
width: size.width,
height: size.height
};
} catch (err) {
console.error(chalk.red.bold(' !!! failed to download', url));
console.error(err);
return {};
}
}

module.exports = {getLocalSpeakerImage, downloadSpeakerImage};
61 changes: 61 additions & 0 deletions scripts/spreadsheet-import/image-utils/sponsor-image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch');
const chalk = require('chalk');

function getLocalSponsorImage(imagePath, sponsor) {
if (!imagePath) {
return null;
}

const filename = sponsor.id + '.svg';
const srcFilename = path.join(imagePath, filename);
const destFilename = path.join('contents/images/sponsor', filename);

if (fs.existsSync(srcFilename)) {
console.log(` --> image found in image-path:`, filename);
const buffer = fs.readFileSync(srcFilename);
fs.writeFileSync(destFilename, buffer);

return {
filename
};
}

return null;
}

async function downloadSponsorImage(sponsor) {
const url = sponsor.logoUrl;
console.log('downloadImage', url);
if (!url) {
console.error(chalk.yellow('no image specified for ' + sponsor.id));
return {};
}

try {
const res = await fetch(url);

if (!res.headers.get('content-type').startsWith('image')) {
console.error(chalk.red.bold(' !!! url is not an image', url));
return {};
}

const buffer = await res.buffer();
const filename = sponsor.id + '.svg';
const fullPath = `contents/images/sponsor/${filename}`;

console.info(' --> image downloaded ', chalk.green(fullPath));
fs.writeFileSync(fullPath, buffer);

return {
filename
};
} catch (err) {
console.error(chalk.red.bold(' !!! failed to download', url));
console.error(err);
return {};
}
}

module.exports = {getLocalSponsorImage, downloadSponsorImage};
Loading