Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Add packageNameFormats export #10

Open
wants to merge 2 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jspm-registry",
"version": "0.4.0",
"version": "0.4.1",
"description": "jspm registry and override endpoint",
"main": "registry.js",
"scripts": {
Expand Down
269 changes: 134 additions & 135 deletions registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,44 @@ var registry = module.exports = function registry(options, ui) {

this.username = options.username;
this.password = options.password;
}
};

registry.packageNameFormats = ['*'];

registry.configure = function(config, ui) {
return ui.input('Enter the registry repo path', config.repo || defaultRepo)
.then(function(repo) {
if (repo.substr(0, 2) == '~/')
repo = path.resolve(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH, repo.substr(2));
else if (repo.substr(0, 1) == '.')
repo = path.resolve(repo);

config.repo = repo;
return config;
});
}
.then(function(repo) {
if (repo.substr(0, 2) == '~/')
repo = path.resolve(process.env.HOME || process.env.USERPROFILE || process.env.HOMEPATH, repo.substr(2));
else if (repo.substr(0, 1) == '.')
repo = path.resolve(repo);

config.repo = repo;
return config;
});
};

registry.prototype.parse = function(name) {
var parts = name.split('/');
return {
package: parts[0],
path: parts.splice(1).join('/')
};
}
};

registry.prototype.locate = function(repo) {
return this.updateRegistry()
.then(function(registry) {
.then(function(registry) {

// NB support versioned redirects
var registryEntry = registry[repo];
// NB support versioned redirects
var registryEntry = registry[repo];

if (!registryEntry)
return { notfound: true };
if (!registryEntry)
return { notfound: true };

return { redirect: registryEntry };
});
}
return { redirect: registryEntry };
});
};


