diff --git a/tasks/gulp/__tests__/after-build-webjar.test.js b/tasks/gulp/__tests__/after-build-webjar.test.js index dde76363..d5e119ce 100644 --- a/tasks/gulp/__tests__/after-build-webjar.test.js +++ b/tasks/gulp/__tests__/after-build-webjar.test.js @@ -11,6 +11,7 @@ describe('webjar/', () => { let packagePackageJson; const cwd = process.cwd(); + const jarPath = './webjar-dist/uk/gov/hmrc/webjars/hmrc-frontend/'; beforeAll(() => { packagePackageJson = JSON.parse(readFileSync('package/package.json', 'utf8')); @@ -31,19 +32,31 @@ describe('webjar/', () => { it('should generate a jar file with the correct name', () => { const { version } = packagePackageJson; - expect(existsSync(`./webjar-dist/hmrc-frontend-${version}.jar`)).toEqual(true); + expect(existsSync(`${jarPath}/${version}/hmrc-frontend-${version}.jar`)).toEqual(true); }); it('should generate a pom file with the correct name', () => { const { version } = packagePackageJson; - expect(existsSync(`./webjar-dist/hmrc-frontend-${version}.pom`)).toEqual(true); + expect(existsSync(`${jarPath}/${version}/hmrc-frontend-${version}.pom`)).toEqual(true); + }); + + it('should generate a pom hash file with the correct name', () => { + const { version } = packagePackageJson; + + expect(existsSync(`${jarPath}/${version}/hmrc-frontend-${version}.pom.sha1`)).toEqual(true); + }); + + it('should generate a jar hash file with the correct name', () => { + const { version } = packagePackageJson; + + expect(existsSync(`${jarPath}/${version}/hmrc-frontend-${version}.jar.sha1`)).toEqual(true); }); it('should generate a jar file containing the correct files', async () => { const { version } = packagePackageJson; - const zip = new AdmZip(`./webjar-dist/hmrc-frontend-${version}.jar`); + const zip = new AdmZip(`${jarPath}/${version}/hmrc-frontend-${version}.jar`); const zipEntries = zip.getEntries().map((entry) => entry.entryName).filter((path) => !path.endsWith('/')).sort(); const webjarFiles = await getFiles('webjar'); @@ -58,6 +71,12 @@ describe('webjar/', () => { expect(groupId).toEqual('uk.gov.hmrc.webjars'); }); + it('should generate a POM hash file', async () => { + const groupId = await getHmrcPomField('/project/groupId'); + + expect(groupId).toEqual('uk.gov.hmrc.webjars'); + }); + it('should have the correct version', async () => { const version = await getHmrcPomField('/project/version'); diff --git a/tasks/gulp/webjar-helpers.js b/tasks/gulp/webjar-helpers.js index b23508dd..7fc31581 100644 --- a/tasks/gulp/webjar-helpers.js +++ b/tasks/gulp/webjar-helpers.js @@ -1,8 +1,10 @@ const { src, series, dest } = require('gulp'); -const fs = require('fs'); +const { + existsSync, copyFileSync, readFileSync, writeFileSync, mkdirSync, +} = require('fs'); const del = require('del'); -const rename = require('gulp-rename'); const { exec } = require('child_process'); +const crypto = require('crypto'); const groupId = 'uk.gov.hmrc.webjars'; @@ -12,7 +14,13 @@ const webJarHelpers = ({ const mavenPath = `${webjarPath}/META-INF/maven/${groupId}/${artifactId}`; const pomPath = `${mavenPath}/pom.xml`; - const getPackageJson = () => JSON.parse(fs.readFileSync(`${packagePath}/package.json`, 'utf8')); + const getPackageJson = () => JSON.parse(readFileSync(`${packagePath}/package.json`, 'utf8')); + + const getArtifactDirectory = () => { + const { version } = getPackageJson(); + + return `${webjarDistPath}/${groupId.replace(/\./g, '/')}/${artifactId}/${version}`; + }; const clean = () => del([ webjarPath, @@ -34,24 +42,43 @@ const webJarHelpers = ({ .pipe(dest(`${webjarPath}/META-INF/resources/webjars/${artifactId}/${version}/`)); }; - const createJar = () => { - const { version } = getPackageJson(); + const createArtifactDirectory = async () => { + const artifactPath = getArtifactDirectory(); - if (!fs.existsSync(webjarDistPath)) { - fs.mkdirSync(webjarDistPath); + if (!existsSync(artifactPath)) { + mkdirSync(artifactPath, { recursive: true }); } + }; + + const getArtifactPathPrefix = () => { + const { version } = getPackageJson(); + + return `${getArtifactDirectory()}/${artifactId}-${version}`; + }; + + const getJarArtifactPath = () => `${getArtifactPathPrefix()}.jar`; - return exec(`jar cMf ${webjarDistPath}/${artifactId}-${version}.jar -C ${webjarPath} .`); + const createJar = () => exec(`jar cMf ${getJarArtifactPath()} -C ${webjarPath} .`); + + const createShaFile = (path) => { + const fileBuffer = readFileSync(path); + const hashSum = crypto.createHash('sha1'); + hashSum.update(fileBuffer); + + writeFileSync(`${path}.sha1`, hashSum.digest('hex')); + }; + + const createJarSha = async () => { + createShaFile(getJarArtifactPath()); }; - const createPomDirectory = (cb) => { - if (!fs.existsSync(mavenPath)) { - fs.mkdirSync(mavenPath, { recursive: true }); + const createPomDirectory = async () => { + if (!existsSync(mavenPath)) { + mkdirSync(mavenPath, { recursive: true }); } - cb(); }; - const createPom = (cb) => { + const createPom = async () => { const { version, dependencies = {} } = getPackageJson(); const githubConnection = `${githubUrl}.git`; @@ -93,28 +120,32 @@ const webJarHelpers = ({ `; - fs.writeFileSync(`${pomPath}`, pom); - - cb(); + writeFileSync(pomPath, pom); }; - const copyPom = () => { - const { version } = getPackageJson(); + const getPomArtifactPath = () => `${getArtifactPathPrefix()}.pom`; - return src(pomPath) - .pipe(rename(`${artifactId}-${version}.pom`)) - .pipe(dest(`${webjarDistPath}/`)); + const copyPom = async () => { + copyFileSync(pomPath, getPomArtifactPath()); + }; + + const createPomSha = async () => { + createShaFile(getPomArtifactPath()); }; const buildWebjar = series( - clean, copyWebjarPackageFiles, createPomDirectory, createPom, createJar, copyPom, + clean, + copyWebjarPackageFiles, + createPomDirectory, + createPom, + createArtifactDirectory, + createJar, + createJarSha, + copyPom, + createPomSha, ); - const publishLocalWebjar = () => { - const { version } = getPackageJson(); - - return exec(`mvn install:install-file -Dfile=${webjarDistPath}/${artifactId}-${version}.jar -DpomFile=${webjarDistPath}/${artifactId}-${version}.pom`); - }; + const publishLocalWebjar = () => exec(`mvn install:install-file -Dfile=${getJarArtifactPath()} -DpomFile=${getPomArtifactPath()}`); return { buildWebjar,