diff --git a/.github/litejs.json b/.github/litejs.json index 0d76a81..ebfe687 100644 --- a/.github/litejs.json +++ b/.github/litejs.json @@ -8,5 +8,11 @@ "*.js", "lib/*.js" ] + }, + "test": { + "tz": "Europe/Tallinn", + "_": [ + "test/index.js" + ] } } diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1894fd3..65ba35d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,7 +9,7 @@ jobs: - run: npm install --no-audit && npm install -g uglify-js - run: npm audit --audit-level=low - run: npx c8 -r lcovonly -r text npm test - - run: npm run lint + - run: npx lj lint - uses: coverallsapp/github-action@v2 name: Upload to coveralls.io with: diff --git a/lib/build.js b/lib/build.js index 2d6b611..aa65575 100644 --- a/lib/build.js +++ b/lib/build.js @@ -92,9 +92,6 @@ function getExt(el) { var ext = getSrc(el).split("?")[0].split(".").pop() return ext === "tpl" || ext === "view" ? "ui" : ext } -function resolve(name) { - return fs.existsSync(name) ? name : require.resolve(defMap(name)) -} function drop(el, content) { var flags = el && el.getAttribute && el.getAttribute("drop") return flags ? content.replace( diff --git a/lib/lint.js b/lib/lint.js index 25a7a0a..178037e 100644 --- a/lib/lint.js +++ b/lib/lint.js @@ -5,22 +5,32 @@ module.exports = function(opts) { var vm = require("vm") + , exit = 0 + , child = require("child_process") , cli = require("..") - , fileNameRe = /^(?:\/?[a-z0-9](?:[-.]?[A-Za-z0-9])*)+\.[a-z0-9]+$/ + , invalidNameRe = /^[-\/]|[^-\/a-z0-9.]|[-\/]$/i , whitespaceErrRe = /^ +\t|^\t+ |\r|[ \t]$/m - , files = cli.ls(opts._[0] ? opts._ : ["*.json","*.js"], { dir: false }) + , files = cli.ls(opts._[0] ? opts._ : ["*.json", "*.js"], { dir: false }) + , jsFiles = files.filter(byExt(".js")) , formatNeeded = [].concat( validate(files, "js", isValidJs), validate(files, "json", isValidJson) ) - if (!files[0]) return console.error("No files to lint: " + opts._) + if (opts.jshint && jsFiles[0]) try { + child.execSync("npx jshint --config=" + opts.jshint + " " + jsFiles.join(" "), { stdio: ["ignore", 1, 2]}) + } catch(e) { + exit = 1 + } + if (formatNeeded.length > 0) { console.log("lint error: Format needed: " + formatNeeded.join(", ")) - process.exit(1) - } else { - console.log("# Lint OK %s files:", files.length, files.join(", ")) + exit = 1 } + if (!files[0]) return console.error("No files to lint: " + opts._) + if (exit) process.exit(exit) + else console.log("# Lint OK %s files:", files.length, files.join(", ")) + function validate(files, ext, validator) { return files.filter(function(name) { return name.split(".").pop() === ext && ( @@ -30,8 +40,14 @@ module.exports = function(opts) { }) } + function byExt(ext) { + return function(name) { + return name.slice(-ext.length) === ext + } + } + function isValidName(name) { - if (fileNameRe.test(name)) return true + if (!invalidNameRe.test(name)) return true console.error("Invalid file name:", JSON.stringify(name)) } diff --git a/opts.js b/opts.js index cf933c6..fd5f884 100644 --- a/opts.js +++ b/opts.js @@ -1,9 +1,7 @@ /* litejs.com/MIT-LICENSE.txt */ -exports.opts = opts - -function opts(defaults, files, argv) { +exports.opts = function opts(defaults, files, argv) { var expect, i, key, val , path = require("path") , short = {} diff --git a/package.json b/package.json index e7f472f..77a0f16 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,7 @@ "lib" ], "scripts": { - "test": "node ./cli.js test test/index.js --tz='Europe/Tallinn'", - "lint": "npx jshint --config=.github/jshint.json *.js lib/*.js" + "test": "node ./cli.js t" }, "dependencies": { "@litejs/dom": "24.12.0" diff --git a/test/opts.js b/test/opts.js index 28f7dd2..01926e4 100644 --- a/test/opts.js +++ b/test/opts.js @@ -44,7 +44,7 @@ describe("opts", function() { ], [ ["-abcd","-e", "f", "-g"], - {a_a:false, b_b:false, c_c:"",e_e:"",g_g:"h"}, {a:true, b:true, c:"d", e:"f",g:"", _valid: ["--a", "--b","--c=d", "--e=f","--g="], _unknown:[],_:[]} + {ax_a:false, bx_b:false, cx_c:"",ex_e:"",g_g:"h"}, {ax:true, bx:true, cx:"d", ex:"f",g:"", _valid: ["--ax", "--bx","--cx=d", "--ex=f","--g="], _unknown:[],_:[]} ], [ [], {print:false, to:"a", x:true}, {print:false, to:"a", x:true, _valid: [], _unknown:[],_:[]} ] ], function(argv, defaults, expected, assert) { diff --git a/test/tools.js b/test/tools.js index ae8c890..005d050 100644 --- a/test/tools.js +++ b/test/tools.js @@ -60,9 +60,9 @@ describe("tools", function() { // Assert options assert.equal(cli.ls(".github/*", { dir: false }).join(" "), ".github/jshint.json .github/litejs.json") assert.equal(cli.ls("*", { cwd: ".github", dir: false }).join(" "), "jshint.json litejs.json") - assert.own(cli.ls("*", { cwd: ".github", stat: true }), [ - { size: 225, name: "jshint.json" }, - { size: 21, name: "litejs.json" }, + assert.own(cli.ls("*", { cwd: ".github", stat: true }).map(s=>({size: s.size, name: s.name})), [ + { size: 232, name: "jshint.json" }, + { size: 247, name: "litejs.json" }, { size: 4096, name: "workflows" } ]) assert.equal(cli.ls(".github/*", { file: false }).join(" "), ".github/workflows")