@zenstackhq/cli 3.0.0-beta.24 → 3.0.0-beta.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +8 -8
- package/dist/index.cjs +76 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +76 -14
- package/dist/index.js.map +1 -1
- package/package.json +11 -12
- package/src/actions/action-utils.ts +23 -3
- package/src/actions/db.ts +6 -2
- package/src/actions/index.ts +2 -1
- package/src/actions/migrate.ts +14 -2
- package/src/actions/seed.ts +38 -0
- package/src/index.ts +29 -0
- package/test/db.test.ts +43 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import colors from 'colors';
|
|
2
|
+
import { execaCommand } from 'execa';
|
|
3
|
+
import { CliError } from '../cli-error';
|
|
4
|
+
import { getPkgJsonConfig } from './action-utils';
|
|
5
|
+
|
|
6
|
+
type Options = {
|
|
7
|
+
noWarnings?: boolean;
|
|
8
|
+
printStatus?: boolean;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* CLI action for seeding the database.
|
|
13
|
+
*/
|
|
14
|
+
export async function run(options: Options, args: string[]) {
|
|
15
|
+
const pkgJsonConfig = getPkgJsonConfig(process.cwd());
|
|
16
|
+
if (!pkgJsonConfig.seed) {
|
|
17
|
+
if (!options.noWarnings) {
|
|
18
|
+
console.warn(colors.yellow('No seed script defined in package.json. Skipping seeding.'));
|
|
19
|
+
}
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const command = `${pkgJsonConfig.seed}${args.length > 0 ? ' ' + args.join(' ') : ''}`;
|
|
24
|
+
|
|
25
|
+
if (options.printStatus) {
|
|
26
|
+
console.log(colors.gray(`Running seed script "${command}"...`));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
await execaCommand(command, {
|
|
31
|
+
stdout: 'inherit',
|
|
32
|
+
stderr: 'inherit',
|
|
33
|
+
});
|
|
34
|
+
} catch (err) {
|
|
35
|
+
console.error(colors.red(err instanceof Error ? err.message : String(err)));
|
|
36
|
+
throw new CliError('Failed to seed the database. Please check the error message above for details.');
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -34,6 +34,10 @@ const formatAction = async (options: Parameters<typeof actions.format>[0]): Prom
|
|
|
34
34
|
await telemetry.trackCommand('format', () => actions.format(options));
|
|
35
35
|
};
|
|
36
36
|
|
|
37
|
+
const seedAction = async (options: Parameters<typeof actions.seed>[0], args: string[]): Promise<void> => {
|
|
38
|
+
await telemetry.trackCommand('db seed', () => actions.seed(options, args));
|
|
39
|
+
};
|
|
40
|
+
|
|
37
41
|
function createProgram() {
|
|
38
42
|
const program = new Command('zen')
|
|
39
43
|
.alias('zenstack')
|
|
@@ -87,8 +91,13 @@ function createProgram() {
|
|
|
87
91
|
.addOption(schemaOption)
|
|
88
92
|
.addOption(new Option('--force', 'skip the confirmation prompt'))
|
|
89
93
|
.addOption(migrationsOption)
|
|
94
|
+
.addOption(new Option('--skip-seed', 'skip seeding the database after reset'))
|
|
90
95
|
.addOption(noVersionCheckOption)
|
|
91
96
|
.description('Reset your database and apply all migrations, all data will be lost')
|
|
97
|
+
.addHelpText(
|
|
98
|
+
'after',
|
|
99
|
+
'\nIf there is a seed script defined in package.json, it will be run after the reset. Use --skip-seed to skip it.',
|
|
100
|
+
)
|
|
92
101
|
.action((options) => migrateAction('reset', options));
|
|
93
102
|
|
|
94
103
|
migrateCommand
|
|
@@ -128,6 +137,26 @@ function createProgram() {
|
|
|
128
137
|
.addOption(new Option('--force-reset', 'force a reset of the database before push'))
|
|
129
138
|
.action((options) => dbAction('push', options));
|
|
130
139
|
|
|
140
|
+
dbCommand
|
|
141
|
+
.command('seed')
|
|
142
|
+
.description('Seed the database')
|
|
143
|
+
.allowExcessArguments(true)
|
|
144
|
+
.addHelpText(
|
|
145
|
+
'after',
|
|
146
|
+
`
|
|
147
|
+
Seed script is configured under the "zenstack.seed" field in package.json.
|
|
148
|
+
E.g.:
|
|
149
|
+
{
|
|
150
|
+
"zenstack": {
|
|
151
|
+
"seed": "ts-node ./zenstack/seed.ts"
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
Arguments following -- are passed to the seed script. E.g.: "zen db seed -- --users 10"`,
|
|
156
|
+
)
|
|
157
|
+
.addOption(noVersionCheckOption)
|
|
158
|
+
.action((options, command) => seedAction(options, command.args));
|
|
159
|
+
|
|
131
160
|
program
|
|
132
161
|
.command('info')
|
|
133
162
|
.description('Get information of installed ZenStack packages')
|
package/test/db.test.ts
CHANGED
|
@@ -15,4 +15,47 @@ describe('CLI db commands test', () => {
|
|
|
15
15
|
runCli('db push', workDir);
|
|
16
16
|
expect(fs.existsSync(path.join(workDir, 'zenstack/dev.db'))).toBe(true);
|
|
17
17
|
});
|
|
18
|
+
|
|
19
|
+
it('should seed the database with db seed with seed script', () => {
|
|
20
|
+
const workDir = createProject(model);
|
|
21
|
+
const pkgJson = JSON.parse(fs.readFileSync(path.join(workDir, 'package.json'), 'utf8'));
|
|
22
|
+
pkgJson.zenstack = {
|
|
23
|
+
seed: 'node seed.js',
|
|
24
|
+
};
|
|
25
|
+
fs.writeFileSync(path.join(workDir, 'package.json'), JSON.stringify(pkgJson, null, 2));
|
|
26
|
+
fs.writeFileSync(
|
|
27
|
+
path.join(workDir, 'seed.js'),
|
|
28
|
+
`
|
|
29
|
+
import fs from 'node:fs';
|
|
30
|
+
fs.writeFileSync('seed.txt', 'success');
|
|
31
|
+
`,
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
runCli('db seed', workDir);
|
|
35
|
+
expect(fs.readFileSync(path.join(workDir, 'seed.txt'), 'utf8')).toBe('success');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should seed the database after migrate reset', () => {
|
|
39
|
+
const workDir = createProject(model);
|
|
40
|
+
const pkgJson = JSON.parse(fs.readFileSync(path.join(workDir, 'package.json'), 'utf8'));
|
|
41
|
+
pkgJson.zenstack = {
|
|
42
|
+
seed: 'node seed.js',
|
|
43
|
+
};
|
|
44
|
+
fs.writeFileSync(path.join(workDir, 'package.json'), JSON.stringify(pkgJson, null, 2));
|
|
45
|
+
fs.writeFileSync(
|
|
46
|
+
path.join(workDir, 'seed.js'),
|
|
47
|
+
`
|
|
48
|
+
import fs from 'node:fs';
|
|
49
|
+
fs.writeFileSync('seed.txt', 'success');
|
|
50
|
+
`,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
runCli('migrate reset --force', workDir);
|
|
54
|
+
expect(fs.readFileSync(path.join(workDir, 'seed.txt'), 'utf8')).toBe('success');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should skip seeding the database without seed script', () => {
|
|
58
|
+
const workDir = createProject(model);
|
|
59
|
+
runCli('db seed', workDir);
|
|
60
|
+
});
|
|
18
61
|
});
|