Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buildmodules.sh in now javascript script build file #706

Open
wants to merge 1 commit 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
73 changes: 73 additions & 0 deletions bin/buildmodules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2023 Brenden Adamczak, Pur3 Ltd. See the file LICENSE for copying permission.
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const minify = require('./minify');

function same(file1,file2){
if(!fs.existsSync(file1)) return false;
if(!fs.existsSync(file2)) return false;
const f1 = fs.readFileSync(file1, 'utf8');
const f2 = fs.readFileSync(file2, 'utf8');
return f1 === f2;
}

function older(file1,file2){
if(!fs.existsSync(file1)) return false;
if(!fs.existsSync(file2)) return false;
return fs.statSync(file1).mtime > fs.statSync(file2).mtime;
}

const DIR= process.cwd();
const HOME = process.env.HOME;
const WEBSITE = `${HOME}/workspace/espruinowebsite`
const MODULEDIR= `${WEBSITE}/www/modules`;

var modules = [];
// Minify all modules
if(typeof process.env.npm_config_file === 'undefined'){
const dirList = ["devices","modules","boards"];
modules = dirList.map((dir)=>{return fs.readdirSync(dir).map((name)=>`${dir}/${name}`)}).flat();
modules = modules.filter(name => name.endsWith('.js') );
}
else{
//mifify a single module
modules[0] = process.env.npm_config_file;
}

modules.forEach(function (module) {
console.log(module);
if(!fs.existsSync(module)) {console.log(`${module} - doesn't exist`); return;}

const BNAME = path.basename(module) //'DS18B20.js'
const MINJS = `${path.parse(BNAME).name}.min.js` // e.g. 'DS18B20.min.js'
const FULL_NAME = `${MODULEDIR}/${BNAME}`;
const FULL_MINJS = `${MODULEDIR}/${MINJS}`;
// An optional externs-file must be in the same directory as the module file.
// Example devices/.../DS18B20.js → devices/.../DS18B20.externs
const externsFile = `${path.dirname(module)}/${path.parse(BNAME).name}.externs` // e.g. <module-path>/DS18B20.externs

// do nothing if ..
// .. the module code haven't changed and
// .. the target module file is newer than an existing externs file (or the externs file does not exist)
if(same(module, FULL_NAME) && older(FULL_NAME, externsFile)){
console.log("already made")
return;
}

//console.log(`Module [${BNAME}] is different or doesn't exist`);
fse.copySync(module, FULL_NAME);


minify(FULL_NAME,FULL_MINJS,externsFile);
console.log(FULL_MINJS)

if (fs.existsSync(FULL_MINJS) && fs.statSync(FULL_MINJS).size > 0) {
console.log(`${FULL_MINJS} compile successful`);
}
else {
if(fs.existsSync(FULL_MINJS)) fs.unlinkSync(FULL_MINJS);
console.log(`${BNAME} compile FAILED.`);
process.exit();
}
});
116 changes: 51 additions & 65 deletions bin/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,9 @@ var fs = require("fs");
const os = require('os');
const path = require('path');
const child_process = require("child_process");
const CLOSURE_JAR = path.join(__dirname, "..", "closure-compiler.jar");

var CLOSURE_JAR = path.join(__dirname, "..", "closure-compiler.jar");

if (process.argv.length!=4 && process.argv.length!=5) {
console.log("USAGE: node minify.js fileIn.js fileOut.min.js [fileIn.externs]");
process.exit(1);
}

var fileIn = process.argv[2];
var fileOut = process.argv[3];
var fileExterns = process.argv[4];

console.log("Minifying ",fileIn,"to",fileOut);
var js = fs.readFileSync(fileIn).toString();

// check if advanced optimization is possible
var advancedOptimisation = false;
var jsExterns = fs.readFileSync( JSEXTERNS_FILE );
if (fs.existsSync(fileExterns)) {
jsExterns += ("\n" + fs.readFileSync(fileExterns).toString());
advancedOptimisation = true;
}

// Now wrap up the JS so that the compiler will strip out any locals in simple optimization mode.
// This is not necessary with advance optimization.
if (!advancedOptimisation) {
js = wrapSelfInvocation( js );
}

var compilation_level = advancedOptimisation ? 'ADVANCED_OPTIMIZATIONS' : 'SIMPLE_OPTIMIZATIONS';
if (js.includes("MINIFY_WHITESPACE_ONLY"))
compilation_level = "WHITESPACE_ONLY";
var language_out = (js.includes('ECMASCRIPT_2015')?'ECMASCRIPT_2015':'STABLE');
console.log("compilation_level =",compilation_level,
"\nlanguage_out =",language_out);

