git-gallery
Version:
A gallery app for showing work based on Git commits.
160 lines (133 loc) • 4.03 kB
JavaScript
const path = require('path');
const fs = require('fs-extra');
const ncp = require('ncp').ncp;
const readline = require('readline');
const fsUtils = require('../lib/fsUtils');
const galleryRoot = fsUtils.galleryRoot;
const commandLineCommands = require('command-line-commands');
const validCommands = [ null, 'init' ];
const { command, argv } = commandLineCommands(validCommands);
if (!command) {
if (ableToStart(true)) {
startServer();
}
} else if (command === 'init') {
if (ableToStart(false)) {
console.log("Found an existing gallery root at " + galleryRoot);
console.log("Aborting init.");
} else {
init();
}
}
function ableToStart(logErrors) {
let galleryExists = fsUtils.directoryExists(galleryRoot);
if (!galleryExists && logErrors) {
console.log("Unable to find gallery root at " + galleryRoot);
console.log("Run 'git-gallery init' to create a new gallery.");
}
let gitDir = path.resolve('./.git');
let gitExists = fsUtils.directoryExists(gitDir);
if (!gitExists && logErrors) {
console.log("Unable to find git repository at " + gitDir);
}
return galleryExists && gitExists;
}
function init() {
// create the .gitGallery directory
fs.mkdirSync(galleryRoot);
// copy in the views directory
let sourceViews = path.join(__dirname, '../views');
let destViews = path.join(galleryRoot, 'views');
ncp(sourceViews, destViews, { "clobber": false }, error => { if (error) console.error(error); });
// add a gallery.json file
let gjFile = path.join(galleryRoot, 'gallery.json');
fsUtils.writeJson({ 'title': '', 'comment': '', 'showCanvas': false }, gjFile, { 'flag': 'wx' }, error => { if (error) console.log(error); });
// create/edit .gitignore file
let gitignore = path.join(galleryRoot, '../.gitignore');
fs.ensureFileSync(gitignore);
var rd = readline.createInterface({
input: fs.createReadStream(gitignore),
output: process.stdout,
terminal: false
});
let alreadyIgnored = false;
rd.on('line', function(line) {
if (line.indexOf('.gitGallery') >= 0)
alreadyIgnored = true;
});
if (!alreadyIgnored) {
let fd = fs.openSync(gitignore, 'a');
fs.writeSync(fd, '\n.gitGallery\n');
}
}
/** Starts the GitGallery server. This code is copied from the autogenerated express file 'www'. */
function startServer() {
const app = require('../app');
const debug = require('debug')('express:server');
const http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Listening for connections on ' + bind);
}
}