Skip to content

Commit

Permalink
feat(cli): use local version when available
Browse files Browse the repository at this point in the history
Fixes #75
  • Loading branch information
ianwremmel committed Apr 11, 2018
1 parent 9c0efea commit 8b8dcea
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
46 changes: 44 additions & 2 deletions bin/clark
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
#!/usr/bin/env node

'use strict';

// eslint-disable-next-line import/no-unassigned-import
require('../lib/cli');
const fs = require('fs');
const path = require('path');

const findRoot = require('find-root');

const {makeDebug} = require('../lib/lib/debug');

const debug = makeDebug(__filename);

try {
debug('looking for locally installed clark');
const root = findRoot(process.cwd(), dir => {
const clarkPath = path.resolve(dir, 'node_modules', '.bin', 'clark');
// eslint-disable-next-line no-sync
return fs.existsSync(clarkPath);
});

debug('found locally installed clark');
// This isn't perfect; it could break for certain layouts of node_modules, but
// I'm not sure those layouts can happen in a way that would also allow clark
// to work in a project.
const localPath = path.resolve(
root,
'node_modules',
'@ianwremmel',
'clark',
'lib',
'cli',
);
debug(`loading clark at ${localPath}`);
require(localPath);
debug('loaded locally installed clark');
} catch (err) {
if (err.message === 'package.json not found in path') {
debug('did not find locally installed clark');
debug('falling back to global clark');
// eslint-disable-next-line import/no-unassigned-import
require('../lib/cli');
debug('loaded globally installed clark');
} else {
throw err;
}
}
5 changes: 5 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {readFileSync} from 'fs';
import {sync as pkgUp} from 'pkg-up';
import y, {Argv} from 'yargs';

const pkg = JSON.parse(readFileSync(pkgUp(__dirname), 'utf-8'));

// so, yargs's d.ts is...weird. This is an unfortunate set of castings to
// convince typescript that all is well.
const yargs = (y as any) as Argv;
Expand All @@ -19,4 +23,5 @@ yargs
.help()
.recommendCommands()
.strict()
.version(pkg.version)
.parse();

0 comments on commit 8b8dcea

Please sign in to comment.