Skip to content

Commit

Permalink
chore: Add script "remove old electron apps" (electron#1805)
Browse files Browse the repository at this point in the history
* add script old electron version
  • Loading branch information
Toinane authored May 31, 2021
1 parent 712f6f3 commit e9dfc48
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
99 changes: 99 additions & 0 deletions lib/old-electron-apps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const fetch = require('node-fetch')
const fsPromises = require('fs').promises
const isUrl = require('is-url')
const path = require('path')
const readdirp = require('readdirp')
const yaml = require('yaml')

const topDir = path.dirname(__dirname)

// walk an object subtree looking for URL strings
const getObjectUrls = (root) => {
const urls = []
const queue = [root]
while (queue.length !== 0) {
const vals = Object.values(queue.shift())
urls.push(...vals.filter(isUrl))
queue.push(...vals.filter((v) => typeof v === 'object'))
}
return urls
}

// scrape a url to see if the link is broken.
// return a Promise that resolves as { url, err }
const scrape = (url) => {
const package =
'https://raw.githubusercontent.com/' +
url.replace(/^.*com/g, '').replace(/.git$/g, '') +
'/master/package.json'

return fetch(package, { method: 'GET' })
.then((res) => (res.status === 200 ? res.json() : {}))
.then((json) => {
let version
if (json.hasOwnProperty('devDependencies')) {
if (json.devDependencies.hasOwnProperty('electron'))
version = json.devDependencies.electron
.replace(/^\^/g, '')
.replace(/^~/g, '')
}
if (json.hasOwnProperty('dependencies')) {
if (json.dependencies.hasOwnProperty('electron'))
version = json.dependencies.electron
.replace(/^\^/g, '')
.replace(/^~/g, '')
}

return version
})
}

// scrape all the urls found in a yml file.
// report broken links to console.log().
// return a Promise that resolves as an array of broken links: [ { url, err }, ... ]
const processYmlEntry = (entry) =>
fsPromises
.readFile(entry.fullPath, { encoding: 'utf8' })
.then((file) => {
try {
return yaml.parse(file)
} catch (error) {
console.error(`Failed to parse ${entry.path}. Skipping.`)
return { disabled: true }
}
})
.then((o) => (o.disabled ? undefined : o.repository))
.then(async (url) => (url ? await scrape(url) : undefined))
.then((version) => {
version
? console.log(`${entry.path} - Electron v${version}`)
: console.log(`${entry.path} - Closed Source`)
return version
})

const findOldElectronApps = (start = 0, end = Infinity) =>
readdirp
.promise(topDir, {
fileFilter: '*.yml',
directoryFilter: (entry) => entry.path.startsWith('apps'),
})
.then(async (entries) => {
const result = []
let limitedEntries = entries

if (start !== 0 || end !== Infinity) {
limitedEntries = entries.slice(start, end)
}

for (const entry of limitedEntries) {
console.log(`Processing ${entry.path}`)
result.push({
entry,
result: await processYmlEntry(entry),
})
}

return result
})

module.exports = findOldElectronApps
59 changes: 59 additions & 0 deletions script/remove-old-electron-apps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env node

const fs = require('fs').promises
const semver = require('semver')

const findOldElectronApps = require('../lib/old-electron')

/* Links can break at any time and it's outside of the repo's control,
so it doesn't make sense to run this script as part of CI. Instead,
this should be run periodically as part of a separate process. */

process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason)
})

const numberArgs = process.argv.filter((v) => /^\d+$/.test(v))
const possibleStart =
numberArgs.length > 0 ? parseInt(numberArgs[0], 10) : undefined
const possibleEnd =
numberArgs.length > 0 ? parseInt(numberArgs[1], 10) : undefined

console.log(
`Checking apps ${possibleStart || 0} through ${
possibleEnd || 'infinity'
} for old electron apps`
)

async function main() {
const oldArrays = (
await findOldElectronApps(possibleStart, possibleEnd)
).filter((old) => {
if (old.result === undefined) return false
try {
return semver.lt(old.result, '4.0.0')
} catch (err) {
return false
}
})

console.log(`Will disable ${oldArrays.length} entries`)

for (const old of oldArrays) {
console.timeLog(old.entry.name)
let data = await fs.readFile(old.entry.fullPath, { encoding: 'utf-8' })

if (!data.endsWith('\n')) {
data += `\n`
}

data += `disabled: true # Old Electron version: v${old.result}\n`

await fs.writeFile(old.entry.fullPath, data, { encoding: 'utf-8' })

console.log(data)
console.log(`\n---\n`)
}
}

main()

0 comments on commit e9dfc48

Please sign in to comment.