Skip to content

Commit

Permalink
Convert to typescript (#13)
Browse files Browse the repository at this point in the history
* convert to Typescript

* Convert Code to Typescript

* Add .npmignore

* Fix Package Name
  • Loading branch information
khattakdev authored Aug 17, 2020
1 parent 759baad commit 8f9ec59
Show file tree
Hide file tree
Showing 11 changed files with 331 additions and 85 deletions.
102 changes: 102 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
See http://help.github.com/ignore-files/ for more about ignoring files.

# compiled output
/dist
/tmp
/out-tsc
out/*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# IDEs and editors
.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# misc
.sass-cache
connect.lock
typings

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*


# Dependency directories
node_modules/
jspm_packages/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next

# Lerna
lerna-debug.log

# System Files
.DS_Store
Thumbs.db

/src
/bin
/template
/.vscode

./src
./bin
./template
./.vscode
File renamed without changes
3 changes: 1 addition & 2 deletions bin/index.js → bin/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node

const cli = require("../src/cli");

import cli from "../src/cli";
cli(process.argv);
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"version": "1.1.1",
"description": "A MongoDB Schema Generator CLI",
"main": "index.js",
"files": [
"dist"
],
"bin": {
"msc": "bin/index.js"
"msc": "dist/bin/index.js"
},
"repository": {
"type": "git",
Expand All @@ -21,8 +24,10 @@
"author": "Arsalan Khattak <akkhattak65@gmail.com>",
"license": "MIT",
"dependencies": {
"@types/inquirer": "^7.3.0",
"@types/listr": "^0.14.2",
"@types/node": "^14.0.27",
"arg": "^4.1.3",
"chalk": "^4.1.0",
"inquirer": "^7.3.3",
"listr": "^0.14.3"
}
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
### [Demo](https://github.com/eKhattak/mongoose-schema-cli)

![Demo](/demo.gif)
![Demo](/assets/demo.gif)

## Install

Expand Down
83 changes: 53 additions & 30 deletions src/cli.js → src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const arg = require("arg");
const inquirer = require("inquirer");
const schema = require("./main");

// export var schemaDetails = {};
function parseArgumentsIntoOptions(rawArgs) {
import arg from "arg";
import inquirer from "inquirer";
import createSchema from "./main";

function parseArgumentsIntoOptions(
rawArgs: any[]
): {
language: boolean;
filePath: string;
} {
const args = arg(
{
"--typescript": Boolean,
Expand All @@ -17,24 +21,39 @@ function parseArgumentsIntoOptions(rawArgs) {
}
);
return {
language: args["--typescript"] || undefined,
language: args["--typescript"] || false,
filePath: args["--filepath"] || "/",
};
}

async function promptForMissingOptions(options) {
type optionsType = {
language: boolean;
filePath?: string;
schemaKeys?: number | 0;
};

type questionType = {
type: string;
name: string;
message: string;
default: string | boolean | number;
choices?: string[] | number[];
};

async function promptForMissingOptions(
options: optionsType
): Promise<{
language: any;
schema: any;
filePath?: string | undefined;
schemaKeys?: number | undefined;
}> {
const defaultOptions = {
language: "Javascript",
schema: "default",
};
// if (options.skipPrompts) {
// return {
// ...options,
// template: options.template || defaultLanguage,
// };
// }

const questions = [];
const questions: questionType[] = [];
//@TODO: Temporary Disabled, Uncomment after adding Typescript Schema
// if (!options.language) {
// questions.push({
Expand All @@ -57,20 +76,18 @@ async function promptForMissingOptions(options) {
return {
...options,
language: options.language || answers.language,
mongoose: options.mongoose || answers.mongoose,
schema: answers.schema,
};
}

async function promptForSchemaObject() {
console.log();
const defaultOptions = {
name: "default",
type: "String",
required: true,
default: "",
};
const questions = [];
const questions: questionType[] = [];

questions.push({
type: "input",
Expand Down Expand Up @@ -101,18 +118,23 @@ async function promptForSchemaObject() {
default: defaultOptions.default,
});

const answers = await inquirer.prompt(questions);
const answers: {
name: string;
type: string;
required: boolean;
default: string;
} = await inquirer.prompt(questions);

return {
schemaName: answers.name,
name: answers.name,
type: answers.type,
isRequired: answers.required,
defaultValue: answers.default,
};
}

async function cli(args) {
var options = parseArgumentsIntoOptions(args);
async function cli(args: string[]) {
var options: optionsType = parseArgumentsIntoOptions(args);
options = await promptForMissingOptions(options);

const { schemaKeys } = await inquirer.prompt({
Expand All @@ -122,19 +144,20 @@ async function cli(args) {
default: 0,
});

// Get Schema Keys' Input
const schemaKeyValues = [];
const schemaKeyValues: {
name: string;
type: string;
isRequired: boolean;
defaultValue: string;
}[] = [];
for (let i = 0; i < schemaKeys; i++) {
const objectValues = await promptForSchemaObject();
schemaKeyValues.push(objectValues);
}

options = {
...options,
schemaKeys,
};
var schema = { ...schemaKeys };

await schema(options, schemaKeyValues);
await createSchema(schema, schemaKeyValues);
}

module.exports = cli;
export default cli;
40 changes: 0 additions & 40 deletions src/main.js

This file was deleted.

Loading

0 comments on commit 8f9ec59

Please sign in to comment.