// registry endpoint is in charge of overrides, special hook
Expand All @@ -78,68 +80,68 @@ registry.prototype.getOverride = function(endpoint, repo, version, givenOverride
var ui = this.ui;

return this.updateRegistry()
.then(function() {
return asp(fs.readdir)(overrideDir);
})
.then(function(files) {
var overrideFile = files
// find the files for this override name
.filter(function(file) {
return file.substr(0, overrideName.length) == overrideName && file.substr(overrideName.length, 1) == '@';
})
// derive versions
.map(function(file) {
return {
version: file.substr(overrideName.length + 1, file.length - overrideName.length - 6),
file: file
};
})
// filter to only semver compatible overrides
.filter(function(item) {
if (semver.valid(version))
return semver.satisfies(version, '^' + item.version);
else
return version == item.version;
.then(function() {
return asp(fs.readdir)(overrideDir);
})
// sort to find latest
.sort(function(a, b) {
return semver.compare(a.version, b.version);
})
.map(function(item) {
return item.file;
})
.pop();

// return that loaded override
if (!overrideFile)
return;

return asp(fs.readFile)(path.resolve(overrideDir, overrideFile))
.then(function(pjson) {
try {
return JSON.parse(pjson);
}
catch(e) {
.then(function(files) {
var overrideFile = files
// find the files for this override name
.filter(function(file) {
return file.substr(0, overrideName.length) == overrideName && file.substr(overrideName.length, 1) == '@';
})
// derive versions
.map(function(file) {
return {
version: file.substr(overrideName.length + 1, file.length - overrideName.length - 6),
file: file
};
})
// filter to only semver compatible overrides
.filter(function(item) {
if (semver.valid(version))
return semver.satisfies(version, '^' + item.version);
else
return version == item.version;
})
// sort to find latest
.sort(function(a, b) {
return semver.compare(a.version, b.version);
})
.map(function(item) {
return item.file;
})
.pop();

// return that loaded override
if (!overrideFile)
return;
}

return asp(fs.readFile)(path.resolve(overrideDir, overrideFile))
.then(function(pjson) {
try {
return JSON.parse(pjson);
}
catch(e) {
return;
}
}, function(err) {
if (err.code === 'ENOENT')
return;
ui.log('warn', 'Override file `' + overrideFile + '` found, but JSON is invalid');
});
}, function(err) {
if (err.code === 'ENOENT')
return;
ui.log('warn', 'Override file `' + overrideFile + '` found, but JSON is invalid');
throw err;
})
.then(function(override) {
// if an existing override, let it extend this override
if (givenOverride)
extend(override = (override || {}), givenOverride);

return override;
});
}, function(err) {
if (err.code === 'ENOENT')
return;
throw err;
})
.then(function(override) {
// if an existing override, let it extend this override
if (givenOverride)
extend(override = (override || {}), givenOverride);

return override;
});
}
};

registry.prototype.createRegistry = function() {
var ui = this.ui;
Expand All @@ -148,16 +150,16 @@ registry.prototype.createRegistry = function() {
var self = this;

return asp(rimraf)(path.resolve(this.registryPath))
.then(function() {
return asp(fs.mkdir)(path.resolve(self.registryPath));
})
.then(function() {
return asp(exec)('git clone --depth=1 ' + self.repo + ' .', self.execOptions);
})
.catch(function(err) {
ui.log('err', 'Error creating registry file\n' + (err.stack || err));
});
}
.then(function() {
return asp(fs.mkdir)(path.resolve(self.registryPath));
})
.then(function() {
return asp(exec)('git clone --depth=1 ' + self.repo + ' .', self.execOptions);
})
.catch(function(err) {
ui.log('err', 'Error creating registry file\n' + (err.stack || err));
});
};

registry.prototype.updateRegistry = function() {
if (this.updatePromise_)
Expand All @@ -170,55 +172,52 @@ registry.prototype.updateRegistry = function() {
var self = this;

return this.updatePromise_ = asp(exec)('git remote show origin -n', execOptions)
.then(function(output) {
output = output.toString();

var curRepoMatch = output.match(/Fetch URL: ([^\n]+)/m);

if (!curRepoMatch || curRepoMatch[1] != self.repo)
return self.createRegistry();
.then(function(output) {
output = output.toString();

var curRepoMatch = output.match(/Fetch URL: ([^\n]+)/m);

if (!curRepoMatch || curRepoMatch[1] != self.repo)
return self.createRegistry();

// if the registry does exist, update it
ui.log('info', 'Updating registry cache...');
return asp(exec)('git fetch --all && git reset --hard origin/master', execOptions)
.then(function(stdout, stderr) {
if (stderr)
throw stderr;
})
.catch(function(err) {
if (typeof err == 'string') {
err = new Error(err);
err.hideStack = true;
}
err.retriable = true;
throw err;
});
}, function(err) {
err = err.toString();

// if the registry does exist, update it
ui.log('info', 'Updating registry cache...');
return asp(exec)('git fetch --all && git reset --hard origin/master', execOptions)
.then(function(stdout, stderr) {
if (stderr)
throw stderr;
// if the registry does not exist, do a git clone
if (err.indexOf('Not a git repo') != -1)
return self.createRegistry();
})
.catch(function(err) {
if (typeof err == 'string') {
err = new Error(err);
err.hideStack = true;
}
err.retriable = true;
throw err;
});
}, function(err) {
err = err.toString();

// if the registry does not exist, do a git clone
if (err.indexOf('Not a git repo') != -1)
return self.createRegistry();
})
.then(function() {
return asp(fs.readFile)(path.resolve(registryPath, 'registry.json'))
.then(function(pjson) {
try {
return JSON.parse(pjson);
}
catch(e) {
return {};
}
}, function(err) {
if (err.code === 'ENOENT')
return {};
ui.log('warn', 'Registry file is invalid.');
.then(function() {
return asp(fs.readFile)(path.resolve(registryPath, 'registry.json'))
.then(function(pjson) {
try {
return JSON.parse(pjson);
}
catch(e) {
return {};
}
}, function(err) {
if (err.code === 'ENOENT')
return {};
ui.log('warn', 'Registry file is invalid.');
});
})
.then(function(json) {
return json;
});
})
.then(function(json) {
return json;
});
}



};