Skip to content

Commit

Permalink
Removed hardcoded text and added a utility function for template retr…
Browse files Browse the repository at this point in the history
…ieval
  • Loading branch information
ritwizsinha committed Feb 29, 2020
1 parent 26ea6e0 commit 624a8ba
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 146 deletions.
155 changes: 9 additions & 146 deletions src/misc/appCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { exec } from 'child_process';
import * as fs from 'fs';
import pascalCase = require('pascal-case');

import { ConvertToCode } from '../templates/template';
import { FolderDetails } from './folderDetails';

export class AppCreator {
Expand All @@ -29,21 +30,7 @@ export class AppCreator {
}

private createMainTypeScriptFile(): void {
const toWrite =
`import {
IAppAccessors,
ILogger,
} from '@rocket.chat/apps-engine/definition/accessors';
import { App } from '@rocket.chat/apps-engine/definition/App';
import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata';
export class ${ pascalCase(this.fd.info.name) }App extends App {
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
}
}
`;

const toWrite = ConvertToCode('MAIN_TYPESCRIPT_FILE', { id: '__APPNAME__', value: pascalCase(this.fd.info.name) });
fs.writeFileSync(this.fd.mergeWithFolder(this.fd.info.classFile), toWrite, 'utf8');
}

Expand All @@ -56,166 +43,42 @@ export class ${ pascalCase(this.fd.info.name) }App extends App {

// tslint:disable:max-line-length
private createdReadme(): void {
const toWrite =
`# ${ this.fd.info.name }
${ this.fd.info.description }
## Getting Started
Now that you have generated a blank default Rocket.Chat App, what are you supposed to do next?
Start developing! Open up your favorite editor, our recommended one is Visual Studio code,
and start working on your App. Once you have something ready to test, you can either
package it up and manually deploy it to your test instance or you can use the CLI to do so.
Here are some commands to get started:
- \`rc-apps package\`: this command will generate a packaged app file (zip) which can be installed **if** it compiles with TypeScript
- \`rc-apps deploy\`: this will do what \`package\` does but will then ask you for your server url, username, and password to deploy it for you
## Documentation
Here are some links to examples and documentation:
- [Rocket.Chat Apps TypeScript Definitions Documentation](https://rocketchat.github.io/Rocket.Chat.Apps-engine/)
- [Rocket.Chat Apps TypeScript Definitions Repository](https://github.com/RocketChat/Rocket.Chat.Apps-engine)
- [Example Rocket.Chat Apps](https://github.com/graywolf336/RocketChatApps)
- Community Forums
- [App Requests](https://forums.rocket.chat/c/rocket-chat-apps/requests)
- [App Guides](https://forums.rocket.chat/c/rocket-chat-apps/guides)
- [Top View of Both Categories](https://forums.rocket.chat/c/rocket-chat-apps)
- [#rocketchat-apps on Open.Rocket.Chat](https://open.rocket.chat/channel/rocketchat-apps)
`;
const toWrite = ConvertToCode('README_FILE', { id: '__APPNAME__', value: this.fd.info.name }, { id: '__APPDESCRIPTION', value: this.fd.info.description });

fs.writeFileSync('README.md', toWrite, 'utf8');
}

private createTsConfig(): void {
const toWrite =
`{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"strictNullChecks": true,
"noImplicitReturns": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
},
"include": [
"**/*.ts"
]
}
`;
const toWrite = ConvertToCode('TS_CONFIG_FILE');

fs.writeFileSync(this.fd.mergeWithFolder('tsconfig.json'), toWrite, 'utf8');
}

private createTsLintConfig(): void {
const toWrite =
`{
"extends": "tslint:recommended",
"rules": {
"array-type": [true, "generic"],
"member-access": true,
"no-console": [false],
"no-duplicate-variable": true,
"object-literal-sort-keys": false,
"quotemark": [true, "single"],
"max-line-length": [true, {
"limit": 160,
"ignore-pattern": "^import | *export .*? {"
}]
}
}
`;
const toWrite = ConvertToCode('TS_LINT_CONFIG_FILE');

fs.writeFileSync(this.fd.mergeWithFolder('tslint.json'), toWrite, 'utf8');
}

private createPackageJson(): void {
const toWrite =
`{
"devDependencies": {
"@rocket.chat/apps-engine": "^1.12.0",
"@types/node": "^10.12.14",
"tslint": "^5.10.0",
"typescript": "^2.9.1"
}
}
`;
const toWrite = ConvertToCode('PACKAGEJSON_FILE');

fs.writeFileSync(this.fd.mergeWithFolder('package.json'), toWrite, 'utf8');
}

private createGitIgnore(): void {
const toWrite =
`# ignore modules pulled in from npm
node_modules/
# rc-apps package output
dist/
# JetBrains IDEs
out/
.idea/
.idea_modules/
# macOS
.DS_Store
.AppleDouble
.LSOverride
._*
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
`;

const toWrite = ConvertToCode('GITIGNORE_FILE');
fs.writeFileSync(this.fd.mergeWithFolder('.gitignore'), toWrite, 'utf8');
}

private createEditorConfig(): void {
const toWrite =
`# EditorConfig is awesome: http://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
indent_style = space
indent_size = 4
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
`;
const toWrite = ConvertToCode('EDITOR_CONFIG_FILE');

fs.writeFileSync(this.fd.mergeWithFolder('.editorconfig'), toWrite, 'utf8');
}

private createVsCodeExts(): void {
const toWrite =
`{
"recommendations": [
"EditorConfig.editorconfig",
"eamodio.gitlens",
"eg2.vscode-npm-script",
"wayou.vscode-todo-highlight",
"minhthai.vscode-todo-parser",
"ms-vscode.vscode-typescript-tslint-plugin",
"rbbit.typescript-hero"
]
}`;
const toWrite = ConvertToCode('VSCODE_EXTS_FILE');

fs.mkdirSync(this.fd.mergeWithFolder('.vscode'));
fs.writeFileSync(this.fd.mergeWithFolder('.vscode/extensions.json'), toWrite, 'utf8');
Expand Down
183 changes: 183 additions & 0 deletions src/templates/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
interface ITemplate {
id: string;
data: string;
}
interface IVar {
id: string;
value: string;
}
const templates: Array<ITemplate> = [
{
id: 'MAIN_TYPESCRIPT_FILE',
data: `
import { IAppAccessors, ILogger,} from '@rocket.chat/apps-engine/definition/accessors';
import { App } from '@rocket.chat/apps-engine/definition/App';
import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata';
export class __APPNAME__App extends App {
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
}
}
`,
},
{
id: 'README_FILE',
data: `
# __APPNAME__
__APPDESCRIPTION__
## Getting Started
Now that you have generated a blank default Rocket.Chat App, what are you supposed to do next?
Start developing! Open up your favorite editor, our recommended one is Visual Studio code,
and start working on your App. Once you have something ready to test, you can either
package it up and manually deploy it to your test instance or you can use the CLI to do so.
Here are some commands to get started:
- \`rc-apps package\`: this command will generate a packaged app file (zip) which can be installed **if** it compiles with TypeScript
- \`rc-apps deploy\`: this will do what \`package\` does but will then ask you for your server url, username, and password to deploy it for you
## Documentation
Here are some links to examples and documentation:
- [Rocket.Chat Apps TypeScript Definitions Documentation](https://rocketchat.github.io/Rocket.Chat.Apps-engine/)
- [Rocket.Chat Apps TypeScript Definitions Repository](https://github.com/RocketChat/Rocket.Chat.Apps-engine)
- [Example Rocket.Chat Apps](https://github.com/graywolf336/RocketChatApps)
- Community Forums
- [App Requests](https://forums.rocket.chat/c/rocket-chat-apps/requests)
- [App Guides](https://forums.rocket.chat/c/rocket-chat-apps/guides)
- [Top View of Both Categories](https://forums.rocket.chat/c/rocket-chat-apps)
- [#rocketchat-apps on Open.Rocket.Chat](https://open.rocket.chat/channel/rocketchat-apps)
`,
},
{
id: 'TS_CONFIG_FILE',
data: `
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"strictNullChecks": true,
"noImplicitReturns": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
},
"include": [
"**/*.ts"
]
}
`,
},
{
id: 'TS_LINT_CONFIG_FILE',
data: `
{
"extends": "tslint:recommended",
"rules": {
"array-type": [true, "generic"],
"member-access": true,
"no-console": [false],
"no-duplicate-variable": true,
"object-literal-sort-keys": false,
"quotemark": [true, "single"],
"max-line-length": [true, {
"limit": 160,
"ignore-pattern": "^import | *export .*? {"
}]
}
}
`,
},
{
id: 'PACKAGEJSON_FILE',
data: `
{
"devDependencies": {
"@rocket.chat/apps-engine": "^1.12.0",
"@types/node": "^10.12.14",
"tslint": "^5.10.0",
"typescript": "^2.9.1"
}
}
`,
},
{
id: 'GITIGNORE_FILE',
data: `
# ignore modules pulled in from npm
node_modules/
# rc-apps package output
dist/
# JetBrains IDEs
out/
.idea/
.idea_modules/
# macOS
.DS_Store
.AppleDouble
.LSOverride
._*
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
`,
},
{
id: 'EDITOR_CONFIG_FILE',
data: `
# EditorConfig is awesome: http://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
indent_style = space
indent_size = 4
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
`,
},
{
id: 'VSCODE_EXTS_FILE',
data: `
{
"recommendations": [
"EditorConfig.editorconfig",
"eamodio.gitlens",
"eg2.vscode-npm-script",
"wayou.vscode-todo-highlight",
"minhthai.vscode-todo-parser",
"ms-vscode.vscode-typescript-tslint-plugin",
"rbbit.typescript-hero"
]
}
`,
},
];
export function ConvertToCode(templateName: string, ...args: Array<IVar>): string {
let { data }: ITemplate = templates.find((template: ITemplate) => template.id === templateName);
args.forEach((element) => {
data = data.replace(element.id, element.value);
});
return data;
}

0 comments on commit 624a8ba

Please sign in to comment.