From 5a26ac146ee0c7afc3dd42d0cd93dbd14ceee4d6 Mon Sep 17 00:00:00 2001 From: Faisal Salman Date: Fri, 14 Apr 2023 23:43:09 +0700 Subject: [PATCH] Create build+test scripts --- .github/workflows/test-ci.yml | 3 +- package.json | 6 +-- script/build-dist.sh | 13 +++++++ script/build-module.js | 73 +++++++++++++++++++++-------------- script/test-all.sh | 21 ++++++++++ 5 files changed, 81 insertions(+), 35 deletions(-) create mode 100755 script/build-dist.sh mode change 100644 => 100755 script/build-module.js create mode 100755 script/test-all.sh diff --git a/.github/workflows/test-ci.yml b/.github/workflows/test-ci.yml index 899ad44..5c02b3d 100644 --- a/.github/workflows/test-ci.yml +++ b/.github/workflows/test-ci.yml @@ -19,6 +19,5 @@ jobs: - name: Run the test run: | npm ci - npm run build npx playwright install - npm run test-ci + npm test diff --git a/package.json b/package.json index 32c627a..81e2905 100644 --- a/package.json +++ b/package.json @@ -163,9 +163,8 @@ "src" ], "scripts": { - "build": "uglifyjs src/ua-parser.js -o dist/ua-parser.min.js --comments '/^ UA/' && uglifyjs src/ua-parser.js -o dist/ua-parser.pack.js --comments '/^ UA/' --compress --mangle && node script/build-module.js", - "test": "jshint src && mocha -R nyan test/mocha*js && npx playwright test && npx lockfile-lint -p package-lock.json", - "test-ci": "jshint src && mocha -R spec test/mocha*js && npx playwright test && npx lockfile-lint -p package-lock.json" + "build": "./script/build-dist.sh && ./script/build-module.js", + "test": "npm run build && ./script/test-all.sh" }, "devDependencies": { "@babel/parser": "7.15.8", @@ -187,6 +186,7 @@ }, "directories": { "dist": "dist", + "script": "script", "src": "src", "test": "test" }, diff --git a/script/build-dist.sh b/script/build-dist.sh new file mode 100755 index 0000000..4df2da7 --- /dev/null +++ b/script/build-dist.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +SRC_PATH="src/ua-parser.js" +MIN_PATH="dist/ua-parser.min.js" +PACK_PATH="dist/ua-parser.pack.js" + +# minified +echo "Generate ${MIN_PATH}" +uglifyjs $SRC_PATH -o $MIN_PATH --comments "/^ UA/" + +# packed +echo "Generate ${PACK_PATH}" +uglifyjs $SRC_PATH -o $PACK_PATH --comments "/^ UA/" --compress --mangle \ No newline at end of file diff --git a/script/build-module.js b/script/build-module.js old mode 100644 new mode 100755 index 5b47349..4d36273 --- a/script/build-module.js +++ b/script/build-module.js @@ -1,40 +1,53 @@ #!/usr/bin/env node +/* jshint esversion: 6 */ const fs = require('fs'); +const PATH = { + main : { + src : 'src/ua-parser.js', + dest : 'src/module/ua-parser.mjs', + title : '' + }, + enum : { + src : 'src/ua-parser-enum.js', + dest :'src/module/ua-parser-enum.mjs', + title : 'enum' + }, + extension : { + src : 'src/ua-parser-extension.js', + dest : 'src/module/ua-parser-extension.mjs', + title : 'extension' + } +}; +const generateMJS = (module, replacers) => { + const { src, dest, title } = PATH[module]; + let text = fs.readFileSync(src, 'utf-8'); + replacers.forEach(replacer => { + text = text.replace(replacer[0], replacer[1]); + }); -/*/////////////// -// ua-parser.mjs -//////////////*/ + console.log(`Generate ${dest}`); -fs.writeFileSync('src/module/ua-parser.mjs', -`// Generated ESM version of UAParser.js + fs.writeFileSync(dest, +`// Generated ESM version of UAParser.js ${title} // DO NOT EDIT THIS FILE! // Source: /src/ua-parser.js -` + fs.readFileSync('src/ua-parser.js','utf-8').replace(/\(func[\s\S]+strict\';/ig,'') - .replace(/esversion\: 3/ig, 'esversion: 6') - .replace(/\/[\/\s]+export[\s\S]+/ig,'export {UAParser};'),'utf-8'); +${text}`, 'utf-8'); +}; + +if (!fs.existsSync('src/module')) { + fs.mkdirSync('src/module', { recursive: true }); +} + +// ua-parser.mjs +generateMJS('main', [ + [/\(func[\s\S]+strict\';/ig, ''], + [/esversion\: 3/ig, 'esversion: 6'], + [/\/[\/\s]+export[\s\S]+/ig,'export {UAParser};'] +]); -/*///////////////////// // ua-parser-enum.mjs -////////////////////*/ +generateMJS('enum', [[/module\.exports =/ig, 'export']]); -fs.writeFileSync('src/module/ua-parser-enum.mjs', -`// Generated ESM version of UAParser.js enums -// DO NOT EDIT THIS FILE! -// Source: /src/ua-parser-enum.js - -` + fs.readFileSync('src/ua-parser-enum.js','utf-8') - .replace(/module\.exports =/ig,'export'),'utf-8'); - -/*////////////////////////// -// ua-parser-extension.mjs -/////////////////////////*/ - -fs.writeFileSync('src/module/ua-parser-extension.mjs', -`// Generated ESM version of UAParser.js extensions -// DO NOT EDIT THIS FILE! -// Source: /src/ua-parser-extension.js - -` + fs.readFileSync('src/ua-parser-extension.js','utf-8') - .replace(/const UA.+\)/ig,'import UAParser from \'ua-parser-js\'') - .replace(/module\.exports =/ig,'export'),'utf-8'); \ No newline at end of file +// ua-parser-extension.mjs +generateMJS('extension', [[/module\.exports =/ig, 'export']]); \ No newline at end of file diff --git a/script/test-all.sh b/script/test-all.sh new file mode 100755 index 0000000..b77df1a --- /dev/null +++ b/script/test-all.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +echo ' +- lint js code +' +jshint src && jshint script || exit 1 + +echo ' +- test using mocha +' +mocha -R list test/mocha*js || exit 1 + +echo ' +- test using playwright +' +npx playwright test || exit 1 + +echo ' +- lint lockfile +' +npx lockfile-lint -p package-lock.json || exit 1 \ No newline at end of file