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

Added fuzzy matching to ngx <script> command #346

Merged
merged 1 commit into from
Jul 30, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ l, list
-n, --npm Show installable add-ons on NPM

<script>
Runs specified script from your package.json.
Works just like npm run <script>
Runs specified script from your package.json
Works like npm run <script> with fuzzy matching
```

## Generating and serving a project via a development server
Expand Down Expand Up @@ -111,7 +111,7 @@ See [ngx-rocket/core](https://github.com/ngx-rocket/core) for the complete docum

In a generated project folder, you can use the command `ngx <script>` to run any `package.json` script.
This is only a convenience shortcut, it works exactly like `npm run <script>`, except that you do need to add `--` to
pass arguments to the underlying command.
pass arguments to the underlying command, and script name is fuzzy matched.

For example in a ngX-Rocket project you can use these commands:
```sh
Expand All @@ -130,6 +130,11 @@ ngx b --build-optimizer
If there is more than one script matching, the first one will be used.
You can then use any number of additional letters to discriminate the script you want to run.

For example you can use first letters of sub-commands:
```sh
ngx cr ios // start cordova:run script
```

# License

[MIT](../LICENSE)
47 changes: 7 additions & 40 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const get = require('lodash.get');
const minimist = require('minimist');
const updateNotifier = require('update-notifier');
const asciiLogo = require('@ngx-rocket/ascii-logo');
const fuzzyRun = require('fuzz-run');
const pkg = require('../package.json');

const isWin = /^win/.test(process.platform);
const addonKey = 'ngx-rocket-addon';
const disabledAddons = 'disabledAddons';
const blacklistedNpmAddons = [
Expand Down Expand Up @@ -45,7 +45,7 @@ ${chalk.blue('l, list')}

${chalk.blue('<script>')}
Runs specified script from your ${chalk.bold(`package.json`)}.
Works just like ${chalk.bold(`npm run <script>`)}
Works like ${chalk.bold(`npm run <script>`)} with fuzzy matching
`;

class NgxCli {
Expand Down Expand Up @@ -92,34 +92,11 @@ class NgxCli {
}

runScript(args) {
const name = args[0];
const packageFile = this._findPackageJson(process.cwd());
const packageManager = this._packageManager();
const projectPackage = packageFile ? require(packageFile) : null;
if (!projectPackage) {
this._help();
}

let scriptName = name;
if (!projectPackage.scripts[name]) {
const matches = this._findMatches(name, Object.keys(projectPackage.scripts));
if (!matches) {
this._help();
}
scriptName = matches[0];
if (matches.length > 1) {
console.warn(chalk.yellow(`Warning, multiple matching scripts. Try to be more specific.`));
}
if (!args[0]) {
return this._help();
}

child.spawnSync(
packageManager,
['run', scriptName].concat(packageManager === 'npm' ? ['--'] : [], args.splice(1)),
{
stdio: 'inherit',
shell: isWin
}
);
const packageManager = this._packageManager();
fuzzyRun(args, packageManager);
}

generate(update, args, addon) {
Expand Down Expand Up @@ -225,7 +202,7 @@ class NgxCli {
if (!components.length) {
return null;
}
const dir = path.join.apply(path, components);
const dir = path.join(...components);
const packageFile = path.join(dir, 'package.json');
return fs.existsSync(packageFile) ? packageFile : find(components.slice(0, -1));
};
Expand Down Expand Up @@ -257,16 +234,6 @@ class NgxCli {
this._exit(help + (details ? detailedHelp : `Use ${chalk.white(`--help`)} for more info.\n`));
}

_findMatches(search, strings) {
const matches = [];
for (const s of strings) {
if (s.startsWith(search)) {
matches.push(s);
}
}
return matches.length > 0 ? matches : null;
}

_exit(error, code = 1) {
console.error(error);
process.exit(code);
Expand Down
Loading