Skip to content

Commit

Permalink
perf: lazily require modules in commands
Browse files Browse the repository at this point in the history
refs #428
- defer requiring modules in commands to as late as possible
  • Loading branch information
acburdine committed Aug 4, 2017
1 parent 4013f2a commit 3f6d8cc
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 68 deletions.
3 changes: 2 additions & 1 deletion lib/commands/buster.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';
const Command = require('../command');
const yarn = require('../utils/yarn');

class BusterCommand extends Command {
run() {
const yarn = require('../utils/yarn');

return this.ui.run(yarn(['cache', 'clean']), 'Clearing yarn cache');
}
}
Expand Down
9 changes: 5 additions & 4 deletions lib/commands/config/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
'use strict';
const url = require('url');
const validator = require('validator');
const isFunction = require('lodash/isFunction');
const Promise = require('bluebird');

const Command = require('../../command');
const advancedOptions = require('./advanced');
const errors = require('../../errors');

class ConfigCommand extends Command {
constructor(ui, system) {
Expand All @@ -16,6 +12,10 @@ class ConfigCommand extends Command {
}

handleAdvancedOptions(argv) {
const errors = require('../../errors');
const Promise = require('bluebird');
const isFunction = require('lodash/isFunction');

return Promise.each(Object.keys(advancedOptions), (key) => {
let option = advancedOptions[key];
let value = argv[key];
Expand Down Expand Up @@ -70,6 +70,7 @@ class ConfigCommand extends Command {
}

getConfigPrompts(argv) {
const validator = require('validator');
let prompts = [];

if (!argv.url) {
Expand Down
21 changes: 9 additions & 12 deletions lib/commands/install.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
'use strict';
const fs = require('fs-extra');
const path = require('path');
const every = require('lodash/every');
const Promise = require('bluebird');
const Command = require('../command');
const symlinkSync = require('symlink-or-copy').sync;

// Utils
const resolveVersion = require('../utils/resolve-version');
const errors = require('../errors');

// Tasks/Commands
const installChecks = require('./doctor/checks/install');
const ensureStructure = require('../tasks/ensure-structure');
const yarnInstall = require('../tasks/yarn-install');
const SetupCommand = require('./setup');

class InstallCommand extends Command {
Expand All @@ -23,6 +11,13 @@ class InstallCommand extends Command {
}

run(argv) {
const fs = require('fs-extra');
const every = require('lodash/every');
const errors = require('../errors');
const yarnInstall = require('../tasks/yarn-install');
const installChecks = require('./doctor/checks/install');
const ensureStructure = require('../tasks/ensure-structure');

// Dir was specified, so we make sure it exists and chdir into it.
if (argv.dir) {
let dir = path.resolve(argv.dir);
Expand Down Expand Up @@ -91,6 +86,8 @@ class InstallCommand extends Command {
}

version(ctx) {
const resolveVersion = require('../utils/resolve-version');

return resolveVersion(ctx.version).then((version) => {
ctx.version = version;
ctx.installPath = path.join(process.cwd(), 'versions', version);
Expand Down
19 changes: 10 additions & 9 deletions lib/commands/log.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
'use strict';
const fs = require('fs-extra');
const path = require('path');
const includes = require('lodash/includes');
const lastLines = require('read-last-lines');
const Tail = require('tail').Tail;
const Promise = require('bluebird');
const PrettyStream = require('ghost-ignition/lib/logging/PrettyStream');

const errors = require('../errors');
const Command = require('../command');

class LogCommand extends Command {
run(argv) {
const fs = require('fs');
const path = require('path');
const includes = require('lodash/includes');
const lastLines = require('read-last-lines');
const PrettyStream = require('ghost-ignition/lib/logging/PrettyStream');

const errors = require('../errors');

if (!argv.name) {
Command.checkValidInstall('log');
}
Expand Down Expand Up @@ -64,6 +63,8 @@ class LogCommand extends Command {
lines.trim().split('\n').forEach(line => prettyStream.write(line));

if (argv.follow) {
const Tail = require('tail').Tail;

let tail = new Tail(logFileName);
tail.on('line', (line) => prettyStream.write(line, 'utf8'));
}
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/ls.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';
const chalk = require('chalk');
const Command = require('../command');

class LsCommand extends Command {
run() {
const chalk = require('chalk');

let instances = this.system.getAllInstances();
let rows = instances.map((instance) => {
let summary = instance.summary();
Expand Down
8 changes: 5 additions & 3 deletions lib/commands/run.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';
const path = require('path');
const spawn = require('child_process').spawn;
const errors = require('../errors');
const Command = require('../command');
const shouldUseGhostUser = require('../utils/use-ghost-user');

class RunCommand extends Command {
run() {
const path = require('path');
const shouldUseGhostUser = require('../utils/use-ghost-user');

// If the user is running this command directly, output a little note
// telling them they're likely looking for `ghost start`
if (process.stdin.isTTY) {
Expand All @@ -28,6 +28,8 @@ class RunCommand extends Command {
}

useGhostUser(instance) {
const errors = require('../errors');

this.ui.log('Running sudo command: node current/index.js', 'gray');

this.child = spawn('sudo', `-E -u ghost ${process.execPath} current/index.js`.split(' '), {
Expand Down
21 changes: 12 additions & 9 deletions lib/commands/setup.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
'use strict';
const os = require('os');
const url = require('url');
const path = require('path');
const get = require('lodash/get');
const omit = require('lodash/omit');
const includes = require('lodash/includes');

const Command = require('../command');
const migrate = require('../tasks/migrate');
const linux = require('../tasks/linux');
const StartCommand = require('./start');
const ConfigCommand = require('./config');

class SetupCommand extends Command {
static configureOptions(commandName, yargs, extensions, onlyOptions) {
const get = require('lodash/get');
const omit = require('lodash/omit');

extensions.forEach((extension) => {
let options = get(extension, 'config.options.setup', false);
if (!options) {
Expand Down Expand Up @@ -44,6 +38,15 @@ class SetupCommand extends Command {
}

run(argv) {
const os = require('os');
const url = require('url');
const path = require('path');
const includes = require('lodash/includes');

const linux = require('../tasks/linux');
const migrate = require('../tasks/migrate');


if (argv.local) {
argv.url = argv.url || 'http://localhost:2368/';
argv.pname = argv.pname || 'ghost-local';
Expand Down
11 changes: 6 additions & 5 deletions lib/commands/start.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict';
const get = require('lodash/get');
const omit = require('lodash/omit');

const Command = require('../command');
const startupChecks = require('./doctor/checks/startup');
const ProcessManager = require('../process-manager');

class StartCommand extends Command {
static configureOptions(commandName, yargs, extensions) {
const get = require('lodash/get');
const omit = require('lodash/omit');

extensions.forEach((extension) => {
let options = get(extension, 'config.options.start', false);
if (!options) {
Expand All @@ -21,6 +19,9 @@ class StartCommand extends Command {
}

run(argv) {
const ProcessManager = require('../process-manager');
const startupChecks = require('./doctor/checks/startup');

let instance = this.system.getInstance();
const runOptions = {quiet: argv.quiet};

Expand Down
14 changes: 8 additions & 6 deletions lib/commands/stop.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
'use strict';
const get = require('lodash/get');
const omit = require('lodash/omit');
const Promise = require('bluebird');

const errors = require('../errors');
const Command = require('../command');
const ProcessManager = require('../process-manager');

class StopCommand extends Command {
static configureOptions(commandName, yargs, extensions) {
const get = require('lodash/get');
const omit = require('lodash/omit');

extensions.forEach((extension) => {
let options = get(extension, 'config.options.start', false);
if (!options) {
Expand All @@ -22,6 +19,9 @@ class StopCommand extends Command {
}

run(argv) {
const errors = require('../errors');
const ProcessManager = require('../process-manager');

const runOptions = {quiet: argv.quiet};

if (argv.all) {
Expand Down Expand Up @@ -58,6 +58,8 @@ class StopCommand extends Command {
}

stopAll() {
const Promise = require('bluebird');

let instances = this.system.getAllInstances(true);
let cwd = process.cwd();

Expand Down
10 changes: 5 additions & 5 deletions lib/commands/uninstall.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';
const fs = require('fs-extra');
const os = require('os');
const path = require('path');

const StopCommand = require('./stop');
const Command = require('../command');

class UninstallCommand extends Command {
run(argv) {
const fs = require('fs-extra');
const os = require('os');
const path = require('path');
const StopCommand = require('./stop');

let prompt;

if (!argv.force && argv.prompt) {
Expand Down
23 changes: 12 additions & 11 deletions lib/commands/update.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
'use strict';
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const semver = require('semver');
const symlinkSync = require('symlink-or-copy').sync;

// Utils
const errors = require('../errors');
const Command = require('../command');
const resolveVersion = require('../utils/resolve-version');

// Tasks/Commands
// TODO: update checks
const migrate = require('../tasks/migrate');
const yarnInstall = require('../tasks/yarn-install');
const StopCommand = require('./stop');
const StartCommand = require('./start');

class UpdateCommand extends Command {
run(argv) {
const chalk = require('chalk');
const semver = require('semver');

const migrate = require('../tasks/migrate');
const yarnInstall = require('../tasks/yarn-install');
const StopCommand = require('./stop');
const StartCommand = require('./start');

let instance = this.system.getInstance();

// If installed with a version < 1.0
Expand Down Expand Up @@ -107,6 +104,8 @@ class UpdateCommand extends Command {
}

version(context) {
const resolveVersion = require('../utils/resolve-version');

if (context.rollback) {
return Promise.resolve(true);
}
Expand All @@ -125,6 +124,8 @@ class UpdateCommand extends Command {
}

link(context) {
const symlinkSync = require('symlink-or-copy').sync;

fs.removeSync(path.join(process.cwd(), 'current'));
symlinkSync(context.installPath, path.join(process.cwd(), 'current'));

Expand Down
5 changes: 3 additions & 2 deletions lib/commands/version.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';
const os = require('os');
const chalk = require('chalk');
const Command = require('../command');

class VersionCommand extends Command {
run() {
const os = require('os');
const chalk = require('chalk');

let cliVersion = this.system.cliVersion;
this.ui.log(`Ghost-CLI version: ${chalk.cyan(cliVersion)}`);

Expand Down

0 comments on commit 3f6d8cc

Please sign in to comment.