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

Fix durable option resolution #762

Merged
merged 7 commits into from
Apr 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 8 additions & 15 deletions bin/lerna.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#!/usr/bin/env node
"use strict";

const globalOptions = require("../lib/Command").builder;
const logger = require("../lib/logger");
const yargs = require("yargs");
const path = require("path");
const dedent = require("dedent");
const globalOptions = require("../lib/Command").builder;

// the options grouped under "Global Options:" header
const globalKeys = Object.keys(globalOptions).concat([
"loglevel",
"help",
"version",
]);
Expand All @@ -19,21 +17,16 @@ function terminalWidth() {
return typeof process.stdout.columns !== "undefined" ? process.stdout.columns : null;
}

logger.setLogLevel(yargs.argv.loglevel);

yargs
.epilogue("For more information, find our manual at https://github.com/lerna/lerna")
.epilogue(dedent`
When a command fails, all logs are written to lerna-debug.log in the current working directory.

For more information, find our manual at https://github.com/lerna/lerna
`)
.usage("Usage: $0 <command> [options]")
.wrap(terminalWidth())
.option("loglevel", {
default: "info",
describe: "What level of logs to report. On failure, all logs are written to lerna-debug.log in the"
+ "current working directory.",
type: "string",
global: true
})
.options(globalOptions).group(globalKeys, "Global Options:")
.commandDir(path.join(__dirname, "..", "lib", "commands"))
.commandDir("../lib/commands")
.demandCommand()
.help("h").alias("h", "help")
.version().alias("v", "version")
Expand Down
115 changes: 64 additions & 51 deletions src/Command.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _ from "lodash";
import dedent from "dedent";
import ChildProcessUtilities from "./ChildProcessUtilities";
import FileSystemUtilities from "./FileSystemUtilities";
import GitUtilities from "./GitUtilities";
Expand All @@ -10,39 +12,48 @@ import logger from "./logger";
const DEFAULT_CONCURRENCY = 4;

export const builder = {
"loglevel": {
default: "info",
describe: "What level of logs to report.",
type: "string",
},
"concurrency": {
describe: "How many threads to use if lerna parallelises the tasks.",
type: "number",
requiresArg: true,
default: DEFAULT_CONCURRENCY,
},
"scope": {
describe: dedent`
Restricts the scope to package names matching the given glob.
(Only for 'run', 'exec', 'clean', 'ls', and 'bootstrap' commands)
`,
type: "string",
requiresArg: true,
},
"ignore": {
describe: "Ignores packages with names matching the given glob (Works only in combination with the "
+ "'run', 'exec', 'clean', 'ls' and 'bootstrap' commands).",
describe: dedent`
Ignore packages with names matching the given glob.
(Only for 'run', 'exec', 'clean', 'ls', and 'bootstrap' commands)
`,
type: "string",
requiresArg: true
requiresArg: true,
},
"include-filtered-dependencies": {
describe: "Flag to force lerna to include all dependencies and transitive dependencies when running "
+ "'bootstrap', even if they should not be included by the scope or ignore flags."
describe: dedent`
Include all transitive dependencies when running a command, regardless of --scope or --ignore.
`,
},
"registry": {
describe: "When run with this flag, forwarded npm commands will use the specified registry for your "
+ "package(s).",
describe: "Use the specified registry for all npm client operations.",
type: "string",
requiresArg: true
},
"scope": {
describe: "Restricts the scope to package names matching the given glob (Works only in combination "
+ "with the 'run', 'exec', 'clean', 'ls' and 'bootstrap' commands).",
type: "string",
requiresArg: true
requiresArg: true,
},
"sort": {
describe: "Sort packages topologically",
describe: "Sort packages topologically (all dependencies before dependents)",
type: "boolean",
default: true
}
default: true,
},
};

