Skip to content

Commit

Permalink
Improve error handling along code review comments and document design…
Browse files Browse the repository at this point in the history
… decisions
  • Loading branch information
WimTibackx committed Jun 15, 2024
1 parent bcee249 commit 11068cb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/schema/tests/generator/prisma-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Prisma generator test', () => {
beforeEach(() => {
origDir = process.cwd();
const r = tmp.dirSync({ unsafeCleanup: true });
console.log(`Project dir: ${r.name}`);
console.log('Project dir: ', r.name);
process.chdir(r.name);

initProjectDir(r.name, packageJsonContents);
Expand Down
31 changes: 17 additions & 14 deletions script/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export const PACKAGE_JSON_FILE = 'package.json';
export const PACKAGE_JSON_CONTENTS = '{"name":"test-project","version":"1.0.0"}';

export function preparePackageJson(dependencies: {[key: string]: string} = {}, devDependencies: {[key: string]: string} = {}): string {
// 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.
const tmpDir = fs.mkdtempSync(path.join(tmpdir(), 'zenstack-test-'));
console.log(`Loading dependencies into store via temp dir ${tmpDir}`);
try {
const packageJsonContents =
`{
Expand All @@ -24,30 +26,31 @@ export function preparePackageJson(dependencies: {[key: string]: string} = {}, d
}
}`;

// 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.
initProjectDir(tmpDir, packageJsonContents, false);

return packageJsonContents;
} finally {
fs.rmSync(tmpDir, {recursive: true, force: true});
console.log(`Loaded dependencies into store via temp dir ${tmpDir}`);
}
}

function execCmdSync(cmd: string, path: string) {
console.log(`Running: ${cmd}, in ${path}`);
export function initProjectDir(projectDir: string, packageJsonContents: string, offline = true) {
try {
execSync(cmd, { cwd: path, stdio: 'ignore' });
} catch (err) {
console.error(`Test project scaffolding cmd error: ${err}`);
throw err;
if (!fs.existsSync(projectDir)) {
fs.mkdirSync(projectDir, { recursive: true });
}
fs.writeFileSync(path.join(projectDir, PACKAGE_JSON_FILE), packageJsonContents, { flag: 'w+' });
fs.writeFileSync(path.join(projectDir, NPM_RC_FILE), NPM_RC_CONTENTS, { flag: 'w+' });
} catch (e) {
console.error(`Failed to set up project dir in ${projectDir}`);
throw e;
}
}

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

0 comments on commit 11068cb

Please sign in to comment.