Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonQDixon committed Jul 13, 2021
1 parent b8cbcc4 commit f45cf33
Show file tree
Hide file tree
Showing 41 changed files with 7,144 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs


dist/

# Mono auto generated files
mono_crash.*

Expand Down
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
dist/
coverage/
3 changes: 3 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tabWidth": 4
}
43 changes: 41 additions & 2 deletions README.md
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
);
```
113 changes: 113 additions & 0 deletions build-cli.js
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;
}
1 change: 1 addition & 0 deletions coverage/badge-branches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions coverage/badge-functions.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions coverage/badge-lines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions coverage/badge-statements.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions coverage/coverage-summary.json
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"}}
}
Loading

0 comments on commit f45cf33

Please sign in to comment.