forked from antfu-collective/ni
-
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.
- Loading branch information
Showing
4 changed files
with
55 additions
and
9 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
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { resolve } from 'path' | ||
import fs from 'fs' | ||
|
||
export function getPackageJSON(): any { | ||
const path = resolve(process.cwd(), 'package.json') | ||
|
||
if (fs.existsSync(path)) { | ||
try { | ||
const raw = fs.readFileSync(path, 'utf-8') | ||
const data = JSON.parse(raw) | ||
return data | ||
} | ||
catch (e) { | ||
console.warn('Failed to parse package.json') | ||
process.exit(0) | ||
} | ||
} | ||
} |
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,6 +1,33 @@ | ||
import inquirer from 'inquirer' | ||
import { parseNr } from './commands' | ||
import { getPackageJSON } from './fs' | ||
import { run } from './runner' | ||
|
||
run(async(agent, args) => { | ||
if (args.length === 0) { | ||
const scripts = getPackageJSON().scripts || {} | ||
|
||
const names = Object.keys(scripts) | ||
|
||
if (!names.length) | ||
return | ||
|
||
try { | ||
const { fn } = await inquirer.prompt({ | ||
name: 'fn', | ||
message: 'script to run', | ||
pageSize: 20, | ||
type: 'list', | ||
choices: names, | ||
}) | ||
if (!fn) | ||
return | ||
args.push(fn) | ||
} | ||
catch (e) { | ||
console.error(e) | ||
process.exit(1) | ||
} | ||
} | ||
return parseNr(agent, args) | ||
}) |
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