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"InitCommand.js","sourcesContent":["import {CommandUtils} from \"./CommandUtils\";\nimport {ObjectLiteral} from \"../common/ObjectLiteral\";\nimport * as path from \"path\";\nimport * as yargs from \"yargs\";\nconst chalk = require(\"chalk\");\n\n/**\n * Generates a new project with TypeORM.\n */\nexport class InitCommand implements yargs.CommandModule {\n command = \"init\";\n describe = \"Generates initial TypeORM project structure. \" +\n \"If name specified then creates files inside directory called as name. \" +\n \"If its not specified then creates files inside current directory.\";\n\n builder(args: yargs.Argv) {\n return args\n .option(\"c\", {\n alias: \"connection\",\n default: \"default\",\n describe: \"Name of the connection on which to run a query\"\n })\n .option(\"n\", {\n alias: \"name\",\n describe: \"Name of the project directory.\"\n })\n .option(\"db\", {\n alias: \"database\",\n describe: \"Database type you'll use in your project.\"\n })\n .option(\"express\", {\n describe: \"Indicates if express should be included in the project.\"\n })\n .option(\"docker\", {\n describe: \"Set to true if docker-compose must be generated as well. False by default.\"\n });\n }\n\n async handler(args: yargs.Arguments) {\n try {\n const database: string = args.database as any || \"mysql\";\n const isExpress = args.express !== undefined ? true : false;\n const isDocker = args.docker !== undefined ? true : false;\n const basePath = process.cwd() + (args.name ? (\"/\" + args.name) : \"\");\n const projectName = args.name ? path.basename(args.name as any) : undefined;\n await CommandUtils.createFile(basePath + \"/package.json\", InitCommand.getPackageJsonTemplate(projectName), false);\n if (isDocker)\n await CommandUtils.createFile(basePath + \"/docker-compose.yml\", InitCommand.getDockerComposeTemplate(database), false);\n await CommandUtils.createFile(basePath + \"/.gitignore\", InitCommand.getGitIgnoreFile());\n await CommandUtils.createFile(basePath + \"/README.md\", InitCommand.getReadmeTemplate({ docker: isDocker }), false);\n await CommandUtils.createFile(basePath + \"/tsconfig.json\", InitCommand.getTsConfigTemplate());\n await CommandUtils.createFile(basePath + \"/ormconfig.json\", InitCommand.getOrmConfigTemplate(database));\n await CommandUtils.createFile(basePath + \"/src/entity/User.ts\", InitCommand.getUserEntityTemplate(database));\n await CommandUtils.createFile(basePath + \"/src/index.ts\", InitCommand.getAppIndexTemplate(isExpress));\n await CommandUtils.createDirectories(basePath + \"/src/migration\");\n\n // generate extra files for express application\n if (isExpress) {\n await CommandUtils.createFile(basePath + \"/src/routes.ts\", InitCommand.getRoutesTemplate());\n await CommandUtils.createFile(basePath + \"/src/controller/UserController.ts\", InitCommand.getControllerTemplate());\n }\n\n const packageJsonContents = await CommandUtils.readFile(basePath + \"/package.json\");\n await CommandUtils.createFile(basePath + \"/package.json\", InitCommand.appendPackageJson(packageJsonContents, database, isExpress));\n\n if (args.name) {\n console.log(chalk.green(`Project created inside ${chalk.blue(basePath)} directory.`));\n\n } else {\n console.log(chalk.green(`Project created inside current directory.`));\n }\n\n } catch (err) {\n console.log(chalk.black.bgRed(\"Error during project initialization:\"));\n console.error(err);\n process.exit(1);\n }\n }\n\n // -------------------------------------------------------------------------\n // Protected Static Methods\n // -------------------------------------------------------------------------\n\n /**\n * Gets contents of the ormconfig file.\n */\n protected static getOrmConfigTemplate(database: string): string {\n const options: ObjectLiteral = { };\n switch (database) {\n case \"mysql\":\n Object.assign(options, {\n type: \"mysql\",\n host: \"localhost\",\n port: 3306,\n username: \"test\",\n password: \"test\",\n database: \"test\",\n });\n break;\n case \"mariadb\":\n Object.assign(options, {\n type: \"mariadb\",\n host: \"localhost\",\n port: 3306,\n username: \"test\",\n password: \"test\",\n database: \"test\",\n });\n break;\n case \"sqlite\":\n Object.assign(options, {\n type: \"sqlite\",\n \"database\": \"database.sqlite\",\n });\n break;\n case \"postgres\":\n Object.assign(options, {\n \"type\": \"postgres\",\n \"host\": \"localhost\",\n \"port\": 5432,\n \"username\": \"test\",\n \"password\": \"test\",\n \"database\": \"test\",\n });\n break;\n case \"cockroachdb\":\n Object.assign(options, {\n \"type\": \"cockroachdb\",\n \"host\": \"localhost\",\n \"port\": 26257,\n \"username\": \"root\",\n \"password\": \"\",\n \"database\": \"defaultdb\",\n });\n break;\n case \"mssql\":\n Object.assign(options, {\n \"type\": \"mssql\",\n \"host\": \"localhost\",\n \"username\": \"sa\",\n \"password\": \"Admin12345\",\n \"database\": \"tempdb\",\n });\n break;\n case \"oracle\":\n Object.assign(options, {\n \"type\": \"oracle\",\n \"host\": \"localhost\",\n \"username\": \"system\",\n \"password\": \"oracle\",\n \"port\": 1521,\n \"sid\": \"xe.oracle.docker\",\n });\n break;\n case \"mongodb\":\n Object.assign(options, {\n \"type\": \"mongodb\",\n \"database\": \"test\",\n });\n break;\n }\n Object.assign(options, {\n synchronize: true,\n logging: false,\n entities: [\n \"src/entity/**/*.ts\"\n ],\n migrations: [\n \"src/migration/**/*.ts\"\n ],\n subscribers: [\n \"src/subscriber/**/*.ts\"\n ],\n cli: {\n entitiesDir: \"src/entity\",\n migrationsDir: \"src/migration\",\n subscribersDir: \"src/subscriber\"\n }\n });\n return JSON.stringify(options, undefined, 3);\n }\n\n /**\n * Gets contents of the ormconfig file.\n */\n protected static getTsConfigTemplate(): string {\n return JSON.stringify({\n compilerOptions: {\n lib: [\"es5\", \"es6\"],\n target: \"es5\",\n module: \"commonjs\",\n moduleResolution: \"node\",\n outDir: \"./build\",\n emitDecoratorMetadata: true,\n experimentalDecorators: true,\n sourceMap: true\n }\n }\n , undefined, 3);\n }\n\n /**\n * Gets contents of the .gitignore file.\n */\n protected static getGitIgnoreFile(): string {\n return `.idea/\n.vscode/\nnode_modules/\nbuild/\ntmp/\ntemp/`;\n }\n\n /**\n * Gets contents of the user entity.\n */\n protected static getUserEntityTemplate(database: string): string {\n return `import {Entity, ${ database === \"mongodb\" ? \"ObjectIdColumn, ObjectID\" : \"PrimaryGeneratedColumn\" }, Column} from \"typeorm\";\n\n@Entity()\nexport class User {\n\n ${ database === \"mongodb\" ? \"@ObjectIdColumn()\" : \"@PrimaryGeneratedColumn()\" }\n id: ${ database === \"mongodb\" ? \"ObjectID\" : \"number\" };\n\n @Column()\n firstName: string;\n\n @Column()\n lastName: string;\n\n @Column()\n age: number;\n\n}\n`;\n }\n\n /**\n * Gets contents of the route file (used when express is enabled).\n */\n protected static getRoutesTemplate(): string {\n return `import {UserController} from \"./controller/UserController\";\n\nexport const Routes = [{\n method: \"get\",\n route: \"/users\",\n controller: UserController,\n action: \"all\"\n}, {\n method: \"get\",\n route: \"/users/:id\",\n controller: UserController,\n action: \"one\"\n}, {\n method: \"post\",\n route: \"/users\",\n controller: UserController,\n action: \"save\"\n}, {\n method: \"delete\",\n route: \"/users/:id\",\n controller: UserController,\n action: \"remove\"\n}];`;\n }\n\n /**\n * Gets contents of the user controller file (used when express is enabled).\n */\n protected static getControllerTemplate(): string {\n return `import {getRepository} from \"typeorm\";\nimport {NextFunction, Request, Response} from \"express\";\nimport {User} from \"../entity/User\";\n\nexport class UserController {\n\n private userRepository = getRepository(User);\n\n async all(request: Request, response: Response, next: NextFunction) {\n return this.userRepository.find();\n }\n\n async one(request: Request, response: Response, next: NextFunction) {\n return this.userRepository.findOne(request.params.id);\n }\n\n async save(request: Request, response: Response, next: NextFunction) {\n return this.userRepository.save(request.body);\n }\n\n async remove(request: Request, response: Response, next: NextFunction) {\n let userToRemove = await this.userRepository.findOne(request.params.id);\n await this.userRepository.remove(userToRemove);\n }\n\n}`;\n }\n\n /**\n * Gets contents of the main (index) application file.\n */\n protected static getAppIndexTemplate(express: boolean): string {\n if (express) {\n return `import \"reflect-metadata\";\nimport {createConnection} from \"typeorm\";\nimport * as express from \"express\";\nimport * as bodyParser from \"body-parser\";\nimport {Request, Response} from \"express\";\nimport {Routes} from \"./routes\";\nimport {User} from \"./entity/User\";\n\ncreateConnection().then(async connection => {\n\n // create express app\n const app = express();\n app.use(bodyParser.json());\n\n // register express routes from defined application routes\n Routes.forEach(route => {\n (app as any)[route.method](route.route, (req: Request, res: Response, next: Function) => {\n const result = (new (route.controller as any))[route.action](req, res, next);\n if (result instanceof Promise) {\n result.then(result => result !== null && result !== undefined ? res.send(result) : undefined);\n\n } else if (result !== null && result !== undefined) {\n res.json(result);\n }\n });\n });\n\n // setup express app here\n // ...\n\n // start express server\n app.listen(3000);\n\n // insert new users for test\n await connection.manager.save(connection.manager.create(User, {\n firstName: \"Timber\",\n lastName: \"Saw\",\n age: 27\n }));\n await connection.manager.save(connection.manager.create(User, {\n firstName: \"Phantom\",\n lastName: \"Assassin\",\n age: 24\n }));\n\n console.log(\"Express server has started on port 3000. Open http://localhost:3000/users to see results\");\n\n}).catch(error => console.log(error));\n`;\n\n } else {\n return `import \"reflect-metadata\";\nimport {createConnection} from \"typeorm\";\nimport {User} from \"./entity/User\";\n\ncreateConnection().then(async connection => {\n\n console.log(\"Inserting a new user into the database...\");\n const user = new User();\n user.firstName = \"Timber\";\n user.lastName = \"Saw\";\n user.age = 25;\n await connection.manager.save(user);\n console.log(\"Saved a new user with id: \" + user.id);\n\n console.log(\"Loading users from the database...\");\n const users = await connection.manager.find(User);\n console.log(\"Loaded users: \", users);\n\n console.log(\"Here you can setup and run express/koa/any other framework.\");\n\n}).catch(error => console.log(error));\n`;\n }\n }\n\n /**\n * Gets contents of the new package.json file.\n */\n protected static getPackageJsonTemplate(projectName?: string): string {\n return JSON.stringify({\n name: projectName || \"new-typeorm-project\",\n version: \"0.0.1\",\n description: \"Awesome project developed with TypeORM.\",\n devDependencies: {\n },\n dependencies: {\n },\n scripts: {\n }\n }, undefined, 3);\n }\n\n /**\n * Gets contents of the new docker-compose.yml file.\n */\n protected static getDockerComposeTemplate(database: string): string {\n\n switch (database) {\n case \"mysql\":\n return `version: '3'\nservices:\n\n mysql:\n image: \"mysql:5.7.10\"\n ports:\n - \"3306:3306\"\n environment:\n MYSQL_ROOT_PASSWORD: \"admin\"\n MYSQL_USER: \"test\"\n MYSQL_PASSWORD: \"test\"\n MYSQL_DATABASE: \"test\"\n\n`;\n case \"mariadb\":\n return `version: '3'\nservices:\n\n mariadb:\n image: \"mariadb:10.1.16\"\n ports:\n - \"3306:3306\"\n environment:\n MYSQL_ROOT_PASSWORD: \"admin\"\n MYSQL_USER: \"test\"\n MYSQL_PASSWORD: \"test\"\n MYSQL_DATABASE: \"test\"\n\n`;\n case \"postgres\":\n return `version: '3'\nservices:\n\n postgres:\n image: \"postgres:9.6.1\"\n ports:\n - \"5432:5432\"\n environment:\n POSTGRES_USER: \"test\"\n POSTGRES_PASSWORD: \"test\"\n POSTGRES_DB: \"test\"\n\n`;\n case \"cockroachdb\":\n return `version: '3'\nservices:\n\n cockroachdb:\n image: \"cockroachdb/cockroach:v2.1.4\"\n command: start --insecure\n ports:\n - \"26257:26257\"\n\n`;\n case \"sqlite\":\n return `version: '3'\nservices:\n`;\n case \"oracle\":\n throw new Error(`You cannot initialize a project with docker for Oracle driver yet.`); // todo: implement for oracle as well\n\n case \"mssql\":\n return `version: '3'\nservices:\n\n mssql:\n image: \"microsoft/mssql-server-linux:rc2\"\n ports:\n - \"1433:1433\"\n environment:\n SA_PASSWORD: \"Admin12345\"\n ACCEPT_EULA: \"Y\"\n\n`;\n case \"mongodb\":\n return `version: '3'\nservices:\n\n mongodb:\n image: \"mongo:4.0.6\"\n container_name: \"typeorm-mongodb\"\n ports:\n - \"27017:27017\"\n\n`;\n }\n return \"\";\n }\n\n /**\n * Gets contents of the new readme.md file.\n */\n protected static getReadmeTemplate(options: { docker: boolean }): string {\n let template = `# Awesome Project Build with TypeORM\n\nSteps to run this project:\n\n1. Run \\`npm i\\` command\n`;\n\n if (options.docker) {\n template += `2. Run \\`docker-compose up\\` command\n`;\n } else {\n template += `2. Setup database settings inside \\`ormconfig.json\\` file\n`;\n }\n\n template += `3. Run \\`npm start\\` command\n`;\n return template;\n }\n\n /**\n * Appends to a given package.json template everything needed.\n */\n protected static appendPackageJson(packageJsonContents: string, database: string, express: boolean /*, docker: boolean*/): string {\n const packageJson = JSON.parse(packageJsonContents);\n\n if (!packageJson.devDependencies) packageJson.devDependencies = {};\n Object.assign(packageJson.devDependencies, {\n \"ts-node\": \"3.3.0\",\n \"@types/node\": \"^8.0.29\",\n \"typescript\": \"3.3.3333\"\n });\n\n if (!packageJson.dependencies) packageJson.dependencies = {};\n Object.assign(packageJson.dependencies, {\n \"typeorm\": require(\"../package.json\").version,\n \"reflect-metadata\": \"^0.1.10\"\n });\n\n switch (database) {\n case \"mysql\":\n case \"mariadb\":\n packageJson.dependencies[\"mysql\"] = \"^2.14.1\";\n break;\n case \"postgres\":\n case \"cockroachdb\":\n packageJson.dependencies[\"pg\"] = \"^7.3.0\";\n break;\n case \"sqlite\":\n packageJson.dependencies[\"sqlite3\"] = \"^4.0.3\";\n break;\n case \"oracle\":\n packageJson.dependencies[\"oracledb\"] = \"^1.13.1\";\n break;\n case \"mssql\":\n packageJson.dependencies[\"mssql\"] = \"^4.0.4\";\n break;\n case \"mongodb\":\n packageJson.dependencies[\"mongodb\"] = \"^3.0.8\";\n break;\n }\n\n if (express) {\n packageJson.dependencies[\"express\"] = \"^4.15.4\";\n packageJson.dependencies[\"body-parser\"] = \"^1.18.1\";\n }\n\n if (!packageJson.scripts) packageJson.scripts = {};\n Object.assign(packageJson.scripts, {\n start: /*(docker ? \"docker-compose up && \" : \"\") + */\"ts-node src/index.ts\"\n });\n return JSON.stringify(packageJson, undefined, 3);\n }\n\n}\n"],"sourceRoot":".."}
|