Skip to content

Instantly share code, notes, and snippets.

@DenisGorbachev
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DenisGorbachev/3affd1b90bd18e695184 to your computer and use it in GitHub Desktop.
Save DenisGorbachev/3affd1b90bd18e695184 to your computer and use it in GitHub Desktop.
autopackage.js
var fs = Npm.require("fs");
var path = Npm.require("path");
getDefaultProfiles = function() {
return [
{
path: "lib/compatibility",
architecture: ["client", "server"]
},{
path: "client/compatibility",
architecture: ["client"],
options: {bare: true}
},{
path: "server/compatibility",
architecture: ["server"]
},{
path: "common/compatibility",
architecture: ["client", "server"]
},{
path: "lib",
architecture: ["client", "server"]
},{
path: "mixin",
architecture: ["server"]
},{
path: "model/Job",
architecture: ["server"],
strategy: "clstree"
},{
path: "model",
architecture: ["client", "server"],
strategy: "clstree"
},{
path: "i18n",
architecture: ["client", "server"]
},{
path: "client",
architecture: ["client"]
},{
path: "server",
architecture: ["server"]
},{
path: "common",
architecture: ["client", "server"]
},{
path: "*", // Must be the last profile
architecture: ["client", "server"]
}
];
};
addFiles = function(api, packageName, profiles) {
var buckets = {};
fill(buckets, packageName, profiles);
//console.log(buckets);
for (var j = 0; j < profiles.length; j++) {
api.addFiles(buckets[profiles[j].path], profiles[j].architecture, profiles[j].options)
}
};
fill = function(buckets, packageName, profiles) {
var root = path.join(getMeteorRootDir(), "packages", packageName);
var i, j, profile;
for (j = 0; j < profiles.length; j++) {
buckets[profiles[j].path] = [];
}
var files = getFiles(root, root), file;
for (i = 0; i < files.length; i++) {
file = files[i];
for (j = 0; j < profiles.length; j++) {
profile = profiles[j];
if (file.indexOf(profile.path) === 0) {
break;
}
}
buckets[profile.path].push(file);
}
for (j = 0; j < profiles.length; j++) {
profile = profiles[j];
if (profile.strategy === "clstree") {
buckets[profile.path].reverse();
}
}
};
var codeExtensions = [".js", ".coffee"];
getFiles = function(directory, root) {
var entries = fs.readdirSync(directory),
files = [],
ownFiles = [];
for (var i = 0; i < entries.length; i++) {
var entryName = entries[i],
entryPath = path.join(directory, entryName);
if (entryName.indexOf(".") === 0 || entryName === "package.js") {
continue;
}
if (fs.lstatSync(entryPath).isFile()) {
var entryRelativePath = entryPath.replace(root + path.sep, "");
if (codeExtensions.indexOf(path.extname(entryName)) !== -1) {
ownFiles.push(entryRelativePath);
} else {
ownFiles.unshift(entryRelativePath);
}
} else {
files = files.concat(getFiles(entryPath, root))
}
}
files = files.concat(ownFiles);
return files;
}
isAppDir = function(filepath) {
try {
return fs.statSync(path.join(filepath, ".meteor", "packages")).isFile();
} catch (e) {
return false;
}
}
getMeteorRootDir = function() {
var currentDir = process.cwd();
while (currentDir) {
var newDir = path.dirname(currentDir);
if (isAppDir(currentDir)) {
break;
} else if (newDir === currentDir) {
return null;
} else {
currentDir = newDir;
}
}
return currentDir;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment