|
#!/usr/bin/env node |
|
|
|
var colors = require('colors'), |
|
prompt = require('prompt'), |
|
ngist = require('ngist'), |
|
child = require('child_process'), |
|
path = require('path'), |
|
eyes = require('eyes'), |
|
fs = require('fs'); |
|
|
|
var base = process.cwd(), |
|
config = ['user', 'token'], |
|
step = config.length, |
|
error = function(err) { |
|
console.log(err.stack.red); |
|
process.exit(1); |
|
}; |
|
|
|
console.log('Start..'.cyan); |
|
console.log('Things'.grey); |
|
console.log('Current directory: ', base); |
|
|
|
// holder for git config stuff |
|
var gh = {}; |
|
|
|
// get github info, for posting gists, from the git global config |
|
child.exec('git config --get github.user', function(err, stdout, stderr) { |
|
if(err) return error(new Error(ghHint('user'))); |
|
gh.user = stdout.trim(); |
|
next(); |
|
}); |
|
|
|
child.exec('git config --get github.token', function(err, stdout, stderr) { |
|
if(err) return error(new Error(ghHint('token'))); |
|
gh.token = stdout.trim(); |
|
next(); |
|
}); |
|
|
|
|
|
|
|
// ## runnnn |
|
// done after the github.user, github.token are ok to be used |
|
function runnnnnn() { |
|
if(process.argv.slice(2)[0] === 'gist') { |
|
return pushGist(); |
|
} |
|
|
|
generateFiddle(); |
|
} |
|
|
|
|
|
|
|
// ## main tasks, creating folder structure, creating gist |
|
function pushGist() { |
|
console.log('Okay, going to publish gists...'.yellow); |
|
|
|
var files = gimmeFiddles(base); |
|
|
|
if(!Object.keys(files).length) return error(new Error('No fiddles to gist!')); |
|
|
|
// must have a fiddle.manifest, also used to setup basic |
|
// gist info, namely description |
|
if(!files['fiddle.manifest']) return error(new Error('No manifest')); |
|
|
|
var desc = files['fiddle.manifest'].match(/description:\s?(.+)/); |
|
|
|
var gists = Object.keys(files).map(function(file) { |
|
return { |
|
name: file, |
|
contents: files[file] |
|
}; |
|
}); |
|
|
|
var options = { |
|
user: gh.user, |
|
token: gh.token, |
|
description: desc ? desc[1] : 'jsfiddle gist', |
|
private: true |
|
}; |
|
|
|
console.log(' Posting gists... '.yellow); |
|
|
|
eyes.inspect(gists); |
|
|
|
console.log('\n With options...'.yellow); |
|
eyes.inspect(options); |
|
|
|
return ngist.send(gists, options, function(err, url) { |
|
if(err) return error(err); |
|
console.log((' ✔ ' + url + ' created...').green); |
|
|
|
var sha = url.split('/').slice(-1); |
|
|
|
var fwkPrompts = [{ |
|
message: 'choose framework', |
|
name: 'framework', |
|
default: 'jquery' |
|
}, { |
|
message: 'version', |
|
name: 'version', |
|
default: '1.6.3' |
|
}]; |
|
|
|
prompt.start(); |
|
|
|
console.log((' Creating fiddle url from ' + sha).grey); |
|
|
|
console.log(' Available options: '.grey); |
|
|
|
fiddleFwks(); |
|
|
|
prompt.get(fwkPrompts, function(err, result) { |
|
if(err) return error(err); |
|
var fiddleurl = ["http://jsfiddle.net/gh/gist", result.framework, result.version, sha].join('/'); |
|
|
|
// may change pbcopy here by open, to automatically open the fiddle url |
|
// in the default browser (both maconly). May allow to configure this with |
|
// environment variables. |
|
child.exec('echo ' + fiddleurl + ' | pbcopy ', function(err) { |
|
if(err) return error(err); |
|
console.log((' » ' +fiddleurl + 'and pasted to your clipboard').grey); |
|
}); |
|
}); |
|
|
|
}); |
|
} |
|
|
|
function generateFiddle() { |
|
// where youd like to put stuff, relative to the current directory |
|
var prompts = { |
|
message: 'Enter the relative path to the output directory', |
|
name: 'directory', |
|
validator: /^[\w-]+$/, |
|
warning: 'Wrong pattern, should comply with /[\w-]/', |
|
empty: false |
|
}; |
|
|
|
// get jsfiddle files in the current repository |
|
var fiddles = gimmeFiddles(__dirname); |
|
|
|
prompt.start(); |
|
prompt.get(prompts, function(err, result) { |
|
if(err) return error(err); |
|
|
|
var output = path.join(base, result.directory); |
|
|
|
child.exec('mkdir -p ' + output, function(code, stdout, stderr) { |
|
if(code > 0) return error(stdout || stderr); |
|
console.log((' ✓ Created dir ' + output).green); |
|
|
|
console.log(('Writing ' + Object.keys(fiddles).length + ' files...').yellow); |
|
Object.keys(fiddles).forEach(function(fiddle) { |
|
fs.writeFileSync(path.join(output, fiddle), fiddles[fiddle]); |
|
console.log((' ✓ Created ' + fiddle).green); |
|
}); |
|
|
|
console.log(('\n\n » Fiddles generated, cd to ' + output + ' and mess with them').green); |
|
}); |
|
}); |
|
} |
|
|
|
|
|
// ## function helpers |
|
function gimmeFiddles(dir) { |
|
var fiddles = {}; |
|
// read this gist content, searching for fiddle files: `fiddle.*` |
|
fs.readdirSync(dir).forEach(function(file) { |
|
var src = path.join(dir, file); |
|
if(!fs.statSync(src).isFile()) return; |
|
if(!/^fiddle\./.test(file)) return; |
|
fiddles[file] = fs.readFileSync(src, 'utf8'); |
|
}); |
|
return fiddles; |
|
} |
|
|
|
function ghHint(config) { |
|
return [ |
|
'Failed to load github ' + config + ' from git config.', |
|
'', |
|
'Run these two commands:', |
|
'', |
|
'git config --global github.user "your-github-username"', |
|
'git config --global github.token "your-github-token"', |
|
'', |
|
'', |
|
'To verify that they have been set, use:', |
|
'', |
|
'git config --get github.user', |
|
'git config --get github.token', |
|
'' |
|
].join('\n'); |
|
} |
|
|
|
function next() { |
|
if(--step === 0) runnnnnn(); |
|
} |
|
|
|
function fiddleFwks() { |
|
// the list of jsfiddle fwks |
|
var fwks = fs.readFileSync(path.join(__dirname, 'jsfiddle.fwk'), 'utf8').split('\n') |
|
// filter empty lines |
|
.filter(function(item) { return item.trim(); }) |
|
.map(function(item) { |
|
var m = item.match(/(\w+)\s([\d\.]+)/); |
|
return !m ? item : { |
|
name: m[1], |
|
version: m[2] |
|
}; |
|
}); |
|
|
|
eyes.inspect(fwks); |
|
return fwks; |
|
} |