Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
Gist doc
Browse files Browse the repository at this point in the history
  • Loading branch information
erwan committed Oct 4, 2014
1 parent 080cee1 commit 37fc0df
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 97 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,4 +4,5 @@ local-build
.idea
doc
.grunt
src/version.js

116 changes: 23 additions & 93 deletions gulpfile.js
@@ -1,33 +1,46 @@
var gulp = require('gulp'),
gutil = require('gulp-util'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
qunit = require('gulp-qunit'),
jsdoc = require('gulp-jsdoc'),
jshint = require('gulp-jshint'),
map = require('vinyl-map'),
https = require('https'),
fs = require('fs'),
gist = require('gulp-gist'),
deploy = require("gulp-gh-pages");

var SOURCES = ['src/api.js', 'src/fragments.js', 'src/predicates.js'];

var pkg = require('./package.json');

gulp.task('concat', function() {
gulp.src(SOURCES)
function string_src(filename, string) {
var src = require('stream').Readable({ objectMode: true });
src._read = function () {
this.push(new gutil.File({ cwd: "", base: "", path: filename, contents: new Buffer(string) }));
this.push(null)
};
return src
}

gulp.task('version', function () {
return string_src("version.js", "Global.Prismic.version = '" + pkg.version + "';\n")
.pipe(gulp.dest('src/'));
});

gulp.task('concat', ['version'], function() {
gulp.src(SOURCES.concat('src/version.js'))
.pipe(concat('prismic.io.js'))
.pipe(gulp.dest('dist'))
});

gulp.task('minify', function() {
gulp.src(SOURCES)
gulp.task('minify', ['version'], function() {
gulp.src(SOURCES.concat('src/version.js'))
.pipe(concat('prismic.io.min.js'))
.pipe(uglify())
.pipe(gulp.dest('dist'))
});

gulp.task('copy', ['test'], function() {
gulp.src(SOURCES)
gulp.task('copy', ['test', 'version'], function() {
gulp.src(SOURCES.concat('src/version.js'))
.pipe(concat('prismic.io-%VERSION%.min.js'.replace('%VERSION%', pkg.version)))
.pipe(uglify())
.pipe(gulp.dest('dist'))
Expand All @@ -43,93 +56,10 @@ gulp.task('deploy:doc', ['doc'], function () {
.pipe(deploy());
});

// TODO: "throw" doesn't give a good error message
// TODO: Move to a plugin to use it in other kits
gulp.task('deploy:gist', ['test:doc'], function () {

// Remove indent from the left, aligning everything with the first line
function leftAlign(lines) {
if (lines.length == 0) return lines;
var distance = lines[0].match(/^\s*/)[0].length;
var result = [];
lines.forEach(function(line){
result.push(line.slice(Math.min(distance, line.match(/^\s*/)[0].length)));
});
return result;
}

function getUserHome() {
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
}

var pushgists = map(function(code, filename) {
var lines = code.toString().split("\n");
var gists = [];
var currentGist = null;
var lineNo = 0;
lines.forEach(function(line) {
if (line.indexOf("// startgist:") === 0) {
console.log("startgist");
if (currentGist) {
throw "L" + lineNo + ": Unexpected startgist: a previous gist was not closed";
}
currentGist = {
"id": line.split(":")[1].trim(),
"filename": line.split(":")[2].trim(),
"lines": []
};
} else if (line.indexOf("// endgist") === 0) {
console.log("endgist");
if (!currentGist) {
throw "L" + lineNo + ": Unexpected endgist: missing startgist earlier";
}
gists.push(currentGist);
currentGist = null;
} else if (currentGist) {
currentGist.lines.push(line);
}
});
if (currentGist) {
throw "Reached end of file but gist is still open";
}
fs.readFile(getUserHome() + '/.gistauth', 'utf8', function (err, auth) {
gists.forEach(function (gist) {
console.log("Gist id = " + gist.id);
console.log(leftAlign(gist.lines));
var json = {
files: {}
};
json.files[gist.filename] = {
'content': leftAlign(gist.lines).join("\n")
};
console.log(json);
var data = JSON.stringify(json);
var req = https.request({
"host": "api.github.com",
"path": "/gists/" + gist.id,
"method": "PATCH",
"headers": {
'User-Agent': 'erwan',
'Authorization': 'Basic ' + new Buffer(auth).toString('base64'),
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data.length
}
}, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.write(data);
req.end();
});
});
});

gulp.src("./test/doc.js")
.pipe(pushgists);
.pipe(gist());
});

gulp.task('jshint', function() {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -13,13 +13,13 @@
"devDependencies": {
"gulp": "~3.8.8",
"gulp-uglify": "~1.0.1",
"gulp-util": "~3.0.1",
"gulp-concat": "~2.4.1",
"gulp-qunit": "~0.3.3",
"gulp-jsdoc": "~0.1.4",
"gulp-rm": "~0.2.0",
"gulp-jshint": "~1.8.5",
"gulp-gh-pages": "~0.3.4",
"vinyl-map": "~1.0.1"
"gulp-gist": "~1.0.0",
"gulp-gh-pages": "~0.3.4"
},
"repository": {
"type": "git",
Expand Down
3 changes: 3 additions & 0 deletions src/api.js
Expand Up @@ -56,6 +56,9 @@
// Open the XHR
xhr.open('GET', url, true);

// Kit version (can't override the user-agent client side)
xhr.setRequestHeader("X-Prismic-User-Agent", "Prismic-javascript-kit/%VERSION%".replace("%VERSION%", Global.Prismic.version));

// Json request
xhr.setRequestHeader('Accept', 'application/json');

Expand Down
27 changes: 26 additions & 1 deletion test/doc.js
Expand Up @@ -18,7 +18,7 @@
setup: function() {}
});

asyncTest('Retrieve the API', 1, function() {
asyncTest('prismic-htmlSerializer.js', 1, function() {
Prismic.Api(testRepository, function (err, Api) {
Api.form('everything').query('[[:d = at(document.id, "UlfoxUnM0wkXYXbl")]]').ref(Api.master()).submit(function (err, documents) {
if (err) {
Expand Down Expand Up @@ -69,4 +69,29 @@
});
});

asyncTest('prismic-api.js', 1, function(){
// startgist:b253d8fddfdd4cceef7a:prismic-api.js
Prismic.Api('https://lesbonneschoses.prismic.io/api', function(err, Api) {
// You can use the Api object inside this block
console.log("References: ", Api.data.refs);
equal(Api.data.refs.length, 1); // gisthide
start(); // gisthide
});
// endgist
});

asyncTest('prismic-api.js', 1, function(){
// startgist:f3f7d4b970e964131271:prismic-simplequery.js
Prismic.Api('https://lesbonneschoses.prismic.io/api', function(err, Api) {
Api.form('everything')
.ref(Api.master())
.query(Prismic.Predicates.at("document.type", "product")).submit(function(err, response) {
// The documents object contains a Response object with all documents of type "product"
equal(response.results.length, 16); // gisthide
start(); // gisthide
});
});
// endgist
});

}(window.Prismic));

0 comments on commit 37fc0df

Please sign in to comment.