|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs').promises; |
| 4 | +const path = require('path'); |
| 5 | +const http = require('http'); |
| 6 | +const globby = require('globby'); |
| 7 | +const got = require('got'); |
| 8 | +const htmlminifier = require('html-minifier').minify; |
| 9 | +const reporter = require('@springernature/util-cli-reporter'); |
| 10 | +const baseTemplate = require('./template'); |
| 11 | + |
| 12 | +// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string |
| 13 | +const semverRegex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/; |
| 14 | +const packageCdnLocation = 'https://cdn.jsdelivr.net/npm/'; |
| 15 | +const packageDemoPath = 'demo/dist/index.html'; |
| 16 | +const minifyOptions = { |
| 17 | + removeAttributeQuotes: true, |
| 18 | + removeComments: true, |
| 19 | + collapseInlineTagWhitespace: true, |
| 20 | + caseSensitive: true, |
| 21 | + minifyCSS: true, |
| 22 | + minifyJS: true |
| 23 | +}; |
| 24 | + |
| 25 | +/** |
| 26 | + * Check the remote package version number is valid semver |
| 27 | + * @private |
| 28 | + * @function checkPackageVersion |
| 29 | + * @param {String} packageName name of the package |
| 30 | + * @param {String} packageVersion version number to check |
| 31 | + */ |
| 32 | +const checkPackageVersion = (packageName, packageVersion) => { |
| 33 | + if (!semverRegex.test(packageVersion)) { |
| 34 | + reporter.fail('getting package from npm', `${packageName} v${packageVersion}`, 'invalid version number'); |
| 35 | + throw new Error(`Invalid semver '${packageVersion}'`); |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * Get the html for displaying the local demo version |
| 41 | + * @async |
| 42 | + * @private |
| 43 | + * @function getLocalPackageHtml |
| 44 | + * @param {String} packageName name and version of package on NPM |
| 45 | + * @return {Promise<Object>} |
| 46 | + */ |
| 47 | +const getLocalPackageHtml = async packageName => { |
| 48 | + const localDemoFiles = await globby(`**/${packageName}/${packageDemoPath}`, { |
| 49 | + expandDirectories: false, |
| 50 | + onlyFiles: true |
| 51 | + }); |
| 52 | + |
| 53 | + if (localDemoFiles.length === 0) { |
| 54 | + reporter.fail('unable to find local package demo', packageName); |
| 55 | + throw new Error('404: local package demo not found'); |
| 56 | + } |
| 57 | + |
| 58 | + // Should only be a single rendered demo |
| 59 | + // Return the html and version number |
| 60 | + const pathToLocalDemo = localDemoFiles[0]; |
| 61 | + const pathToPackageJson = path.join(pathToLocalDemo.replace(packageDemoPath, ''), 'package.json'); |
| 62 | + const version = require(path.resolve(pathToPackageJson)).version; |
| 63 | + const html = await fs.readFile(pathToLocalDemo, 'utf-8'); |
| 64 | + |
| 65 | + return { |
| 66 | + html: html, |
| 67 | + version: version |
| 68 | + }; |
| 69 | +}; |
| 70 | + |
| 71 | +/** |
| 72 | + * Create a server and display demos side by side |
| 73 | + * @async |
| 74 | + * @private |
| 75 | + * @function createServer |
| 76 | + * @param {String} port port to open local server on |
| 77 | + * @param {String} packageName name of the package to display |
| 78 | + * @param {Object} remote html and version from npm demo |
| 79 | + * @param {Object} local html and version from local demo |
| 80 | + * @return {Promise} |
| 81 | + */ |
| 82 | +const createServer = (port, packageName, remote, local) => { |
| 83 | + return new Promise(function (resolve, reject) { |
| 84 | + const server = http.createServer(); |
| 85 | + const remoteHtmlMinified = htmlminifier(remote.html, minifyOptions); |
| 86 | + const localHtmlMinified = htmlminifier(local.html, minifyOptions); |
| 87 | + const page = baseTemplate(packageName, { |
| 88 | + html: remoteHtmlMinified, |
| 89 | + version: remote.version |
| 90 | + }, { |
| 91 | + html: localHtmlMinified, |
| 92 | + version: local.version |
| 93 | + }); |
| 94 | + |
| 95 | + server.on('request', (_req, res) => { |
| 96 | + res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'}); |
| 97 | + res.end(page); |
| 98 | + }).listen(port, () => { |
| 99 | + reporter.info('manual diff available', `http://localhost:${port}/`); |
| 100 | + resolve(); |
| 101 | + }); |
| 102 | + |
| 103 | + server.on('error', error => { |
| 104 | + reporter.fail('unable to create server', `http://localhost:${port}/`); |
| 105 | + reject(error); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}; |
| 109 | + |
| 110 | +/** |
| 111 | + * Compare local version of package with one from NPM |
| 112 | + * @async |
| 113 | + * @function diffPackage |
| 114 | + * @param {String} npmPackage name and version of package on NPM |
| 115 | + * @param {String} scope NPM scope |
| 116 | + * @param {Number} port local server port |
| 117 | + * @return {Promise} |
| 118 | + */ |
| 119 | +const diffPackage = async (npmPackage, scope, port) => { |
| 120 | + const npmPackageArray = npmPackage.split('@'); |
| 121 | + const packageName = npmPackageArray[0]; |
| 122 | + const packageVersion = npmPackageArray[1]; |
| 123 | + const npmPackageUrl = path.join(packageCdnLocation, `${scope}/${npmPackage}`, packageDemoPath); |
| 124 | + let npmPackageHtml; |
| 125 | + let localPackage; |
| 126 | + |
| 127 | + reporter.info('visual comparison for package', packageName); |
| 128 | + |
| 129 | + // Check valid semver convention |
| 130 | + checkPackageVersion(packageName, packageVersion); |
| 131 | + |
| 132 | + // Get remote package html |
| 133 | + try { |
| 134 | + npmPackageHtml = await got(npmPackageUrl); |
| 135 | + } catch (error) { |
| 136 | + reporter.fail('getting package from npm', npmPackage, 'invalid get request'); |
| 137 | + throw error; |
| 138 | + } |
| 139 | + |
| 140 | + // Get local package html and version |
| 141 | + try { |
| 142 | + localPackage = await getLocalPackageHtml(packageName); |
| 143 | + } catch (error) { |
| 144 | + reporter.fail('getting package from local', packageName, 'could not find package html'); |
| 145 | + throw error; |
| 146 | + } |
| 147 | + |
| 148 | + // Create server for local comparison |
| 149 | + await createServer(port, packageName, { |
| 150 | + html: npmPackageHtml.body, |
| 151 | + version: packageVersion |
| 152 | + }, { |
| 153 | + html: localPackage.html, |
| 154 | + version: localPackage.version |
| 155 | + }); |
| 156 | +}; |
| 157 | + |
| 158 | +module.exports = diffPackage; |
0 commit comments