Skip to content

Commit ccc7f9f

Browse files
authored
Merge pull request #71 from twilio/typescript-support
support typescript
2 parents b955862 + 16be5c5 commit ccc7f9f

File tree

9 files changed

+151
-4
lines changed

9 files changed

+151
-4
lines changed

packages/create-flex-plugin/src/lib/cli.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@ export interface CLIArguments {
2020
y?: boolean;
2121
template?: string;
2222
t?: string;
23+
typescript?: boolean;
24+
s?: boolean;
2325
}
2426

2527
class CLI {
2628
private readonly parser: Argv<CLIArguments>;
2729
private options: { [key: string]: Options; } = {
30+
typescript: {
31+
alias: 's',
32+
type: 'boolean',
33+
describe: 'Create a TypeScript project',
34+
default: false,
35+
},
2836
template: {
2937
alias: 't',
3038
type: 'string',

packages/create-flex-plugin/src/lib/create-flex-plugin.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const moveFile = promisify(fs.rename);
1515
const templatesRootDir = resolve(__dirname, '../../templates');
1616
const templateCorePath = resolve(templatesRootDir, 'core');
1717
const templateJsPath = resolve(templatesRootDir, 'js');
18+
const templateTsPath = resolve(templatesRootDir, 'ts');
1819

1920
export interface FlexPluginArguments extends CLIArguments {
2021
targetDirectory: string;
@@ -94,6 +95,9 @@ export const _scaffold = async (config: FlexPluginArguments): Promise<boolean> =
9495

9596
// Get src directory from template URL if provided
9697
let srcPath = templateJsPath;
98+
if (config.typescript) {
99+
srcPath = templateTsPath;
100+
}
97101
if (config.template) {
98102
dirObject = tmp.dirSync();
99103
await downloadFromGitHub(config, config.template, dirObject.name);
@@ -109,9 +113,11 @@ export const _scaffold = async (config: FlexPluginArguments): Promise<boolean> =
109113

110114
// Rename plugins
111115
if (!dirObject) {
116+
const ext = config.typescript ? 'tsx' : 'js';
117+
112118
await moveFile(
113-
join(config.targetDirectory, 'src/DemoPlugin.js'),
114-
join(config.targetDirectory, `src/${config.pluginClassName}.js`),
119+
join(config.targetDirectory, `src/DemoPlugin.${ext}`),
120+
join(config.targetDirectory, `src/${config.pluginClassName}.${ext}`),
115121
);
116122
}
117123

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "{{name}}",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"bootstrap": "flex-check-start",
7+
"postinstall": "npm run bootstrap",
8+
"prestart": "npm run bootstrap",
9+
"start": "craco start",
10+
"build": "craco build",
11+
"test": "craco test --env=jsdom",
12+
"eject": "craco eject"
13+
},
14+
"devDependencies": {
15+
"@twilio/flex-ui": "{{flexSdkVersion}}"
16+
},
17+
"dependencies": {
18+
"@craco/craco": "^5.0.2",
19+
"@types/jest": "^24.0.12",
20+
"@types/node": "^12.0.0",
21+
"@types/react": "^16.8.16",
22+
"@types/react-dom": "^16.8.4",
23+
"core-js": "^2.6.5",
24+
"craco-config-flex-plugin": "{{cracoConfigVersion}}",
25+
"flex-plugin": "{{flexPluginVersion}}",
26+
"react": "^16.8.6",
27+
"react-dom": "^16.8.6",
28+
"react-scripts": "^3.0.0",
29+
"typescript": "^3.4.5"
30+
},
31+
"browserslist": {
32+
"production": [
33+
">0.2%",
34+
"not dead",
35+
"not op_mini all"
36+
],
37+
"development": [
38+
"last 1 chrome version",
39+
"last 1 firefox version",
40+
"last 1 safari version"
41+
]
42+
}
43+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react';
2+
import * as Flex from '@twilio/flex-ui';
3+
import { FlexPlugin } from 'flex-plugin';
4+
5+
import CustomTaskListComponent from './components/CustomTaskListComponent';
6+
7+
const PLUGIN_NAME = '{{pluginClassName}}';
8+
9+
export default class {{pluginClassName}} extends FlexPlugin {
10+
constructor() {
11+
super(PLUGIN_NAME);
12+
}
13+
14+
/**
15+
* This code is run when your plugin is being started
16+
* Use this to modify any UI components or attach to the actions framework
17+
*
18+
* @param flex { typeof import('@twilio/flex-ui') }
19+
* @param manager { import('@twilio/flex-ui').Manager }
20+
*/
21+
init(flex: typeof Flex, manager: Flex.Manager) {
22+
flex.AgentDesktopView.Panel1.Content.add(
23+
<CustomTaskListComponent key="demo-component" />,
24+
{
25+
sortOrder: -1,
26+
}
27+
);
28+
}
29+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
3+
const taskListStyles = {
4+
padding: '10px',
5+
margin: '0px',
6+
color: '#fff',
7+
background: '#000',
8+
};
9+
10+
const CustomTaskListComponent = () => {
11+
return (
12+
<div style={taskListStyles}>This is a demo component</div>
13+
);
14+
};
15+
16+
export default CustomTaskListComponent;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import * as FlexPlugin from 'flex-plugin';
2+
import {{pluginClassName}} from './{{pluginClassName}}';
3+
4+
FlexPlugin.loadPlugin({{pluginClassName}});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": "src",
4+
"rootDir": ".",
5+
"outDir": "build",
6+
"target": "es5",
7+
"lib": [
8+
"dom",
9+
"dom.iterable",
10+
"esnext"
11+
],
12+
"allowJs": true,
13+
"skipLibCheck": true,
14+
"esModuleInterop": true,
15+
"allowSyntheticDefaultImports": true,
16+
"strict": true,
17+
"forceConsistentCasingInFileNames": true,
18+
"module": "esnext",
19+
"moduleResolution": "node",
20+
"resolveJsonModule": true,
21+
"isolatedModules": true,
22+
"noEmit": true,
23+
"jsx": "preserve",
24+
"noUnusedLocals": false,
25+
"noUnusedParameters": false
26+
},
27+
"include": [
28+
"./src/**/*"
29+
],
30+
"exclude": [
31+
"./**/*.test.ts",
32+
"./**/*.test.tsx",
33+
"./**/__mocks__/*.ts",
34+
"./**/__mocks__/*.tsx"
35+
]
36+
}

packages/create-flex-plugin/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"baseUrl": ".",
66
"rootDir": "src"
77
},
8-
"exclude": ["node_modules", "__tests__", "dist"]
8+
"exclude": ["node_modules", "__tests__", "dist", "templates"]
99
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"extends": ["../../tslint.json"]
2+
"extends": ["../../tslint.json"],
3+
"linterOptions": {
4+
"exclude": [
5+
"templates/**/*"
6+
]
7+
}
38
}

0 commit comments

Comments
 (0)