|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { exec as nodeExec } from "node:child_process"; |
| 4 | +import util from "node:util"; |
| 5 | +import { minify } from "./minify.mjs"; |
| 6 | + |
| 7 | +const exec = util.promisify( nodeExec ); |
| 8 | + |
| 9 | +const pkg = JSON.parse( await fs.readFile( "./package.json", "utf8" ) ); |
| 10 | + |
| 11 | +async function isCleanWorkingDir() { |
| 12 | + const { stdout } = await exec( "git status --untracked-files=no --porcelain" ); |
| 13 | + return !stdout.trim(); |
| 14 | +} |
| 15 | + |
| 16 | +async function versionForDist( { srcPath, destPath, version } ) { |
| 17 | + const code = await fs.readFile( srcPath, "utf8" ); |
| 18 | + const compiledContents = code.replace( /@VERSION/g, version ); |
| 19 | + |
| 20 | + await fs.mkdir( path.dirname( destPath ), { recursive: true } ); |
| 21 | + await fs.writeFile( destPath, compiledContents ); |
| 22 | + console.log( `${ destPath } v${ version } created.` ); |
| 23 | +} |
| 24 | + |
| 25 | +export async function build( { version = process.env.VERSION } = {} ) { |
| 26 | + |
| 27 | + // Add the short commit hash to the version string |
| 28 | + // when the version is not for a release. |
| 29 | + if ( !version ) { |
| 30 | + const { stdout } = await exec( "git rev-parse --short HEAD" ); |
| 31 | + const isClean = await isCleanWorkingDir(); |
| 32 | + |
| 33 | + // "+SHA" is semantically correct |
| 34 | + // Add ".dirty" as well if the working dir is not clean |
| 35 | + version = `${ pkg.version }+${ stdout.trim() }${ |
| 36 | + isClean ? "" : ".dirty" |
| 37 | + }`; |
| 38 | + } |
| 39 | + |
| 40 | + await versionForDist( { |
| 41 | + srcPath: "src/jquery.mousewheel.js", |
| 42 | + destPath: "dist/jquery.mousewheel.js", |
| 43 | + version |
| 44 | + } ); |
| 45 | + |
| 46 | + await minify( { |
| 47 | + srcPath: "dist/jquery.mousewheel.js", |
| 48 | + destPath: "dist/jquery.mousewheel.min.js", |
| 49 | + version |
| 50 | + } ); |
| 51 | +} |
0 commit comments