Skip to content

Commit

Permalink
Merge pull request #298 from wmde/check-content
Browse files Browse the repository at this point in the history
Ensure latest content when building
  • Loading branch information
moiikana authored Dec 5, 2023
2 parents 00db0f6 + 6a53ede commit 0965fd0
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 3 deletions.
50 changes: 50 additions & 0 deletions check-content-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { exec } from 'child_process';

// This is a script that checks if the fundraising-frontend-content package has a never version
// than the one currently installed. This is useful to check if the content has been updated
// in the remote repository and the package-lock.json file needs to be updated.
// Run this before builing the banners

const CONTENT_REPO_URL = 'github.com/wmde/fundraising-frontend-content.git';
const CONTENT_REPO_BRANCH = 'production';

async function execShellCommand( cmd: string ): Promise<string> {
return new Promise( ( resolve, reject ) => {
// eslint-disable-next-line security/detect-child-process
exec( cmd, ( error, stdout, stderr ) => {
if ( error ) {
console.error( stderr );
reject( error );
}
resolve( stdout );
} );
} );
}

async function getCommitIdFromPackageLock(): Promise<string> {
const output = await execShellCommand( 'npm list --package-lock-only' );
const commitId = output.match( new RegExp( `${CONTENT_REPO_URL}#([0-9a-f]+)` ) );
if ( !commitId || !commitId[ 1 ] ) {
throw new Error( 'Could not determine commit id for content from package-lock.json' );
}
return commitId[ 1 ];
}

async function getCommitIdFromRemoteRepo(): Promise<string> {
const output = await execShellCommand( `git ls-remote --heads https://${CONTENT_REPO_URL}` );
const commitId = output.match( new RegExp( `([0-9a-f]+)\\s+refs/heads/${CONTENT_REPO_BRANCH}` ) );
if ( !commitId || !commitId[ 1 ] ) {
throw new Error( 'Could not determine commit it for content from remote repository' );
}
return commitId[ 1 ];
}

Promise.all( [ getCommitIdFromPackageLock(), getCommitIdFromRemoteRepo() ] ).then( ( [ localCommitId, remoteCommitId ] ) => {
if ( localCommitId !== remoteCommitId ) {
console.log(
`Content version mismatch, local version is ${localCommitId}, remote is ${remoteCommitId}.
Please run "npm run update-content" to update the package-lock.json file.`.replace( /\n\s+/g, '\n' )
);
process.exit( 1 );
}
} );
135 changes: 133 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"test": "npm-run-all test:*",
"test:unit": "vitest --run",
"watch": "webpack --watch",
"build": "webpack --config webpack.production.js",
"build": "npm-run-all -s check-content-version build:banners",
"build:banners": "webpack --config webpack.production.js",
"start": "npm run dev",
"dev": "nodemon",
"clean": "rimraf dist/*.js dist/*.wikitext dist/*.map",
Expand All @@ -17,6 +18,7 @@
"lint:ts": "vue-tsc --noEmit",
"lint:css": "stylelint '**/*.{scss,vue}'",
"ci": "npm-run-all lint test",
"check-content-version": "ts-node check-content-version.ts",
"coverage": "vitest run --coverage",
"update-content": "npm update fundraising-frontend-content"
},
Expand Down Expand Up @@ -62,6 +64,7 @@
"stylelint-webpack-plugin": "^4.1.1",
"toml": "^3.0.0",
"ts-loader": "^9.5.0",
"ts-node": "^10.9.1",
"typescript": "^5.2.2",
"vitest": "^0.34.6",
"vue-loader": "^17.3.0",
Expand Down

0 comments on commit 0965fd0

Please sign in to comment.