Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
egandro committed Jan 13, 2017
0 parents commit 61d4894
Show file tree
Hide file tree
Showing 61 changed files with 2,955 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Editor configuration, see http://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = off
trim_trailing_whitespace = false
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# compiled output
/dist
/tmp

# sqlite files in test
/test/*.db

# test files in test
/test/*-sdb.json

# dependencies
/node_modules
/bower_components

# IDEs and editors
/.idea
/.vscode
.project
.classpath
.c9/
*.launch
.settings/

# misc
/.sass-cache
/connect.lock
/coverage/*
/libpeerconnection.log
npm-debug.log
testem.log
/typings

# e2e
/e2e/*.js
/e2e/*.map

#System Files
.DS_Store
Thumbs.db
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Nicassa Generator

nicassa-generator is a CLI toolkit for creating source code.

You require parsers such as nicassa-db-parser or nicassa-parser-ts-express-api

## Installation

`$ sudo npm install -g nicassa-generator`

Please note nicassa-generator is written in TypeScript.

## Features

TBD
5 changes: 5 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TODO

# express.ts.routes

Add a dynamic BasePath als prefix to all Controller Endpoints
19 changes: 19 additions & 0 deletions launcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env node

// workaround for multiuser enviorment
var cacheDirectory = process.env.TMP;
if (process.platform != 'win32') {
// we only have to take care in non windows enviorments
if (process.env.HOME != '') {
cacheDirectory = process.env.HOME + '/.ts-node-cache';
}
}

require('ts-node').register({
ignore: [], // needed in order to run from /usr/local...
cacheDirectory: cacheDirectory,
cache: true,
project: __dirname
});

require('./lib/cli.ts');
146 changes: 146 additions & 0 deletions lib/actions/addgen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
let fs = require('fs');
let process = require('process');

import { TopLevel } from '../persistance/toplevel';
import { NicassaGenerator } from '../persistance/nicassagenerator';
import { GeneratorConfigBasic } from '../persistance/generatorconfig.basic';

import { CodeGeneratorFactory } from '../generator/codegeneratorfactory.class';

export class AddGenerator {
fileName: string;
type: string;
name: string;

run(opts: any) {
this.fileName = opts.file;
this.type = opts.type;
this.name = opts.name;
if (opts.name === undefined) {
this.name = this.type;
}

if (!fs.existsSync(this.fileName)) {
console.error('error: file not found "' + this.fileName + '"');
process.exit(-1);
}

if (!this.sectionExistInFile()) {
console.error('error: nicassa generators section does not exist in "' + this.fileName + '". Dir you run init?');
process.exit(-1);
}

if (!this.isGeneratorTypeOK()) {
console.error('error: unknown generator type: ' + this.type);
process.exit(-1);
}

if (!this.isTypeAndNameOK()) {
console.error('error: generator type already exist or no/invalid name was given in "' + this.fileName + '"');
process.exit(-1);
}

let str = fs.readFileSync(this.fileName);
let codeGenerator = CodeGeneratorFactory.getCodeGenerator(this.type, <any>null, str);
let config = codeGenerator.getDefaultConfig(this.name);
let data = this.createJsonString(config);

try {
fs.writeFileSync(this.fileName, data);
} catch (err) {
console.error('error: can\'t create file "' + this.fileName + '"');
// console.error(err);
process.exit(-1);
}
}

protected createJsonString(generatorConfig: GeneratorConfigBasic): string {
let str = null;

try {
if (fs.existsSync(this.fileName)) {
str = fs.readFileSync(this.fileName);
}
} catch (err) {
console.error('error: can\'t read file "' + this.fileName + '"');
// console.error(err);
process.exit(-1);
}

if (str === null) {
let toplevel: TopLevel = {
nicassaGenerator: <any>null
}
str = JSON.stringify(toplevel, null, 2);
}

let toplevel: TopLevel = JSON.parse(str);
let nicassaGenerator: NicassaGenerator = <NicassaGenerator>toplevel.nicassaGenerator;
if (nicassaGenerator.generators === undefined) {
nicassaGenerator.generators = [];
}

nicassaGenerator.generators.push(generatorConfig);

let result = JSON.stringify(toplevel, null, 2);
return result;
}

protected sectionExistInFile(): boolean {
let str = null;

try {
str = fs.readFileSync(this.fileName);
} catch (err) {
console.error('error: can\'t read file "' + this.fileName + '"');
process.exit(-1);
}

let toplevel: TopLevel = JSON.parse(str);
return (toplevel.nicassaGenerator !== undefined);
}

protected isGeneratorTypeOK(): boolean {
let arr: string[] = [
'sequelize.ts.dal',
'express.ts.routes'
];
return (arr.indexOf(this.type) !== -1);
}

protected isTypeAndNameOK(): boolean {
let str = null;

try {
str = fs.readFileSync(this.fileName);
} catch (err) {
console.error('error: can\'t read file "' + this.fileName + '"');
process.exit(-1);
}

let toplevel: TopLevel = JSON.parse(str);
let nicassaGenerator: NicassaGenerator = <NicassaGenerator>toplevel.nicassaGenerator;

if (nicassaGenerator.generators === undefined ||
nicassaGenerator.generators === null ||
nicassaGenerator.generators.length < 1) {
return true;
}

let names: string[] = [];
for (let i = 0; i < nicassaGenerator.generators.length; i++) {
let gen = nicassaGenerator.generators[i];
if (gen.type === this.type) {
names.push(gen.name);
}
}

// if we have a same name, we have a clash
return (names.indexOf(this.name) === -1);
}
}

export default function run(opts: any) {
let instance = new AddGenerator();
return instance.run(opts);
}
86 changes: 86 additions & 0 deletions lib/actions/gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
let fs = require('fs');
let process = require('process');

import { TopLevel } from '../persistance/toplevel';
import { NicassaGenerator } from '../persistance/nicassagenerator';
import { GeneratorConfigBasic } from '../persistance/generatorconfig.basic';

import { CodeGeneratorFactory } from '../generator/codegeneratorfactory.class';
import { BaseGenerator } from '../generator/basegenerator';

export class RunGenerator {
fileName: string;
name: string;
generators: GeneratorConfigBasic[] = [];

public async run(opts: any) : Promise<boolean>{
this.fileName = opts.file;
this.name = opts.name;

if (!fs.existsSync(this.fileName)) {
console.error('error: file not found "' + this.fileName + '"');
process.exit(-1);
}

if (this.name !== undefined) {
let gen: GeneratorConfigBasic = BaseGenerator.getGeneratorByName(this.fileName, this.name);
if (gen === null) {
console.error('error: no generator with name: "' + this.name + '" was found in "' + this.fileName + '"');
process.exit(-1);
}
this.generators.push(gen);
} else {
this.generators = this.getAllActiveGenerators();
}

if (this.generators.length === 0) {
console.error('error: no active generators in "' + this.fileName + '"');
process.exit(-1);
}

let str = fs.readFileSync(this.fileName);
return await this.runGenerators(str);
}

protected async runGenerators(str: string) : Promise<boolean>{
for (let i = 0; i < this.generators.length; i++) {
let gen = this.generators[i];
let codeGenerator = CodeGeneratorFactory.getCodeGenerator(gen.type, gen, str);
await codeGenerator.run();
}
return await true;
}

protected getAllActiveGenerators(): GeneratorConfigBasic[] {
let str = null;

try {
str = fs.readFileSync(this.fileName);
} catch (err) {
console.error('error: can\'t read file "' + this.fileName + '"');
process.exit(-1);
}

let result: GeneratorConfigBasic[] = [];

let toplevel: TopLevel = JSON.parse(str);
let nicassaGenerator: NicassaGenerator = <NicassaGenerator>toplevel.nicassaGenerator;
if (nicassaGenerator === undefined || nicassaGenerator.generators === undefined) {
return result;
}

for (let i = 0; i < nicassaGenerator.generators.length; i++) {
let gen = nicassaGenerator.generators[i];
if (gen.active) {
result.push(gen);
}
}

return result;
}
}

export default async function run(opts: any) {
let instance = new RunGenerator();
return await instance.run(opts);
}
Loading

0 comments on commit 61d4894

Please sign in to comment.