-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): use local version when available
Fixes #75
- Loading branch information
1 parent
9c0efea
commit 8b8dcea
Showing
2 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters