summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-10-12 11:13:31 +0200
committerSamy Pesse <samypesse@gmail.com>2016-10-12 11:13:31 +0200
commit0e7aece96d462771679ad7a3bd64e54f748f2b35 (patch)
tree4b1a27c6add25f239017860edfede9453e41bb79
parentb7c245d75adfd21264c74316dd7480ddd2f23dfe (diff)
downloadgitbook-0e7aece96d462771679ad7a3bd64e54f748f2b35.zip
gitbook-0e7aece96d462771679ad7a3bd64e54f748f2b35.tar.gz
gitbook-0e7aece96d462771679ad7a3bd64e54f748f2b35.tar.bz2
Use a single instance of Git for output
-rw-r--r--packages/gitbook/src/models/output.js5
-rw-r--r--packages/gitbook/src/output/__tests__/ebook.js1
-rw-r--r--packages/gitbook/src/output/createTemplateEngine.js3
-rw-r--r--packages/gitbook/src/parse/parseIgnore.js27
-rw-r--r--packages/gitbook/src/templating/__tests__/conrefsLoader.js32
-rw-r--r--packages/gitbook/src/templating/conrefsLoader.js6
-rw-r--r--packages/gitbook/src/utils/__tests__/git.js20
-rw-r--r--packages/gitbook/src/utils/git.js35
8 files changed, 78 insertions, 51 deletions
diff --git a/packages/gitbook/src/models/output.js b/packages/gitbook/src/models/output.js
index ab37c67..fe4aa75 100644
--- a/packages/gitbook/src/models/output.js
+++ b/packages/gitbook/src/models/output.js
@@ -1,5 +1,6 @@
const { Record, OrderedMap, Map, List } = require('immutable');
+const Git = require('../utils/git');
const LocationUtils = require('../utils/location');
const Book = require('./book');
const URIIndex = require('./uriIndex');
@@ -19,7 +20,9 @@ const DEFAULTS = {
// Internal state for the generation
state: Map(),
// Index of urls
- urls: new URIIndex()
+ urls: new URIIndex(),
+ // Git repositories manager
+ git: new Git()
};
class Output extends Record(DEFAULTS) {
diff --git a/packages/gitbook/src/output/__tests__/ebook.js b/packages/gitbook/src/output/__tests__/ebook.js
index 425ab6b..8b7096c 100644
--- a/packages/gitbook/src/output/__tests__/ebook.js
+++ b/packages/gitbook/src/output/__tests__/ebook.js
@@ -13,4 +13,3 @@ describe('EbookGenerator', function() {
});
});
});
-
diff --git a/packages/gitbook/src/output/createTemplateEngine.js b/packages/gitbook/src/output/createTemplateEngine.js
index 93b1c58..537038b 100644
--- a/packages/gitbook/src/output/createTemplateEngine.js
+++ b/packages/gitbook/src/output/createTemplateEngine.js
@@ -15,6 +15,7 @@ const defaultFilters = require('../constants/defaultFilters');
* @return {TemplateEngine}
*/
function createTemplateEngine(output) {
+ const { git } = output;
const plugins = output.getPlugins();
const book = output.getBook();
const rootFolder = book.getContentRoot();
@@ -29,7 +30,7 @@ function createTemplateEngine(output) {
// Create loader
const transformFn = Templating.replaceShortcuts.bind(null, blocks);
- const loader = new Templating.ConrefsLoader(rootFolder, transformFn, logger);
+ const loader = new Templating.ConrefsLoader(rootFolder, transformFn, logger, git);
// Create API context
const context = Api.encodeGlobal(output);
diff --git a/packages/gitbook/src/parse/parseIgnore.js b/packages/gitbook/src/parse/parseIgnore.js
index 3059447..a42805b 100644
--- a/packages/gitbook/src/parse/parseIgnore.js
+++ b/packages/gitbook/src/parse/parseIgnore.js
@@ -19,11 +19,11 @@ const DEFAULT_IGNORES = [
];
/**
- Parse ignore files
-
- @param {Book}
- @return {Book}
-*/
+ * Parse ignore files
+ *
+ * @param {Book} book
+ * @return {Book} book
+ */
function parseIgnore(book) {
if (book.isLanguageBook()) {
return Promise.reject(new Error('Ignore files could be parsed for language books'));
@@ -34,16 +34,19 @@ function parseIgnore(book) {
ignore = ignore.add(DEFAULT_IGNORES);
- return Promise.serie(IGNORE_FILES, function(filename) {
+ return Promise.serie(IGNORE_FILES, (filename) => {
return fs.readAsString(filename)
- .then(function(content) {
- ignore = ignore.add(content.toString().split(/\r?\n/));
- }, function(err) {
- return Promise();
- });
+ .then(
+ (content) => {
+ ignore = ignore.add(content.toString().split(/\r?\n/));
+ },
+ (err) => {
+ return Promise();
+ }
+ );
})
- .then(function() {
+ .then(() => {
return book.setIgnore(ignore);
});
}
diff --git a/packages/gitbook/src/templating/__tests__/conrefsLoader.js b/packages/gitbook/src/templating/__tests__/conrefsLoader.js
index 18dd5dd..1b8e92f 100644
--- a/packages/gitbook/src/templating/__tests__/conrefsLoader.js
+++ b/packages/gitbook/src/templating/__tests__/conrefsLoader.js
@@ -19,22 +19,22 @@ describe('ConrefsLoader', () => {
it('should include content from git', () => {
return renderTemplate(engine, fileName, '{% include "git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test.md" %}')
- .then(function(out) {
- expect(out.getContent()).toBe('Hello from git');
+ .then((out) => {
+ expect(out).toBe('Hello from git');
});
});
it('should handle deep inclusion (1)', () => {
return renderTemplate(engine, fileName, '{% include "git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test2.md" %}')
- .then(function(out) {
- expect(out.getContent()).toBe('First Hello. Hello from git');
+ .then((out) => {
+ expect(out).toBe('First Hello. Hello from git');
});
});
it('should handle deep inclusion (2)', () => {
return renderTemplate(engine, fileName, '{% include "git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test3.md" %}')
- .then(function(out) {
- expect(out.getContent()).toBe('First Hello. Hello from git');
+ .then((out) => {
+ expect(out).toBe('First Hello. Hello from git');
});
});
});
@@ -51,15 +51,15 @@ describe('ConrefsLoader', () => {
describe('Relative', () => {
it('should resolve basic relative filepath', () => {
return renderTemplate(engine, fileName, '{% include "include.md" %}')
- .then(function(out) {
- expect(out.getContent()).toBe('Hello World');
+ .then((out) => {
+ expect(out).toBe('Hello World');
});
});
it('should resolve basic parent filepath', () => {
return renderTemplate(engine, path.join(dirName, 'hello/test.md'), '{% include "../include.md" %}')
- .then(function(out) {
- expect(out.getContent()).toBe('Hello World');
+ .then((out) => {
+ expect(out).toBe('Hello World');
});
});
});
@@ -67,15 +67,15 @@ describe('ConrefsLoader', () => {
describe('Absolute', function() {
it('should resolve absolute filepath', () => {
return renderTemplate(engine, fileName, '{% include "/include.md" %}')
- .then(function(out) {
- expect(out.getContent()).toBe('Hello World');
+ .then((out) => {
+ expect(out).toBe('Hello World');
});
});
it('should resolve absolute filepath when in a directory', () => {
return renderTemplate(engine, path.join(dirName, 'hello/test.md'), '{% include "/include.md" %}')
- .then(function(out) {
- expect(out.getContent()).toBe('Hello World');
+ .then((out) => {
+ expect(out).toBe('Hello World');
});
});
});
@@ -103,8 +103,8 @@ describe('ConrefsLoader', () => {
it('should transform included content', () => {
return renderTemplate(engine, fileName, '{% include "include.md" %}')
- .then(function(out) {
- expect(out.getContent()).toBe('test-Hello World-endtest');
+ .then((out) => {
+ expect(out).toBe('test-Hello World-endtest');
});
});
});
diff --git a/packages/gitbook/src/templating/conrefsLoader.js b/packages/gitbook/src/templating/conrefsLoader.js
index 4024a19..3660d17 100644
--- a/packages/gitbook/src/templating/conrefsLoader.js
+++ b/packages/gitbook/src/templating/conrefsLoader.js
@@ -2,9 +2,9 @@ const path = require('path');
const nunjucks = require('nunjucks');
const fs = require('../utils/fs');
-const Git = require('../utils/git');
const LocationUtils = require('../utils/location');
const PathUtils = require('../utils/path');
+const Git = require('../utils/git');
/**
@@ -20,11 +20,11 @@ const PathUtils = require('../utils/path');
const ConrefsLoader = nunjucks.Loader.extend({
async: true,
- init(rootFolder, transformFn, logger) {
+ init(rootFolder, transformFn, logger, git = new Git()) {
this.rootFolder = rootFolder;
this.transformFn = transformFn;
this.logger = logger;
- this.git = new Git();
+ this.git = git;
},
getSource(sourceURL, callback) {
diff --git a/packages/gitbook/src/utils/__tests__/git.js b/packages/gitbook/src/utils/__tests__/git.js
index 445b8eb..29be4a1 100644
--- a/packages/gitbook/src/utils/__tests__/git.js
+++ b/packages/gitbook/src/utils/__tests__/git.js
@@ -1,13 +1,11 @@
const path = require('path');
-const os = require('os');
-
const Git = require('../git');
-describe('Git', function() {
+describe('Git', () => {
- describe('URL parsing', function() {
+ describe('URL parsing', () => {
- it('should correctly validate git urls', function() {
+ it('should correctly validate git urls', () => {
// HTTPS
expect(Git.isUrl('git+https://github.com/Hello/world.git')).toBeTruthy();
@@ -19,7 +17,7 @@ describe('Git', function() {
expect(Git.isUrl('README.md')).toBeFalsy();
});
- it('should parse HTTPS urls', function() {
+ it('should parse HTTPS urls', () => {
const parts = Git.parseUrl('git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test.md');
expect(parts.host).toBe('https://gist.github.com/69ea4542e4c8967d2fa7.git');
@@ -27,7 +25,7 @@ describe('Git', function() {
expect(parts.filepath).toBe('test.md');
});
- it('should parse HTTPS urls with a reference', function() {
+ it('should parse HTTPS urls with a reference', () => {
const parts = Git.parseUrl('git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test.md#1.0.0');
expect(parts.host).toBe('https://gist.github.com/69ea4542e4c8967d2fa7.git');
@@ -35,7 +33,7 @@ describe('Git', function() {
expect(parts.filepath).toBe('test.md');
});
- it('should parse SSH urls', function() {
+ it('should parse SSH urls', () => {
const parts = Git.parseUrl('git+git@github.com:GitbookIO/gitbook.git/directory/README.md#e1594cde2c32e4ff48f6c4eff3d3d461743d74e1');
expect(parts.host).toBe('git@github.com:GitbookIO/gitbook.git');
@@ -44,9 +42,9 @@ describe('Git', function() {
});
});
- describe('Cloning and resolving', function() {
- it('should clone an HTTPS url', function() {
- const git = new Git(path.join(os.tmpdir(), 'test-git-' + Date.now()));
+ describe('Cloning and resolving', () => {
+ it('should clone an HTTPS url', () => {
+ const git = new Git();
return git.resolve('git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test.md')
.then(function(filename) {
expect(path.extname(filename)).toBe('.md');
diff --git a/packages/gitbook/src/utils/git.js b/packages/gitbook/src/utils/git.js
index 8ec23dd..2b2a3e3 100644
--- a/packages/gitbook/src/utils/git.js
+++ b/packages/gitbook/src/utils/git.js
@@ -25,7 +25,9 @@ class Git {
allocateDir() {
const that = this;
- if (this.tmpDir) return Promise();
+ if (this.tmpDir) {
+ return Promise();
+ }
return fs.tmpDir()
.then(function(dir) {
@@ -33,7 +35,12 @@ class Git {
});
}
- // Clone a git repository if non existant
+ /**
+ * Clone a git repository if non existant
+ * @param {String} host: url of the git repository
+ * @param {String} ref: branch/commit/tag to checkout
+ * @return {Promise<String>} repoPath
+ */
clone(host, ref) {
const that = this;
@@ -63,7 +70,11 @@ class Git {
});
}
- // Get file from a git repo
+ /**
+ * Resole a git url, clone the repo and return the path to the right file.
+ * @param {String} giturl
+ * @return {Promise<String>} filePath
+ */
resolve(giturl) {
// Path to a file in a git repo?
if (!Git.isUrl(giturl)) {
@@ -80,7 +91,11 @@ class Git {
});
}
- // Return root of git repo from a filepath
+ /**
+ * Return root of git repo from a filepath
+ * @param {String} filePath
+ * @return {String} repoPath
+ */
resolveRoot(filepath) {
// No git repo cloned, or file is not in a git repository
if (!this.tmpDir || !pathUtil.isInRoot(this.tmpDir, filepath)) return null;
@@ -97,12 +112,20 @@ class Git {
return path.resolve(this.tmpDir, repoId);
}
- // Check if an url is a git dependency url
+ /**
+ * Check if an url is a git dependency url
+ * @param {String} giturl
+ * @return {Boolean} isUrl
+ */
static isUrl(giturl) {
return (giturl.indexOf(GIT_PREFIX) === 0);
}
- // Parse and extract infos
+ /**
+ * Parse and extract infos
+ * @param {String} giturl
+ * @return {Object} { host, ref, filepath }
+ */
static parseUrl(giturl) {
if (!Git.isUrl(giturl)) {
return null;