export default class Command {
Expand All @@ -52,13 +63,14 @@ export default class Command {

this.lernaVersion = require("../package.json").version;
this.logger = logger;
this.repository = new Repository(cwd);
this.logger.setLogLevel(flags.loglevel);
this.progressBar = progressBar;
this.repository = new Repository(cwd);
}

get concurrency() {
if (!this._concurrency) {
const { concurrency } = this.getOptions();
const { concurrency } = this.options;
this._concurrency = Math.max(1, +concurrency || DEFAULT_CONCURRENCY);
}

Expand All @@ -67,7 +79,7 @@ export default class Command {

get toposort() {
if (!this._toposort) {
const { sort } = this.getOptions();
const { sort } = this.options;
// If the option isn't present then the default is to sort.
this._toposort = sort == null || sort;
}
Expand Down Expand Up @@ -100,33 +112,31 @@ export default class Command {
return [];
}

getOptions(...objects) {

// Command config object is either "commands" or "command".
const { commands, command } = this.repository.lernaJson;

// Items lower down override items higher up.
return Object.assign(
{},

// Deprecated legacy options in `lerna.json`.
this._legacyOptions(),

// Global options from `lerna.json`.
this.repository.lernaJson,

// Option overrides for commands.
// Inherited options from `otherCommandConfigs` come before the current
// command's configuration.
...[...this.otherCommandConfigs, this.name]
.map((name) => (commands || command || {})[name]),

// For example, the item from the `packages` array in config.
...objects,
get options() {
if (!this._options) {
// Command config object is either "commands" or "command".
const { commands, command } = this.repository.lernaJson;

// The current command always overrides otherCommandConfigs
const lernaCommandOverrides = [
this.name,
...this.otherCommandConfigs,
].map((name) => (commands || command || {})[name]);

this._options = _.defaults(
{},
// CLI flags, which if defined overrule subsequent values
this.flags,
// Namespaced command options from `lerna.json`
...lernaCommandOverrides,
// Global options from `lerna.json`
this.repository.lernaJson,
// Deprecated legacy options in `lerna.json`
this._legacyOptions()
);
}

// CLI flags always override everything.
this.flags
);
return this._options;
}

run() {
Expand All @@ -142,7 +152,6 @@ export default class Command {
}

runValidations() {
const { independent, onlyExplicitUpdates } = this.getOptions();
if (!GitUtilities.isInitialized(this.repository.rootPath)) {
this.logger.warn("This is not a git repository, did you already run `git init` or `lerna init`?");
this._complete(null, 1);
Expand All @@ -161,7 +170,7 @@ export default class Command {
return;
}

if (independent && !this.repository.isIndependent()) {
if (this.options.independent && !this.repository.isIndependent()) {
this.logger.warn(
"You ran lerna with `--independent` or `-i`, but the repository is not set to independent mode. " +
"To use independent mode you need to set your `lerna.json` \"version\" to \"independent\". " +
Expand Down Expand Up @@ -204,7 +213,7 @@ export default class Command {
return;
}

if (onlyExplicitUpdates) {
if (this.options.onlyExplicitUpdates) {
this.logger.warn("`--only-explicit-updates` has been removed. This flag was only ever added for Babel and we never should have exposed it to everyone.");
this._complete(null, 1);
return;
Expand All @@ -213,23 +222,27 @@ export default class Command {
}

runPreparations() {
const { scope, ignore, registry } = this.getOptions();
const { scope, ignore, registry } = this.options;

if (scope) {
this.logger.info(`Scoping to packages that match '${scope}'`);
}

if (ignore) {
this.logger.info(`Ignoring packages that match '${ignore}'`);
}

if (registry) {
this.npmRegistry = registry;
}

try {
this.repository.buildPackageGraph();
this.packages = this.repository.packages;
this.packageGraph = this.repository.packageGraph;
this.filteredPackages = PackageUtilities.filterPackages(this.packages, { scope, ignore });
if (this.getOptions().includeFilteredDependencies) {

if (this.options.includeFilteredDependencies) {
this.filteredPackages = PackageUtilities.addDependencies(this.filteredPackages, this.packageGraph);
}
} catch (err) {
Expand Down
18 changes: 9 additions & 9 deletions src/UpdatedPackagesCollector.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class UpdatedPackagesCollector {
this.packages = command.filteredPackages;
this.packageGraph = command.repository.packageGraph;
this.progressBar = command.progressBar;
this.flags = command.getOptions();
this.options = command.options;
}

getUpdates() {
Expand All @@ -42,11 +42,11 @@ export default class UpdatedPackagesCollector {

let commits;

if (this.flags.canary) {
if (this.options.canary) {
let currentSHA;

if (this.flags.canary !== true) {
currentSHA = this.flags.canary;
if (this.options.canary !== true) {
currentSHA = this.options.canary;
} else {
currentSHA = GitUtilities.getCurrentSHA(this.execOpts);
}
Expand All @@ -68,7 +68,7 @@ export default class UpdatedPackagesCollector {
return true;
}

const forcePublish = (this.flags.forcePublish || "").split(",");
const forcePublish = (this.options.forcePublish || "").split(",");

if (forcePublish.indexOf("*") > -1) {
return true;
Expand Down Expand Up @@ -137,8 +137,8 @@ export default class UpdatedPackagesCollector {
return this.packages.filter((pkg) => {
return (
this.updatedPackages[pkg.name] ||
(this.flags[SECRET_FLAG] ? false : this.dependents[pkg.name]) ||
this.flags.canary
(this.options[SECRET_FLAG] ? false : this.dependents[pkg.name]) ||
this.options.canary
);
}).map((pkg) => {
return new Update(pkg);
Expand All @@ -163,9 +163,9 @@ export default class UpdatedPackagesCollector {
return file.replace(folder + path.sep, "");
});

if (this.flags.ignore) {
if (this.options.ignore) {
changedFiles = changedFiles.filter((file) => {
return !find(this.flags.ignore, (pattern) => {
return !find(this.options.ignore, (pattern) => {
return minimatch(file, pattern, { matchBase: true });
});
});
Expand Down