-
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
1 parent
b8cbcc4
commit f45cf33
Showing
41 changed files
with
7,144 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
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,3 @@ | ||
node_modules/ | ||
dist/ | ||
coverage/ |
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,3 @@ | ||
{ | ||
"tabWidth": 4 | ||
} |
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,2 +1,41 @@ | ||
# js-console-scripting | ||
Functionality to streamline the process of scripting for the console with javascript | ||
# js-cli-scripting | ||
|
||
Streamline the process of creating a CLI using javascript / typescript. | ||
|
||
## Getting Started | ||
This readme will outline how to use this project. If you'd like to view an example, or just jump in, the **example-cli.ts** script contains | ||
a basic cli application with a few sample commands. | ||
|
||
## How To Use | ||
1. Create one or more **commands** for the program to use. | ||
1. Setup the application to use these commands with optional arguments for logging and requesting user input. | ||
1. Let the CliApplication class orchestrate the rest. | ||
|
||
```typescript | ||
const TestCommand: iCliCommand = { | ||
name: 'Test Command', //name for internal use | ||
displayText: "Print some test text to the console", | ||
tokens: ["test-print", "t-p"], | ||
requiredParams: [{ | ||
name: "txt", | ||
displayText: "Text to print" | ||
}], | ||
execute: async (params: {txt: string;}, cliOutputter: iCliOutputter): Promise<void> => { | ||
cliOutputter.pushMessage("Txt from user ==>", params.txt); | ||
} | ||
} | ||
``` | ||
|
||
With this kind of definition, the program will request everything under **requiredParams** from the user, then execute the command and pass in those required commands. | ||
|
||
Then, we setup our application runner | ||
```typescript | ||
new CliApplication().startApp( | ||
{ | ||
startup: { | ||
initialOutput: "Welcome to the example application" | ||
} | ||
}, | ||
commands | ||
); | ||
``` |
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,113 @@ | ||
const execSync = require("child_process").execSync; | ||
const fs = require("fs"); | ||
|
||
let args = process.argv; | ||
args.splice(0, 2); | ||
|
||
const command = args[0].toLowerCase().trim(); | ||
let exitCode; | ||
switch (command) { | ||
case "build": | ||
exitCode = build(); | ||
break; | ||
case "test": | ||
exitCode = test(); | ||
break; | ||
case "publish": | ||
exitCode = publish(); | ||
break; | ||
default: | ||
console.error(`Unknown command ${command}`); | ||
exitCode = 1; | ||
break; | ||
} | ||
process.exit(exitCode); | ||
|
||
function build() { | ||
const testResponse = test(); | ||
if (testResponse !== 0) { | ||
throw new Error(`Build failed because test execution failed!`); | ||
} | ||
|
||
console.log(`Building...`); | ||
|
||
let opts; | ||
opts = { | ||
cwd: `${__dirname}`, | ||
stdio: [0, 1, 2], | ||
}; | ||
|
||
opts = { | ||
cwd: `${__dirname}`, | ||
stdio: [0, 1, 2], | ||
}; | ||
try { | ||
execSync(`tsc`, opts); | ||
} catch (err) { | ||
console.log("ec", exitCode); | ||
console.error(`Error building project, check output above!`); | ||
return 1; | ||
} | ||
opts = { | ||
cwd: __dirname, | ||
stdio: [0, 1, 2], | ||
}; | ||
console.log(`Updating package.json version and copying package.json.`); | ||
try { | ||
const jsonFile = `${__dirname}\\package.json`; | ||
let packageJsonStr = fs.readFileSync(jsonFile, { | ||
encoding: "utf-8", | ||
}); | ||
const packageJson = JSON.parse(packageJsonStr); | ||
const version = (packageJson.version || "1.0.0").split("."); | ||
version[2] = parseInt(version[2]) + 1; | ||
packageJson.version = version.join("."); | ||
console.log(`New package.json version is ${packageJson.version}.`); | ||
packageJsonStr = JSON.stringify(packageJson, null, 4); | ||
fs.writeFileSync(jsonFile, packageJsonStr); | ||
|
||
execSync(`node node_modules/cpy-cli/cli.js package.json dist`, opts); | ||
execSync(`node node_modules/cpy-cli/cli.js readme.md dist`, opts); | ||
} catch (err) { | ||
console.error(`Error copying package.json / readme.md!`, err); | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
function test() { | ||
console.log(`Testing...`); | ||
let opts; | ||
opts = { | ||
cwd: `${__dirname}`, | ||
stdio: [0, 1, 2], | ||
}; | ||
try { | ||
execSync(`npm run test:coverage`, opts); | ||
} catch (err) { | ||
console.error(`Error running tests, check output above!`); | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
function publish() { | ||
console.log(`Publishing...`); | ||
const buildCode = build(); | ||
if (buildCode !== 0) { | ||
return buildCode; | ||
} | ||
let opts; | ||
opts = { | ||
cwd: `${__dirname}\\dist`, | ||
stdio: [0, 1, 2], | ||
}; | ||
|
||
try { | ||
execSync(`npm publish --access public`, opts); | ||
} catch (err) { | ||
console.error(`Error publishing, check output above!`); | ||
return 1; | ||
} | ||
return 0; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,2 @@ | ||
{"total": {"lines":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"},"statements":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"},"functions":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"},"branches":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"}} | ||
} |
Oops, something went wrong.