Skip to content

Commit

Permalink
feat(scripts): generate-package command generates jest.config.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jbazant authored and vytick committed Jan 31, 2025
1 parent f4b8c91 commit a56381e
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 7 deletions.
16 changes: 10 additions & 6 deletions scripts/generatePackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@ import fsExtra from 'fs-extra';
import prettier from 'prettier';
import sortPackageJson from 'sort-package-json';

import templatePackageJson from './package-template/package.json';
import templatePackageJsonWeb from './package-template/package.json';
import templatePackageJsonNative from './package-template-native/package.json';
// todo: calling yarn generate-package failed on not resolving destructuring imports. default imports seem to work.
import import1 from './utils/getPrettierConfig';
import import2 from './utils/getWorkspacesList';

const { getPrettierConfig } = import1;
const { getWorkspacesList } = import2;

const templatePath = './scripts/package-template';

const scopes = {
'@suite-common': {
path: 'suite-common/',
templatePath: 'package-template/',
templatePackageJson: templatePackageJsonWeb,
},
'@suite-native': {
path: 'suite-native/',
templatePath: 'package-template/',
templatePath: 'package-template-native/',
templatePackageJson: templatePackageJsonNative,
},
'@trezor': {
path: 'packages/',
templatePath: 'package-template/',
templatePackageJson: templatePackageJsonWeb,
},
};

Expand Down Expand Up @@ -57,7 +59,9 @@ const exitWithErrorMessage = errorMessage => {
);
}

const packagePath = `${scopes[packageScope].path}/${packageName}`;
const { path, templatePath, templatePackageJson } = scopes[packageScope];
const packagePath = `${path}/${packageName}`;

const workspacesNames = Object.keys(getWorkspacesList());
if (fs.existsSync(packagePath)) {
exitWithErrorMessage(
Expand All @@ -80,7 +84,7 @@ const exitWithErrorMessage = errorMessage => {
prettier.format(JSON.stringify(config).replace(/\\\\/g, '/'), prettierConfig);

try {
fsExtra.copySync(templatePath, packagePath);
fsExtra.copySync(`./scripts/${templatePath}`, packagePath);
fs.writeFileSync(`${packagePath}/package.json`, await serializeConfig(packageJson));
} catch (error) {
exitWithErrorMessage(`${error}\n${chalk.bold.red('Package creation failed.')}`);
Expand Down
11 changes: 11 additions & 0 deletions scripts/package-template-native/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Jest configuration for native packages.
* Keeping this file next to the package.json file instead of providing configuration
* with `-c ../../jest.config.native` option in package.json scripts
* allows us to run jest tests directly from IDEs.
*/
const { ...baseConfig } = require('../../jest.config.native');

module.exports = {
...baseConfig,
};
14 changes: 14 additions & 0 deletions scripts/package-template-native/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "suite-package-template",
"version": "1.0.0",
"main": "src/index",
"license": "See LICENSE.md in repo root",
"private": true,
"sideEffects": false,
"scripts": {
"depcheck": "yarn g:depcheck",
"type-check": "yarn g:tsc --build",
"test:unit": "yarn g:jest"
},
"dependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { logHello } from '../logHello';

describe('hello logger', () => {
it('logger returns true', () => {
expect(logHello()).toBeTruthy();
});
});
1 change: 1 addition & 0 deletions scripts/package-template-native/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { logHello } from './logHello';
5 changes: 5 additions & 0 deletions scripts/package-template-native/src/logHello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const logHello = () => {
console.log('hello world');

return true;
};
5 changes: 5 additions & 0 deletions scripts/package-template-native/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": { "outDir": "libDev" },
"references": []
}
11 changes: 11 additions & 0 deletions scripts/package-template/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Jest configuration for web packages.
* Keeping this file next to the package.json file instead of providing configuration
* with `-c ../../jest.config.base` option in package.json scripts
* allows us to run jest tests directly from IDEs.
*/
const { ...baseConfig } = require('../../jest.config.base');

module.exports = {
...baseConfig,
};
2 changes: 1 addition & 1 deletion scripts/package-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"depcheck": "yarn g:depcheck",
"type-check": "yarn g:tsc --build",
"test:unit": "yarn g:jest -c ../../jest.config.base.js"
"test:unit": "yarn g:jest"
},
"dependencies": {}
}

0 comments on commit a56381e

Please sign in to comment.