-
-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathindex.ts
More file actions
152 lines (133 loc) · 4.13 KB
/
index.ts
File metadata and controls
152 lines (133 loc) · 4.13 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import colors from 'colors';
import { Command } from 'commander';
import { execSync, type StdioOptions } from 'node:child_process';
import fs from 'node:fs';
import ora from 'ora';
import { STARTER_MAIN_TS, STARTER_ZMODEL } from './templates';
// detect package manager
const npmAgent = process.env['npm_config_user_agent'];
let agent = 'npm';
let agentExec = 'npx';
let saveDev = '--save-dev';
if (npmAgent?.includes('pnpm')) {
agent = 'pnpm';
agentExec = 'pnpm';
} else if (npmAgent?.includes('yarn')) {
agent = 'yarn';
agentExec = 'npx';
saveDev = '--dev';
} else if (npmAgent?.includes('bun')) {
agent = 'bun';
agentExec = 'bun';
}
const program = new Command('create-zenstack');
program.arguments('<project-name>').action((projectName) => {
initProject(projectName);
});
program.parse(process.argv);
function initProject(name: string) {
// create folder
if (fs.existsSync(name)) {
console.log(colors.red(`Directory ${name} already exists.`));
process.exit(1);
}
fs.mkdirSync(name);
process.chdir(name);
console.log(colors.gray(`Using package manager: ${agent}`));
// create package.json
fs.writeFileSync(
'package.json',
JSON.stringify(
{
name: 'zenstack-app',
version: '1.0.0',
description: 'Scaffolded with create-zenstack',
type: 'module',
scripts: {
dev: 'tsx main.ts',
},
license: 'ISC',
},
null,
2,
),
);
// create VSCode config files
createVsCodeConfig();
// install packages
const packages = [
{ name: '@zenstackhq/cli@latest', dev: true },
{ name: '@zenstackhq/schema@latest', dev: false },
{ name: '@zenstackhq/orm@latest', dev: false },
{ name: 'better-sqlite3', dev: false },
{ name: '@types/better-sqlite3', dev: true },
{ name: 'typescript', dev: true },
{ name: 'tsx', dev: true },
{ name: '@types/node', dev: true },
];
for (const pkg of packages) {
installPackage(pkg);
}
// create tsconfig.json
fs.writeFileSync(
'tsconfig.json',
JSON.stringify(
{
compilerOptions: {
module: 'esnext',
target: 'esnext',
moduleResolution: 'bundler',
sourceMap: true,
outDir: 'dist',
strict: true,
skipLibCheck: true,
esModuleInterop: true,
},
},
null,
2,
),
);
// create schema.zmodel
fs.mkdirSync('zenstack');
fs.writeFileSync('zenstack/schema.zmodel', STARTER_ZMODEL);
// create main.ts
fs.writeFileSync('main.ts', STARTER_MAIN_TS);
// run `zen generate`
runCommand(`${agentExec} zen generate`, 'Running `zen generate`');
// run `zen db push`
runCommand(`${agentExec} zen db push`, 'Running `zen db push`');
// run `$agent run dev`
console.log(`Running \`${agent} run dev\``);
execSync(`${agent} run dev`, { stdio: 'inherit' });
console.log(colors.green('Project setup completed!'));
}
function installPackage(pkg: { name: string; dev: boolean }) {
runCommand(`${agent} install ${pkg.name} ${pkg.dev ? saveDev : ''}`, `Installing "${pkg.name}"`);
}
function runCommand(cmd: string, status: string, stdio: StdioOptions = 'ignore') {
const spinner = ora(status).start();
try {
execSync(cmd, { stdio });
spinner.succeed();
} catch (e) {
spinner.fail();
throw e;
}
}
function createVsCodeConfig() {
fs.mkdirSync('.vscode', { recursive: true });
fs.writeFileSync(
'.vscode/settings.json',
JSON.stringify(
{
'files.associations': {
'*.zmodel': 'zmodel-v3',
},
},
null,
4,
),
);
fs.writeFileSync('.vscode/extensions.json', JSON.stringify({ recommendations: ['zenstack.zenstack-v3'] }, null, 4));
}