-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #298 from wmde/check-content
Ensure latest content when building
- Loading branch information
Showing
3 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} ); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters