-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-content-version.ts
50 lines (44 loc) · 1.95 KB
/
check-content-version.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 );
}
} );