function closureOnline() {
function closureOnline(js,jsExterns,fileOut, compilation_level,advancedOptimisation) {
var https = require("https");
var options = [
['compilation_level',compilation_level],
Expand Down Expand Up @@ -120,7 +86,7 @@ function closureOnline() {
var minified = jsonResult.compiledCode + '\n';

console.log('Minification complete! ' );
codeMinified( minified );
codeMinified(fileOut, minified,advancedOptimisation );
});
});
// post the data
Expand All @@ -129,7 +95,7 @@ function closureOnline() {
}


function closureOffline() {
function closureOffline(js,jsExterns,fileOut,compilation_level,advancedOptimisation) {
// wow. closure CLI doesn't like binary literals??
//js = js.replace(/(0b[01]+)/g, n => parseInt(n.substr(2),2));
// create files
Expand All @@ -152,25 +118,17 @@ function closureOffline() {
var cli;// = "closure-compiler";
cli = "java -jar "+'"'+CLOSURE_JAR+'"';
cli += " "+options.map( o => "--"+o[0]+" "+o[1]).join(" ");
child_process.exec(cli, (error, stdout, stderr) => {
fs.unlinkSync(tmpPath+".js", js)
fs.unlinkSync(tmpPath+".ext.js", js)
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
}
console.log(`stdout: ${stdout}`);
var finalJS = fs.readFileSync(tmpPath+".out.js").toString();
fs.unlinkSync(tmpPath+".out.js", js)
codeMinified(finalJS);
});
child_process.execSync(cli);

fs.unlinkSync(tmpPath+".js", js)
fs.unlinkSync(tmpPath+".ext.js", js)

var finalJS = fs.readFileSync(tmpPath+".out.js").toString();
fs.unlinkSync(tmpPath+".out.js", js)
codeMinified(fileOut,finalJS,advancedOptimisation);
}

function codeMinified(minified) {
function codeMinified(fileOut, minified,advancedOptimisation) {
if (!advancedOptimisation) {
minified = unwrapSelfInvocation( minified );
}
Expand Down Expand Up @@ -200,17 +158,45 @@ function unwrapSelfInvocation( wrappedJs ) {
}
}

// any other way to test for existence??
if (!fs.existsSync(CLOSURE_JAR)) {
console.log(CLOSURE_JAR);
console.log("===================================================================");
console.log("Using online closure compiler. To use faster offline version download");
console.log("the closure compiler jar to closure-compiler.jar");
console.log("===================================================================");
closureOnline();
} else {
console.log("Using offline closure compiler");
closureOffline();
module.exports = function(fileIn,fileOut,fileExterns){


console.log("Minifying ",fileIn,"to",fileOut);
var js = fs.readFileSync(fileIn).toString();

// check if advanced optimization is possible
var advancedOptimisation = false;
var jsExterns = fs.readFileSync( JSEXTERNS_FILE );
if (fs.existsSync(fileExterns)) {
jsExterns += ("\n" + fs.readFileSync(fileExterns).toString());
advancedOptimisation = true;
}

// Now wrap up the JS so that the compiler will strip out any locals in simple optimization mode.
// This is not necessary with advance optimization.
if (!advancedOptimisation) {
js = wrapSelfInvocation( js );
}

var compilation_level = advancedOptimisation ? 'ADVANCED_OPTIMIZATIONS' : 'SIMPLE_OPTIMIZATIONS';
if (js.includes("MINIFY_WHITESPACE_ONLY"))
compilation_level = "WHITESPACE_ONLY";
var language_out = (js.includes('ECMASCRIPT_2015')?'ECMASCRIPT_2015':'STABLE');
console.log("compilation_level =",compilation_level,
"\nlanguage_out =",language_out);

// any other way to test for existence??
if (!fs.existsSync(CLOSURE_JAR)) {
console.log(CLOSURE_JAR);
console.log("===================================================================");
console.log("Using online closure compiler. To use faster offline version download");
console.log("the closure compiler jar to closure-compiler.jar");
console.log("===================================================================");
closureOnline(js,jsExterns,fileOut,compilation_level,advancedOptimisation);
} else {
console.log("Using offline closure compiler");
closureOffline(js,jsExterns,fileOut,compilation_level,advancedOptimisation);
}
}


50 changes: 0 additions & 50 deletions buildmodules.sh

This file was deleted.

1 change: 0 additions & 1 deletion moduleignore

This file was deleted.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"eslintdevices": "./node_modules/.bin/eslint devices/*.js",
"eslintmodules": "./node_modules/.bin/eslint modules/*.js",
"eslintboards": "./node_modules/.bin/eslint boards/*.js",
"start": "node app.js"
"start": "node app.js",
"build_modules": "node ./bin/buildmodules.js"
},
"repository": {
"type": "git",
Expand All @@ -33,6 +34,7 @@
"acorn-walk": "^6.1.0",
"eslint": "^5.14.1",
"express": "^4.13.3",
"fs-extra": "^11.2.0",
"highlight.js": "^8.9.1",
"marked": "^0.3.6",
"tern": "^0.16.0"
Expand Down