Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for user-specifiable name for cache directory #5

Open
wants to merge 5 commits 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
18 changes: 10 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ function getHint(str) {
return arr && arr[1];
}

function getTarFile(obj) {
return path.join(DIR, obj.site, obj.repo, `${obj.type}.tar.gz`);
function getTarFile(obj, name) {
const dir = name ? path.join(HOME, `.${name}`) : DIR;
return path.join(dir, obj.site, obj.repo, `${obj.type}.tar.gz`);
}

function getTarUrl(obj) {
Expand All @@ -58,10 +59,10 @@ function parser(uri, host) {
return { site, repo, type };
}

function exists(file) {
function exists(file, name) {
// file is a `user/repo#tag`
if (!path.isAbsolute(file)) {
file = getTarFile( parser(file) );
file = getTarFile(parser(file), name);
}
return fs.existsSync(file) && file;
}
Expand All @@ -77,10 +78,10 @@ function run(arr) {
exports.fetch = function (repo, opts) {
opts = opts || {};
const info = parser(repo, opts.host);
const file = getTarFile(info);
const file = getTarFile(info, opts.name);
const uri = getTarUrl(info);

const local = _ => Promise.resolve( exists(file) );
const local = _ => Promise.resolve(exists(file, opts.name));
const remote = _ => download(uri, file);

return new Promise((res, rej) => {
Expand All @@ -100,11 +101,12 @@ exports.fetch = function (repo, opts) {
}

exports.extract = function (file, dest, opts) {
file = exists(file);
opts = opts || {};
file = exists(file, opts.name);
dest = path.resolve(dest || '.');
return new Promise((res, rej) => {
const ok = _ => res(dest);
opts = Object.assign({ strip:1 }, opts, { file, cwd:dest });
opts = Object.assign({ strip:1, name:null }, opts, { file, cwd:dest });
return file ? mkdirp(dest, err => err ? rej(err) : tar.extract(opts).then(ok).catch(rej)) : rej();
});
}
21 changes: 21 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ Only attempt to use an existing, cached file. No network requests will be dispat

> **Note:** Gittar enacts this option if it detects that there is no internet connectivity.

#### options.dirname
Type: `String`<br>
Default: `gittar`

The name of gittar's cache directory.

```js
gittar.fetch('herber/cargo', { name: 'custom' }).then(console.log);
//=> ~/.custom/github/herber/cargo/master.tar.gz
```

### gittar.extract(file, target, options)

Expand Down Expand Up @@ -189,6 +199,17 @@ gittar.extract(file, 'foo', { strip:0 });
//=> contents: foo/mri-master/**
```

#### options.name
Type: `String`<br>
Default: `gittar`

The name of gittar's cache directory.

```js
gittar.extract('herber/cargo#master', 'foo', { name: 'custom' });
// Searches in ~/.custom/ for the tarball
//=> contents: foo/cargo-master/**
```

## License

Expand Down
25 changes: 23 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,18 @@ test('gittar.fetch (local 404)', t => {
})
});

test('gittar.extract (user/repo)', t => {
test('gittar.fetch (user/repo) with custom name', t => {
t.plan(4);
const isOk = validate(t);
fn.fetch('lukeed/mri', { name: 'custom-name' }).then(isOk).then(cleanup).then(str => {
t.true( str.includes('github'), 'assumes `github` host by default' );
t.true( str.includes('master'), 'assumes `master` branch by default' );
t.true( str.includes('custom-name'), 'directory includes user-specified name' );
});
});

test('gittar.extract (user/repo)', t => {
t.plan(7);

const isOk = validate(t);
const repo = 'lukeed/mri';
Expand All @@ -146,11 +156,22 @@ test('gittar.extract (user/repo)', t => {
t.is(err, undefined, 'silently fails');
}).then(_ => {
// ensure file exists
fn.fetch(repo).then(zip => {
return fn.fetch(repo).then(zip => {
fn.extract(zip, tmp).then(isOk).then(str => {
t.true(stats(str).isDirectory(), 'creates the directory');
cleanup(zip) && cleanup(str);
});
});
}).then(_ => {
fn.fetch(repo, { name: 'custom-name' }).then(zip => {
t.true( zip.includes('custom-name'), 'directory includes user-specified name' );

fn.extract(zip, tmp, { name: 'custom-name' }).then(isOk).then(str => {
t.true(stats(str).isDirectory(), 'untars in user-specified directory');
cleanup(zip) && cleanup(str);
}).catch((err) => {
t.fail(err);
});
});
});
});