Skip to content

Commit ed50b9a

Browse files
committed
Add support for custom commands
1 parent e5cd652 commit ed50b9a

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

bin/util/_sample.js renamed to bin/_sample.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@
99
const fs = require('fs');
1010
const path = require('path');
1111
const chokidar = require('chokidar');
12-
const alerts = require(`${process.env.PWD}/config/alerts`);
13-
// Create a config file for this script
14-
// const config = require(`${process.env.PWD}/config/sample`);
12+
13+
const cnsl = require(`${process.env.PWD}/node_modules/nycopportunity/pttrn/bin/util/console`);
14+
const global = require(`${process.env.PWD}/node_modules/nycopportunity/pttrn/config/global`);
15+
const alerts = require(`${process.env.PWD}/node_modules/nycopportunity/pttrn/config/alerts`);
16+
const config = resolve(`${process.env.PWD}/config/sample`);
1517

1618
/**
1719
* Constants
1820
*/
1921

20-
const SOURCE = path.join(process.env.PWD, opts.src);
21-
const DIST = path.join(process.env.PWD, opts.dist);
22-
const BASE_PATH = `${SOURCE}/${opts.views}`;
22+
const SOURCE = path.join(global.base, global.src);
23+
const DIST = path.join(global.base, global.dist);
24+
const BASE_PATH = path.join(SOURCE, global.entry.views);
2325

2426
const EXT = '.ext';
2527
const GLOBS = [
@@ -48,6 +50,7 @@ const watcher = chokidar.watch(GLOBS, {
4850
*
4951
* @param {String} file The file source
5052
* @param {Object} data The data to pass to the file
53+
*
5154
* @return {undefined} The result of fs.writeFileSync()
5255
*/
5356
const write = async (file, data) => {

bin/cli.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
#!/usr/bin/env node
22

3+
/**
4+
* Dependencies
5+
*/
6+
7+
const resolve = require(`${__dirname}/util/resolve`);
8+
9+
/**
10+
* Args
11+
*/
12+
313
let script = process.argv[2];
414

15+
/**
16+
* Main
17+
*/
18+
519
if (undefined === script || script[0] === '-') {
620
process.argv.splice(2, 0, 'default');
721

8-
require(`${__dirname}/default`).run();
22+
resolve('bin/default').run();
923
} else {
1024
try {
11-
require(`${__dirname}/${script}`).run();
25+
resolve(`bin/${script}`).run();
1226
} catch (error) {
1327
console.log(`"${script}" is not a supported command.`);
1428
}

readme.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,11 @@ Each major feature uses a [configuration file](https://github.com/CityOfNewYork/
387387
* [Flags](#flags)
388388
* [Alerts](#alerts)
389389
* [ES Configuration](#es-configuration)
390+
* [Custom Commands](#custom-commands)
390391
* [NPM Scripts](#npm-scripts)
391392

392393
* [Guide: Creating a new `make` command template](#creating-a-new-make-command-template)
393394
* [Optional dependencies](#optional-dependencies)
394-
* [Documentation](#documentation)
395395

396396
## Installation
397397

@@ -954,6 +954,8 @@ Optional [flags](#flags) can be passed to the commands for signaling watching an
954954
- [`serve`](#serve)
955955
- [`publish`](#publish)
956956

957+
[Custom commands](#custom-commands) have been newly introduced and are described below.
958+
957959
---
958960

959961
#### Default
@@ -1217,13 +1219,13 @@ A custom `publish` configuration could be used to push to different remote GitHu
12171219

12181220
### Flags
12191221

1220-
Flag | Description
1221-
-----------------------------------------------------------------|-
1222-
<span style="white-space:nowrap">`-w` or `--watch`</span> | Use Chokidar to watch for changes on concerned source files and run their scripts when changes are detected.
1223-
<span style="white-space:nowrap">`-nd` or `--nondescript`</span> | Silence detailed logging (such as file writing writing) for commands. All other logs (such as script start and success) will display. **This can be used on all commands**.
1224-
<span style="white-space:nowrap">`-s` or `--silent`</span> | Disable all logging output. Note, some output will always log such as linting and errors. **This can be used on all commands**.
1225-
<span style="white-space:nowrap">`-nl` or `--no-lint`</span> | Disable ESLint and stylelint. This only works the `rollup` and `sass` command respectively. Running `npx pttrn lint -nl` will have no effect.
1226-
<span style="white-space:nowrap">`-np` or `--no-pa11y`</span> | Disable Pa11y linting. This only works for the `slm` command. Running `npx pttrn pa11y -np` command will have no effect.
1222+
Flag | Description
1223+
---------------------------------------------------------------|-
1224+
<div style="white-space:nowrap">`-w` or `--watch`</div> | Use Chokidar to watch for changes on concerned source files and run their scripts when changes are detected.
1225+
<div style="white-space:nowrap">`-nd` or `--nondescript`</div> | Silence detailed logging (such as file writing writing) for commands. All other logs (such as script start and success) will display. **This can be used on all commands**.
1226+
<div style="white-space:nowrap">`-s` or `--silent`</div> | Disable all logging output. Note, some output will always log such as linting and errors. **This can be used on all commands**.
1227+
<div style="white-space:nowrap">`-nl` or `--no-lint`</div> | Disable ESLint and stylelint. This only works the `rollup` and `sass` command respectively. Running `npx pttrn lint -nl` will have no effect.
1228+
<div style="white-space:nowrap">`-np` or `--no-pa11y`</div> | Disable Pa11y linting. This only works for the `slm` command. Running `npx pttrn pa11y -np` command will have no effect.
12271229

12281230
### Alerts
12291231

@@ -1233,6 +1235,18 @@ Flag | Description
12331235

12341236
You may have noticed the `scaffold` command will create a `.config/rollup.mjs` configuration file. [Node.js has stable support of the ECMAScript module spec](https://nodejs.org/api/esm.html) and they can be imported into CommonJS (Node modules). The configuration script will resolve ES modules with the **.mjs** extension over **.js** files, however, use of ES module configuration hasn't been fully tested with all of the commands.
12351237

1238+
### Custom Commands
1239+
1240+
As the commands above will look for a custom configuration file for each command in the **./config** directory of your project the CLI will also resolve custom command scripts in the **./bin** directory of your project. A [sample script](bin/_sample.js) is included in this repo and can be used to start the creation of a custom command. The bare minimum a command script should include is a `run()` method.
1241+
1242+
```javascript
1243+
module.exports = {
1244+
run: () => {
1245+
console.log('My custom command');
1246+
}
1247+
};
1248+
```
1249+
12361250
### NPM Scripts
12371251

12381252
The recommended [npm scripts](https://docs.npmjs.com/misc/scripts) below create shortcuts for using the cli and hook into other npm methods to make starting, versioning, and publishing more convenient. They can be modified to suit the needs of a particular project. Add them to your project's *package.json* file.

0 commit comments

Comments
 (0)