Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

Open Source Sustainability Wars: Episode V - The Funding Strikes Back #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .npmignore

This file was deleted.

6 changes: 0 additions & 6 deletions .travis.yml

This file was deleted.

78 changes: 0 additions & 78 deletions README.md

This file was deleted.

23 changes: 0 additions & 23 deletions bin/funding.js

This file was deleted.

147 changes: 36 additions & 111 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,119 +1,44 @@
const boxen = require('boxen')
const chalk = require('chalk')

const {
isHyper,
isITerm,
isCI,
isSilentMode
} = require('./lib/detect')

const { isShownRecently, markShown } = require('./lib/limit')

const { checkMessage } = require('./lib/check')
const messages = require('./messages.json')
const wrap = require('./lib/wrap')

function formatTitle (title) {
title = wrap(title)

if (!isCI()) {
title = chalk.black(title)
}

if (!isHyper() && !isITerm()) {
title = chalk.bold(title)
}

return title
#!/usr/bin/env node
const path = require('path')
const os = require('os')
const fs = require('fs')
const util = require('util')
const _exec = require('child_process').execSync
const exec = (cmd) => _exec(cmd, {encoding: 'utf8'}).trim()

if (require.main !== module) return

if (!process.argv[2] || process.argv[3]) {
const example = `${os.homedir}/Library/Application Support/BraveSoftware/Brave-Browser/Default`
console.warn(`
Usage: funding <Brave profile directory>
(eg '${example}' on OSX)

This will monkey-patch your npm to add any node modules you install to the
contribution list in Brave Rewards.
`.trim())

process.exit(1)
}

function formatText (text) {
text = wrap(text)

text = text.replace(
/{{([^}]*?)}}/g,
(match, url) => chalk.blue.underline(url)
)

if (!isCI()) {
text = chalk.black(text)
}
const PROFILE = process.argv[2]

return text
}

function formatUrl (url) {
url = wrap(url, { cut: true })
return chalk.blue.underline(url)
}
console.log('Monkeying around with npm-lifecycle...')
const npmRoot = exec('npm root -g')
process.chdir(path.join(npmRoot, 'npm', 'node_modules', 'npm-lifecycle'))

function formatMessage (message) {
const { title, text, url } = message
console.log('Installing sqlite (this might take a while)...')
exec('npm install better-sqlite3')

const coloredMessage = formatTitle(title) + '\n\n' + formatText(text) +
'\n\n' + formatUrl(url)
console.log('Writing global hook...')
const template = fs.readFileSync(path.join(__dirname, 'patch.template.js'), 'utf8')
fs.writeFileSync('./index-patch.js', template.replace('{{PROFILE}}', PROFILE))

const opts = {
align: 'center',
borderStyle: {
topLeft: ' ',
topRight: ' ',
bottomLeft: ' ',
bottomRight: ' ',
horizontal: ' ',
vertical: ' '
},
float: 'center',
margin: 0,
padding: {
top: 1,
right: 4,
bottom: 1,
left: 4
}
}
console.log('Patching package.json...')
const pkg = JSON.parse(fs.readFileSync('./package.json'))
pkg.main = 'index-patch.js'
fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2))

if (!isCI()) {
Object.assign(opts, {
backgroundColor: 'white'
})
}
console.log('Reticulating splines...')

return boxen(coloredMessage, opts)
}

function printMessage () {
// Do not print message when npm is run in silent mode
if (isSilentMode()) return

// Do not print message when one has been shown recently
if (isShownRecently()) return

// Skip running if no messages are available
if (messages.length === 0) return

// Select a random message
const i = Math.floor(Math.random() * messages.length)
const message = messages[i]

// Check if the strings are safe to print to the terminal. Specifically, the
// string should be plain ASCII, excluding control characters. This is
// paranoid and not strictly necessary since (1) we curate the messages.json
// file by hand and will never include non-ASCII text, and (2) we check the
// strings at package publish time (see test/messages.js). But it doesn't hurt
// to check again in the client and assert that messages are plain ASCII. This
// is the security principle of defense-in-depth.
checkMessage(message)

// Format the message and print it
const formattedMessage = formatMessage(message)
console.log(formattedMessage + '\n')

// Limit the frequency that messages are shown
markShown()
}

module.exports = {
printMessage
}
console.log('Done!')
32 changes: 0 additions & 32 deletions lib/check.js

This file was deleted.

43 changes: 0 additions & 43 deletions lib/detect.js

This file was deleted.

37 changes: 0 additions & 37 deletions lib/limit.js

This file was deleted.

Loading