Skip to content

Commit

Permalink
Add support for configurable package locations
Browse files Browse the repository at this point in the history
  • Loading branch information
gigabo authored and jamiebuilds committed Oct 7, 2016
1 parent 494de9d commit 24b35ca
Show file tree
Hide file tree
Showing 29 changed files with 335 additions and 114 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"cmd-shim": "^2.0.2",
"command-join": "^1.1.1",
"cross-spawn": "^4.0.0",
"glob": "^7.0.6",
"inquirer": "^0.12.0",
"lodash.find": "^4.3.0",
"lodash.unionwith": "^4.2.0",
Expand Down
12 changes: 3 additions & 9 deletions src/Command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ChildProcessUtilities from "./ChildProcessUtilities";
import FileSystemUtilities from "./FileSystemUtilities";
import PackageUtilities from "./PackageUtilities";
import ExitHandler from "./ExitHandler";
import progressBar from "./progressBar";
import Repository from "./Repository";
Expand Down Expand Up @@ -39,12 +38,6 @@ export default class Command {
return;
}

if (!FileSystemUtilities.existsSync(this.repository.packagesLocation)) {
this.logger.warning("`packages/` directory does not exist, have you run `lerna init`?");
this._complete(null, 1);
return;
}

if (!FileSystemUtilities.existsSync(this.repository.packageJsonLocation)) {
this.logger.warning("`package.json` does not exist, have you run `lerna init`?");
this._complete(null, 1);
Expand Down Expand Up @@ -101,8 +94,9 @@ export default class Command {

runPreparations() {
try {
this.packages = PackageUtilities.getPackages(this.repository.packagesLocation);
this.packageGraph = PackageUtilities.getPackageGraph(this.packages);
this.repository.buildPackageGraph();
this.packages = this.repository.packages;
this.packageGraph = this.repository.packageGraph;
} catch (err) {
this.logger.error("Errored while collecting packages and package graph", err);
this._complete(null, 1);
Expand Down
27 changes: 14 additions & 13 deletions src/PackageUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import FileSystemUtilities from "./FileSystemUtilities";
import PackageGraph from "./PackageGraph";
import Package from "./Package";
import path from "path";
import {sync as globSync} from "glob";
import minimatch from "minimatch";

export default class PackageUtilities {
Expand All @@ -27,25 +28,25 @@ export default class PackageUtilities {
return require(PackageUtilities.getPackageConfigPath(packagesPath, name));
}

static getPackages(packagesPath) {
static getPackages(repository) {
const packages = [];

FileSystemUtilities.readdirSync(packagesPath).forEach((packageDirectory) => {
if (packageDirectory[0] === ".") {
return;
}
repository.packageConfigs.forEach(({glob}) => {

const packagePath = PackageUtilities.getPackagePath(packagesPath, packageDirectory);
const packageConfigPath = PackageUtilities.getPackageConfigPath(packagesPath, packageDirectory);
globSync(glob)
.map((fn) => path.resolve(fn))
.forEach((packageConfigPath) => {
const packagePath = path.dirname(packageConfigPath);

if (!FileSystemUtilities.existsSync(packageConfigPath)) {
return;
}
if (!FileSystemUtilities.existsSync(packageConfigPath)) {
return;
}

const packageJson = require(packageConfigPath);
const pkg = new Package(packageJson, packagePath);
const packageJson = require(packageConfigPath);
const pkg = new Package(packageJson, packagePath);

packages.push(pkg);
packages.push(pkg);
});
});

return packages;
Expand Down
29 changes: 28 additions & 1 deletion src/Repository.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import GitUtilities from "./GitUtilities";
import FileSystemUtilities from "./FileSystemUtilities";
import PackageUtilities from "./PackageUtilities";
import path from "path";
import logger from "./logger";

const DEFAULT_PACKAGE_GLOB = "packages/*/package.json";

export default class Repository {
constructor() {
if (!GitUtilities.isInitialized()) {
Expand All @@ -13,7 +16,6 @@ export default class Repository {
this.rootPath = path.resolve(GitUtilities.getTopLevelDirectory());
this.lernaJsonLocation = path.join(this.rootPath, "lerna.json");
this.packageJsonLocation = path.join(this.rootPath, "package.json");
this.packagesLocation = path.join(this.rootPath, "packages");

// Legacy
this.versionLocation = path.join(this.rootPath, "VERSION");
Expand Down Expand Up @@ -43,7 +45,32 @@ export default class Repository {
return this.lernaJson && this.lernaJson.bootstrapConfig || {};
}

get packageConfigs() {
return (this.lernaJson || {}).packages || [{
glob: DEFAULT_PACKAGE_GLOB,
}];
}

get packages() {
if (!this._packages) {
this.buildPackageGraph();
}
return this._packages;
}

get packageGraph() {
if (!this._packageGraph) {
this.buildPackageGraph();
}
return this._packageGraph;
}

isIndependent() {
return this.version === "independent";
}

buildPackageGraph() {
this._packages = PackageUtilities.getPackages(this);
this._packageGraph = PackageUtilities.getPackageGraph(this.packages);
}
}
10 changes: 5 additions & 5 deletions src/UpdatedPackagesCollector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import PackageUtilities from "./PackageUtilities";
import GitUtilities from "./GitUtilities";
import progressBar from "./progressBar";
import minimatch from "minimatch";
Expand All @@ -13,9 +12,10 @@ class Update {
}

export default class UpdatedPackagesCollector {
constructor(packages, packageGraph, flags, publishConfig) {
this.packages = packages;
this.packageGraph = packageGraph;
constructor(repository, flags, publishConfig) {
this.repository = repository;
this.packages = repository.packages;
this.packageGraph = repository.packageGraph;
this.flags = flags;
this.publishConfig = publishConfig;
}
Expand Down Expand Up @@ -140,7 +140,7 @@ export default class UpdatedPackagesCollector {
}

hasDiffSinceThatIsntIgnored(pkg, commits) {
const folder = PackageUtilities.getPackagePath(PackageUtilities.getPackagesPath(""), pkg.name);
const folder = path.relative(this.repository.rootPath, pkg.location);
const diff = GitUtilities.diffSinceIn(commits, pkg.location);

if (diff === "") {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/DiffCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class DiffCommand extends Command {

this.filePath = this.package
? this.package.location
: this.repository.packagesLocation;
: this.repository.rootPath;

this.lastCommit = GitUtilities.hasTags()
? GitUtilities.getLastTaggedCommit()
Expand Down
17 changes: 7 additions & 10 deletions src/commands/InitCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,13 @@ export default class InitCommand extends Command {
}

execute(callback) {
this.ensurePackagesDirectory();
this.ensurePackageJSON();
this.ensureLernaJson();
this.ensureNoVersionFile();
this.logger.success("Successfully created Lerna files");
callback(null, true);
}

ensurePackagesDirectory() {
const packagesLocation = this.repository.packagesLocation;
if (!FileSystemUtilities.existsSync(packagesLocation)) {
this.logger.info("Creating packages folder.");
FileSystemUtilities.mkdirSync(packagesLocation);
}
}

ensurePackageJSON() {
let {packageJsonLocation, packageJson} = this.repository;

Expand All @@ -51,7 +42,12 @@ export default class InitCommand extends Command {
}

ensureLernaJson() {
let {versionLocation, lernaJsonLocation, lernaJson} = this.repository;
let {
versionLocation,
lernaJsonLocation,
lernaJson,
packageConfigs,
} = this.repository;

let version;

Expand All @@ -74,6 +70,7 @@ export default class InitCommand extends Command {

objectAssign(lernaJson, {
lerna: this.lernaVersion,
packages: packageConfigs,
version: version
});

Expand Down
3 changes: 1 addition & 2 deletions src/commands/PublishCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export default class PublishCommand extends Command {
}

const updatedPackagesCollector = new UpdatedPackagesCollector(
this.packages,
this.packageGraph,
this.repository,
this.flags,
this.repository.publishConfig
);
Expand Down
3 changes: 1 addition & 2 deletions src/commands/UpdatedCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import Command from "../Command";
export default class UpdatedCommand extends Command {
initialize(callback) {
const updatedPackagesCollector = new UpdatedPackagesCollector(
this.packages,
this.packageGraph,
this.repository,
this.flags,
this.repository.publishConfig
);
Expand Down
Loading

0 comments on commit 24b35ca

Please sign in to comment.