-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
107 lines (92 loc) · 2.55 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env node
// Usage: npx create-my-template my-app --next| --react-ts
const { execSync } = require('child_process');
const fs = require('fs-extra');
const path = require('path');
const spawn = require('child_process').spawn;
enum TemplateType {
Next = '--next',
ReactTS = '--react-ts',
}
interface Template {
type: TemplateType;
description: string;
directory: string;
}
const templates: Record<TemplateType, Template> = {
[TemplateType.Next]: {
type: TemplateType.Next,
description: 'Next.js template with TrustAuthX SDK',
directory: 'NextJS-Example-SSR',
},
[TemplateType.ReactTS]: {
type: TemplateType.ReactTS,
description: 'ReactJs-Typescript template with TrustAuthX SDK',
directory: 'example-reactjs-starter',
},
};
if (process.argv.length < 4) {
console.log(
'You have to provide a name to your app and the template to use.'
);
console.log('For example :');
console.log(`npx trustauthx my-app ${Object.values(TemplateType).join(
' | '
)}`);
process.exit(1);
}
const appName = process.argv[2];
const templateType = process.argv[3] as TemplateType;
const appPath = path.join(process.cwd(), appName);
const getTemplate = (type: TemplateType): Template | undefined => {
return templates[type];
};
const createAppDirectory = () => {
try {
fs.mkdirSync(appPath);
} catch (err) {
console.error(`Failed to create directory ${appPath}: ${err}`);
process.exit(1);
}
};
const copyTemplateFiles = (template: Template) => {
const templateDir = path.resolve(
__dirname,
'..',
'Templates',
template.directory
);
console.log(`Creating a new ${template.description} in \n${appPath}.`);
fs.copySync(templateDir, appPath);
};
const printRunInstructions = (template: Template) => {
console.log('Run the App:');
console.log(`cd ${appName}`);
if (template.type === TemplateType.Next) {
console.log(' npm run dev');
} else if (template.type === TemplateType.ReactTS) {
console.log(' npm start');
}
};
const installDependencies = () => {
process.chdir(appPath);
console.log('Installing dependencies...');
execSync('npm install', { stdio: 'inherit' });
console.log('Dependencies installed.');
};
const main = () => {
const template = getTemplate(templateType);
if (!template) {
console.error(
`Invalid template. Only ${Object.values(TemplateType).join(
' and '
)} are supported.`
);
process.exit(1);
}
createAppDirectory();
copyTemplateFiles(template);
installDependencies();
printRunInstructions(template);
};
main();