-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
132 additions
and
28 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
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
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
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,76 @@ | ||
// @ts-ignore | ||
import * as packageJson from './package.json'; | ||
import { getJson, spawnPromise, writeFilePromise } from './utils'; | ||
|
||
(async () => { | ||
if ((await spawnPromise('git', 'status', '--porcelain')) !== '') { | ||
console.error('Your working directory needs to be clean!'); | ||
process.exit(1); | ||
} | ||
|
||
console.info('Checks for updates...'); | ||
|
||
const MDN_DATA = 'mdn-data'; | ||
const MDN_COMPAT = 'mdn-browser-compat-data'; | ||
|
||
const [mdnDataRepo, currentMdnDataCommit] = packageJson.devDependencies[MDN_DATA].split('#'); | ||
const [mdnCompatRepo, currentMdnCompatCommit] = packageJson.devDependencies[MDN_COMPAT].split('#'); | ||
|
||
const [mdnDataMaster, mdnCompatMaster] = [ | ||
await getJson({ | ||
hostname: 'api.github.com', | ||
path: '/repos/mdn/data/branches/master', | ||
headers: { 'User-Agent': 'NodeJS' }, | ||
}), | ||
await getJson({ | ||
hostname: 'api.github.com', | ||
path: '/repos/mdn/browser-compat-data/branches/master', | ||
headers: { 'User-Agent': 'NodeJS' }, | ||
}), | ||
]; | ||
|
||
const latestMdnDataCommit = mdnDataMaster.commit.sha; | ||
const latestMdnCompatCommit = mdnCompatMaster.commit.sha; | ||
|
||
if (latestMdnDataCommit !== currentMdnDataCommit || latestMdnCompatCommit !== currentMdnCompatCommit) { | ||
console.info('Update found, upgrading and building...'); | ||
|
||
packageJson.devDependencies[MDN_DATA] = `${mdnDataRepo}#${latestMdnDataCommit}`; | ||
packageJson.devDependencies[MDN_COMPAT] = `${mdnCompatRepo}#${latestMdnCompatCommit}`; | ||
|
||
await writeFilePromise('./package.json', JSON.stringify(packageJson, null, 2) + '\n'); | ||
|
||
try { | ||
await spawnPromise('yarn.cmd', '--silent', '--no-progress'); | ||
} catch (e) { | ||
console.error(e); | ||
process.exit(1); | ||
} | ||
|
||
const [indexDtsDiff, indexFlowDiff] = [ | ||
await spawnPromise('git', '--no-pager', 'diff', 'index.d.ts'), | ||
await spawnPromise('git', '--no-pager', 'diff', 'index.js.flow'), | ||
]; | ||
|
||
if (indexDtsDiff !== '' || indexFlowDiff !== '') { | ||
await spawnPromise('git', 'commit', '-am', 'Bump MDN'); | ||
|
||
const [major, minor, patch] = packageJson.version.split('.'); | ||
const version = `${major}.${minor}.${Number(patch) + 1}`; | ||
|
||
packageJson.version = version; | ||
await writeFilePromise('./package.json', JSON.stringify(packageJson, null, 2) + '\n'); | ||
await spawnPromise('git', 'commit', '-am', `v${version}`); | ||
await spawnPromise('git', 'tag', `v${version}`); | ||
|
||
console.info('Changes detected! The changes are committed and tagged. You just need to:'); | ||
console.info('- `git push origin HEAD --tags`'); | ||
console.info('- `npm publish`'); | ||
} else { | ||
console.info('No changes detected, resetting...'); | ||
await spawnPromise('git', 'reset', '--hard'); | ||
} | ||
} else { | ||
console.info('Nothing to update!'); | ||
} | ||
})(); |
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,48 @@ | ||
import { spawn } from 'child_process'; | ||
import { writeFile } from 'fs'; | ||
import { get, RequestOptions } from 'https'; | ||
|
||
const ROOT_DIR = __dirname; | ||
|
||
export function writeFilePromise(filename: string, content: string) { | ||
return new Promise((resolve, reject) => { | ||
writeFile(filename, content, 'utf-8', error => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
export function spawnPromise(command: string, ...args: string[]) { | ||
return new Promise((resolve, reject) => { | ||
const cp = spawn(command, args, { | ||
cwd: ROOT_DIR, | ||
}); | ||
let data = ''; | ||
cp.stdout.on('data', chunk => (data += chunk)); | ||
cp.on('close', code => (code === 0 ? resolve(data) : reject(data))); | ||
cp.on('error', reject); | ||
}); | ||
} | ||
|
||
export function getJson(url: RequestOptions): Promise<any> { | ||
return new Promise((resolve, reject) => { | ||
const req = get(url, res => { | ||
let data = ''; | ||
res.on('data', chunk => (data += chunk)); | ||
res.on('end', () => { | ||
try { | ||
const json = JSON.parse(data); | ||
resolve(json); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
}); | ||
req.on('error', reject); | ||
req.end(); | ||
}); | ||
} |