Skip to content

Commit

Permalink
tweak build process
Browse files Browse the repository at this point in the history
  • Loading branch information
chmln committed Apr 11, 2017
1 parent d8b8814 commit 1dbe300
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 339 deletions.
55 changes: 31 additions & 24 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const fs = require('fs');
const glob = require("glob-fs")({ gitignore: true });
const glob = require("glob");

const babel = require("babel-core");
const uglifyJS = require("uglify-js");
const ncp = require("ncp");
const watch = require("node-watch");
const chokidar = require("chokidar");
const stylus = require("stylus");
const stylus_autoprefixer = require("autoprefixer-stylus");

Expand All @@ -24,7 +24,7 @@ const paths = {
}

function logErr(e){
console.error(e.message);
console.error(e);
}

function lint(code, filename){
Expand All @@ -49,17 +49,6 @@ async function recursiveCopy(src, dest) {
})
}

async function resolveGlob(path) {
return new Promise((resolve, reject) => {
glob.readdir(path, (err, files) => {
if (err)
reject(err);
else
resolve(files);
});
});
}

async function readFileAsync(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, (err, buffer) => {
Expand Down Expand Up @@ -104,23 +93,36 @@ async function buildScripts(){
writeFileAsync("./dist/flatpickr.min.js", uglify(transpiled)).catch(logErr);
}

async function buildExtras(folder){
function resolveGlob(g) {
return new Promise((resolve, reject) => {
glob(g, (err, files) =>{
if (err)
reject(err);

else
resolve(files);
});
});
}

function buildExtras(folder){
return async function(){
await recursiveCopy(`./src/${folder}`, `./dist/${folder}`);
const js = await resolveGlob(`./dist/${folder}/**/*.js`);
js.forEach(path => {
const paths = await resolveGlob(`./dist/${folder}/**/*.js`);

paths.forEach(path => {
readFileAsync(path)
.then(src => {
writeFileAsync(path, transpile(src))
.catch(logErr);
})
.catch(logErr);
});

}
}

async function transpileStyle(src, compress=false){

return new Promise((resolve, reject) => {
stylus(src, {
compress
Expand Down Expand Up @@ -157,7 +159,7 @@ async function buildStyle(){
async function buildThemes(){
const themePaths = await resolveGlob("./src/style/themes/*.styl");
themePaths.forEach(themePath => {
const themeName = /\/(\w+).styl/.exec(themePath)[1];
const themeName = /themes\/(.+).styl/.exec(themePath)[1];
readFileAsync(themePath)
.then(transpileStyle)
.then(css => writeFileAsync(`./dist/themes/${themeName}.css`, css));
Expand All @@ -166,22 +168,29 @@ async function buildThemes(){

function setupWatchers(){
watch(paths.script, buildScripts);
watch("./src/plugins", {recursive: true}, buildExtras("plugins"));
watch("./src/plugins", buildExtras("plugins"));
watch(paths.style, buildStyle);
watch("./src/style/themes", {recursive: true}, buildThemes);
watch("./src/style/themes", buildThemes);
}

function serve(){
livereload.createServer().watch("./dist");
server.createServer().listen(8080);
}

function watch(path, cb){
chokidar.watch(path)
.on('change', cb)
.on('error', logErr);
}

function start(){
process.stdout.write('\033c');
const devMode = process.argv.includes("--dev");
if (devMode) {
setupWatchers();
serve();
opn("http://localhost:8080");
}

else {
Expand All @@ -195,6 +204,4 @@ function start(){

start();

process.on('unhandledRejection', (reason, p) => {
logErr(reason);
});
process.on('unhandledRejection', logErr);
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@
"babel-plugin-transform-object-assign": "next",
"babel-plugin-transform-remove-strict-mode": "latest",
"babel-preset-es2015": "next",
"chokidar": "^1.6.1",
"coveralls": "latest",
"eslint": "latest",
"glob-fs": "^0.1.6",
"glob": "^7.1.1",
"http-server": "latest",
"jest": "latest",
"livereload": "latest",
"ncp": "latest",
"node-watch": "^0.5.2",
"nodemon": "^1.11.0",
"npm-run-all": "latest",
"object-assign": "^4.1.1",
"onchange": "latest",
"opn": "^4.0.2",
"rtlcss": "latest",
"semver": "latest",
Expand Down
Loading

0 comments on commit 1dbe300

Please sign in to comment.