Enforce style, split build into build and server, parametrize build.js and server.js

This commit is contained in:
Theo Cavignac 2020-04-04 09:36:08 +02:00 committed by Théo (Lattay) Cavignac
parent a10453c7cb
commit 4123c069e2
4 changed files with 102 additions and 65 deletions

34
.eslintrc.json Normal file
View File

@ -0,0 +1,34 @@
{
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}

View File

@ -1,12 +1,12 @@
var fs = require('fs-extra'); const fs = require('fs-extra');
var hbs = require('handlebars'); const hbs = require('handlebars');
var glob = require("glob"); const glob = require('glob');
var sass = require("sass"); const sass = require('sass');
var path = require("path"); const path = require('path');
var gulp = require('gulp'); const gulp = require('gulp');
var include = require('gulp-include'); const include = require('gulp-include');
const BUILDDIR = './build'; const BUILDDIR = process.argv[2] || './build/';
const SLUG = 'pixel-editor'; const SLUG = 'pixel-editor';
console.log('Building Pixel Editor'); console.log('Building Pixel Editor');
@ -15,79 +15,43 @@ hbs.registerHelper('svg', require('handlebars-helper-svg'));
require('hbs-register-helpers')(hbs,'./_ext/modules/hbs/helpers/**/*.js'); require('hbs-register-helpers')(hbs,'./_ext/modules/hbs/helpers/**/*.js');
require('hbs-register-partials')(hbs,'./_ext/modules/hbs/_*.hbs'); require('hbs-register-partials')(hbs,'./_ext/modules/hbs/_*.hbs');
//empty the build folder, or create it // empty the build folder, or create it
fs.emptyDirSync(BUILDDIR); fs.emptyDirSync(BUILDDIR);
//copy images // copy images
fs.copySync('./images','./build/'+SLUG); fs.copySync('./images',path.join(BUILDDIR, SLUG));
//render js // render js
gulp.src('./js/*.js') gulp.src('./js/*.js')
.pipe(include({includePaths: [ .pipe(include({includePaths: [
'_ext/scripts', '_ext/scripts',
'js', 'js',
'!js/_*.js', '!js/_*.js',
]})) ]}))
.on('error', console.log) .on('error', console.log)
.pipe(gulp.dest('build/'+SLUG)); .pipe(gulp.dest(path.join(BUILDDIR, SLUG)));
//render css // render css
var sassFiles = glob.sync('css/*.scss'); var sassFiles = glob.sync('css/*.scss');
sassFiles.forEach((s) => { sassFiles.forEach((s) => {
var f = sass.renderSync({file: s, outFile: 'test.css', includePaths: ['css', '_ext/sass', '_ext/modules/css']}); var f = sass.renderSync({file: s, outFile: 'test.css', includePaths: ['css', '_ext/sass', '_ext/modules/css']});
console.log('compiling:',path.basename(f.stats.entry)) console.log('compiling:',path.basename(f.stats.entry));
fs.writeFileSync('build/'+SLUG+'/'+path.basename(f.stats.entry,'scss')+'css', f.css); fs.writeFileSync(path.join(BUILDDIR, SLUG, path.basename(f.stats.entry,'scss') + 'css'), f.css);
}); });
//compile page // compile page
var pageTemplate = hbs.compile(fs.readFileSync('./views/'+SLUG+'.hbs',{encoding: 'utf8'})); var pageTemplate = hbs.compile(fs.readFileSync(path.join('./views/', SLUG + '.hbs'),{encoding: 'utf8'}));
var page = pageTemplate({ var page = pageTemplate({
projectSlug: SLUG, projectSlug: SLUG,
title: 'Lospec Pixel Editor', title: 'Lospec Pixel Editor',
layout: false, layout: false,
}); });
//save output // save output
fs.writeFileSync('./build/index.htm',page); fs.writeFileSync(path.join(BUILDDIR, 'index.htm'),page);
//server
const express = require('express');
const app = express();
//ROUTE - index.htm
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);
}
});
});
//ROUTE - other files
app.use(express.static(path.join(__dirname, 'build')));
//start server
var server = app.listen(3000, () => {
console.log('\nTemp server started at http://localhost:3000!');
//console.log('press ctrl+c to stop ');
var opn = require('opn');
// opens the url in the default browser
opn('http://localhost:3000');
});

View File

@ -4,7 +4,9 @@
"description": "Online pixel art creation tool", "description": "Online pixel art creation tool",
"main": "build.js", "main": "build.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "build": "node ./build.js ./build",
"serve": "node ./server.js ./build 3000",
"test": "npm run build && npm run serve"
}, },
"author": "Lospec", "author": "Lospec",
"license": "ISC", "license": "ISC",

37
server.js Normal file
View File

@ -0,0 +1,37 @@
const path = require('path');
const express = require('express');
const app = express();
const BUILDDIR = process.argv[2] || './build';
const PORT = process.argv[3] || 3000;
//ROUTE - index.htm
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, BUILDDIR, 'index.htm'), {}, function (err) {
if(err){
console.log('error sending file',err);
} else {
setTimeout(()=>{
console.log('closing server');
server.close();
process.exit();
},1000*10);
}
});
});
//ROUTE - other files
app.use(express.static(path.join(__dirname, BUILDDIR)));
//start server
var server = app.listen(PORT, () => {
console.log(`\nTemp server started at http://localhost:${PORT}!`);
//console.log('press ctrl+c to stop ');
var opn = require('opn');
// opens the url in the default browser
opn(`http://localhost:${PORT}`);
});