Skip to content

Commit

Permalink
Pass index file name to mkdirp and add comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjrv committed Dec 9, 2015
1 parent 6609624 commit ce4aa2a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/cget/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class Cache {

createCachePath(urlRemote: string) {
return(this.getCachePath(urlRemote).then((cachePath: string) => {
return(mkdirp(path.dirname(cachePath)).then(() => cachePath));
return(mkdirp(path.dirname(cachePath), this.indexName).then(() => cachePath));
}));
}

Expand Down
8 changes: 8 additions & 0 deletions src/cget/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@

import * as Promise from 'bluebird';

/** Task wraps a promise, delaying it until some resource gets less busy. */

export class Task<ResultType> {
constructor(func?: () => Promise<ResultType>) {
this.func = func;
}

/** Start the task immediately and call onFinish callback when done. */

start(onFinish: (err?: NodeJS.ErrnoException) => void) {
// These fix atom-typescript syntax highlight: ))
return(this.func().finally(onFinish));
}

/** Wrap task result in a new promise so it can be resolved later. */

delay() {
return(new Promise((resolve: (result: ResultType) => void, reject: (err: any) => void) => {
this.resolve = resolve;
this.reject = reject;
}));
}

/** Resolve the result of a delayed task and call onFinish when done. */

resume(onFinish: (err?: NodeJS.ErrnoException) => void) {
// These fix atom-typescript syntax highlight: ))
return(this.start(onFinish).then(this.resolve).catch(this.reject));
Expand Down
11 changes: 8 additions & 3 deletions src/cget/TaskQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
import {Task} from './Task'

export class TaskQueue {
/** Add a new task to the queue.
* It will start when the number of other concurrent tasks is low enough. */

add(task: Task<any>) {
if(this.busyCount < TaskQueue.concurrency) {
// Start the task immediately.

++this.busyCount;
return(task.start(() => this.next()));
} else {
// Schedule the task and return a promise that will behave exactly
// like what task.start() returns.
// Schedule the task and return a promise resolving
// to the result of task.start().

this.backlog.push(task);
return(task.delay());
}
}

next() {
/** Start the next task from the backlog. */

private next() {
var task = this.backlog.shift();

if(task) task.resume(() => this.next());
Expand Down
20 changes: 16 additions & 4 deletions src/cget/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import * as url from 'url';
import * as path from 'path';
import * as Promise from 'bluebird';

/** Asynchronous versions of fs methods, wrapped by Bluebird. */

export var fsa = {
stat: Promise.promisify(fs.stat),
open: Promise.promisify(fs.open),
Expand All @@ -22,6 +24,8 @@ export var fsa = {
var againSymbol = {};
var again = () => againSymbol;

/** Promise while loop. */

export function repeat<T>(fn: (again: () => {}) => Promise<T>): Promise<T> {
return(Promise.try(() =>
fn(again)
Expand All @@ -30,6 +34,8 @@ export function repeat<T>(fn: (again: () => {}) => Promise<T>): Promise<T> {
));
}

/** Copy all members of src object to dst object. */

export function extend(dst: {[key: string]: any}, src: {[key: string]: any}) {
for(var key of Object.keys(src)) {
dst[key] = src[key];
Expand All @@ -38,11 +44,17 @@ export function extend(dst: {[key: string]: any}, src: {[key: string]: any}) {
return(dst);
}

/** Make shallow clone of object. */

export function clone(src: Object) {
return(extend({}, src));
}

export function mkdirp(pathName: string) {
/** Create a new directory and its parent directories.
* If a path component to create conflicts with an existing file,
* rename to file to <component>/<indexName>. */

export function mkdirp(pathName: string, indexName: string) {
var partList = path.resolve(pathName).split(path.sep);
var prefixList = partList.slice(0);
var pathPrefix: string;
Expand All @@ -59,14 +71,14 @@ export function mkdirp(pathName: string) {
// Trying to convert a file into a directory.
// Rename the file to indexName and move it into the new directory.

var tempPath = pathPrefix + '.' + this.makeTempSuffix(6);
var tempPath = pathPrefix + '.' + makeTempSuffix(6);

return(Promise.try(() =>
fsa.rename(pathPrefix, tempPath)
).then(() =>
fsa.mkdir(pathPrefix)
).then(() =>
fsa.rename(tempPath, path.join(pathPrefix, this.indexName))
fsa.rename(tempPath, path.join(pathPrefix, indexName))
));
} else if(!stats.isDirectory()) {
throw(new Error('Tried to create a directory inside something weird: ' + pathPrefix));
Expand Down Expand Up @@ -99,7 +111,7 @@ export function mkdirp(pathName: string) {
));
}

// Create a string of random letters and numbers.
/** Create a string of random letters and numbers. */

export function makeTempSuffix(length: number) {
return(
Expand Down

0 comments on commit ce4aa2a

Please sign in to comment.