Skip to content

Commit

Permalink
feat: add prisma template
Browse files Browse the repository at this point in the history
  • Loading branch information
neikvon committed Feb 28, 2020
1 parent 72edf2f commit 01307b2
Show file tree
Hide file tree
Showing 17 changed files with 499 additions and 51 deletions.
4 changes: 2 additions & 2 deletions .fbi.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
factory: {
id: 'factory',
id: 'factory-factory',
version: '',
template: 'factory',
features: {"typescript":true}
features: { typescript: true }
}
}
1 change: 1 addition & 0 deletions lib/commands/db.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default class CommandDb extends Command {
flags: string[][];
description: string;
constructor(factory: Factory);
disable(): false | "Because there is no database model to maintain.";
run(args: any, flags: any): Promise<void>;
}
28 changes: 25 additions & 3 deletions lib/commands/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ class CommandDb extends fbi_1.Command {
this.args = '';
this.flags = [
['-u, --up', 'Apply any migrations that have not been applied yet'],
['-d, --down', 'Undo migrations']
['-d, --down', 'Undo migrations'],
['-s, --studio', 'Modern database IDE']
];
this.description = 'database migrations';
this.description = 'database migrations and IDE';
}
disable() {
return this.context.get('config.factory.features.prisma')
? false
: 'Because there is no database model to maintain.';
}
async run(args, flags) {
this.debug(`Factory: (${this.factory.id})`, 'from command', `"${this.id}"`, { args, flags });
Expand All @@ -23,7 +29,20 @@ class CommandDb extends fbi_1.Command {
name: 'action',
message: 'Choose an action:',
hint: 'Use arrow-keys, <return> to submit',
choices: ['up', 'down']
choices: [
{
name: 'up',
hint: 'Apply any migrations that have not been applied yet'
},
{
name: 'down',
hint: 'Undo migrations'
},
{
name: 'studio',
hint: 'Modern database IDE'
}
]
});
action = ret.action;
}
Expand All @@ -40,6 +59,9 @@ class CommandDb extends fbi_1.Command {
case 'down':
await this.exec.command('prisma2 migrate down --experimental', execOpts);
break;
case 'studio':
await this.exec.command('prisma2 studio --experimental', execOpts);
break;
}
this.logEnd('database migrate successfully');
}
Expand Down
1 change: 1 addition & 0 deletions lib/commands/generate.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default class CommandGenerate extends Command {
flags: never[];
description: string;
constructor(factory: Factory);
disable(): false | "Because there is no database model to maintain.";
run(args: any, flags: any): Promise<void>;
}
5 changes: 5 additions & 0 deletions lib/commands/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class CommandGenerate extends fbi_1.Command {
this.flags = [];
this.description = 'command generate description';
}
disable() {
return this.context.get('config.factory.features.prisma')
? false
: 'Because there is no database model to maintain.';
}
async run(args, flags) {
this.debug(`Factory: (${this.factory.id})`, 'from command', `"${this.id}"`);
this.logStart(`Start generating:`);
Expand Down
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import TemplateGraphql from './templates/graphql';
export default class FactoryNode extends Factory {
id: string;
description: string;
commands: (CommandBuild | CommandServe | CommandDb | CommandGenerate)[];
commands: (CommandServe | CommandDb | CommandGenerate | CommandBuild)[];
templates: TemplateGraphql[];
execOpts: any;
}
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const graphql_1 = require("./templates/graphql");
class FactoryNode extends fbi_1.Factory {
constructor() {
super(...arguments);
this.id = 'node';
this.id = 'factory-node';
this.description = 'factory for node.js application development';
this.commands = [
new build_1.default(this),
Expand Down
9 changes: 5 additions & 4 deletions lib/templates/graphql.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Template } from 'fbi';
import * as ejs from 'ejs';
import Factory from '..';
import SubTemplatePrisma from './graphql/prisma';
export default class TemplateGraphql extends Template {
factory: Factory;
id: string;
description: string;
path: string;
renderer: typeof ejs.render;
templates: SubTemplatePrisma[];
constructor(factory: Factory);
protected prompting(): Promise<void>;
protected start(): Promise<void>;
protected gathering(): Promise<void>;
protected writing(): Promise<void>;
protected install(): Promise<void>;
protected end(): Promise<void>;
protected installing(flags: Record<string, any>): Promise<void>;
protected ending(): Promise<void>;
private _promptDB;
}
26 changes: 10 additions & 16 deletions lib/templates/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path_1 = require("path");
const fbi_1 = require("fbi");
const ejs = require("ejs");
const utils_1 = require("fbi/lib/utils");
const prisma_1 = require("./graphql/prisma");
class TemplateGraphql extends fbi_1.Template {
constructor(factory) {
super();
Expand All @@ -12,8 +13,9 @@ class TemplateGraphql extends fbi_1.Template {
this.description = 'template graphql description';
this.path = 'templates/graphql';
this.renderer = ejs.render;
this.templates = [new prisma_1.default(this.factory)];
}
async prompting() {
async gathering() {
this.data.project = await this.prompt([
{
type: 'input',
Expand Down Expand Up @@ -50,12 +52,10 @@ class TemplateGraphql extends fbi_1.Template {
}
}
]);
if (this.data.project.features.prisma) {
this.data.project['database'] = await this._promptDB();
}
}
async start() {
const { factory, project } = this.data;
if (project.features.prisma) {
project['database'] = await this._promptDB();
}
this.spinner = this.createSpinner(`Creating project...`).start(`Creating ${this.style.bold.green(project.name)} via ${factory.id} from ${factory.template}...`);
}
async writing() {
Expand All @@ -74,28 +74,22 @@ class TemplateGraphql extends fbi_1.Template {
}
},
'src/index.ts',
project.features.prisma
? 'prisma/**/*'
: // {
// from: 'prisma/',
// to: 'prisma/'
// }
'',
project.features.prisma ? 'prisma/**/*' : '',
project.features.permissions ? 'src/permissions' : ''
],
renderOptions: {
async: true
}
};
}
async install() {
async installing(flags) {
const { project } = this.data;
this.spinner.succeed(`Created project ${this.style.cyan.bold(project.name)}`);
const { dependencies, devDependencies } = require(path_1.join(this.targetDir, 'package.json'));
if (utils_1.isValidObject(dependencies) || utils_1.isValidObject(devDependencies)) {
const installSpinner = this.createSpinner(`Installing dependencies...`).start();
try {
const { packageManager } = this.context.get('config');
const packageManager = flags.packageManager || this.context.get('config').packageManager;
const cmds = packageManager === 'yarn' ? [packageManager] : [packageManager, 'install'];
this.debug(`\nrunning \`${cmds.join(' ')}\` in ${this.targetDir}`);
await this.exec(cmds[0], cmds.slice(1), {
Expand All @@ -109,7 +103,7 @@ class TemplateGraphql extends fbi_1.Template {
}
}
}
async end() {
async ending() {
const { project } = this.data;
const projectName = this.style.cyan.bold(project.name);
if (this.errors) {
Expand Down
16 changes: 16 additions & 0 deletions lib/templates/graphql/prisma.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Template } from 'fbi';
import * as ejs from 'ejs';
import Factory from '../..';
export default class TemplateGraphql extends Template {
factory: Factory;
id: string;
description: string;
path: string;
renderer: typeof ejs.render;
constructor(factory: Factory);
protected gathering(): Promise<void>;
protected writing(): Promise<void>;
protected installing(flags: Record<string, any>): Promise<void>;
protected ending(): Promise<void>;
private _promptDB;
}

0 comments on commit 01307b2

Please sign in to comment.