Created
July 2, 2019 19:00
-
-
Save ckerr/41f8e7c8add833074c8142ed4858da99 to your computer and use it in GitHub Desktop.
throwaway script to update apps links from http to https. Migrates the link iff the migration doesn't cause the link to break. Note: written quickly to just get the job done
This file contains hidden or 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
#!/usr/bin/env node | |
const fetch = require('node-fetch') | |
const fsPromises = require('fs').promises | |
const isUrl = require('is-url') | |
const path = require('path') | |
const process = require('process') | |
const readdirp = require('readdirp') | |
const yaml = require('yamljs') | |
const { URL } = require('url') | |
// scrape a url to see if the link is broken. | |
// return a Promise that resolves as { url, err } | |
const scrape = url => | |
fetch(url, { method: 'HEAD' }) // just scrape headers; body not needed | |
.then(res => ({ url, err: res.ok ? null : `${res.status} ${res.statusText}` }), | |
err => ({ url, err })) | |
// modifies `root` inline: replaces any 'http://' URL with 'https://' if the latter resolves | |
// returns a Promise that resolves with `root` iff anything changed | |
const fixObject = root => { | |
const promises = [ ] | |
const queue = [ root ] | |
while (queue.length !== 0) { | |
const cur = queue.shift() | |
for (const [ key, val ] of Object.entries(cur)) { | |
if (!isUrl(val)) continue | |
const url = new URL(val) | |
if (url.protocol !== 'http:') continue | |
url.protocol = 'https:' | |
console.log(`${url} -- checking`) | |
promises.push(scrape(url.href).then(res => { | |
console.log(`${url} -- ${res.err ? res.err : 'ok'}`) | |
if (!res.err) cur[key] = url.href; | |
})) | |
} | |
} | |
return promises.length ? Promise.all(promises).then(() => root) : Promise.resolve() | |
} | |
// process all the yml app files | |
const processYmlEntry = entry => | |
fsPromises.readFile(entry.fullPath, { encoding: 'utf8' }) | |
.then(yaml.parse) | |
.then(fixObject) | |
.then(o => o ? fsPromises.writeFile(entry.fullPath, yaml.stringify(o, 2)) : Promise.resolve()) | |
const topDir = path.dirname(__dirname) | |
readdirp.promise(topDir, { fileFilter: '*.yml', directoryFilter: entry => entry.path.startsWith('apps') }) | |
.then(entries => entries.map(processYmlEntry)) | |
.then(processPromises => Promise.all(processPromises)) | |
.then(() => process.exit(0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment