Skip to content

Commit 08b9402

Browse files
committed
Improve error handling along code review comments and document design decisions
1 parent f92239d commit 08b9402

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

packages/schema/tests/generator/prisma-generator.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('Prisma generator test', () => {
2525
beforeEach(() => {
2626
origDir = process.cwd();
2727
const r = tmp.dirSync({ unsafeCleanup: true });
28-
console.log(`Project dir: ${r.name}`);
28+
console.log('Project dir: ', r.name);
2929
process.chdir(r.name);
3030

3131
initProjectDir(r.name, packageJsonContents);

script/test-utils.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ export const PACKAGE_JSON_FILE = 'package.json';
1010
export const PACKAGE_JSON_CONTENTS = '{"name":"test-project","version":"1.0.0"}';
1111

1212
export function preparePackageJson(dependencies: {[key: string]: string} = {}, devDependencies: {[key: string]: string} = {}): string {
13+
// Given that this is a loose file included from elsewhere, I couldn't rely on the tmp package here and had to go with built-in node functions. I saw no significant downsides in this case, versus the upside in developer experience of not needing to do a build step when changing these utils.
1314
const tmpDir = fs.mkdtempSync(path.join(tmpdir(), 'zenstack-test-'));
15+
console.log(`Loading dependencies into store via temp dir ${tmpDir}`);
1416
try {
1517
const packageJsonContents =
1618
`{
@@ -24,30 +26,31 @@ export function preparePackageJson(dependencies: {[key: string]: string} = {}, d
2426
}
2527
}`;
2628

29+
// I considered doing a `pnpm store add` here instead of a plain install. While that worked, I decided against it in the end because it's a secondary way of processing the dependencies and I didn't see a significant downside to just installing and throwing the local project away right after.
2730
initProjectDir(tmpDir, packageJsonContents, false);
2831

2932
return packageJsonContents;
3033
} finally {
3134
fs.rmSync(tmpDir, {recursive: true, force: true});
32-
console.log(`Loaded dependencies into store via temp dir ${tmpDir}`);
3335
}
3436
}
3537

36-
function execCmdSync(cmd: string, path: string) {
37-
console.log(`Running: ${cmd}, in ${path}`);
38+
export function initProjectDir(projectDir: string, packageJsonContents: string, offline = true) {
3839
try {
39-
execSync(cmd, { cwd: path, stdio: 'ignore' });
40-
} catch (err) {
41-
console.error(`Test project scaffolding cmd error: ${err}`);
42-
throw err;
40+
if (!fs.existsSync(projectDir)) {
41+
fs.mkdirSync(projectDir, { recursive: true });
42+
}
43+
fs.writeFileSync(path.join(projectDir, PACKAGE_JSON_FILE), packageJsonContents, { flag: 'w+' });
44+
fs.writeFileSync(path.join(projectDir, NPM_RC_FILE), NPM_RC_CONTENTS, { flag: 'w+' });
45+
} catch (e) {
46+
console.error(`Failed to set up project dir in ${projectDir}`);
47+
throw e;
4348
}
44-
}
4549

46-
export function initProjectDir(projectDir: string, packageJsonContents: string, offline = true) {
47-
if (!fs.existsSync(projectDir)) {
48-
fs.mkdirSync(projectDir, { recursive: true });
50+
try {
51+
execSync(`pnpm install ${offline ? '--offline ' : ''}--ignore-workspace`, {cwd: projectDir, stdio: 'ignore'});
52+
} catch (e) {
53+
console.error(`Failed to initialize project dependencies in ${projectDir}${offline ? '(offline mode)' : '(online mode)'}`);
54+
throw e;
4955
}
50-
fs.writeFileSync(path.join(projectDir, PACKAGE_JSON_FILE), packageJsonContents, { flag: 'w+' });
51-
fs.writeFileSync(path.join(projectDir, NPM_RC_FILE), NPM_RC_CONTENTS, { flag: 'w+' });
52-
execCmdSync(`pnpm install ${offline ? '--offline ' : ''}--ignore-workspace`, projectDir);
5356
}

0 commit comments

Comments
 (0)