pixel-editor/build.js

94 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-03-27 02:20:54 +03:00
var fs = require('fs-extra');
var hbs = require('handlebars');
var glob = require("glob");
var sass = require("sass");
var path = require("path");
var gulp = require('gulp');
var include = require('gulp-include');
const BUILDDIR = './build';
2019-03-28 05:19:15 +03:00
const SLUG = 'pixel-editor';
2019-03-27 02:20:54 +03:00
2019-03-28 05:19:15 +03:00
console.log('Building Pixel Editor');
2019-03-27 02:20:54 +03:00
hbs.registerHelper('svg', require('handlebars-helper-svg'));
require('hbs-register-helpers')(hbs,'./_ext/modules/hbs/helpers/**/*.js');
require('hbs-register-partials')(hbs,'./_ext/modules/hbs/_*.hbs');
//empty the build folder, or create it
fs.emptyDirSync(BUILDDIR);
//copy images
2019-03-28 05:19:15 +03:00
fs.copySync('./images','./build/'+SLUG);
2019-03-27 02:20:54 +03:00
//render js
gulp.src('./js/*.js')
.pipe(include({includePaths: [
'_ext/scripts',
2019-03-28 05:19:15 +03:00
'js',
'!js/_*.js',
2019-03-27 02:20:54 +03:00
]}))
.on('error', console.log)
2019-03-28 05:19:15 +03:00
.pipe(gulp.dest('build/'+SLUG));
2019-03-27 02:20:54 +03:00
//render css
var sassFiles = glob.sync('css/*.scss');
sassFiles.forEach((s) => {
var f = sass.renderSync({file: s, outFile: 'test.css', includePaths: ['css', '_ext/sass', '_ext/modules/css']});
console.log('compiling:',path.basename(f.stats.entry))
2019-03-28 05:19:15 +03:00
fs.writeFileSync('build/'+SLUG+'/'+path.basename(f.stats.entry,'scss')+'css', f.css);
2019-03-27 02:20:54 +03:00
});
//compile page
2019-03-28 05:19:15 +03:00
var pageTemplate = hbs.compile(fs.readFileSync('./views/'+SLUG+'.hbs',{encoding: 'utf8'}));
2019-03-27 02:20:54 +03:00
var page = pageTemplate({
2019-03-28 05:19:15 +03:00
projectSlug: SLUG,
title: 'Lospec Pixel Editor',
layout: false,
2019-03-27 02:20:54 +03:00
});
//save output
fs.writeFileSync('./build/index.htm',page);
//server
const express = require('express');
const app = express();
//ROUTE - index.htm
2019-03-28 05:19:15 +03:00
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname+'/build/index.htm'), {}, function (err) {
if (err) console.log('error sending file',err);
else {
setTimeout(()=>{
console.log('closing server')
server.close();
process.exit();
},1000*10);
}
});
});
2019-03-27 02:20:54 +03:00
//ROUTE - other files
app.use(express.static(path.join(__dirname, 'build')));
//start server
2019-03-28 05:19:15 +03:00
var server = app.listen(3000, () => {
2019-03-27 02:20:54 +03:00
console.log('\nTemp server started at http://localhost:3000!');
2019-03-28 05:19:15 +03:00
//console.log('press ctrl+c to stop ');
2019-03-27 02:20:54 +03:00
var opn = require('opn');
// opens the url in the default browser
opn('http://localhost:3000');
2019-03-28 05:19:15 +03:00
2019-03-27 02:20:54 +03:00
});